ó
    Š*£hÍ.  ã                  óò   • S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJrJr  0 SS	_S
S_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_S S!_S"S#_S$S%_S&S'_S(S)_S*S+S,S-S..Er " S/ S0\
5      rS4S2 jrS3 rg1)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                  óØ   • \ rS rSr% SrSrSr\" \R                  40 S0 SS.D6r	S\
S	'   0 4S
 jrS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg) ÚJavascriptCodePrinteré.   zK"A Printer to convert Python expressions to strings of JavaScript code
    Ú_javascriptÚ
JavaScripté   T)Ú	precisionÚuser_functionsÚcontractzdict[str, Any]Ú_default_settingsc                ó²   • [         R                  " X5        [        [        5      U l        UR	                  S0 5      nU R                  R                  U5        g )Nr&   )r   Ú__init__ÚdictÚknown_functionsÚgetÚupdate)ÚselfÚsettingsÚ	userfuncss      ÚR/home/mande/repo/quber/.venv/lib/python3.13/site-packages/sympy/printing/jscode.pyr*   ÚJavascriptCodePrinter.__init__:   sB   € Ü×Ò˜TÔ,Ü#¤OÓ4ˆÔØ—L‘LÐ!1°2Ó6ˆ	Ø×Ñ×#Ñ# IÕ.ó    c                ó   • US-  $ )Né   © )r/   Úps     r2   Ú_rate_index_positionÚ*JavascriptCodePrinter._rate_index_position@   s   € Ø�‰sˆ
r4   c                ó   • SU-  $ )Nz%s;r7   )r/   Ú
codestrings     r2   Ú_get_statementÚ$JavascriptCodePrinter._get_statementC   s   € Ø�zÑ!Ð!r4   c                ó$   • SR                  U5      $ )Nz// {})Úformat)r/   Útexts     r2   Ú_get_commentÚ"JavascriptCodePrinter._get_commentF   s   € Ø�~‰~˜dÓ#Ð#r4   c                ó\   • SR                  XR                  U R                  S   5      5      $ )Nzvar {} = {};r%   )r@   ÚevalfÚ	_settings)r/   ÚnameÚvalues      r2   Ú_declare_number_constÚ+JavascriptCodePrinter._declare_number_constI   s%   € Ø×$Ñ$ T¯;©;°t·~±~ÀkÑ7RÓ+SÓTÐTr4   c                ó$   • U R                  U5      $ ©N)Úindent_code)r/   Úliness     r2   Ú_format_codeÚ"JavascriptCodePrinter._format_codeL   s   € Ø×Ñ Ó&Ð&r4   c                óL   ^• UR                   u  nmU4S j[        U5       5       $ )Nc              3  óP   >#   • U  H  n[        T5        H  o!U4v •  M
     M     g 7frL   )Úrange)Ú.0ÚiÚjÚcolss      €r2   Ú	<genexpr>ÚAJavascriptCodePrinter._traverse_matrix_indices.<locals>.<genexpr>Q   s   øé € ÐA¢˜1´U¸4·[°�A•±[‘¢ùs   ƒ#&)ÚshaperS   )r/   ÚmatÚrowsrW   s      @r2   Ú_traverse_matrix_indicesÚ.JavascriptCodePrinter._traverse_matrix_indicesO   s   ø€ Ø—Y‘Y‰
ˆˆdÜA¤ d¤ÓAÐAr4   c           
     ó  • / n/ nSnU Hz  nUR                  UU R                  UR                  5      U R                  UR                  5      U R                  UR                  S-   5      S.-  5        UR                  S5        M|     X#4$ )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Ú	loopstartrU   s         r2   Ú_get_loop_opening_endingÚ.JavascriptCodePrinter._get_loop_opening_endingS   s„   € Øˆ
ØˆØWˆ	ÛˆAà×Ñ˜iØŸ+™+ a§g¡gÓ.ØŸ™ Q§W¡WÓ-Ø—{‘{ 1§7¡7¨Q¡;Ó/ñ+1ñ 1ô 2ð ×Ñ˜sÖ#ñ ð Ð&Ð&r4   c                óö  • [        U5      n[        UR                  S5      (       a  SU R                  UR                  U5      -  $ [        UR                  S5      (       a  SU R                  UR                  5      -  $ UR                  [        R                  S-  :X  a  SU R                  UR                  5      -  $ SU R                  UR                  5      < SU R                  UR                  5      < S	3$ )
Néÿÿÿÿz1/%sg      à?zMath.sqrt(%s)é   zMath.cbrt(%s)z	Math.pow(z, Ú))r   r   r   ÚparenthesizeÚbaserf   r   ÚOne)r/   ÚexprÚPRECs      r2   Ú
_print_PowÚ JavascriptCodePrinter._print_Pow`   s¿   € Ü˜$ÓˆÜ˜Ÿ™ "×%Ñ%Ø˜T×.Ñ.¨t¯y©y¸$Ó?Ñ@Ð@Ü˜$Ÿ(™( C×(Ñ(Ø" T§[¡[°·±Ó%;Ñ;Ð;Ø�X‰XœŸ™˜q™Ó Ø" T§[¡[°·±Ó%;Ñ;Ð;ð !ð *.¯©°T·Y±YÖ)?Ø!%§¡¨T¯X©XÖ!6ð8ð 8r4   c                ó`   • [        UR                  5      [        UR                  5      p2SX#4-  $ )Nz%d/%d)Úintr8   Úq)r/   rw   r8   r}   s       r2   Ú_print_RationalÚ%JavascriptCodePrinter._print_Rationall   s&   € Ü�4—6‘6‹{œC §¡›Kˆ1Ø˜!˜ÑÐr4   c                óJ  • UR                   u  p#[        U5      nUR                    Vs/ s H  oPR                  XT5      PM     snu  pgUR                  (       a  UR                  (       d"  UR                  (       a  UR                  (       a  U SU 3$ SU SU SU SU 3$ s  snf )Nz % ú((z) + z) % )Úargsr   rt   Úis_nonnegativeÚis_nonpositive)r/   rw   ÚnumÚdenrx   ÚargÚsnumÚsdens           r2   Ú
_print_ModÚ JavascriptCodePrinter._print_Modp   s”   € Ø—9‘9‰ˆÜ˜$ÓˆØ>B¿iºiÓHºi°s×'Ñ'¨Ö2¹iÑH‰
ˆð ×× 3×#5×#5Ø×× 3×#5×#5Ø�V˜3˜t˜fÐ%Ð%Ø�D�6˜˜T˜F $ t f¨D°°Ð7Ð7ùò Is   ¨B c                óª   • U R                  UR                  5      nU R                  UR                  5      nUR                  nSR	                  X$U5      $ )Nz{} {} {})rf   ÚlhsÚrhsÚrel_opr@   )r/   rw   Úlhs_codeÚrhs_codeÚops        r2   Ú_print_RelationalÚ'JavascriptCodePrinter._print_Relational|   sB   € Ø—;‘;˜tŸx™xÓ(ˆØ—;‘;˜tŸx™xÓ(ˆØ�[‰[ˆØ× Ñ  ¨xÓ8Ð8r4   c                óR  • UR                   n[        R                  n[        R                  n[	        [        UR                  5      5       H  nX1R                  U   U-  -  nXBU   -  nM      U R                  UR                  R                  5      < SU R                  U5      < S3$ )NÚ[Ú])rZ   r   ÚZerorv   ÚreversedrS   Úrankrj   rf   ru   rg   )r/   rw   ÚdimsÚelemÚoffsetrU   s         r2   Ú_print_IndexedÚ$JavascriptCodePrinter._print_Indexed‚   s}   € à�z‰zˆÜ�v‰vˆÜ—‘ˆÜœ% §	¡	Ó*Ö+ˆAØ—L‘L ‘O FÑ*Ñ*ˆDØ˜1‘gÑŠFñ ,ð  Ÿ;™; t§y¡y§¡Ö7¸¿¹ÀTÖ9JÐKÐKr4   c                ó   • g)NzMath.Er7   ©r/   rw   s     r2   Ú_print_Exp1Ú!JavascriptCodePrinter._print_Exp1Œ   s   € Ør4   c                ó   • g)NzMath.PIr7   r¡   s     r2   Ú	_print_PiÚJavascriptCodePrinter._print_Pi�   s   € Ør4   c                ó   • g)NzNumber.POSITIVE_INFINITYr7   r¡   s     r2   Ú_print_InfinityÚ%JavascriptCodePrinter._print_Infinity’   ó   € Ø)r4   c                ó   • g)NzNumber.NEGATIVE_INFINITYr7   r¡   s     r2   Ú_print_NegativeInfinityÚ-JavascriptCodePrinter._print_NegativeInfinity•   rª   r4   c           
     ó¶  • SSK Jn  UR                  S   R                  S:w  a  [	        S5      e/ nUR                  U5      (       aæ  [        UR                  5       H¼  u  nu  pVUS:X  a$  UR                  SU R                  U5      -  5        OWU[        UR                  5      S-
  :X  a  US:X  a  UR                  S5        O#UR                  S	U R                  U5      -  5        U R                  U5      nUR                  U5        UR                  S
5        M¾     SR                  U5      $ UR                  S S  VVs/ s H.  u  pVSU R                  U5      < SU R                  U5      < S3PM0     nnnSU R                  UR                  S   R                  5      -  n	SR                  U5      U	-   SR                  S[        U5      -  /5      -   $ s  snnf )Nr   )Ú
Assignmentrq   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) {r`   zelse {zelse if (%s) {rd   Ú
r�   z) ? (
z
)
z: (
%s
)z: Ú rs   )Úsympy.codegen.astr¯   r‚   ÚcondÚ
ValueErrorÚhasÚ	enumeratere   rf   ÚlenÚjoinrw   )
r/   rw   r¯   rN   rU   ÚeÚcÚcode0ÚecpairsÚ	last_lines
             r2   Ú_print_PiecewiseÚ&JavascriptCodePrinter._print_Piecewise˜   sŽ  € Ý0Ø�9‰9�R‰=×Ñ Ó%ô ð /ó 0ð 0ð
 ˆØ�8‰8�J×ÑÜ& t§y¡yÖ1‘	�‘6�AØ˜“6Ø—L‘L ¨t¯{©{¸1«~Ñ!=Õ>Øœ#˜dŸi™i›.¨1Ñ,Ó,°°d³Ø—L‘L Õ*à—L‘LÐ!1°D·K±KÀ³NÑ!BÔCØŸ™ A›�Ø—‘˜UÔ#Ø—‘˜SÖ!ñ 2ð —9‘9˜UÓ#Ð#ð !%§	¡	¨#¨2¡ô0Ú .™˜˜1ð 04¯{©{¸1®~¸t¿{¹{È1¾~ÓNÙ .ð ñ 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   Ä?5Gc                óÈ   • SR                  U R                  UR                  [        S   SS9UR                  UR
                  UR                  R                  S   -  -   5      $ )Nz{}[{}]ÚAtomT)Ústrictr`   )r@   rt   Úparentr	   rV   rU   rZ   r¡   s     r2   Ú_print_MatrixElementÚ*JavascriptCodePrinter._print_MatrixElement¹   sY   € Ø�‰˜t×0Ñ0°·±Ü�vÑ tð  1ð  -à�F‰F�T—V‘V˜DŸK™K×-Ñ-¨aÑ0Ñ0Ñ0ó2ð 	2r4   c                óŒ  • [        U[        5      (       a1  U R                  UR                  S5      5      nSR	                  U5      $ SnSnSnU Vs/ s H  ofR                  S5      PM     nnU Vs/ s H+  n[        [        [        UR                  U5      5      5      PM-     nnU Vs/ s H+  n[        [        [        UR                  U5      5      5      PM-     nn/ n	Sn
[        U5       HE  u  p¶US;   a  U	R                  U5        M  X¨U   -  n
U	R                  X:-  < U< 35        X§U   -  n
MG     U	$ s  snf s  snf s  snf )	z0Accepts a string of code or a list of code linesTÚ z   )Ú{Ú(z{
z(
)rd   rs   z 	r   )rÇ   r°   )Ú
isinstanceÚstrrM   Ú
splitlinesr¸   Úlstripr|   ÚanyÚmapÚendswithÚ
startswithr¶   re   )r/   ÚcodeÚ
code_linesÚtabÚ	inc_tokenÚ	dec_tokenÚlineÚincreaseÚdecreaseÚprettyÚlevelÚns               r2   rM   Ú!JavascriptCodePrinter.indent_code¾   s3  € ô �dœC× Ñ Ø×)Ñ)¨$¯/©/¸$Ó*?Ó@ˆJØ—7‘7˜:Ó&Ð&àˆØ,ˆ	Øˆ	á04Ó6²¨—‘˜UÖ#±ˆÐ6áIMÓOÊÀ”SœœS §¡°	Ó:Ó;Ö<ÉˆÐOá!%ó(Ú!%˜ô œœS §¡°)Ó<Ó=Ö>Ù!%ð 	ð (ð ˆØˆÜ  –‰GˆAØ�zÓ!Ø—‘˜dÔ#ÙØ˜a‘[Ñ ˆEØ�M‰M C£IªtÐ4Ô5Ø˜a‘[Ñ ŠEñ 'ð ˆùò 7ùâOùò(s   ÁD7Á12D<Â)2E)r,   N) Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚprintmethodÚlanguager+   r   r(   Ú__annotations__r*   r9   r=   rB   rI   rO   r]   rn   ry   r~   rŠ   r“   rž   r¢   r¥   r¨   r¬   r¾   rÄ   rM   Ú__static_attributes__r7   r4   r2   r    r    .   s­   ‡ ñà€KØ€Há(,¨[×-JÑ-Jñ )ØØØñOñ )Ð�~ó ð !#ô /òò"ò$òUò'òBò'ò
8ò ò
8ò9òLòòò*ò*òQòB2õ
r4   r    Nc                ó6   • [        U5      R                  X5      $ )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)rw   Ú	assign_tor0   s      r2   Újscoderê   Û   s   € ôR ! Ó*×2Ñ2°4ÓCÐCr4   c                ó.   • [        [        U 40 UD65        g)zuPrints the Javascript representation of the given expression.

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