� ��g���R�ddlmZddlmZddlmZddlmZmZm Z m Z m Z m Z ddl mZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZmZGd�d��Z Gd�de!��Z"Gd�de#��Z$d�Z%Gd�de��Z&dd�Z'd�Z(d d�Z)d�Z*d!d�Z+dS)"�)� annotations)�Any��wraps)�Add�Mul�Pow�S�sympify�Float)�Basic)�UnevaluatedExpr)�Lambda)� _keep_coeff)�default_sort_key)�Symbol)�re)� StrPrinter)� precedence� PRECEDENCEc��eZdZdZd�Zd�ZdS)�requiresz: Decorator for registering requirements on print methods. c ��||_dS�N)�_req)�self�kwargss �j/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/printing/codeprinter.py�__init__zrequires.__init__s ���� � � �c�B�����fd�}t���|��S)Nc����j���D](\}}t||���|���)�|g|�Ri|��Sr)r�items�getattr�update)�self_�argsr�k�v�methodrs ��r�_method_wrapperz*requires.__call__.<locals>._method_wrappersc���� ���)�)� ,� ,���1���q�!�!�(�(��+�+�+�+��6�%�1�$�1�1�1�&�1�1� 1r r)rr*r+s`` r�__call__zrequires.__call__s:���� 2� 2� 2� 2� 2� 2��u�V�}�}�_�-�-�-r N)�__name__� __module__� __qualname__�__doc__rr,�r rrrs8������D�D����.�.�.�.�.r rc��eZdZdZdS)�AssignmentErrorzA Raised if an assignment variable for a loop is missing. N�r-r.r/r0r1r rr3r3��������� �Dr r3c��eZdZdZdS)�PrintMethodNotImplementedErrorz@ Raised if a _print_* method is missing in the Printer. Nr4r1r rr7r7%r5r r7c��t|t��rddlm}|d�|D���St|t��rt d�|D����S|S)Nr)�Listc3�4K�|]}t|��V��dSr��_convert_python_lists��.0�es r� <genexpr>z(_convert_python_lists.<locals>.<genexpr>.s+����<�<�1�+�A�.�.�<�<�<�<�<�<r c3�4K�|]}t|��V��dSrr;r=s rr@z(_convert_python_lists.<locals>.<genexpr>0s+����;�;�!�*�1�-�-�;�;�;�;�;�;r )� isinstance�list�sympy.codegen.abstract_nodesr9�tuple)�argr9s rr<r<+sv���#�t����5�5�5�5�5�5��t�<�<��<�<�<�=�=� �C�� � ���;�;�s�;�;�;�;�;�;�� r c ���eZdZUdZdddd�Zdddd d dddd �Zd ed <iddgf�ddgf�ddgf�ddgf�ddgf�ddgf�ddgf�ddgf�ddgf�ddgf�d dgf�d!dgf�d"d#gf�d$d%gf�d&d%gf�d'd#gf�d(dd)gf�id*d+gf�d,d#gf�d-d#d)gf�d.d/gf�d0d#d)gf�d1d#d)gf�d2d#gf�d3d+gf�d4d)gf�d5d)gf�d6d)gf�d7d8gf�d9d8gf�d:d;gf�d<d;gf�d=d>gf�d?d>d#gf��d@d)gfi�Zdh�fdA� ZdB�Z dhdC�Z dhdD�Z dE�Z dF�Z dG�ZdH�ZdI�ZdJ�ZdK�ZdL�ZdM�ZdN�ZdO�ZdP�ZdQ�ZdR�ZdS�ZdT�ZdU�Z�fdV�ZdW�ZdX�ZeZ dZ!dY�Z"dZ�Z#d[�Z$d\�Z%d]�Z&d^�Z'd_�Z(d`�Z)da�Z*db�Z+dc�Z,dd�Z-de�Z.df�Z/dg�Z0e0Z1e0Z2e0Z3e0Z4e0Z5e0Z6e0Z7e0Z8e0Z9e0Z:e0Z;e0Z<e0Z=e0Z>e0Z?e0Z@e0ZAe0ZBe0ZCe0ZDe0ZEe0ZFe0ZG�xZHS)i� CodePrinterz6 The base class for code-printing subclasses. z&&z||�!)�and�or�notN�autoF�_T)�order� full_prec�error_on_reserved�reserved_word_suffix�human�inline�allow_unknown_functions�strictzdict[str, Any]�_default_settings�cot�tan�csc�sin�sec�cos�acot�atan�acsc�asin�asec�acos�coth�exp�csch�sech�acoth�log�acsch�asech�catalan�gamma� fibonacci�sqrt�lucas�beta�sinc� Piecewise�Mod�floor� factorial� factorial2� subfactorial� uppergamma�RisingFactorial�FallingFactorial�binomial�frac�Max�Min� Heaviside�erf2�erf�erfc�Li�li�Ei� dirichlet_eta�zeta� riemann_xi�SingularityFunctionc�"��t���|���|j�dd��dkr'|j�dd��dk|jd<t |d��st ��|_dSdS)N)�settingsrVTrS�reserved_words)�superr� _settings�get�hasattr�setr�)rr�� __class__s �rrzCodePrinter.__init__ts���� �����(��+�+�+� �>� � �h�� -� -�� 5� 5�'+�~�'9�'9�'�4�'H�'H�D�'P�D�N�8� $��t�-�.�.� (�"%�%�%�D� � � � (� (r c�:�|�td���S)Nc�r�t|t��r|jdjr|nt |��S)Nr)rBrr'�is_realr)rFs r�<lambda>z5CodePrinter._handle_UnevaluatedExpr.<locals>.<lambda>}s=��:� ��4"�4"�-G�&)�h�q�k�&9�-G�C�C�?A�#�w�w�r )�replacer�r�exprs r�_handle_UnevaluatedExprz#CodePrinter._handle_UnevaluatedExpr|s)���|�|�B�!G�!G�H�H� Hr c�`�� � � � �ddlm� ddlm� m� � � � � �fd�� t |��}� ||��}��|��}t���_t���_ �� |��� ��}�j d�rg}�jr�|� ��d��j������t#�jt$���D]<}|� ��t'|��j�����=t#�j t$���D].\}}|� ��||�����/||z}��|��}d�|��}nE��|��}�fd ��j D��}|�jd�|��f}t���_t���_ |S) a� Print the expression as code. Parameters ---------- expr : Expression The expression to be printed. assign_to : Symbol, string, MatrixSymbol, list of strings or Symbols (optional) If provided, the printed code will set the expression to a variable or multiple variables with the name or names given in ``assign_to``. r�� MatrixSymbol)� CodeBlock� Assignmentc���|�t|��St|ttf��r|t |��t |��kr=t d�t |��t |���������fd�t||��D���St|t��r&|j r�|g|j �R�}ngt|��}nWt|t��sBtd�t���jt|��������||��S)Nz;Failed to assign an expression of length {} to {} variablesc�.��g|]\}}�||����Sr1r1)r>�lhs�rhs�_handle_assign_tos �r� <listcomp>zBCodePrinter.doprint.<locals>._handle_assign_to.<locals>.<listcomp>�s+���"d�"d�"d�8�3��#4�#4�S�#�#>�#>�"d�"d�"dr z%{} cannot assign to object of type {})r rBrCrE�len� ValueError�format�zip�str� is_Matrix�shaperr � TypeError�typer-)r�� assign_tor�r�r�r�rs �����rr�z.CodePrinter.doprint.<locals>._handle_assign_to�sb���� ��t�}�}�$��)�d�E�]�3�3� f��t�9�9��I���.�.�$�%b�%i�%i�jm�nr�js�js�ux�zC�vD�vD�&E�&E�F�F�F� �y�"d�"d�"d�"d�s�SW�Yb�Oc�Oc�"d�"d�"d�e�e��)�S�)�)� ?��>�2� ,� �Y� D��� D� D� D�I�I� &�y� 1� 1�I�I�� �5�1�1� ?�� G� N� N��T� � �+�T�)�_�_�!>�!>�?�?�?��:�i��.�.� .r rSzNot supported in {}:��key� c�D��h|]\}}|��|��f��Sr1��_print)r>r(r)rs �r� <setcomp>z&CodePrinter.doprint.<locals>.<setcomp>�s,���M�M�M���1��D�K�K��N�N�+�M�M�Mr )�"sympy.matrices.expressions.matexprr��sympy.codegen.astr�r�r<r�r��_not_supported�_number_symbolsr�� splitlinesr��append� _get_commentr��language�sortedr�r�r-�_declare_number_const� _format_code�join) rr�r��lines� frontlines�name�value�result�num_symsr�r�r�r�s ` @@@@r�doprintzCodePrinter.doprint�se������� D�C�C�C�C�C�;�;�;�;�;�;�;�;� /� /� /� /� /� /� /� /� /�"%�T�*�*�� � ��y�1�1���+�+�D�1�1��"�e�e���"�u�u���� � �D�!�!�,�,�.�.�� �>�'� "� G��J��"� N��!�!�$�"3�"3�.�5�5�d�m�D�D�#F�#F�G�G�G�"�4�#6�C�@�@�@�N�N�D��%�%�d�&7�&7��T� � �8K�&L�&L�M�M�M�M�%�d�&:��D�D�D� K� K� ��e��!�!�$�"<�"<�T�5�"I�"I�J�J�J�J���&�E��%�%�e�,�,�E��Y�Y�u�%�%�F�F��%�%�e�,�,�E�M�M�M�M��8L�M�M�M�H��� 3�T�Y�Y�u�5E�5E�F�F�!�e�e���"�u�u���� r c ��|j�dd��r(ddlm}|�||��}||��}ng}d|fi}|�|��\}}d|vr#t j|t|d���}nt j|d��}|� |��} g} || kr[| � |��|�|� | �d|����}| � |��| � |��|D�][} t| t���rB|�| |��}|�|��\} } || D�] }||vr5d�||D��d�||D��kst!d���|�t#d ���|�|��rt'd ���| � |��| � | ��| �dt j|||z����}| � |� |����| � | ��| � |���� ��]d �| ��S) N�contractTr)�get_contraction_structure� = c�P�g|]#}t|�������$Sr1)rC�keys�r>�fs rr�z.CodePrinter._doprint_loops.<locals>.<listcomp>�s&��0W�0W�0W�A��a�f�f�h�h���0W�0W�0Wr c��g|]}dg��Srr1r�s rr�z.CodePrinter._doprint_loops.<locals>.<listcomp>�s��>�>�>�1���>�>�>r z0FIXME: no support for contractions in factor yetz"need assignment variable for loopszZFIXME: lhs present in rhs, this is undefined in CodePrinterr�)r�r�� sympy.tensorr��_get_expression_indices�_get_loop_opening_endingrr�rr��extend�_get_statementr�rBrE�_sort_optimized�NotImplementedErrorr3�hasr�r�)rr�r�r��indices�dummies�openloop� closeloop�text� lhs_printedr��d� openloop_d� closeloop_d�terms r�_doprint_loopszCodePrinter._doprint_loops�s��� �>� � �j�$� /� /� &� >� >� >� >� >� >��2�2�4��C�C�G�/�/��5�5�G�G��G��d�W�o�G�"�;�;�G�D�D���)� �7�?�?��%�d�C����,?�@�@�D�D��%�d�A�.�.�D��k�k�)�,�,� ��� �;� � � �L�L�� "� "� "��$��*�*� � � �T�T�+J�K�K�� �L�L�� � � � �L�L�� #� #� #��( 0�( 0�A��!�U�#�#�' 0��.�.�q�$�7�7��*.�*G�*G��+�+�'� �K�$�A�J�"0�"0�D��w���0W�0W��QU��0W�0W�0W�>�>��� �>�>�>�1?�1?� 2�N�P�P�P�%�,�"1� D�#F�#F�F��8�8�I�.�.�C�",�.B�#C�#C�C�� � �X�.�.�.�� � �Z�0�0�0�,7�K�K��9K� �)�d�"2�:4�:4�:4� 5��� � �T�%8�%8��%>�%>�?�?�?�� � �[�1�1�1�� � �Y�/�/�/�/���y�y����r c��ddlm}||��\}}||��\}}|r|s|}||krtd|z���|�||��S)Nr)� get_indicesz2lhs indices must match non-dummy rhs indices in %s)r�r�r�r�)rr�r�r��rinds�junk�lindss rr�z#CodePrinter._get_expression_indicess���,�,�,�,�,�,�!�k�$�'�'� ��t�!�k�)�,�,� ��t� � �� ��E� �E�>�>��)�+/�0�1�1� 1��#�#�E�9�5�5�5r c�(� �ddlm}|sgSi� |D]}d� |<�|�|��}|D]P}t|j��D]9\}} � |xx|�|��z cc<�*#t $rY�6wxYw�Qt|� fd����S)Nr)�Indexedc����|Srr1)�x� score_tables �rr�z-CodePrinter._sort_optimized.<locals>.<lambda>5s ���[��^�r r�)�sympy.tensor.indexedr��atoms� enumerater��_rate_index_position�KeyErrorr�) rr�r�r��i�arrays�arr�p�indr�s @rr�zCodePrinter._sort_optimized s����0�0�0�0�0�0�� ��I�� �� � �A��K��N�N����G�$�$��� � �C�#�C�K�0�0� � ���3����$�$�$��(A�(A�!�(D�(D�D�$�$�$�$�������D����� � �g�#;�#;�#;�#;�<�<�<�<s� #A/�/ A<�;A<c� �td���)z�function to calculate score based on position among indices This method is used to sort loops in an optimized order, see CodePrinter._sort_optimized() �=This function must be implemented by subclass of CodePrinter.�r�)rr�s rr�z CodePrinter._rate_index_position7s�� "�#=�>�>� >r c� �td���)z1Formats a codestring with the proper line ending.r�r�)r� codestrings rr�zCodePrinter._get_statement@���!�#=�>�>� >r c� �td���)z#Formats a text string as a comment.r�r�)rr�s rr�zCodePrinter._get_commentErr c� �td���)z3Declare a numeric constant at the top of a functionr�r�)rr�r�s rr�z!CodePrinter._declare_number_constJrr c� �td���)z~Take in a list of lines of code, and format them accordingly. This may include indenting, wrapping long lines, etc...r�r�)rr�s rr�zCodePrinter._format_codeOs��"�#=�>�>� >r c� �td���)zOReturns a tuple (open_lines, close_lines) containing lists of codelinesr�r�)rr�s rr�z$CodePrinter._get_loop_opening_endingVs��"�#=�>�>� >r c�l�|j�d��r d|jzSd|j|jfzS)N�Dummy_rNz%s_%d)r�� startswith� dummy_indexr�s r� _print_DummyzCodePrinter._print_Dummy\s;�� �9� � �� )� )� ;����?� "��d�i��)9�:�:� :r c�P��d��fd�|jD����S)Nr�c�:��g|]}��|����Sr1r�)r>r�rs �rr�z0CodePrinter._print_CodeBlock.<locals>.<listcomp>cs#���<�<�<�Q�$�+�+�a�.�.�<�<�<r )r�r'r�s` r�_print_CodeBlockzCodePrinter._print_CodeBlockbs,����y�y�<�<�<�<�$�)�<�<�<�=�=�=r c� �t|��Sr)r��r�strings r� _print_StringzCodePrinter._print_Stringes���6�{�{�r c��d|jzS)Nz"%s")r�)rrFs r�_print_QuotedStringzCodePrinter._print_QuotedStringhs����� � r c�F�|�t|����Sr)r�r�rs r�_print_CommentzCodePrinter._print_Commentks��� � ��V���-�-�-r c��ddlm}ddlm}ddlm}ddlm}|j}|j }t|j |��rmg}g} |j D]9\} } |� ||| ����| � | ���:|t|| ���} |�| ��St||��rxg} |�|��D]K\}}||||f|||f��} |�| ��}| � |���Ld�| ��S|j�dd��r@|�|��s|�|��r|�||��S|�|��}|�|��}|�|�d |����S) Nr)r�)rsr�)� IndexedBaser�r�Fr�)r�r��$sympy.functions.elementary.piecewisersr�r�r�rr�r�rBr'r�r�r��_traverse_matrix_indicesr�r�r�r�r�r�)rr�r�rsr�rr�r�� expressions� conditionsr?�c�tempr�r��j�code0�lhs_code�rhs_codes r�_print_AssignmentzCodePrinter._print_Assignmentns��0�0�0�0�0�0�B�B�B�B�B�B�C�C�C�C�C�C�4�4�4�4�4�4��h���h�� �d�h� � *� *� I��K��J��(� %� %���A��"�"�:�:�c�1�#5�#5�6�6�6��!�!�!�$�$�$�$��9�c�+�z�:�:�;�D��;�;�t�$�$� $� ��\� *� *� I��E��7�7��<�<� $� $���A�!�z�#�a��d�)�S��A��Y�7�7��� � �D�)�)��� � �U�#�#�#�#��9�9�U�#�#� #� �^� � � �E� 2� 2� I���� �8L�8L� I���� �$�$� I��&�&�s�C�0�0� 0��{�{�3�'�'�H��{�{�3�'�'�H��&�&�H�H�H�h�h�'G�H�H� Hr c������|j��}��|j��}��dj�fd�||j|fD�����S)Nz{} {} {}c3�B�K�|]}��|��V��dSrr��r>rFrs �rr@z9CodePrinter._print_AugmentedAssignment.<locals>.<genexpr>�s/����� H� H�3�d�k�k�#��� H� H� H� H� H� Hr )r�r�r�r�r��op)rr�rr s` r�_print_AugmentedAssignmentz&CodePrinter._print_AugmentedAssignment�sr����;�;�t�x�(�(���;�;�t�x�(�(���"�"�#4�:�#4� H� H� H� H�8�T�W�h�*G� H� H� H�$J�K�K� Kr c�f��|j�dd��fd�|jD�����d�S)N�(�, c3�B�K�|]}��|��V��dSrr�r$s �rr@z2CodePrinter._print_FunctionCall.<locals>.<genexpr>�s/�����F�F�C�t�{�{�3�'�'�F�F�F�F�F�Fr �))r�r�� function_argsr�s` r�_print_FunctionCallzCodePrinter._print_FunctionCall�sE��� �I�I�I� �I�I�F�F�F�F�4�3E�F�F�F� G� G� G� G�I� Ir c�6�|�|j��Sr)r��symbolr�s r�_print_VariablezCodePrinter._print_Variable�s���{�{�4�;�'�'�'r c����t���|��}||jvrA|jdr$d}t |�|�����||jdzS|S)NrQzVThis expression includes the symbol "{}" which is a reserved keyword in this language.rR)r�� _print_Symbolr�r�r�r�)rr�r��msgr�s �rr2zCodePrinter._print_Symbol�sq����w�w�$�$�T�*�*�� �4�&� &� &��~�1�2� 3�<�� ����D�!1�!1�2�2�2��$�.�)?�@�@� @��Kr c�\�||jvp#t|d�|��d��S)z� Check if function ``name`` is either a known function or has its own printing method. Used to check if rewriting is possible.z _print_{}F)�known_functionsr$r�)rr�s r� _can_printzCodePrinter._can_print�s3���t�+�+�]�w�t�[�=O�=O�PT�=U�=U�W\�/]�/]�]r c�4��|jj�jvr��j|jj}t|t��r!|�d��|jd���d�S|D]\}}||j�rn�|�H |�fd�|jD���S#t$r$|�d��|jd���d�cYSwxYwn�t|d��r<t|j t��r"�� |j |j���S|jj�j vrx�j |jj\}}�� |��rIt�fd�|D����r.d�� |�|����zdzS|jrf�j�dd��rK�� |j���dd�t)�j |j�����d�S��|��S) Nr(r)r+c�<��g|]}��|d����S)r�� parenthesize)r>�itemrs �rr�z/CodePrinter._print_Function.<locals>.<listcomp>�s)���%W�%W�%W�T�d�&7�&7��a�&@�&@�%W�%W�%Wr �_imp_c3�B�K�|]}��|��V��dSr)r6)r>r�rs �rr@z.CodePrinter._print_Function.<locals>.<genexpr>�s/�����0Y�0Y������1C�1C�0Y�0Y�0Y�0Y�0Y�0Yr rUF)�funcr-r5rBr�� stringifyr'r�r�r<rr��_rewriteable_functionsr6�all�rewrite� is_Functionr�r�r��map�_print_not_supported)rr�� cond_func�condr>�target_f� required_fss` r�_print_FunctionzCodePrinter._print_Function�sY��� �9� ��!5� 5� 5��,�T�Y�-?�@�I��)�S�)�)� R�#,�9�9�d�n�n�T�Y��.M�.M�.M�.M�N�N�"+���J�D�$��t�T�Y�'������#�R�#�t�%W�%W�%W�%W�T�Y�%W�%W�%W�X�X��$�R�R�R�+/�4�4����� �4�1P�1P�1P�1P�Q�Q�Q�Q�R����$� �T�7� #� #� G� �4�:�v�(F�(F� G��;�;�z�t�z�4�9�5�6�6� 6� �Y� �4�#>� >� >�$(�$?�� �@R�$S� !�H�k����x�(�(� G�S�0Y�0Y�0Y�0Y�[�0Y�0Y�0Y�-Y�-Y� G��T�[�[����h�)?�)?�@�@�@�3�F�F� � � 3��� 2� 2�3L�e� T� T� 3�#�{�{�4�9�5�5�5�5�t�y�y��T�[�RV�R[�A\�A\�7]�7]�7]�7]�^� ^��,�,�T�2�2� 2s�9B�+B=�<B=c �d�|j�dd��r@|�t|�|jd������S|j�|t|�|jd����f��t|��S)NrTF� precision)r�r�r�r �evalfr��addr�r�s r�_print_NumberSymbolzCodePrinter._print_NumberSymbol�s��� �>� � �h�� .� .� ��;�;�u�T�Z�Z���{�0K�%L�%L�M�M�N�N� N� � � $� $�d��d�j�j��� �!<�=�=�>�>�&@� A� A� A��t�9�9� r c�,�|�|��Sr�rOr�s r�_print_CatalanzCodePrinter._print_Catalan�����'�'��-�-�-r c�,�|�|��SrrQr�s r�_print_EulerGammazCodePrinter._print_EulerGamma�rSr c�,�|�|��SrrQr�s r�_print_GoldenRatiozCodePrinter._print_GoldenRatio�rSr c�,�|�|��SrrQr�s r�_print_TribonacciConstantz%CodePrinter._print_TribonacciConstant�rSr c�,�|�|��SrrQr�s r� _print_Exp1zCodePrinter._print_Exp1�rSr c�,�|�|��SrrQr�s r� _print_PizCodePrinter._print_Pi�rSr c����t|���d�jdz���fd�t|jt ���D����S)N� %s rJc3�D�K�|]}��|���V��dSrr9�r>�a�PRECrs ��rr@z)CodePrinter._print_And.<locals>.<genexpr>�sO�����6B�6B��7;�6G�6G��4�6P�6P�6B�6B�6B�6B�6B�6Br r��r� _operatorsr�r�r'r�rr�rcs` @r� _print_AndzCodePrinter._print_And�s{�����$��������/�/�5�5�6B�6B�6B�6B�6B��� �/?�@�@�@�6B�6B�6B�B�B� Br c����t|���d�jdz���fd�t|jt ���D����S)Nr_rKc3�D�K�|]}��|���V��dSrr9ras ��rr@z(CodePrinter._print_Or.<locals>.<genexpr>�sO�����5B�5B��6:�5F�5F�q�$�5O�5O�5B�5B�5B�5B�5B�5Br r�rdrfs` @r� _print_OrzCodePrinter._print_Or�s{�����$��������.�.�4�4�5B�5B�5B�5B�5B��� �/?�@�@�@�5B�5B�5B�B�B� Br c�����j�d���'��|�����St |���d�jdz���fd�|jD����S)N�xorr_c3�D�K�|]}��|���V��dSrr9ras ��rr@z)CodePrinter._print_Xor.<locals>.<genexpr>�sG�����6$�6$��7;�6G�6G��4�6P�6P�6$�6$�6$�6$�6$�6$r �rer�r��to_nnfrr�r'rfs` @r� _print_XorzCodePrinter._print_Xor�s����� �?� � �u� %� %� -��;�;�t�{�{�}�}�-�-� -��$��������/�/�5�5�6$�6$�6$�6$�6$���6$�6$�6$�$�$� $r c�����j�d���'��|�����St |���d�jdz���fd�|jD����S)N� equivalentr_c3�D�K�|]}��|���V��dSrr9ras ��rr@z0CodePrinter._print_Equivalent.<locals>.<genexpr>sI�����=$�=$��>B�=N�=N�q�RV�=W�=W�=$�=$�=$�=$�=$�=$r rnrfs` @r�_print_EquivalentzCodePrinter._print_Equivalent�s����� �?� � �|� ,� ,� 4��;�;�t�{�{�}�}�-�-� -��$��������6�6�<�<�=$�=$�=$�=$�=$���=$�=$�=$�$�$� $r c�~�t|��}|jd|�|jd|��zS)NrLr)rrer:r'rfs r� _print_NotzCodePrinter._print_Nots7���$������u�%��(9�(9�$�)�A�,��(M�(M�M�Mr c�P�|�|�����Sr)r�ror�s r�_print_BooleanFunctionz"CodePrinter._print_BooleanFunctions���{�{�4�;�;�=�=�)�)�)r c��� �t|��� |���\}}|dkrt| |��}d}nd}g}g}g}�jdvr|���}nt j|��}|D]�} | jr�| jr�| j j r�| j j r�| j dkr1|� t| j| j d������dt| jdj��dkr/t#| jt ��r|� | ��|� t| j| j ������|� | ����|p t$jg}t|��dkr@|dkr:��|dd t*d t*d zz��g} n� �fd �|D��} � �fd �|D��} |D]I} | j|vr>d| |�| j��z| |�| j��<�J|s|d�| ��zSt|��dkr$|d�| ��zdz| dzS|d�| ��zdd�| ��zzS)Nr�-�)�old�none�����F)�evaluate�g�?r rc�<��g|]}��|�����Sr1r9�r>r��precrs ��rr�z*CodePrinter._print_Mul.<locals>.<listcomp>5s)���;�;�;�A�T�&�&�q�$�/�/�;�;�;r c�<��g|]}��|�����Sr1r9r�s ��rr�z*CodePrinter._print_Mul.<locals>.<listcomp>6s)���7�7�7���"�"�1�d�+�+�7�7�7r z(%s)�*�/z/(%s))r� as_coeff_MulrrO�as_ordered_factorsr� make_args�is_commutative�is_Powre� is_Rational� is_negativer�r �baser�r'rBr �Oner:r�indexr�) rr�rr?�signrb�b� pow_parenr'r;�a_str�b_strr�s ` @r� _print_MulzCodePrinter._print_Mul s������$����� � �"�"���1� �q�5�5���r�1�%�%�D��D�D��D� �� ��� � �:�_� ,� ,��*�*�,�,�D�D��=��&�&�D�� � �D��"� �t�{� �t�x�7K� �PT�PX�Pd� ��8�r�>�>��H�H�S���T�X�I��F�F�F�G�G�G�G��4�9�Q�<�,�-�-��2�2�z�$�)�S�7Q�7Q�2�!�(�(��.�.�.��H�H�S���T�X�I�6�6�7�7�7�7��������� �L�!�%��� �q�6�6�Q�;�;�4�3�;�;� �&�&�q��t�S�*�U�2C�J�u�DU�2U�-V�W�W�X�E�E�;�;�;�;�;��;�;�;�E�7�7�7�7�7�Q�7�7�7��� O� O�D��y�A�~�~�,2�U�1�7�7�4�9�;M�;M�5N�,N��a�g�g�d�i�(�(�)��� F��#�(�(�5�/�/�)� )� ��V�V�q�[�[��#�(�(�5�/�/�)�C�/�%��(�:� :��#�(�(�5�/�/�)�G�c�h�h�u�o�o�,E�E� Er c �R�|j�dd��rLtdtt |�����dtt |������dz��� |j�|��n#t$rYnwxYw|�|��S)NrVFzUnsupported by z: zV Set the printer option 'strict' to False in order to generate partially printed code.) r�r�r7r�r�r�rNr�� emptyPrinterr�s rrEz CodePrinter._print_not_supportedDs��� �>� � �h�� .� .� x�0�0�C�PT�UY�PZ�PZ�O�O�O�O�]`�ae�fj�ak�ak�]l�]l�]l�1m�v�2w�x�x� x� � � � #� #�D� )� )� )� )��� � � � �D� ����� � ��&�&�&s�)B� B�Br)Ir-r.r/r0rerW�__annotations__r@rr�r�r�r�r�r�r�r�r�r�r�r r rrrr!r&r-r0r2r6rJ� _print_Expr�_print_HeavisiderOrRrUrWrYr[r]rgrjrprtrvrxr�rE� _print_Basic�_print_ComplexInfinity�_print_Derivative�_print_ExprCondPair�_print_GeometryEntity�_print_Infinity�_print_Integral�_print_Interval�_print_AccumulationBounds� _print_Limit�_print_MatrixBase�_print_DeferredVector� _print_NaN�_print_NegativeInfinity� _print_Order� _print_RootOf�_print_RootsOf�_print_RootSum�_print_Uniform� _print_Unit� _print_Wild�_print_WildFunction�_print_Relational� __classcell__)r�s@rrHrH5s����������� �����J���"� #���#(�� )� )�� � � � �$� �E�2�;�$� �E�2�;�$� �E�2�;�$� �V�R�L� $� �V�R�L� $� �V�R�L� $� �U�B�K�$� �U�B�K�$� �U�B�K�$� �e�R�[�$� �e�R�[�$� �e�R�[�$� ���}�$� �&�"��$� �f�b�\�$� �W�b�M�!$�" �U�[�M�*�#$�$�$ �G�R�=�%$�& �'�2��'$�( �7�[�M�2�)$�* �\�2�.�+$�, ��+��7�-$�. ��;�-� 8�/$�0 ��"� �1$�2 �W�b�M�3$�4 �K��$�5$�6 �K��$�7$�8 �+�r�*�9$�: �U�B�K�;$�< �U�B�K�=$�> �4��*�?$�@ �4��*�A$�B �f�b�\�C$�D �6�G�9�-�E$�$�F "�K��#4�G$�$��L(�(�(�(�(�(�H�H�H�A�A�A�A�FM �M �M �M �^ 6� 6� 6�=�=�=�.>�>�>�>�>�>� >�>�>� >�>�>� >�>�>�>�>�>� ;�;�;� >�>�>����!�!�!�.�.�.�#I�#I�#I�JK�K�K� I�I�I� (�(�(� � � � � �^�^�^� 3�3�3�8"�K������.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�B�B�B� B�B�B� $�$�$�$�$�$�N�N�N�*�*�*�8F�8F�8F�t '� '� '�(�L�1��,��.��0��*�O�*�O�*�O� 4��'�L�,��0��%�J�2��'�L�(�M�)�N�)�N�)�N�&�K�&�K�.��,�����r rHN�c99c �|�ddlm}||���|���||��S)a�Converts an expr to a string of c 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. standard : str, optional String specifying the standard. If your compiler supports a more modern standard you may set this to 'c99' to allow the printer to use more math functions. [default='c89']. precision : integer, optional The precision for numbers such as pi [default=17]. 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)] or [(argument_test, cfunction_formater)]. 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 ccode, symbols, Rational, sin, ceiling, Abs, Function >>> x, tau = symbols("x, tau") >>> expr = (2*tau)**Rational(7, 2) >>> ccode(expr) '8*M_SQRT2*pow(tau, 7.0/2.0)' >>> ccode(expr, math_macros={}) '8*sqrt(2)*pow(tau, 7.0/2.0)' >>> ccode(sin(x), assign_to="s") 's = sin(x);' >>> from sympy.codegen.ast import real, float80 >>> ccode(expr, type_aliases={real: float80}) '8*M_SQRT2l*powl(tau, 7.0L/2.0L)' 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"), ... (lambda x: x.is_integer, "ABS")], ... "func": "f" ... } >>> func = Function('func') >>> ccode(func(Abs(x) + ceiling(x)), standard='C89', user_functions=custom_functions) 'f(fabs(x) + CEIL(x))' or if the C-function takes a subset of the original arguments: >>> ccode(2**x + 3**x, standard='C99', user_functions={'Pow': [ ... (lambda b, e: b == 2, lambda b, e: 'exp2(%s)' % e), ... (lambda b, e: b != 2, 'pow')]}) 'exp2(x) + pow(3, 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(ccode(expr, tau, standard='C89')) 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])) >>> ccode(e.rhs, assign_to=e.lhs, contract=False, standard='C89') '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(ccode(mat, A, standard='C89')) A[0] = pow(x, 2); if (x > 0) { A[1] = x + 1; } else { A[1] = x; } A[2] = sin(x); r)�c_code_printers)�sympy.printing.cr��lowerr�)r�r��standardr�r�s r�ccoder�msF��D1�0�0�0�0�0� ,�?�8�>�>�+�+� ,�X� 6� 6� >� >�t�Y� O� O�Or c �:�tt|fi|����dS)z0Prints C representation of the given expression.N)�printr��r�r�s r� print_ccoder��s&�� �%�� !� !�� !� !�"�"�"�"�"r c �L�ddlm}||���||��S)aKConverts an expr to a string of fortran 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 DEPRECATED. Use type_mappings instead. The precision for numbers such as pi [default=17]. 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, cfunction_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]. source_format : optional The source format can be either 'fixed' or 'free'. [default='fixed'] standard : integer, optional The Fortran standard to be followed. This is specified as an integer. Acceptable standards are 66, 77, 90, 95, 2003, and 2008. Default is 77. Note that currently the only distinction internally is between standards before 95, and those 95 and after. This may change later as more features are added. name_mangling : bool, optional If True, then the variables that would become identical in case-insensitive Fortran are mangled by appending different number of ``_`` at the end. If False, SymPy Will not interfere with naming of variables. [default=True] Examples ======== >>> from sympy import fcode, symbols, Rational, sin, ceiling, floor >>> x, tau = symbols("x, tau") >>> fcode((2*tau)**Rational(7, 2)) ' 8*sqrt(2.0d0)*tau**(7.0d0/2.0d0)' >>> fcode(sin(x), assign_to="s") ' s = 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, cfunction_string)]. >>> custom_functions = { ... "ceiling": "CEIL", ... "floor": [(lambda x: not x.is_integer, "FLOOR1"), ... (lambda x: x.is_integer, "FLOOR2")] ... } >>> fcode(floor(x) + ceiling(x), user_functions=custom_functions) ' CEIL(x) + FLOOR1(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(fcode(expr, tau)) if (x > 0) then tau = x + 1 else tau = x end if 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])) >>> fcode(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(fcode(mat, A)) A(1, 1) = x**2 if (x > 0) then A(2, 1) = x + 1 else A(2, 1) = x end if A(3, 1) = sin(x) r)� FCodePrinter)�sympy.printing.fortranr�r�)r�r�r�r�s r�fcoder��s7��h4�3�3�3�3�3� �<�� !� !� )� )�$� � :� :�:r c �:�tt|fi|����dS)z|Prints the Fortran representation of the given expression. See fcode for the meaning of the optional arguments. N)r�r�r�s r� print_fcoder�ns(��  �%�� !� !�� !� !�"�"�"�"�"r �c++11c �|�ddlm}||���|���||��S)z$ C++ equivalent of :func:`~.ccode`. r)�cxx_code_printers)�sympy.printing.cxxr�r�r�)r�r�r�r�r�s r�cxxcoder�usD��4�4�4�4�4�4� .� �X�^�^�-�-� .�x� 8� 8� @� @��y� Q� Q�Qr )Nr�r)Nr�),� __future__r�typingr� functoolsr� sympy.corerrr r r r �sympy.core.basicr �sympy.core.exprr�sympy.core.functionr�sympy.core.mulr�sympy.core.sortingr�sympy.core.symbolr�$sympy.functions.elementary.complexesr�sympy.printing.strr�sympy.printing.precedencerrr� Exceptionr3r�r7r<rHr�r�r�r�r�r1r r�<module>r�sJ��"�"�"�"�"�"�������������7�7�7�7�7�7�7�7�7�7�7�7�7�7�7�7�"�"�"�"�"�"�+�+�+�+�+�+�&�&�&�&�&�&�&�&�&�&�&�&�/�/�/�/�/�/�$�$�$�$�$�$�3�3�3�3�3�3�)�)�)�)�)�)�<�<�<�<�<�<�<�<� .� .� .� .� .� .� .� .� � � � � �i� � � �  � � � � �%8� � � � ���q-�q-�q-�q-�q-�*�q-�q-�q-�pCP�CP�CP�CP�J#�#�#�u;�u;�u;�u;�p#�#�#�R�R�R�R�R�Rr
Memory