� ��g/����dZddlmZddlmZddlmZddlmZddl m Z ddl m Z m Z idd �d d �d d �dd�dd�dd�dd�dd�dd�dd�dd�dd�d d!�d"d#�d$d%�d&d'�d(d)�d*d+d,d-d.��ZGd/�d0e ��Zd4d2�Zd3�Zd1S)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�AbszMath.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�coszMath.cos�coshz Math.cosh�expzMath.exp�floorz Math.floor�logzMath.log�MaxzMath.max�MinzMath.min�signz Math.signzMath.sinz Math.sinhzMath.tanz Math.tanh)�sin�sinh�tan�tanhc���eZdZUdZdZdZeejfididd���Zde d<ifd �Z d �Z d �Z d �Z d �Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZdS)�JavascriptCodePrinterzK"A Printer to convert Python expressions to strings of JavaScript code � _javascript� JavaScript�T)� precision�user_functions�contractzdict[str, Any]�_default_settingsc���tj||��tt��|_|�di��}|j�|��dS)Nr%)r�__init__�dict�known_functions�get�update)�self�settings� userfuncss �e/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/printing/jscode.pyr)zJavascriptCodePrinter.__init__:sV����T�8�,�,�,�#�O�4�4����L�L�!1�2�6�6� � ��#�#�I�.�.�.�.�.�c� �|dzS)N��)r.�ps r1�_rate_index_positionz*JavascriptCodePrinter._rate_index_position@s ����s� r2c� �d|zS)Nz%s;r5)r.� codestrings r1�_get_statementz$JavascriptCodePrinter._get_statementCs ���z�!�!r2c�,�d�|��S)Nz// {})�format)r.�texts r1� _get_commentz"JavascriptCodePrinter._get_commentFs���~�~�d�#�#�#r2c�j�d�||�|jd����S)Nz var {} = {};r$)r<�evalf� _settings)r.�name�values r1�_declare_number_constz+JavascriptCodePrinter._declare_number_constIs+���$�$�T�5�;�;�t�~�k�7R�+S�+S�T�T�Tr2c�,�|�|��S�N)� indent_code)r.�liness r1� _format_codez"JavascriptCodePrinter._format_codeLs������&�&�&r2c�N��|j\}��fd�t|��D��S)Nc3�D�K�|]}t���D]}||fV�� �dSrF)�range)�.0�i�j�colss �r1� <genexpr>zAJavascriptCodePrinter._traverse_matrix_indices.<locals>.<genexpr>Qs:�����A�A�1�U�4�[�[�A�A���A��A�A�A�A�A�A�Ar2)�shaperL)r.�mat�rowsrPs @r1�_traverse_matrix_indicesz.JavascriptCodePrinter._traverse_matrix_indicesOs.����Y� ��d�A�A�A�A��d� � �A�A�A�Ar2c ��g}g}d}|D]~}|�||�|j��|�|j��|�|jdz��d�z��|�d���||fS)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� loopstartrNs r1�_get_loop_opening_endingz.JavascriptCodePrinter._get_loop_opening_endingSs���� �� �W� �� $� $�A� � � �i��+�+�a�g�.�.����Q�W�-�-��{�{�1�7�Q�;�/�/�+1�+1�1� 2� 2� 2� � � �s� #� #� #� #��;�&�&r2c���t|��}t|jd��rd|�|j|��zSt|jd��rd|�|j��zS|jt jdz krd|�|j��zSd|�|j���d|�|j���d �S) N�����z1/%sg�?z Math.sqrt(%s)�z Math.cbrt(%s)z Math.pow(z, �))rrr� 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� 8r2c�`�t|j��t|j��}}d||fzS)Nz%d/%d)�intr6�q)r.rmr6rrs r1�_print_Rationalz%JavascriptCodePrinter._print_Rationalls*���4�6�{�{�C���K�K�1���!�Q���r2c�����|j\}}t|�����fd�|jD��\}}|jr|js|jr|jr|�d|��Sd|�d|�d|�d|��S)Nc�<��g|]}��|�����Sr5)rj)rM�argrnr.s ��r1� <listcomp>z4JavascriptCodePrinter._print_Mod.<locals>.<listcomp>ss)���H�H�H�s�d�'�'��T�2�2�H�H�Hr2z % �((z) + z) % )�argsr�is_nonnegative�is_nonpositive)r.rm�num�den�snum�sdenrns` @r1� _print_Modz JavascriptCodePrinter._print_Modps������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�7r2c��|�|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�8r2c�4�|j}tj}tj}t t |j����D] }||j||zz }|||z}�!|�|j j ���d|�|���d�S)N�[�]) rRr�Zerorl�reversedrL�rankrar]rkr^)r.rm�dims�elem�offsetrNs 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�Kr2c�6�|�|j��SrF)r]r^�r.rms r1� _print_Idxz JavascriptCodePrinter._print_Idx�s���{�{�4�:�&�&�&r2c��dS)NzMath.Er5r�s r1� _print_Exp1z!JavascriptCodePrinter._print_Exp1�s���xr2c��dS)NzMath.PIr5r�s r1� _print_PizJavascriptCodePrinter._print_Pi�s���yr2c��dS)NzNumber.POSITIVE_INFINITYr5r�s r1�_print_Infinityz%JavascriptCodePrinter._print_Infinity����)�)r2c��dS)NzNumber.NEGATIVE_INFINITYr5r�s r1�_print_NegativeInfinityz-JavascriptCodePrinter._print_NegativeInfinity�r�r2c���ddlm}|jdjdkrt d���g}|�|���rt |j��D]�\}\}}|dkr,|�d��|��z��nb|t|j��dz kr|dkr|�d��n+|�d ��|��z����|��}|�|��|�d ����d � |��S�fd �|jdd�D��}d ��|jdj ��z} d� |��| zd� dt|��zg��zS)Nr)� AssignmentrgTz�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) {rWzelse {zelse if (%s) {r[� c�t��g|]4\}}d��|���d��|���d���5S)rxz) ? ( z ) )r])rM�e�cr.s �r1rwz:JavascriptCodePrinter._print_Piecewise.<locals>.<listcomp>�sP���0�0�0���1�1�04�{�{�1�~�~�~�~�t�{�{�1�~�~�~�~�N�0�0�0r2z: ( %s )z: � ri) �sympy.codegen.astr�ry�cond� ValueError�has� enumerater\r]�len�joinrm) r.rmr�rHrNr�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� Pr2c��d�|�|jtdd���|j|j|jjdzz��S)Nz{}[{}]�AtomT)�strictrW)r<rj�parentr rOrNrRr�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� 2r2c��� � �t|t��r=|�|�d����}d�|��Sd}d� d� d�|D��}� fd�|D��}� fd�|D��}g}d }t |��D]Q\}} | d vr|�| ���|||z}|�||z�| ����|||z }�R|S) z0Accepts a string of code or a list of code linesT�z )�{�(z{ z( )r[ric�8�g|]}|�d����S)z )�lstrip)rM�lines r1rwz5JavascriptCodePrinter.indent_code.<locals>.<listcomp>�s$��6�6�6�����U�#�#�6�6�6r2c �n��g|]1}ttt|j���������2Sr5)rq�any�map�endswith)rMr�� inc_tokens �r1rwz5JavascriptCodePrinter.indent_code.<locals>.<listcomp>�s5���O�O�O��S��S��� �:�:�;�;�<�<�O�O�Or2c �n��g|]1}ttt|j���������2Sr5)rqr�r�� startswith)rMr�� dec_tokens �r1rwz5JavascriptCodePrinter.indent_code.<locals>.<listcomp>�sE���(�(�(����S���)�<�<�=�=�>�>�(�(�(r2r)r�r�)� isinstance�strrG� splitlinesr�r�r\) r.�code� code_lines�tab�increase�decrease�pretty�level�nr�r�r�s @@r1rGz!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�� r2N)�__name__� __module__� __qualname__�__doc__� printmethod�languager*rr'�__annotations__r)r7r:r>rDrIrUrerorsr�r�r�r�r�r�r�r�r�r�rGr5r2r1r 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�B2�2�2� ����r2r 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�Cr2c �:�tt|fi|����dS)z�Prints the Javascript representation of the given expression. See jscode for the meaning of the optional arguments. N)�printr�)rmr/s r1� print_jscoder�Js(��  �&�� "� "�� "� "�#�#�#�#�#r2rF)r�� __future__r�typingr� sympy.corer�sympy.core.numbersr�sympy.printing.codeprinterr�sympy.printing.precedencerr r+r r�r�r5r2r1�<module>r�s�����#�"�"�"�"�"�������������+�+�+�+�+�+�2�2�2�2�2�2�<�<�<�<�<�<�<�<� � �:�� �K�� �\�� �K� �  �\� �  �K� � �\�� �\���{�� �:�� �K�� �:�� �\�� �:�� �:��  �:�!�" �K�#�$ � � � �+����2m�m�m�m�m�K�m�m�m�`iD�iD�iD�iD�X$�$�$�$�$r2
Memory