� ��g�[����dZddlmZddlmZddlmZmZmZm Z ddl m Z ddl m Z ddlmZddlmZmZdd lmZgd �Zd d d ddddd�ZGd�de��Zdd�Zd�ZdS)a Julia code printer The `JuliaCodePrinter` converts SymPy expressions into Julia expressions. A complete code generator, which uses `julia_code` extensively, can be found in `sympy.utilities.codegen`. The `codegen` module can be used to generate complete source code files. �)� annotations)�Any)�Mul�Pow�S�Rational)� _keep_coeff)� equal_valued)� CodePrinter)� precedence� PRECEDENCE��search)3�sin�cos�tan�cot�sec�csc�asin�acos�atan�acot�asec�acsc�sinh�cosh�tanh�coth�sech�csch�asinh�acosh�atanh�acoth�asech�acsch�sinc�atan2�sign�floor�log�exp�cbrt�sqrt�erf�erfc�erfi� factorial�gamma�digamma�trigamma� polygamma�beta�airyai� airyaiprime�airybi� airybiprime�besselj�bessely�besseli�besselk�erfinv�erfcinv�abs�ceil�conj�hankelh1�hankelh2�imag�real)�Abs�ceiling� conjugate�hankel1�hankel2�im�rec �z��eZdZUdZdZdZdddd�Zeej fidid d d ���Z d e d <if�fd � Z d�Z d�Z d�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z�fd�Zd�Z�fd�Z�fd�Z�fd�Z�fd�Zd�Zd �Zd!�Zd"�Z d#�Z!d$�Z"e"Z#d%�Z$d&�Z%d'�Z&d(�Z'd)�Z(d*�Z)d+�Z*d,�Z+d-�Z,d.�Z-d/�Z.d0�Z/d1�Z0d2�Z1d3�Z2d4�Z3d5�Z4d6�Z5�xZ6S)7�JuliaCodePrinterzD A printer to convert expressions to strings of Julia code. �_julia�Juliaz&&z||�!)�and�or�not�T)� precision�user_functions�contract�inlinezdict[str, Any]�_default_settingsc�Z��t���|��tttt����|_|j�tt����|�di��}|j�|��dS)Nr[) �super�__init__�dict�zip�known_fcns_src1�known_functions�update�known_fcns_src2�get)�self�settings� userfuncs� __class__s ��d/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/printing/julia.pyrazJuliaCodePrinter.__init__Gs���� ������"�"�"�#�C���$I�$I�J�J��� ��#�#�D��$9�$9�:�:�:��L�L�!1�2�6�6� � ��#�#�I�.�.�.�.�.�c� �|dzS)N��)ri�ps rm�_rate_index_positionz%JuliaCodePrinter._rate_index_positionOs ����s� rnc� �d|zS)Nz%srq)ri� codestrings rm�_get_statementzJuliaCodePrinter._get_statementSs ���j� � rnc�,�d�|��S)Nz# {}��format)ri�texts rm� _get_commentzJuliaCodePrinter._get_commentWs���}�}�T�"�"�"rnc�.�d�||��S)Nz const {} = {}rx)ri�name�values rm�_declare_number_constz&JuliaCodePrinter._declare_number_const[s���%�%�d�E�2�2�2rnc�,�|�|��S�N)� indent_code)ri�liness rm� _format_codezJuliaCodePrinter._format_code_s������&�&�&rnc�N��|j\�}�fd�t|��D��S)Nc3�D�K�|]}t���D]}||fV�� �dSr�)�range)�.0�j�i�rowss �rm� <genexpr>z<JuliaCodePrinter._traverse_matrix_indices.<locals>.<genexpr>fs:�����A�A�1�U�4�[�[�A�A���A��A�A�A�A�A�A�Arn)�shaper�)ri�mat�colsr�s @rm�_traverse_matrix_indicesz)JuliaCodePrinter._traverse_matrix_indicescs.����Y� ��d�A�A�A�A��d� � �A�A�A�Arnc ���g}g}|D]f}t|j|j|jdz|jdzg��\}}}|�d|�d|�d|����|�d���g||fS)N�zfor � = �:�end)�map�_print�label�lower�upper�append)ri�indices� open_lines� close_linesr��var�start�stops rm�_get_loop_opening_endingz)JuliaCodePrinter._get_loop_opening_endingis���� �� �� &� &�A�"�4�;��W�a�g��k�1�7�Q�;�7� 9� 9� �C��� � � � �#�#�#�u�u�u�d�d�C� D� D� D� � � �u� %� %� %� %��;�&�&rnc�J���|jrL|jrE|���djr&d��t j |z��zSt|���|���\}}|dkrt| |��}d}nd}g}g}g}�j dvr|� ��}ntj |��}|D�]D} | j r�| jr�| jjr�| jjr�| jdkr1|�t'| j| j d������et+| jdj��d kr/t/| jt��r|�| ��|�t'| j| j ������| jrB| t jur4| jd kr)|�t5| j������/|�| ����F|p t jg}��fd �|D��} ��fd �|D��} |D]I} | j|vr>d | |�| j��z| |�| j��<�Jd �} |s|| || ��zSt+|��d kr.|djrdnd} || || ��z�d| �d| d��St=d�|D����rdnd} || || ��z�d| �d| || ���d�S)Nrz%sim�-�)�old�none�����F)�evaluater�c�<��g|]}��|�����Srq�� parenthesize�r��x�precris ��rm� <listcomp>z/JuliaCodePrinter._print_Mul.<locals>.<listcomp>��)���7�7�7���"�"�1�d�+�+�7�7�7rnc�<��g|]}��|�����Srqr�r�s ��rmr�z/JuliaCodePrinter._print_Mul.<locals>.<listcomp>�r�rn�(%s)c��|d}tdt|����D]&}||dz jrdnd}|�d|�d||��}�'|S)Nrr��*z.*� )r��len� is_number)�a�a_str�rr��mulsyms rm�multjoinz-JuliaCodePrinter._print_Mul.<locals>.multjoin�sd���a��A��1�c�!�f�f�%�%� 7� 7�� !�!�A�#�� 0�:���d��"#�!�!�V�V�V�U�1�X�X�6����Hrn�/�./r�c3�$K�|] }|jV�� dSr��r�)r��bis rmr�z.JuliaCodePrinter._print_Mul.<locals>.<genexpr>�s$����9�9��� �9�9�9�9�9�9rnz (�))r�� is_imaginary� as_coeff_Mul� is_integerr�r� ImaginaryUnitr r �order�as_ordered_factorsr� make_args�is_commutative�is_Powr-� is_Rational� is_negativer�r�baser��args� isinstance�Infinityrrr�q�One�index�all)ri�expr�c�er*r��b� pow_parenr��itemr��b_strr��divsymr�s` @rm� _print_MulzJuliaCodePrinter._print_Mulus����� �N� ?�t�0� ?��!�!�#�#�A�&�1� ?��D�K�K���(8��(=�>�>�>� >��$����� � �"�"���1� �q�5�5���r�1�%�%�D��D�D��D� �� ��� � �:�_� ,� ,��*�*�,�,�D�D��=��&�&�D�� � �D��#� �� � ���8L� ���,� ��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��!� �d�!�*�&<�&<���1��� ����$�&�)�)�*�*�*�*��������� �L�!�%���7�7�7�7�7�Q�7�7�7��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�(�(�)�� � � �� Z��(�(�1�e�,�,�,� ,� ��V�V�q�[�[��a�D�N�4�S�S��F�!%�h�h�q�%�&8�&8�!8�!8�!8�&�&�&�%��(�(�K� K��9�9�q�9�9�9�9�9�C�S�S�t�F�#'�(�(�1�e�*<�*<�#<�#<�#<�f�f�f�h�h�q�RW�FX�FX�FX�FX�Y� Yrnc��|�|j��}|�|j��}|j}d�|||��S)Nz{} {} {})r��lhs�rhs�rel_opry)rir��lhs_code�rhs_code�ops rm�_print_Relationalz"JuliaCodePrinter._print_Relational�sG���;�;�t�x�(�(���;�;�t�x�(�(�� �[��� � ��2�x�8�8�8rnc�f�td�|jD����rdnd}t|��}t|jd��rd|�|j��zS|jr�t|jd��r1|jjrdnd}d |�d |�|j���d �St|jd ��r1|jjrdnd}d |�d |� |j|����S|� |j|���d |�d |� |j|����S)Nc3�$K�|] }|jV�� dSr�r�)r�r�s rmr�z.JuliaCodePrinter._print_Pow.<locals>.<genexpr>�s$����>�>�q�q�{�>�>�>�>�>�>rn�^z.^g�?zsqrt(%s)g�r�r�z1 z sqrt(r�r�r�) r�r�r r r-r�r�r�r�r�)rir�� powsymbol�PREC�syms rm� _print_PowzJuliaCodePrinter._print_Pow�sM���>�>�D�I�>�>�>�>�>�H�C�C�D� ��$���� ���#� &� &� 7��� � �D�I� 6� 6�6� 6� � � N��D�H�d�+�+� G�!�Y�0�:�c�c�d���*-�#�#�t�{�{�4�9�/E�/E�/E�/E�F�F��D�H�b�)�)� N�!�Y�0�:�c�c�d���%(�S�S�$�*;�*;�D�I�t�*L�*L�*L�M�M�!�.�.�t�y�$�?�?�?�?�����,�,�T�X�t�<�<�<�>� >rnc��t|��}|�|j|���d|�|j|����S)Nz ^ )r r�r�r-�rir�r�s rm� _print_MatPowzJuliaCodePrinter._print_MatPow�sL���$���� �-�-�d�i��>�>�>�>��+�+�D�H�d�;�;�;�=� =rnc�d��|jdrdSt���|��S)Nr]�pi�� _settingsr`�_print_NumberSymbol�rir�rls �rm� _print_PizJuliaCodePrinter._print_Pi�s/��� �>�(� #� 5��4��7�7�.�.�t�4�4� 4rnc��dS)NrOrq�rir�s rm�_print_ImaginaryUnitz%JuliaCodePrinter._print_ImaginaryUnit�s���trnc�d��|jdrdSt���|��S)Nr]r�r�r�s �rm� _print_Exp1zJuliaCodePrinter._print_Exp1�s/��� �>�(� #� 5��3��7�7�.�.�t�4�4� 4rnc�d��|jdrdSt���|��S)Nr]� eulergammar�r�s �rm�_print_EulerGammaz"JuliaCodePrinter._print_EulerGamma�s/��� �>�(� #� 5��<��7�7�.�.�t�4�4� 4rnc�d��|jdrdSt���|��S)Nr]�catalanr�r�s �rm�_print_CatalanzJuliaCodePrinter._print_Catalan�s/��� �>�(� #� 5��9��7�7�.�.�t�4�4� 4rnc�d��|jdrdSt���|��S)Nr]�goldenr�r�s �rm�_print_GoldenRatioz#JuliaCodePrinter._print_GoldenRatio�s/��� �>�(� #� 5��8��7�7�.�.�t�4�4� 4rnc��ddlm}ddlm}ddlm}|j}|j}|jds�t|j|��rmg}g}|j D]9\} } |� ||| ����|� | ���:|t||���} |� | ��S|jdr@|�|��s|�|��r|�||��S|� |��} |� |��} |�| �d| ����S)Nr)� Assignment)� Piecewise)� IndexedBaser]r\r�)�sympy.codegen.astr�$sympy.functions.elementary.piecewiser �sympy.tensor.indexedr r�r�r�r�r�r�rcr��has�_doprint_loopsrv)rir�rr r r�r�� expressions� conditionsr�r��tempr�r�s rm�_print_Assignmentz"JuliaCodePrinter._print_Assignmentsz��0�0�0�0�0�0�B�B�B�B�B�B�4�4�4�4�4�4��h���h���~�h�'� %�J�t�x��,K�,K� %��K��J��(� %� %���A��"�"�:�:�c�1�#5�#5�6�6�6��!�!�!�$�$�$�$��9�c�+�z�:�:�;�D��;�;�t�$�$� $� �>�*� %� I�3�7�7�;�+?�+?� I���� �$�$� I��&�&�s�C�0�0� 0��{�{�3�'�'�H��{�{�3�'�'�H��&�&�H�H�H�h�h�'G�H�H� Hrnc��dS)N�Infrqr�s rm�_print_Infinityz JuliaCodePrinter._print_Infinity#����urnc��dS)Nz-Infrqr�s rm�_print_NegativeInfinityz(JuliaCodePrinter._print_NegativeInfinity'����vrnc��dS)N�NaNrqr�s rm� _print_NaNzJuliaCodePrinter._print_NaN+rrnc�R��dd��fd�|D����zdzS)NzAny[�, c3�B�K�|]}��|��V��dSr��r��r�r�ris �rmr�z/JuliaCodePrinter._print_list.<locals>.<genexpr>0s-�����!?�!?�Q�$�+�+�a�.�.�!?�!?�!?�!?�!?�!?rn�])�joinr�s` rm� _print_listzJuliaCodePrinter._print_list/s4����� � �!?�!?�!?�!?�$�!?�!?�!?�?�?�?�#�E�Ernc��t|��dkrd|�|d��zSd|�|d��zS)Nr�z(%s,)rr�r)r�r�� stringifyr�s rm� _print_tuplezJuliaCodePrinter._print_tuple3sE�� �t�9�9��>�>��T�[�[��a��1�1�1� 1��D�N�N�4��6�6�6� 6rnc��dS)N�truerqr�s rm�_print_BooleanTruez#JuliaCodePrinter._print_BooleanTrue;rrnc��dS)N�falserqr�s rm�_print_BooleanFalsez$JuliaCodePrinter._print_BooleanFalse?s���wrnc�D�t|�����Sr�)�strr�r�s rm� _print_boolzJuliaCodePrinter._print_boolCs���4�y�y��� � � rnc�p��tj|jvrd|j�d|j�d�S|j|jfdkr d|dzS|jdkrd|��ddd � ��zS|jdkr$dd��fd �|D����zSd|��ddd d � ��zS)Nzzeros(rr�)r�r�z[%s])rrr�r�r�)�rowstart�rowend�colsepc�:��g|]}��|����Srqr!r"s �rmr�z6JuliaCodePrinter._print_MatrixBase.<locals>.<listcomp>Us#���&A�&A�&A�!�t�{�{�1�~�~�&A�&A�&Arnz; )r3r4�rowsepr5)r�Zeror�r�r��tabler$)ri�As` rm�_print_MatrixBasez"JuliaCodePrinter._print_MatrixBaseKs���� �6�Q�W� � � �&'�f�f�f�a�f�f�f�5� 5��f�a�f� �� '� '��A�d�G�#� #� �V�q�[�[��A�G�G�D�2�b��G�M�M�M� M� �V�q�[�[��D�I�I�&A�&A�&A�&A�q�&A�&A�&A�B�B�B� B������r�"�',�S� �:�:�:� :rnc �^�ddlm}|���}|d�|D����}|d�|D����}|d�|D����}d|�|���d|�|���d|�|���d|j�d|j�d� S) Nr)�Matrixc�$�g|] }|ddz��S)rr�rq�r��ks rmr�z;JuliaCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>^� ��(�(�(��A�a�D�1�H�(�(�(rnc�$�g|] }|ddz��S)r�rqr?s rmr�z;JuliaCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>_rArnc��g|] }|d�� S)�rqr?s rmr�z;JuliaCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>`s��&�&�&�q�a��d�&�&�&rnzsparse(rr�)�sympy.matricesr=�col_listr�r�r�)rir:r=�L�I�J�AIJs rm�_print_SparseRepMatrixz'JuliaCodePrinter._print_SparseRepMatrixZs���)�)�)�)�)�)� �J�J�L�L�� �F�(�(�a�(�(�(� )� )�� �F�(�(�a�(�(�(� )� )���f�&�&�A�&�&�&�'�'���/3�{�{�1�~�~�~�~�t�{�{�1�~�~�~�~�,0�K�K��,<�,<�,<�,<�a�f�f�f�a�f�f�f�N� Nrnc��|�|jtdd���d|jdz�d|jdz�d�zS)N�AtomT)�strict�[r��,r#)r��parentr r�r�r�s rm�_print_MatrixElementz%JuliaCodePrinter._print_MatrixElementesJ��� � ���j��.@�� �N�N�N��6�A�:�:�:�t�v��z�z�z�2�3� 3rnc�����fd�}��|j��dz||j|jjd��zdz||j|jjd��zdzS)Nc�<��|ddz}|d}|d}��|��}||krdn��|��}|dkr|dkr||krdS||kr|S|dz|zSd�|��|��|f��S)Nrr�rDr�r�)r�r$)r��lim�l�h�step�lstr�hstrris �rm�strslicez5JuliaCodePrinter._print_MatrixSlice.<locals>.strsliceks�����!��q��A��!��A��Q�4�D��;�;�q�>�>�D���H�H�5�5�$�+�+�a�.�.�D��q�y�y���6�6�a�3�h�h��3���6�6��K��#�:��,�,��x�x��t�{�{�4�'8�'8�$� ?�@�@�@rnrOrrPr�r#)r�rQ�rowslicer��colslice)rir�r[s` rm�_print_MatrixSlicez#JuliaCodePrinter._print_MatrixSlicejs���� A� A� A� A� A�� � �D�K�(�(�3�.������ �(9�!�(<�=�=�>�@C�D������ �(9�!�(<�=�=�>�@C�D� Ernc����fd�|jD��}��|jj���dd�|���d�S)Nc�:��g|]}��|����Srqr!)r�r�ris �rmr�z3JuliaCodePrinter._print_Indexed.<locals>.<listcomp>�s#���7�7�7�A����Q���7�7�7rnrOrPr#)r�r�r�r�r$)rir��indss` rm�_print_IndexedzJuliaCodePrinter._print_IndexedsM���7�7�7�7���7�7�7���;�;�t�y��7�7�7�7����$�����H�Hrnc�6�|�|j��Sr�)r�r�r�s rm� _print_IdxzJuliaCodePrinter._print_Idx�s���{�{�4�:�&�&�&rnc�H�d|�|jd��zS)Nzeye(%s)r)r�r�r�s rm�_print_Identityz JuliaCodePrinter._print_Identity�s���4�;�;�t�z�!�}�5�5�5�5rnc�T���d���fd��jD����S)Nz .* c�V��g|]%}��|t�������&Srq�r�r �r��argr�ris ��rmr�z;JuliaCodePrinter._print_HadamardProduct.<locals>.<listcomp>�sA���0�0�0�!�!�-�-�c�:�d�3C�3C�D�D�0�0�0rn)r$r�r�s``rm�_print_HadamardProductz'JuliaCodePrinter._print_HadamardProduct�sG�����{�{�0�0�0�0�0�%)�Y�0�0�0�1�1� 1rnc��t|��}d�|�|j|��|�|j|��g��S)Nz.**)r r$r�r�r-r�s rm�_print_HadamardPowerz%JuliaCodePrinter._print_HadamardPower�sT���$�����z�z� � � �d�i�� .� .� � � �d�h�� -� -���� rnc�b�|jdkrt|j��S|j�d|j��S)Nr�z // )r�r0rrr�s rm�_print_Rationalz JuliaCodePrinter._print_Rational�s0�� �6�Q�;�;��t�v�;�;� �!�V�V�V�T�V�V�,�,rnc��ddlm}m}|j}|tjd|zz ��||jtjz|��z}|�|��S)Nr)r/r=rD) �sympy.functionsr/r=�argumentr�Pir��Halfr�)rir�r/r=r��expr2s rm� _print_jnzJuliaCodePrinter._print_jn��f��1�1�1�1�1�1�1�1� �M����Q�T�1�Q�3�Z� � �����a�f�)<�a�!@�!@�@���{�{�5�!�!�!rnc��ddlm}m}|j}|tjd|zz ��||jtjz|��z}|�|��S)Nr)r/r>rD) rrr/r>rsrrtr�rur�)rir�r/r>r�rvs rm� _print_ynzJuliaCodePrinter._print_yn�rxrnc�r��|jdjdkrtd���g}�jdrc�fd�|jdd�D��}d��|jdj��z}d�|��|z}d|zd zSt|j��D]�\}\}}|d kr,|�d ��|��z��nb|t|j��d z kr|dkr|�d ��n+|�d��|��z����|��} |�| ��|t|j��d z kr|�d����d�|��S)Nr�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.r]c���g|]A\}}d���|����|������BS)z ({}) ? ({}) :)ryr�)r�r�r�ris �rmr�z5JuliaCodePrinter._print_Piecewise.<locals>.<listcomp>�sV���3�3�3��1�a�'�-�-�� � �A���� � �A���8�8�3�3�3rnz (%s)� �(r�rzif (%s)r��elsez elseif (%s)r�) r��cond� ValueErrorr�r�r�r$� enumerater�r�) rir�r��ecpairs�elast�pwr�r�r��code0s ` rm�_print_Piecewisez!JuliaCodePrinter._print_Piecewise�s���� �9�R�=� �� %� %��/�0�0� 0� �� �>�(� #� $�3�3�3�3�#'�9�S�b�S�>�3�3�3�G��d�k�k�$�)�B�-�*<�=�=�=�E����7�#�#�e�+�B���8�c�>� !�&�t�y�1�1� (� (� ��6�A�q���6�6��L�L��T�[�[��^�^�!;�<�<�<�<��#�d�i�.�.�1�,�,�,��d����L�L��(�(�(�(��L�L�����Q���!?�@�@�@�� � �A����� � �U�#�#�#���D�I����*�*�*��L�L��'�'�'���9�9�U�#�#� #rnc�N�������\}}d}|jrZ|���\}}|jr|jrt | |���d}n!|jr|jrt | |���d}|d���fd��jD����zS)Nr�r�z * c3�^�K�|]'}��|t�����V��(dSr�rirjs ��rmr�z1JuliaCodePrinter._print_MatMul.<locals>.<genexpr>�s;����� K� K�#�T� � �s�J�t�$4�$4� 5� 5� K� K� K� K� K� Krn)� as_coeff_mmulr�� as_real_imag�is_zeror�r r$r�)rir�r��mr*rPrOs`` rm� _print_MatMulzJuliaCodePrinter._print_MatMul�s������!�!�#�#���1��� �;� ��^�^�%�%�F�B���z� �b�n� �"�A�2�q�)�)������� ��� �"�A�2�q�)�)�����e�j�j� K� K� K� K� K��� K� K� K� � � � rnc��� � �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 )z ^function z^if �^elseif �^else$z^for )z^end$r�r�c�8�g|]}|�d����S)z )�lstrip)r��lines rmr�z0JuliaCodePrinter.indent_code.<locals>.<listcomp>�s$��6�6�6�����U�#�#�6�6�6rnc�b���g|]*�tt�fd��D��������+S)c3�8�K�|]}t|���V��dSr�r�r�rPr�s �rmr�z:JuliaCodePrinter.indent_code.<locals>.<listcomp>.<genexpr>��-�����B�B�"�V�B��-�-�B�B�B�B�B�Brn��int�any)r�r�� inc_regexs @�rmr�z0JuliaCodePrinter.indent_code.<locals>.<listcomp>��O����(�(�(����B�B�B�B� �B�B�B�B�B�C�C�(�(�(rnc�b���g|]*�tt�fd��D��������+S)c3�8�K�|]}t|���V��dSr�rr�s �rmr�z:JuliaCodePrinter.indent_code.<locals>.<listcomp>.<genexpr>�r�rnr�)r�r�� dec_regexs @�rmr�z0JuliaCodePrinter.indent_code.<locals>.<listcomp>�r�rnr)r�r})r�r0r�� splitlinesr$r�r�) ri�code� code_lines�tab�increase�decrease�pretty�level�nr�r�r�s @@rmr�zJuliaCodePrinter.indent_code�sD���� �d�C� � � '��)�)�$�/�/�$�*?�*?�@�@�J��7�7�:�&�&� &���I� �3� �7�6��6�6�6��(�(�(�(�!%�(�(�(��(�(�(�(�!%�(�(�(������ ���� !� !�G�A�t��z�!�!�� � �d�#�#�#�� �X�a�[� �E� �M�M�C��I�I�t�t�4� 5� 5� 5� �X�a�[� �E�E�� rn)7�__name__� __module__� __qualname__�__doc__� printmethod�language� _operatorsrbr r^�__annotations__rarsrvr{rr�r�r�r�r�r�r�r�r�r�rrrrrrrr%r(� _print_Tupler+r.r1r;rKrRr^rbrdrfrlrnrprwrzr�r�r�� __classcell__)rls@rmrRrR0sz�����������K��H������J� )-��[�-J�)�)����� O�O�)�)������!#�/�/�/�/�/�/����!�!�!�#�#�#�3�3�3�'�'�'�B�B�B� '� '� '�HZ�HZ�HZ�T9�9�9� >�>�>�(=�=�=� 5�5�5�5�5����5�5�5�5�5�5�5�5�5�5�5�5�5�5�5�5�5�5�5�5�I�I�I�:���������F�F�F�7�7�7�  �L�������!�!�!� :� :� :�N�N�N�3�3�3� E�E�E�*I�I�I� '�'�'�6�6�6�1�1�1����-�-�-� "�"�"�"�"�"�"$�"$�"$�H � � �$������rnrRNc �H�t|���||��S)a)Converts `expr` to a string of Julia 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 can be helpful for expressions that generate multi-line statements. precision : integer, optional The precision for numbers such as pi [default=16]. 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]. inline: bool, optional If True, we try to create single-statement code instead of multiple statements. [default=True]. Examples ======== >>> from sympy import julia_code, symbols, sin, pi >>> x = symbols('x') >>> julia_code(sin(x).series(x).removeO()) 'x .^ 5 / 120 - x .^ 3 / 6 + x' >>> from sympy import Rational, ceiling >>> x, y, tau = symbols("x, y, tau") >>> julia_code((2*tau)**Rational(7, 2)) '8 * sqrt(2) * tau .^ (7 // 2)' Note that element-wise (Hadamard) operations are used by default between symbols. This is because its possible in Julia to write "vectorized" code. It is harmless if the values are scalars. >>> julia_code(sin(pi*x*y), assign_to="s") 's = sin(pi * x .* y)' If you need a matrix product "*" or matrix power "^", you can specify the symbol as a ``MatrixSymbol``. >>> from sympy import Symbol, MatrixSymbol >>> n = Symbol('n', integer=True, positive=True) >>> A = MatrixSymbol('A', n, n) >>> julia_code(3*pi*A**3) '(3 * pi) * A ^ 3' This class uses several rules to decide which symbol to use a product. Pure numbers use "*", Symbols use ".*" and MatrixSymbols use "*". A HadamardProduct can be used to specify componentwise multiplication ".*" of two MatrixSymbols. There is currently there is no easy way to specify scalar symbols, so sometimes the code might have some minor cosmetic issues. For example, suppose x and y are scalars and A is a Matrix, then while a human programmer might write "(x^2*y)*A^3", we generate: >>> julia_code(x**2*y*A**3) '(x .^ 2 .* y) * A ^ 3' Matrices are supported using Julia inline notation. When using ``assign_to`` with matrices, the name can be specified either as a string or as a ``MatrixSymbol``. The dimensions must align in the latter case. >>> from sympy import Matrix, MatrixSymbol >>> mat = Matrix([[x**2, sin(x), ceiling(x)]]) >>> julia_code(mat, assign_to='A') 'A = [x .^ 2 sin(x) ceil(x)]' ``Piecewise`` expressions are implemented with logical masking by default. Alternatively, you can pass "inline=False" to use if-else conditionals. 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 >>> pw = Piecewise((x + 1, x > 0), (x, True)) >>> julia_code(pw, assign_to=tau) 'tau = ((x > 0) ? (x + 1) : (x))' Note that any expression that can be generated normally can also exist inside a Matrix: >>> mat = Matrix([[x**2, pw, sin(x)]]) >>> julia_code(mat, assign_to='A') 'A = [x .^ 2 ((x > 0) ? (x + 1) : (x)) 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)]. This can be used to call a custom Julia function. >>> from sympy import Function >>> f = Function('f') >>> g = Function('g') >>> custom_functions = { ... "f": "existing_julia_fcn", ... "g": [(lambda x: x.is_Matrix, "my_mat_fcn"), ... (lambda x: not x.is_Matrix, "my_fcn")] ... } >>> mat = Matrix([[1, x]]) >>> julia_code(f(x) + g(x) + g(mat), user_functions=custom_functions) 'existing_julia_fcn(x) + my_fcn(x) + my_mat_fcn([1 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])) >>> julia_code(e.rhs, assign_to=e.lhs, contract=False) 'Dy[i] = (y[i + 1] - y[i]) ./ (t[i + 1] - t[i])' )rR�doprint)r�� assign_torjs rm� julia_coder�s#��L �H� %� %� -� -�d�I� >� >�>rnc �:�tt|fi|����dS)z~Prints the Julia representation of the given expression. See `julia_code` for the meaning of the optional arguments. N)�printr�)r�rjs rm�print_julia_coder��s(��  �*�T� &� &�X� &� &�'�'�'�'�'rnr�)r�� __future__r�typingr� sympy.corerrrr�sympy.core.mulr �sympy.core.numbersr �sympy.printing.codeprinterr �sympy.printing.precedencer r rPrrdrgrRr�r�rqrnrm�<module>r�sR�� � �#�"�"�"�"�"�������,�,�,�,�,�,�,�,�,�,�,�,�&�&�&�&�&�&�+�+�+�+�+�+�2�2�2�2�2�2�<�<�<�<�<�<�<�<������� (� (� (�� ����� � ����M�M�M�M�M�{�M�M�M�`F?�F?�F?�F?�R(�(�(�(�(rn
Memory