B
    ed/                 @   s   d Z ddlmZmZ ddlmZ ddlmZ ddl	m
Z
mZ dddd	d
dddddddddddddddddZG dd deZd#dd Zd!d" ZdS )$z
Javascript code printer

The JavascriptCodePrinter converts single SymPy expressions into single
Javascript expressions, using the functions defined in the Javascript
Math object where possible.

    )AnyDict)S)CodePrinter)
precedence
PRECEDENCEzMath.absz	Math.acosz
Math.acoshz	Math.asinz
Math.asinhz	Math.atanz
Math.atan2z
Math.atanhz	Math.ceilzMath.cosz	Math.coshzMath.expz
Math.floorzMath.logzMath.maxzMath.minz	Math.signzMath.sinz	Math.sinhzMath.tanz	Math.tanh)ZAbsacosacoshasinasinhatanatan2atanhZceilingcoscoshexpfloorlogZMaxZMinsignsinsinhtantanhc               @   s   e Zd ZdZdZdZdddi dddd	Zi fd
dZdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 ZdS )4JavascriptCodePrinterzK"A Printer to convert Python expressions to strings of JavaScript code
    Z_javascript
JavaScriptNauto   TF)orderZ	full_prec	precisionuser_functionsZhumanZallow_unknown_functionsZcontractc             C   s2   t | | tt| _|di }| j| d S )Nr   )r   __init__dictknown_functionsgetupdate)selfsettingsZ	userfuncs r'   b/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sympy/printing/jscode.pyr    <   s    
zJavascriptCodePrinter.__init__c             C   s   |d S )N   r'   )r%   pr'   r'   r(   _rate_index_positionB   s    z*JavascriptCodePrinter._rate_index_positionc             C   s   d| S )Nz%s;r'   )r%   Z
codestringr'   r'   r(   _get_statementE   s    z$JavascriptCodePrinter._get_statementc             C   s
   d |S )Nz// {})format)r%   textr'   r'   r(   _get_commentH   s    z"JavascriptCodePrinter._get_commentc             C   s   d ||| jd S )Nzvar {} = {};r   )r-   ZevalfZ	_settings)r%   namevaluer'   r'   r(   _declare_number_constK   s    z+JavascriptCodePrinter._declare_number_constc             C   s
   |  |S )N)indent_code)r%   linesr'   r'   r(   _format_codeN   s    z"JavascriptCodePrinter._format_codec                s    |j \}  fddt|D S )Nc             3   s$   | ]}t  D ]}||fV  qqd S )N)range).0ij)colsr'   r(   	<genexpr>S   s    zAJavascriptCodePrinter._traverse_matrix_indices.<locals>.<genexpr>)shaper6   )r%   matrowsr'   )r:   r(   _traverse_matrix_indicesQ   s    
z.JavascriptCodePrinter._traverse_matrix_indicesc          
   C   s^   g }g }d}xH|D ]@}| || |j| |j| |jd d  | d qW ||fS )NzAfor (var %(varble)s=%(start)s; %(varble)s<%(end)s; %(varble)s++){   )Zvarblestartend})append_printlabellowerupper)r%   indicesZ
open_linesZclose_linesZ	loopstartr8   r'   r'   r(   _get_loop_opening_endingU   s    


z.JavascriptCodePrinter._get_loop_opening_endingc             C   s~   t |}|jdkr$d| |j| S |jdkr>d| |j S |jtjd kr^d| |j S d| |j| |jf S d S )Nz1/%sg      ?zMath.sqrt(%s)   zMath.cbrt(%s)zMath.pow(%s, %s))r   r   parenthesizebaserE   r   One)r%   exprPRECr'   r'   r(   
_print_Powb   s    

z JavascriptCodePrinter._print_Powc             C   s"   t |jt |j }}d||f S )Nz%d/%d)intr*   q)r%   rP   r*   rT   r'   r'   r(   _print_Rationaln   s    z%JavascriptCodePrinter._print_Rationalc                sn   |j \}}t|  fdd|j D \}}|jr8|jsD|jrR|jrR| d| S d| d| d| d| S )Nc                s   g | ]} | qS r'   )rM   )r7   arg)rQ   r%   r'   r(   
<listcomp>u   s    z4JavascriptCodePrinter._print_Mod.<locals>.<listcomp>z % z((z) + z) % )argsr   Zis_nonnegativeZis_nonpositive)r%   rP   numZdenZsnumZsdenr'   )rQ   r%   r(   
_print_Modr   s    
z JavascriptCodePrinter._print_Modc             C   s,   |  |j}|  |j}|j}d|||S )Nz{} {} {})rE   lhsrhsZrel_opr-   )r%   rP   Zlhs_codeZrhs_codeopr'   r'   r(   _print_Relational~   s    z'JavascriptCodePrinter._print_Relationalc             C   sd   |j }tj}tj}x4tt|jD ]"}||j| | 7 }||| 9 }q"W d| |j	j
| |f S )Nz%s[%s])r<   r   ZZerorO   reversedr6   ZrankrI   rE   rN   rF   )r%   rP   Zdimselemoffsetr8   r'   r'   r(   _print_Indexed   s    z$JavascriptCodePrinter._print_Indexedc             C   s   |  |jS )N)rE   rF   )r%   rP   r'   r'   r(   
_print_Idx   s    z JavascriptCodePrinter._print_Idxc             C   s   dS )NzMath.Er'   )r%   rP   r'   r'   r(   _print_Exp1   s    z!JavascriptCodePrinter._print_Exp1c             C   s   dS )NzMath.PIr'   )r%   rP   r'   r'   r(   	_print_Pi   s    zJavascriptCodePrinter._print_Pic             C   s   dS )NzNumber.POSITIVE_INFINITYr'   )r%   rP   r'   r'   r(   _print_Infinity   s    z%JavascriptCodePrinter._print_Infinityc             C   s   dS )NzNumber.NEGATIVE_INFINITYr'   )r%   rP   r'   r'   r(   _print_NegativeInfinity   s    z-JavascriptCodePrinter._print_NegativeInfinityc       
         s&  ddl m} |jd jdkr$tdg }||rxt|jD ]\}\}}|dkrh|d |  n:|t	|jd kr|dkr|d n|d	 |   |}|| |d
 q>W d
|S  fdd|jd d D }d |jd j }	d
||	 d
dt	| g S d S )Nr   )
AssignmentrK   TzAll Piecewise expressions must contain an (expr, True) statement to be used as a default condition. Without one, the generated expression may not evaluate to anything under some condition.z	if (%s) {r@   zelse {zelse if (%s) {rC   
c                s(   g | ] \}}d   |  |f qS )z((%s) ? (
%s
)
)rE   )r7   ec)r%   r'   r(   rW      s   z:JavascriptCodePrinter._print_Piecewise.<locals>.<listcomp>z: (
%s
)z:  ))Zsympy.codegen.astrh   rX   Zcond
ValueErrorhas	enumeraterD   rE   lenjoinrP   )
r%   rP   rh   r4   r8   rj   rk   Zcode0Zecpairs	last_liner'   )r%   r(   _print_Piecewise   s&    




z&JavascriptCodePrinter._print_Piecewisec             C   s2   d | j|jtd dd|j|j|jjd   S )Nz{}[{}]ZAtomT)strictr@   )r-   rM   parentr   r9   r8   r<   )r%   rP   r'   r'   r(   _print_MatrixElement   s    z*JavascriptCodePrinter._print_MatrixElementc       
         s   t |tr$| |d}d|S d}dd dd |D }fdd|D } fd	d|D }g }d
}xVt|D ]J\}}	|	dkr||	 qt||| 8 }|d|| |	f  ||| 7 }qtW |S )z0Accepts a string of code or a list of code linesT z   ){(z{
z(
)rC   rm   c             S   s   g | ]}| d qS )z 	)lstrip)r7   liner'   r'   r(   rW      s    z5JavascriptCodePrinter.indent_code.<locals>.<listcomp>c                s    g | ]}t tt|j qS r'   )rS   anymapendswith)r7   r|   )	inc_tokenr'   r(   rW      s    c                s    g | ]}t tt|j qS r'   )rS   r}   r~   
startswith)r7   r|   )	dec_tokenr'   r(   rW      s   r   )rx   ri   z%s%s)
isinstancestrr3   
splitlinesrr   rp   rD   )
r%   codeZ
code_linestabZincreaseZdecreaseprettylevelnr|   r'   )r   r   r(   r3      s(    



z!JavascriptCodePrinter.indent_code)__name__
__module____qualname____doc__ZprintmethodlanguageZ_default_settingsr    r+   r,   r/   r2   r5   r?   rJ   rR   rU   rZ   r^   rb   rc   rd   re   rf   rg   rt   rw   r3   r'   r'   r'   r(   r   ,   s>   
!r   Nc             K   s   t || |S )a  Converts an expr to a string of javascript code

    Parameters
    ==========

    expr : Expr
        A SymPy expression to be converted.
    assign_to : optional
        When given, the argument is used as the name of the variable to which
        the expression is assigned. Can be a string, ``Symbol``,
        ``MatrixSymbol``, or ``Indexed`` type. This is helpful in case of
        line-wrapping, or for expressions that generate multi-line statements.
    precision : integer, optional
        The precision for numbers such as pi [default=15].
    user_functions : dict, optional
        A dictionary where keys are ``FunctionClass`` instances and values are
        their string representations. Alternatively, the dictionary value can
        be a list of tuples i.e. [(argument_test, js_function_string)]. See
        below for examples.
    human : bool, optional
        If True, the result is a single string that may contain some constant
        declarations for the number symbols. If False, the same information is
        returned in a tuple of (symbols_to_declare, not_supported_functions,
        code_text). [default=True].
    contract: bool, optional
        If True, ``Indexed`` instances are assumed to obey tensor contraction
        rules and the corresponding nested loops over indices are generated.
        Setting contract=False will not generate loops, instead the user is
        responsible to provide values for the indices in the code.
        [default=True].

    Examples
    ========

    >>> from sympy import jscode, symbols, Rational, sin, ceiling, Abs
    >>> x, tau = symbols("x, tau")
    >>> jscode((2*tau)**Rational(7, 2))
    '8*Math.sqrt(2)*Math.pow(tau, 7/2)'
    >>> jscode(sin(x), assign_to="s")
    's = Math.sin(x);'

    Custom printing can be defined for certain types by passing a dictionary of
    "type" : "function" to the ``user_functions`` kwarg. Alternatively, the
    dictionary value can be a list of tuples i.e. [(argument_test,
    js_function_string)].

    >>> custom_functions = {
    ...   "ceiling": "CEIL",
    ...   "Abs": [(lambda x: not x.is_integer, "fabs"),
    ...           (lambda x: x.is_integer, "ABS")]
    ... }
    >>> jscode(Abs(x) + ceiling(x), user_functions=custom_functions)
    'fabs(x) + CEIL(x)'

    ``Piecewise`` expressions are converted into conditionals. If an
    ``assign_to`` variable is provided an if statement is created, otherwise
    the ternary operator is used. Note that if the ``Piecewise`` lacks a
    default term, represented by ``(expr, True)`` then an error will be thrown.
    This is to prevent generating an expression that may not evaluate to
    anything.

    >>> from sympy import Piecewise
    >>> expr = Piecewise((x + 1, x > 0), (x, True))
    >>> print(jscode(expr, tau))
    if (x > 0) {
       tau = x + 1;
    }
    else {
       tau = x;
    }

    Support for loops is provided through ``Indexed`` types. With
    ``contract=True`` these expressions will be turned into loops, whereas
    ``contract=False`` will just print the assignment expression that should be
    looped over:

    >>> from sympy import Eq, IndexedBase, Idx
    >>> len_y = 5
    >>> y = IndexedBase('y', shape=(len_y,))
    >>> t = IndexedBase('t', shape=(len_y,))
    >>> Dy = IndexedBase('Dy', shape=(len_y-1,))
    >>> i = Idx('i', len_y-1)
    >>> e=Eq(Dy[i], (y[i+1]-y[i])/(t[i+1]-t[i]))
    >>> jscode(e.rhs, assign_to=e.lhs, contract=False)
    'Dy[i] = (y[i + 1] - y[i])/(t[i + 1] - t[i]);'

    Matrices are also supported, but a ``MatrixSymbol`` of the same dimensions
    must be provided to ``assign_to``. Note that any expression that can be
    generated normally can also exist inside a Matrix:

    >>> from sympy import Matrix, MatrixSymbol
    >>> mat = Matrix([x**2, Piecewise((x + 1, x > 0), (x, True)), sin(x)])
    >>> A = MatrixSymbol('A', 3, 1)
    >>> print(jscode(mat, A))
    A[0] = Math.pow(x, 2);
    if (x > 0) {
       A[1] = x + 1;
    }
    else {
       A[1] = x;
    }
    A[2] = Math.sin(x);
    )r   Zdoprint)rP   Z	assign_tor&   r'   r'   r(   jscode   s    ir   c             K   s   t t| f| dS )zPrints the Javascript representation of the given expression.

       See jscode for the meaning of the optional arguments.
    N)printr   )rP   r&   r'   r'   r(   print_jscodeL  s    r   )N)r   typingr   r   ZtDictZ
sympy.corer   Zsympy.printing.codeprinterr   Zsympy.printing.precedencer   r   r"   r   r   r   r'   r'   r'   r(   <module>   s:    5
l