� ��g�R� �P�dZddlmZddlmZddlmZmZmZm Z ddl m Z ddl m Z idd�dd �d d �d d �dd�ddfd�ddfd�ddfd�ddfd�ddfd�ddfd�ddfg�d d!�d dfg�d"d#�d$d%�d&d'�d(d(�d)d)�d*d*�d+d+�d,d,�d-d-�d.d.�d/d/�d0d1d2d3d4dd5��Zgd6�ZGd7�d8e ��Zd<d:�Zd;�Zd9S)=a Rust code printer The `RustCodePrinter` converts SymPy expressions into Rust expressions. A complete code generator, which uses `rust_code` extensively, can be found in `sympy.utilities.codegen`. The `codegen` module can be used to generate complete source code files. �)� annotations)�Any)�S�Rational�Float�Lambda�� equal_valued)� CodePrinter�floor�ceiling�ceil�Abs�abs�sign�signum�Powc�"�t|d��S)N�����r ��base�exps �c/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/printing/rust.py�<lambda>rIs��|�C��4�4���recip�c�"�t|d��S)Ng�?r rs rrrJs��|�C��5�5�r�sqrtc�"�t|d��S)Ng�r rs rrrKs��|�C��6�6�rz sqrt().recipc�*�|tdd��kS)N��)rrs rrrLs��s�h�q�!�n�n�4�r�cbrtc�"�t|d��S)Nrr rs rrrMs��|�D�!�4�4�r�exp2r#c��|jS�N�� is_integerrs rrrNs��s�~�r�powir"c��|j Sr(r)rs rrrOs ��3�>�1�r�powfrc��dS)NT�)rs rrrPs���r�log�ln�Max�max�Min�min�sin�cos�tan�asin�acos�atan�atan2�sinh�cosh�tanh�asinh�acosh�atanh)r>r?r@rArBr)4�abstract�alignof�as�become�box�break�const�continue�crate�do�else�enum�extern�false�final�fn�for�if�impl�in�let�loop�macro�match�mod�move�mut�offsetof�override�priv�proc�pub�pure�ref�return�Self�self�sizeof�static�struct�super�trait�true�type�typeof�unsafe�unsized�use�virtual�where�while�yieldc �R��eZdZUdZdZdZeejfidide ��d���Zde d<ifd �Z d �Z d �Z d �Zd �Zd�Zd�Zd�Zd�Zd�Zd�Zd,�fd� Zd,�fd� Zd�Zd�Zd�Zd�Zd�Zd,d�Zd,d�Zd,d�Zd,d�Z d,d �Z!d,d!�Z"d,d"�Z#d,d#�Z$d$�Z%d%�Z&d&�Z'd'�Z(d(�Z)�fd)�Z*d*�Z+d+�Z,�xZ-S)-�RustCodePrinterz>A printer to convert SymPy expressions to strings of Rust code� _rust_code�Rust�T)� precision�user_functions�contract� dereferencezdict[str, Any]�_default_settingsc�D�tj||��tt��|_|�di��}|j�|��t |�dg����|_t t��|_dS)Nr}r) r �__init__�dict�known_functions�get�update�set� _dereference�reserved_words)rg�settings� userfuncss rr�zRustCodePrinter.__init__�s�����T�8�,�,�,�#�O�4�4����L�L�!1�2�6�6� � ��#�#�I�.�.�.��� � �]�B� ?� ?�@�@���!�.�1�1����rc� �|dzS)N�r/)rg�ps r�_rate_index_positionz$RustCodePrinter._rate_index_position�s ����s� rc� �d|zS)Nz%s;r/)rg� codestrings r�_get_statementzRustCodePrinter._get_statement�s ���z�!�!rc� �d|zS)Nz// %sr/)rg�texts r� _get_commentzRustCodePrinter._get_comment�s ����~�rc��d|�d|�d�S)Nzconst z: f64 = �;r/)rg�name�values r�_declare_number_constz%RustCodePrinter._declare_number_const�s���(,���e�e�e�4�4rc�,�|�|��Sr()� indent_code)rg�liness r� _format_codezRustCodePrinter._format_code�s������&�&�&rc�N��|j\}��fd�t|��D��S)Nc3�D�K�|]}t���D]}||fV�� �dSr()�range)�.0�i�j�colss �r� <genexpr>z;RustCodePrinter._traverse_matrix_indices.<locals>.<genexpr>s:�����A�A�1�U�4�[�[�A�A���A��A�A�A�A�A�A�Ar)�shaper�)rg�mat�rowsr�s @r�_traverse_matrix_indicesz(RustCodePrinter._traverse_matrix_indices�s.����Y� ��d�A�A�A�A��d� � �A�A�A�Arc ��g}g}d}|D]y}|�||�|��|�|j��|�|jdz��d�z��|�d���z||fS)Nz#for %(var)s in %(start)s..%(end)s {r")�var�start�end�})�append�_print�lower�upper)rg�indices� open_lines� close_lines� loopstartr�s r�_get_loop_opening_endingz(RustCodePrinter._get_loop_opening_endings���� �� �9� �� $� $�A� � � �i��{�{�1�~�~����Q�W�-�-��{�{�1�7�Q�;�/�/�+1�+1�1� 2� 2� 2� � � �s� #� #� #� #��;�&�&rc���t|j��dkrd|�|��zdzS|jr|�|d���S|�|��S)Nr"�(�)T)�_type)�len�argsr�� is_number�rg�exprs r�_print_caller_varz!RustCodePrinter._print_caller_varse�� �t�y�>�>�A� � �����T�*�*�*�S�0� 0� �^� %��;�;�t�4�;�0�0� 0��;�;�t�$�$� $rc�d��|jj�jv�r2�j|jj}d}d}t|t��r|}n|D]\}}}||j�rn�|��|dkrcd��|jd��|t|j��dkr#��|jdd�d��ndd�z}n{|dkr'd ��|jd��|d �z}nN|d kr'd ��|jd��|d �z}n!d |��|jd��d �z}|SdSt|d��r<t|j t��r"�� |j |j���S|jj�j vrv�j |jj\}}��|��rCt�fd�|D����r*�� |�|����SdSdS��|��S)a< basic function for printing `Function` Function Style : 1. args[0].func(args[1:]), method with arguments 2. args[0].func(), method without arguments 3. args[1].func(), method without arguments (e.g. (e, x) => x.exp()) 4. func(args), function with arguments Nr"z%(var)s.%(method)s(%(args)s)r�, �)r��methodr�rz%(var)s.%(method)s())r�r�r#z%(func)s(%(args)s))�funcr��_imp_c3�B�K�|]}��|��V��dSr()� _can_print)r��frgs �rr�z2RustCodePrinter._print_Function.<locals>.<genexpr>Ms/�����0Y�0Y������1C�1C�0Y�0Y�0Y�0Y�0Y�0Yr)r��__name__r�� isinstance�strr�r�r�� stringify�hasattrr�rr��_rewriteable_functionsr��all�rewrite�_print_not_supported) rgr�� cond_funcr��style�cond�ret�target_f� required_fss ` r�_print_FunctionzRustCodePrinter._print_Functions���� �9� ��!5� 5� 5��,�T�Y�-?�@�I��D��E��)�S�)�)� � ���)2���%�D�$���t�T�Y�'��������A�:�:�8�#�5�5�d�i��l�C�C�"&�GJ�4�9�~�~�XY�GY�GY����t�y����}�d� C� C� C�_a�<�<��C�C� �a�Z�Z�0�#�5�5�d�i��l�C�C�"&�4�4��C�C��a�Z�Z�0�#�5�5�d�i��l�C�C�"&�4�4��C�C� /� $� $���t�y�$� ?� ?�2�2��C�� �- ��.�T�7� #� #� 3� �4�:�v�(F�(F� 3��;�;�z�t�z�4�9�5�6�6� 6� �Y� �4�#>� >� >�$(�$?�� �@R�$S� !�H�k����x�(�(� ;�S�0Y�0Y�0Y�0Y�[�0Y�0Y�0Y�-Y�-Y� ;��{�{�4�<�<��#9�#9�:�:�:� ;� ;� ;� ;��,�,�T�2�2� 2rc���|jjrQ|jjsEt|��t |j��|j��}|�|��S|�|��Sr()rr*rrnrr�r�r�s r� _print_PowzRustCodePrinter._print_PowRsd�� �9� � %���(;� %��4��:�:�e�D�I�.�.���9�9�D��;�;�t�$�$� $��#�#�D�)�)�)rFc�X��t���|��}|r|dzS|S)N�_f64)rk� _print_Float�rgr�r�r�� __class__s �rr�zRustCodePrinter._print_FloatXs1����g�g�"�"�4�(�(�� � ���<� ��Jrc�X��t���|��}|r|dzS|S)N�_i32)rk�_print_Integerr�s �rr�zRustCodePrinter._print_Integer_s1����g�g�$�$�T�*�*�� � ���<� ��Jrc�`�t|j��t|j��}}d||fzS)Nz %d_f64/%d.0)�intr��q)rgr�r�r�s r�_print_RationalzRustCodePrinter._print_Rationalfs*���4�6�{�{�C���K�K�1����1�v�%�%rc��|�|j��}|�|j��}|j}d�|||��S)Nz{} {} {})r��lhs�rhs�rel_op�format)rgr��lhs_code�rhs_code�ops r�_print_Relationalz!RustCodePrinter._print_RelationaljsG���;�;�t�x�(�(���;�;�t�x�(�(�� �[��� � ��2�x�8�8�8rc�4�|j}tj}tj}t t |j����D] }||j||zz }|||z}�!|�|j j ���d|�|���d�S)N�[�]) r�r�Zero�One�reversedr��rankr�r�r�label)rgr��dims�elem�offsetr�s r�_print_IndexedzRustCodePrinter._print_Indexedps����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�Krc��|jjSr()r�r�r�s r� _print_IdxzRustCodePrinter._print_Idxzs ���z��rc��|jSr()r�r�s r� _print_DummyzRustCodePrinter._print_Dummy}s ���y�rc��dS)N�Er/�rgr�r�s r� _print_Exp1zRustCodePrinter._print_Exp1�s���src��dS)N�PIr/rs r� _print_PizRustCodePrinter._print_Pi�s���trc��dS)N�INFINITYr/rs r�_print_InfinityzRustCodePrinter._print_Infinity�s���zrc��dS)N� NEG_INFINITYr/rs r�_print_NegativeInfinityz'RustCodePrinter._print_NegativeInfinity�s���~rc��dS)Nrmr/rs r�_print_BooleanTruez"RustCodePrinter._print_BooleanTrue�s���vrc��dS)NrPr/rs r�_print_BooleanFalsez#RustCodePrinter._print_BooleanFalse�s���wrc�D�t|�����Sr()r�r�rs r� _print_boolzRustCodePrinter._print_bool�s���4�y�y��� � � rc��dS)N�NANr/rs r� _print_NaNzRustCodePrinter._print_NaN�s���urc��|jdjdkrtd���g}t|j��D]�\}\}}|dkr,|�d|�|��z��nX|t |j��dz kr|dkr|dxxdz cc<n&|dxxd|�|��zz cc<|�|��}|�|��|�d ����|jd rd �|��Sd �|��S) NrTz�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.rz if (%s) {r"z else {z else if (%s) {r��inline� � ) r�r�� ValueError� enumerater�r�r�� _settings�join)rgr�r�r��e�c�code0s r�_print_Piecewisez RustCodePrinter._print_Piecewise�sI�� �9�R�=� �� %� %��/�0�0� 0� ��"�4�9�-�-� � �I�A�v��1��A�v�v�� � �[�4�;�;�q�>�>�9�:�:�:�:��c�$�)�n�n�q�(�(�(�Q�$�Y�Y��b� � � �Y�&� � � � ��b� � � �.����Q���?�?� � � ��K�K��N�N�E� �L�L�� � � � �L�L�� � � � � �>�(� #� $��8�8�E�?�?� "��9�9�U�#�#� #rc�b�ddlm}|�|�|d�����S)Nr)� PiecewiseF)�deep)�sympy.functionsr&r�r�)rgr�r&s r� _print_ITEzRustCodePrinter._print_ITE�s6��-�-�-�-�-�-��{�{�4�<�<� ��<�>�>�?�?�?rc���|jdkr$dd��fd�|D����zStd���)Nr"z[%s]r�c3�B�K�|]}��|��V��dSr()r�)r��args �rr�z4RustCodePrinter._print_MatrixBase.<locals>.<genexpr>�s-�����%@�%@��d�k�k�!�n�n�%@�%@�%@�%@�%@�%@rzLFull Matrix Support in Rust need Crates (https://crates.io/keywords/matrix).)r�r r)rg�As` r�_print_MatrixBasez!RustCodePrinter._print_MatrixBase�sI��� �6�Q�;�;��D�I�I�%@�%@�%@�%@�a�%@�%@�%@�@�@�@� @��k�l�l� lrc�,�|�|��Sr()r�)rgr�s r�_print_SparseRepMatrixz&RustCodePrinter._print_SparseRepMatrix�s���(�(��-�-�-rc�\�|j�d|j|j|jjdzz�d�S)Nr�r"r�)�parentr�r�r�r�s r�_print_MatrixElementz$RustCodePrinter._print_MatrixElement�s8���;�;�;��6�D�F�4�;�+<�Q�+?�$?�?�?�?�A� Arc�f��t���|��}||jvrd|zS|S)Nz(*%s))rk� _print_Symbolr�)rgr�r�r�s �rr5zRustCodePrinter._print_Symbol�s7����w�w�$�$�T�*�*�� �4�$� $� $��T�>� !��Krc�L�ddlm}|j}|j}|jdr@|�|��s|�|��r|�||��S|�|��}|�|��}|�|�d|����S)Nr)� IndexedBaser~z = ) �sympy.tensor.indexedr7r�r�r�has�_doprint_loopsr�r�)rgr�r7r�r�r�r�s r�_print_Assignmentz!RustCodePrinter._print_Assignment�s���4�4�4�4�4�4��h���h�� �>�*� %� I�3�7�7�;�+?�+?� I���� �$�$� I��&�&�s�C�0�0� 0��{�{�3�'�'�H��{�{�3�'�'�H��&�&�H�H�H�h�h�'G�H�H� Hrc��� � �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 linesTr�z )�{r�z{ z( )r�r�c�8�g|]}|�d����S)z )�lstrip)r��lines r� <listcomp>z/RustCodePrinter.indent_code.<locals>.<listcomp>�s$��6�6�6�����U�#�#�6�6�6rc �n��g|]1}ttt|j���������2Sr/)r��any�map�endswith)r�r@� inc_tokens �rrAz/RustCodePrinter.indent_code.<locals>.<listcomp>�s5���O�O�O��S��S��� �:�:�;�;�<�<�O�O�Orc �n��g|]1}ttt|j���������2Sr/)r�rCrD� startswith)r�r@� dec_tokens �rrAz/RustCodePrinter.indent_code.<locals>.<listcomp>�sE���(�(�(����S���)�<�<�=�=�>�>�(�(�(rr)r�r)r�r�r�� splitlinesr rr�) rg�code� code_lines�tab�increase�decrease�pretty�level�nr@rIrFs @@rr�zRustCodePrinter.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�� r)F).r�� __module__� __qualname__�__doc__� printmethod�languager�r r�r��__annotations__r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr r rrrrrr$r)r.r0r3r5r;r�� __classcell__)r�s@rrxrx�s���������H�H��K��H�(,��[�-J�)�)�����s�u�u� O�O�)�)������!#�2�2�2�2����"�"�"����5�5�5�'�'�'�B�B�B� '� '� '� %� %� %�63�63�63�p*�*�*� ������������&�&�&�9�9�9� L�L�L�������������������������������!�!�!�!�����$�$�$�6@�@�@�m�m�m� .�.�.�A�A�A������ I� I� I�������rrxNc �H�t|���||��S)a�Converts an expr to a string of Rust 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 the keys are string representations of either ``FunctionClass`` or ``UndefinedFunction`` instances and the values are their desired C string representations. Alternatively, the dictionary value can be a list of tuples i.e. [(argument_test, cfunction_string)]. See below for examples. dereference : iterable, optional An iterable of symbols that should be dereferenced in the printed code expression. These would be values passed by address to the function. For example, if ``dereference=[a]``, the resulting code would print ``(*a)`` instead of ``a``. 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 rust_code, symbols, Rational, sin, ceiling, Abs, Function >>> x, tau = symbols("x, tau") >>> rust_code((2*tau)**Rational(7, 2)) '8*1.4142135623731*tau.powf(7_f64/2.0)' >>> rust_code(sin(x), assign_to="s") 's = x.sin();' Simple 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, cfunction_string)]. >>> custom_functions = { ... "ceiling": "CEIL", ... "Abs": [(lambda x: not x.is_integer, "fabs", 4), ... (lambda x: x.is_integer, "ABS", 4)], ... "func": "f" ... } >>> func = Function('func') >>> rust_code(func(Abs(x) + ceiling(x)), user_functions=custom_functions) '(fabs(x) + x.CEIL()).f()' ``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(rust_code(expr, tau)) tau = if (x > 0) { x + 1 } else { 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])) >>> rust_code(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(rust_code(mat, A)) A = [x.powi(2), if (x > 0) { x + 1 } else { x }, x.sin()]; )rx�doprint)r�� assign_tor�s r� rust_coder]�s#��Z �8� $� $� ,� ,�T�9� =� =�=rc �:�tt|fi|����dS)z3Prints Rust representation of the given expression.N)�printr])r�r�s r�print_rust_coder`is&�� �)�D� %� %�H� %� %�&�&�&�&�&rr()rU� __future__r�typingr� sympy.corerrrr�sympy.core.numbersr �sympy.printing.codeprinterr r�r�rxr]r`r/rr�<module>rfsj�� � �B#�"�"�"�"�"�������1�1�1�1�1�1�1�1�1�1�1�1�+�+�+�+�+�+�2�2�2�2�2�2�$2�  �W� 2��v�2� �5�2� �H�2�  � 4� 4�g�q� A� 5� 5�v�q� A� 6� 6��� J� 4� 4�f�a� @� 4� 4�f�a� @� -� -�v�q� 9� 1� 1�6�1� =�  ?�!2�. � � �u�a� (� )�/2�0 �4�12�< �5�=2�> �5�?2�B �5�C2�D �5�E2�F �5�G2�H �F�I2�J �F�K2�L �F�M2�N �W�O2�V �F�W2�X � � � � � �c2�2�2��X3�3�3��l[�[�[�[�[�k�[�[�[�|m>�m>�m>�m>�`'�'�'�'�'r
Memory