Ë
    7^(hÍ.  ã                  óð   — d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZ i dd	“d
d“dd“dd“dd“dd“dd“dd“d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/„ d0e
«      Zd4d2„Zd3„ Zy1)5zÅ
Javascript code printer

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

é    )Úannotations)ÚAny)ÚS)Úequal_valued)ÚCodePrinter)Ú
precedenceÚ
PRECEDENCEÚAbszMath.absÚacosz	Math.acosÚacoshz
Math.acoshÚasinz	Math.asinÚasinhz
Math.asinhÚatanz	Math.atanÚatan2z
Math.atan2Úatanhz
Math.atanhÚceilingz	Math.ceilÚcoszMath.cosÚcoshz	Math.coshÚexpzMath.expÚfloorz
Math.floorÚlogzMath.logÚMaxzMath.maxÚMinzMath.minÚsignz	Math.signzMath.sinz	Math.sinhzMath.tanz	Math.tanh)ÚsinÚsinhÚtanÚtanhc                  óÎ   — e Zd ZU dZdZdZ eej                  fi di ddœ¤ŽZde	d<   i fd	„Z
d
„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zy)ÚJavascriptCodePrinterzK"A Printer to convert Python expressions to strings of JavaScript code
    Ú_javascriptÚ
JavaScripté   T)Ú	precisionÚuser_functionsÚcontractzdict[str, Any]Ú_default_settingsc                ó²   — t        j                  | |«       t        t        «      | _        |j	                  di «      }| j                  j                  |«       y )Nr%   )r   Ú__init__ÚdictÚknown_functionsÚgetÚupdate)ÚselfÚsettingsÚ	userfuncss      úS/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/printing/jscode.pyr)   zJavascriptCodePrinter.__init__:   sD   € Ü×Ñ˜T 8Ô,Ü#¤OÓ4ˆÔØ—L‘LÐ!1°2Ó6ˆ	Ø×Ñ×#Ñ# IÕ.ó    c                ó   — |dz  S )Né   © )r.   Úps     r1   Ú_rate_index_positionz*JavascriptCodePrinter._rate_index_position@   s   € Ø�‰sˆ
r2   c                ó   — d|z  S )Nz%s;r5   )r.   Ú
codestrings     r1   Ú_get_statementz$JavascriptCodePrinter._get_statementC   s   € Ø�zÑ!Ð!r2   c                ó$   — dj                  |«      S )Nz// {})Úformat)r.   Útexts     r1   Ú_get_commentz"JavascriptCodePrinter._get_commentF   s   € Ø�~‰~˜dÓ#Ð#r2   c                ó^   — dj                  ||j                  | j                  d   «      «      S )Nzvar {} = {};r$   )r<   ÚevalfÚ	_settings)r.   ÚnameÚvalues      r1   Ú_declare_number_constz+JavascriptCodePrinter._declare_number_constI   s'   € Ø×$Ñ$ T¨5¯;©;°t·~±~ÀkÑ7RÓ+SÓTÐTr2   c                ó$   — | j                  |«      S ©N)Úindent_code)r.   Úliness     r1   Ú_format_codez"JavascriptCodePrinter._format_codeL   s   € Ø×Ñ Ó&Ð&r2   c                óJ   ‡— |j                   \  }Šˆfd„t        |«      D «       S )Nc              3  óF   •K  — | ]  }t        ‰«      D ]  }||f–— Œ
 Œ y ­wrF   )Úrange)Ú.0ÚiÚjÚcolss      €r1   ú	<genexpr>zAJavascriptCodePrinter._traverse_matrix_indices.<locals>.<genexpr>Q   s%   øè ø€ ÒA˜1´U¸4³[ÒA°��A”ÐA�ÑAùs   ƒ!)ÚshaperL   )r.   ÚmatÚrowsrP   s      @r1   Ú_traverse_matrix_indicesz.JavascriptCodePrinter._traverse_matrix_indicesO   s   ø€ Ø—Y‘Y‰
ˆˆdÛA¤ d£ÔAÐAr2   c           
     ó  — g }g }d}|D ]y  }|j                  || j                  |j                  «      | j                  |j                  «      | j                  |j                  dz   «      dœz  «       |j                  d«       Œ{ ||fS )NzAfor (var %(varble)s=%(start)s; %(varble)s<%(end)s; %(varble)s++){é   )ÚvarbleÚstartÚendú})ÚappendÚ_printÚlabelÚlowerÚupper)r.   ÚindicesÚ
open_linesÚclose_linesÚ	loopstartrN   s         r1   Ú_get_loop_opening_endingz.JavascriptCodePrinter._get_loop_opening_endingS   s‹   € Øˆ
ØˆØWˆ	Øò 	$ˆAà×Ñ˜iØŸ+™+ a§g¡gÓ.ØŸ™ Q§W¡WÓ-Ø—{‘{ 1§7¡7¨Q¡;Ó/ñ+1ñ 1ô 2ð ×Ñ˜sÕ#ð	$ð ˜;Ð&Ð&r2   c                óÜ  — t        |«      }t        |j                  d«      rd| j                  |j                  |«      z  S t        |j                  d«      rd| j                  |j                  «      z  S |j                  t        j                  dz  k(  rd| j                  |j                  «      z  S d| j                  |j                  «      ›d| j                  |j                  «      ›d	�S )
Néÿÿÿÿz1/%sg      à?zMath.sqrt(%s)é   zMath.cbrt(%s)z	Math.pow(z, ú))r   r   r   ÚparenthesizeÚbaser]   r   ÚOne)r.   ÚexprÚPRECs      r1   Ú
_print_Powz JavascriptCodePrinter._print_Pow`   s¹   € Ü˜$ÓˆÜ˜Ÿ™ "Ô%Ø˜T×.Ñ.¨t¯y©y¸$Ó?Ñ@Ð@Ü˜$Ÿ(™( CÔ(Ø" T§[¡[°·±Ó%;Ñ;Ð;Ø�X‰XœŸ™˜q™Ò Ø" T§[¡[°·±Ó%;Ñ;Ð;ð !ð *.¯©°T·Y±YÕ)?Ø!%§¡¨T¯X©XÕ!6ð8ð 8r2   c                ód   — t        |j                  «      t        |j                  «      }}d||fz  S )Nz%d/%d)Úintr6   Úq)r.   rm   r6   rr   s       r1   Ú_print_Rationalz%JavascriptCodePrinter._print_Rationall   s*   € Ü�4—6‘6‹{œC §¡›Kˆ1ˆØ˜!˜Q˜ÑÐr2   c                ó$  — |j                   \  }}t        |«      }|j                   D �cg c]  }| j                  ||«      ‘Œ c}\  }}|j                  r|j                  s|j                  r|j                  r|› d|› �S d|› d|› d|› d|› �S c c}w )Nz % ú((z) + z) % )Úargsr   rj   Úis_nonnegativeÚis_nonpositive)r.   rm   ÚnumÚdenrn   ÚargÚsnumÚsdens           r1   Ú
_print_Modz JavascriptCodePrinter._print_Modp   s˜   € Ø—9‘9‰ˆˆSÜ˜$ÓˆØ>B¿i¹iÖH°s�d×'Ñ'¨¨TÕ2ÒH‰
ˆˆdð ×Ò 3×#5Ò#5Ø×Ò 3×#5Ò#5Ø�V˜3˜t˜fÐ%Ð%Ø�D�6˜˜T˜F $ t f¨D°°Ð7Ð7ùò Is   ©Bc                ó¬   — | j                  |j                  «      }| j                  |j                  «      }|j                  }dj	                  |||«      S )Nz{} {} {})r]   ÚlhsÚrhsÚrel_opr<   )r.   rm   Úlhs_codeÚrhs_codeÚops        r1   Ú_print_Relationalz'JavascriptCodePrinter._print_Relational|   sD   € Ø—;‘;˜tŸx™xÓ(ˆØ—;‘;˜tŸx™xÓ(ˆØ�[‰[ˆØ× Ñ  ¨2¨xÓ8Ð8r2   c                óN  — |j                   }t        j                  }t        j                  }t	        t        |j                  «      «      D ]  }||j                  |   |z  z  }|||   z  }Œ! | j                  |j                  j                  «      ›d| j                  |«      ›d�S )Nú[ú])rR   r   ÚZerorl   ÚreversedrL   Úrankra   r]   rk   r^   )r.   rm   ÚdimsÚelemÚoffsetrN   s         r1   Ú_print_Indexedz$JavascriptCodePrinter._print_Indexed‚   s†   € à�z‰zˆÜ�v‰vˆÜ—‘ˆÜœ% §	¡	Ó*Ó+ò 	ˆAØ�D—L‘L ‘O FÑ*Ñ*ˆDØ�d˜1‘gÑ‰Fð	ð  Ÿ;™; t§y¡y§¡Õ7¸¿¹ÀTÕ9JÐKÐKr2   c                 ó   — y)NzMath.Er5   ©r.   rm   s     r1   Ú_print_Exp1z!JavascriptCodePrinter._print_Exp1Œ   s   € Ør2   c                 ó   — y)NzMath.PIr5   r’   s     r1   Ú	_print_PizJavascriptCodePrinter._print_Pi�   s   € Ør2   c                 ó   — y)NzNumber.POSITIVE_INFINITYr5   r’   s     r1   Ú_print_Infinityz%JavascriptCodePrinter._print_Infinity’   ó   € Ø)r2   c                 ó   — y)NzNumber.NEGATIVE_INFINITYr5   r’   s     r1   Ú_print_NegativeInfinityz-JavascriptCodePrinter._print_NegativeInfinity•   r˜   r2   c           
     óš  — ddl m} |j                  d   j                  dk7  rt	        d«      ‚g }|j                  |«      rât        |j                  «      D ]¹  \  }\  }}|dk(  r$|j                  d| j                  |«      z  «       nU|t        |j                  «      dz
  k(  r|dk(  r|j                  d«       n#|j                  d	| j                  |«      z  «       | j                  |«      }|j                  |«       |j                  d
«       Œ» dj                  |«      S |j                  d d D ��cg c],  \  }}d| j                  |«      ›d| j                  |«      ›d�‘Œ. }}}d| j                  |j                  d   j                  «      z  }	dj                  |«      |	z   dj                  dt        |«      z  g«      z   S c c}}w )Nr   )Ú
Assignmentrg   Tz¼All 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) {rW   zelse {zelse if (%s) {r[   ú
ru   z) ? (
z
)
z: (
%s
)z: ú ri   )Úsympy.codegen.astrœ   rv   ÚcondÚ
ValueErrorÚhasÚ	enumerater\   r]   ÚlenÚjoinrm   )
r.   rm   rœ   rH   rN   ÚeÚcÚcode0ÚecpairsÚ	last_lines
             r1   Ú_print_Piecewisez&JavascriptCodePrinter._print_Piecewise˜   s�  € Ý0Ø�9‰9�R‰=×Ñ Ò%ô ð /ó 0ð 0ð
 ˆØ�8‰8�JÔÜ& t§y¡yÓ1ò 	"‘	�‘6�A�qØ˜’6Ø—L‘L ¨t¯{©{¸1«~Ñ!=Õ>Øœ#˜dŸi™i›.¨1Ñ,Ò,°°d²Ø—L‘L Õ*à—L‘LÐ!1°D·K±KÀ³NÑ!BÔCØŸ™ A›�Ø—‘˜UÔ#Ø—‘˜SÕ!ð	"ð —9‘9˜UÓ#Ð#ð !%§	¡	¨#¨2 ÷0Ù˜™1ð 04¯{©{¸1­~¸t¿{¹{È1½~ÒNð 0ˆGñ 0à$ t§{¡{°4·9±9¸R±=×3EÑ3EÓ'FÑFˆIØ—9‘9˜WÓ%¨	Ñ1°C·H±H¸cÄ#ÀgÃ,Ñ>NÐ=OÓ4PÑPÐPùó0s   Ä51Gc                óÎ   — dj                  | j                  |j                  t        d   d¬«      |j                  |j
                  |j                  j                  d   z  z   «      S )Nz{}[{}]ÚAtomT)ÚstrictrW   )r<   rj   Úparentr	   rO   rN   rR   r’   s     r1   Ú_print_MatrixElementz*JavascriptCodePrinter._print_MatrixElement¹   sY   € Ø�‰˜t×0Ñ0°·±Ü�vÑ tð  1ó  -à�F‰F�T—V‘V˜DŸK™K×-Ñ-¨aÑ0Ñ0Ñ0ó2ð 	2r2   c                ól  — t        |t        «      r1| j                  |j                  d«      «      }dj	                  |«      S d}d}d}|D �cg c]  }|j                  d«      ‘Œ }}|D �cg c]*  }t        t        t        |j                  |«      «      «      ‘Œ, }}|D �cg c]*  }t        t        t        |j                  |«      «      «      ‘Œ, }}g }	d}
t        |«      D ]C  \  }}|dv r|	j                  |«       Œ|
||   z  }
|	j                  ||
z  ›|›�«       |
||   z  }
ŒE |	S c c}w c c}w c c}w )	z0Accepts a string of code or a list of code linesTÚ z   )ú{ú(z{
z(
)r[   ri   z 	r   )r²   r�   )Ú
isinstanceÚstrrG   Ú
splitlinesr¥   Úlstriprq   ÚanyÚmapÚendswithÚ
startswithr£   r\   )r.   ÚcodeÚ
code_linesÚtabÚ	inc_tokenÚ	dec_tokenÚlineÚincreaseÚdecreaseÚprettyÚlevelÚns               r1   rG   z!JavascriptCodePrinter.indent_code¾   s:  € ô �dœCÔ Ø×)Ñ)¨$¯/©/¸$Ó*?Ó@ˆJØ—7‘7˜:Ó&Ð&àˆØ,ˆ	Øˆ	à04Ö6¨�—‘˜UÕ#Ð6ˆÐ6àIMÖOÀ”SœœS §¡°	Ó:Ó;Õ<ÐOˆÐOà!%ö(Øô œœS §¡°)Ó<Ó=Õ>ð (ˆð (ð ˆØˆÜ  “ò 	!‰GˆAˆtØ�zÑ!Ø—‘˜dÔ#ØØ�X˜a‘[Ñ ˆEØ�M‰M C¨¢I©tÐ4Ô5Ø�X˜a‘[Ñ ‰Eð	!ð ˆùò 7ùâOùò(s   ÁD'Á*/D,Â/D1N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚprintmethodÚlanguager*   r   r'   Ú__annotations__r)   r7   r:   r>   rD   rI   rU   re   ro   rs   r~   r†   r�   r“   r•   r—   rš   r«   r°   rG   r5   r2   r1   r    r    .   s­   … ñà€KØ€Há(,¨[×-JÑ-Jñ )ØØØñOñ )Ð�~ó ð !#ó /òò"ò$òUò'òBò'ò
8ò ò
8ò9òLòòò*ò*òQòB2ó
r2   r    Nc                ó8   — t        |«      j                  | |«      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    Údoprint)rm   Ú	assign_tor/   s      r1   ÚjscoderÒ   Û   s   € ôR ! Ó*×2Ñ2°4¸ÓCÐCr2   c                ó.   — t        t        | fi |¤Ž«       y)z€Prints the Javascript representation of the given expression.

       See jscode for the meaning of the optional arguments.
    N)ÚprintrÒ   )rm   r/   s     r1   Úprint_jscoderÕ   G  s   € ô
 
Œ&�Ñ
"˜Ñ
"Õ#r2   rF   )rË   Ú
__future__r   Útypingr   Ú
sympy.corer   Úsympy.core.numbersr   Úsympy.printing.codeprinterr   Úsympy.printing.precedencer   r	   r+   r    rÒ   rÕ   r5   r2   r1   ú<module>rÜ      s  ðñõ #Ý å Ý +Ý 2ß <ð
Ø	ˆ:ðà
ˆKðð ˆ\ðð ˆKð	ð
 ˆ\ðð ˆKðð ˆ\ðð ˆ\ðð ˆ{ðð 
ˆ:ðð ˆKðð 
ˆ:ðð ˆ\ðð 
ˆ:ðð 
ˆ:ðð  
ˆ:ð!ð" ˆKð#ð$ ØØØò+€ô2j˜Kô jóZiDóX$r2   