§
    OŠtjÍ.  ã                  óø   — 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d1S )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dS )Ú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          ¦  «        | _        |                     di ¦  «        }| j                             |¦  «         d S )Nr%   )r   Ú__init__ÚdictÚknown_functionsÚgetÚupdate)ÚselfÚsettingsÚ	userfuncss      úS/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/printing/jscode.pyr)   zJavascriptCodePrinter.__init__:   sV   € ÝÔ˜T 8Ñ,Ô,Ð,Ý#¥OÑ4Ô4ˆÔØ—L’LÐ!1°2Ñ6Ô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                      |¦  «        S )Nz// {})Úformat)r.   Útexts     r1   Ú_get_commentz"JavascriptCodePrinter._get_commentF   s   € Ø�~Š~˜dÑ#Ô#Ð#r2   c                ój   — d                      ||                     | 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Ô+SÑTÔTÐTr2   c                ó,   — |                       |¦  «        S ©N)Úindent_code)r.   Úliness     r1   Ú_format_codez"JavascriptCodePrinter._format_codeL   s   € Ø×Ò Ñ&Ô&Ð&r2   c                óN   ‡— |j         \  }Šˆfd„t          |¦  «        D ¦   «         S )Nc              3  óD   •K  — | ]}t          ‰¦  «        D ]}||fV — Œ	Œd S rF   )Úrange)Ú.0ÚiÚjÚcolss      €r1   ú	<genexpr>zAJavascriptCodePrinter._traverse_matrix_indices.<locals>.<genexpr>Q   s:   øè è € ÐAÐA˜1µU¸4±[´[ÐAÐA°��A�ÐAÐAÐAÐAÐAÐAÐAr2   )ÚshaperL   )r.   ÚmatÚrowsrP   s      @r1   Ú_traverse_matrix_indicesz.JavascriptCodePrinter._traverse_matrix_indicesO   s.   ø€ Ø”Y‰
ˆˆdØAÐAÐAÐA¥ d¡¤ÐAÑAÔAÐAr2   c           
     ó  — g }g }d}|D ]~}|                      ||                      |j        ¦  «        |                      |j        ¦  «        |                      |j        dz   ¦  «        dœz  ¦  «         |                      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Ñ.Ô.ØŸš Q¤WÑ-Ô-Ø—{’{ 1¤7¨Q¡;Ñ/Ô/ð+1ð +1ñ 1ñ 2ô 2ð 2ð ×Ò˜sÑ#Ô#Ð#Ð#Ø˜;Ð&Ð&r2   c                óÆ  — t          |¦  «        }t          |j        d¦  «        rd|                      |j        |¦  «        z  S t          |j        d¦  «        rd|                      |j        ¦  «        z  S |j        t          j        dz  k    rd|                      |j        ¦  «        z  S d|                      |j        ¦  «        ›d|                      |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Ø   € Ý˜$ÑÔˆÝ˜œ "Ñ%Ô%ð 	8Ø˜T×.Ò.¨t¬y¸$Ñ?Ô?Ñ@Ð@Ý˜$œ( CÑ(Ô(ð 	8Ø" T§[¢[°´Ñ%;Ô%;Ñ;Ð;ØŒX�œ˜q™Ò Ð Ø" T§[¢[°´Ñ%;Ô%;Ñ;Ð;ð !ð *.¯ª°T´YÑ)?Ô)?Ð)?Ð)?Ø!%§¢¨T¬XÑ!6Ô!6Ð!6Ð!6ð8ð 8r2   c                ó`   — 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‰{Œ{�C ¤™KœKˆ1ˆØ˜!˜Q˜ÑÐr2   c                óÈ   ‡ ‡— |j         \  }}t          |¦  «        Šˆˆ fd„|j         D ¦   «         \  }}|j        r|j        s|j        r|j        r|› d|› �S d|› d|› d|› d|› �S )Nc                ó<   •— g | ]}‰                      |‰¦  «        ‘ŒS r5   )rj   )rM   Úargrn   r.   s     €€r1   ú
<listcomp>z4JavascriptCodePrinter._print_Mod.<locals>.<listcomp>s   s)   ø€ ÐHÐHÐH°s�d×'Ò'¨¨TÑ2Ô2ÐHÐHÐHr2   z % ú((z) + z) % )Úargsr   Úis_nonnegativeÚis_nonpositive)r.   rm   ÚnumÚdenÚsnumÚsdenrn   s   `     @r1   Ú
_print_Modz JavascriptCodePrinter._print_Modp   s¥   øø€ Ø”9‰ˆˆSÝ˜$ÑÔˆØHÐHÐHÐHÐH¸d¼iÐHÑHÔH‰
ˆˆdð Ôð 	& 3Ô#5ð 	&ØÔð	&Ø#&Ô#5ð	&àÐ%Ð%˜tÐ%Ð%Ð%Ø7�DÐ7Ð7˜TÐ7Ð7 tÐ7Ð7°Ð7Ð7Ð7r2   c                ó¦   — |                       |j        ¦  «        }|                       |j        ¦  «        }|j        }d                     |||¦  «        S )Nz{} {} {})r]   ÚlhsÚrhsÚrel_opr<   )r.   rm   Úlhs_codeÚrhs_codeÚops        r1   Ú_print_Relationalz'JavascriptCodePrinter._print_Relational|   sG   € Ø—;’;˜tœxÑ(Ô(ˆØ—;’;˜tœxÑ(Ô(ˆØŒ[ˆØ× Ò  ¨2¨xÑ8Ô8Ð8r2   c                ó4  — |j         }t          j        }t          j        }t	          t          |j        ¦  «        ¦  «        D ] }||j        |         |z  z  }|||         z  }Œ!|                      |j	        j
        ¦  «        ›d|                      |¦  «        ›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ˆÝŒvˆÝ”ˆÝ�% ¤	Ñ*Ô*Ñ+Ô+ð 	ð 	ˆAØ�D”L ”O FÑ*Ñ*ˆDØ�d˜1”gÑˆFˆFØŸ;š; t¤y¤Ñ7Ô7Ð7Ð7¸¿ºÀTÑ9JÔ9JÐ9JÐ9JÐKÐKr2   c                ó   — dS )NzMath.Er5   ©r.   rm   s     r1   Ú_print_Exp1z!JavascriptCodePrinter._print_Exp1Œ   s   € Øˆxr2   c                ó   — dS )NzMath.PIr5   r”   s     r1   Ú	_print_PizJavascriptCodePrinter._print_Pi�   s   € Øˆyr2   c                ó   — dS )NzNumber.POSITIVE_INFINITYr5   r”   s     r1   Ú_print_Infinityz%JavascriptCodePrinter._print_Infinity’   ó   € Ø)Ð)r2   c                ó   — dS )NzNumber.NEGATIVE_INFINITYr5   r”   s     r1   Ú_print_NegativeInfinityz-JavascriptCodePrinter._print_NegativeInfinity•   rš   r2   c                ó˜  ‡ — ddl m} |j        d         j        dk    rt	          d¦  «        ‚g }|                     |¦  «        �rt          |j        ¦  «        D ]Û\  }\  }}|dk    r,|                     d‰                      |¦  «        z  ¦  «         nb|t          |j        ¦  «        dz
  k    r|dk    r|                     d¦  «         n+|                     d	‰                      |¦  «        z  ¦  «         ‰                      |¦  «        }|                     |¦  «         |                     d
¦  «         ŒÜd 
                    |¦  «        S ˆ fd„|j        d d…         D ¦   «         }d‰                      |j        d         j        ¦  «        z  }	d 
                    |¦  «        |	z   d 
                    dt          |¦  «        z  g¦  «        z   S )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[   ú
c                ót   •— g | ]4\  }}d ‰                      |¦  «        ›d‰                      |¦  «        ›d�‘Œ5S )rx   z) ? (
z
)
)r]   )rM   ÚeÚcr.   s      €r1   rw   z:JavascriptCodePrinter._print_Piecewise.<locals>.<listcomp>´   sP   ø€ ð 0ð 0ð 0Ù˜˜1˜1ð 04¯{ª{¸1©~¬~¨~¨~¸t¿{º{È1¹~¼~¸~¸~ÐNð 0ð 0ð 0r2   z: (
%s
)z: ú ri   )Úsympy.codegen.astrž   ry   ÚcondÚ
ValueErrorÚhasÚ	enumerater\   r]   ÚlenÚjoinrm   )
r.   rm   rž   rH   rN   r¡   r¢   Úcode0ÚecpairsÚ	last_lines
   `         r1   Ú_print_Piecewisez&JavascriptCodePrinter._print_Piecewise˜   sÒ  ø€ Ø0Ð0Ð0Ð0Ð0Ð0ØŒ9�RŒ=Ô Ò%Ð%õ ð /ñ 0ô 0ð 0ð
 ˆØ�8Š8�JÑÔñ 	QÝ& t¤yÑ1Ô1ð 	"ð 	"‘	�‘6�A�qØ˜’6�6Ø—L’L ¨t¯{ª{¸1©~¬~Ñ!=Ñ>Ô>Ð>Ð>Ø�#˜dœi™.œ.¨1Ñ,Ò,Ð,°°d²°Ø—L’L Ñ*Ô*Ð*Ð*à—L’LÐ!1°D·K²KÀ±N´NÑ!BÑCÔCÐCØŸš A™œ�Ø—’˜UÑ#Ô#Ð#Ø—’˜SÑ!Ô!Ð!Ð!Ø—9’9˜UÑ#Ô#Ð#ð0ð 0ð 0ð 0Ø $¤	¨#¨2¨#¤ð0ñ 0ô 0ˆGà$ t§{¢{°4´9¸R´=Ô3EÑ'FÔ'FÑFˆIØ—9’9˜WÑ%Ô%¨	Ñ1°C·H²H¸cÅ#ÀgÁ,Ä,Ñ>NÐ=OÑ4PÔ4PÑPÐPr2   c                óº   — d                      |                      |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¹   sX   € Ø�Š˜t×0Ò0°´Ý�vÔ tð  1ñ  -ô  -àŒF�T”V˜DœKÔ-¨aÔ0Ñ0Ñ0ñ2ô 2ð 	2r2   c                óÔ  ‡
‡— t          |t          ¦  «        r=|                      |                     d¦  «        ¦  «        }d                     |¦  «        S d}dŠdŠ
d„ |D ¦   «         }ˆfd„|D ¦   «         }ˆ
fd„|D ¦   «         }g }d	}t          |¦  «        D ]Q\  }}	|	d
v r|                     |	¦  «         Œ|||         z  }|                     ||z  ›|	›�¦  «         |||         z  }ŒR|S )z0Accepts a string of code or a list of code linesTÚ z   )Ú{ú(z{
z(
)r[   ri   c                ó8   — g | ]}|                      d ¦  «        ‘ŒS )z 	)Úlstrip)rM   Úlines     r1   rw   z5JavascriptCodePrinter.indent_code.<locals>.<listcomp>É   s$   € Ð6Ð6Ð6¨�—’˜UÑ#Ô#Ð6Ð6Ð6r2   c           
     ón   •— g | ]1}t          t          t          |j        ‰¦  «        ¦  «        ¦  «        ‘Œ2S r5   )rq   ÚanyÚmapÚendswith)rM   rº   Ú	inc_tokens     €r1   rw   z5JavascriptCodePrinter.indent_code.<locals>.<listcomp>Ë   s5   ø€ ÐOÐOÐOÀ•S��S ¤°	Ñ:Ô:Ñ;Ô;Ñ<Ô<ÐOÐOÐOr2   c           
     ón   •— g | ]1}t          t          t          |j        ‰¦  «        ¦  «        ¦  «        ‘Œ2S r5   )rq   r¼   r½   Ú
startswith)rM   rº   Ú	dec_tokens     €r1   rw   z5JavascriptCodePrinter.indent_code.<locals>.<listcomp>Ì   sE   ø€ ð (ð (ð (Øõ ��S ¤°)Ñ<Ô<Ñ=Ô=Ñ>Ô>ð (ð (ð (r2   r   )rµ   rŸ   )Ú
isinstanceÚstrrG   Ú
splitlinesrª   r¨   r\   )r.   ÚcodeÚ
code_linesÚtabÚincreaseÚdecreaseÚprettyÚlevelÚnrº   rÂ   r¿   s             @@r1   rG   z!JavascriptCodePrinter.indent_code¾   s3  øø€ õ �d�CÑ Ô ð 	'Ø×)Ò)¨$¯/ª/¸$Ñ*?Ô*?Ñ@Ô@ˆJØ—7’7˜:Ñ&Ô&Ð&àˆØ,ˆ	Øˆ	à6Ð6°Ð6Ñ6Ô6ˆàOÐOÐOÐOÈÐOÑOÔOˆð(ð (ð (ð (Ø!%ð(ñ (ô (ˆð ˆØˆÝ  ‘”ð 	!ð 	!‰GˆAˆtØ�zÐ!Ð!Ø—’˜dÑ#Ô#Ð#ØØ�X˜a”[Ñ ˆEØ�MŠM C¨¡I I¨t¨tÐ4Ñ5Ô5Ð5Ø�X˜a”[Ñ ˆEˆEØˆr2   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ð )ð )ØØØðOð Oð )ð )Ðð ð ð ñ ð !#ð /ð /ð /ð /ðð ð ð"ð "ð "ð$ð $ð $ðUð Uð Uð'ð 'ð 'ðBð Bð Bð'ð 'ð 'ð
8ð 
8ð 
8ð ð  ð  ð
8ð 
8ð 
8ð9ð 9ð 9ðLð Lð Lðð ð ðð ð ð*ð *ð *ð*ð *ð *ðQð Qð QðB2ð 2ð 2ð
ð ð ð ð r2   r    Nc                óH   — 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    Údoprint)rm   Ú	assign_tor/   s      r1   ÚjscoderØ   Û   s#   € õR ! Ñ*Ô*×2Ò2°4¸ÑCÔCÐCr2   c                ó:   — t          t          | fi |¤Ž¦  «         dS )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Ð 2Ð 2Ð 2Ð 2Ð 2Ø <Ð <Ð <Ð <Ð <Ð <Ð <Ð <ð
Ø	ˆ:ðà
ˆKðð ˆ\ðð ˆKð	ð
 ˆ\ðð ˆKðð ˆ\ðð ˆ\ðð ˆ{ðð 
ˆ:ðð ˆKðð 
ˆ:ðð ˆ\ðð 
ˆ:ðð 
ˆ:ðð  
ˆ:ð!ð" ˆKð#ð$ ØØØð+ð ð €ð2jð jð jð jð j˜Kñ jô jð jðZiDð iDð iDð iDðX$ð $ð $ð $ð $r2   