� ��g�c� �*�dZddlmZddlmZddlmZmZmZm Z ddl m Z ddl m Z ddlmZddlmZmZdd lmZgd �Zid 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/d0d1d2d3d4��ZGd5�d6e��Zd:d8�Zd9�Zd7S);ai Octave (and Matlab) code printer The `OctaveCodePrinter` converts SymPy expressions into Octave expressions. It uses a subset of the Octave language for Matlab compatibility. A complete code generator, which uses `octave_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)1�sin�cos�tan�cot�sec�csc�asin�acos�acot�atan�atan2�asec�acsc�sinh�cosh�tanh�coth�csch�sech�asinh�acosh�atanh�acoth�asech�acsch�erfc�erfi�erf�erfinv�erfcinv�besseli�besselj�besselk�bessely� bernoulli�beta�euler�exp� factorial�floor�fresnelc�fresnels�gamma�harmonic�log�polylog�sign�zeta�legendre�Abs�abs�arg�angle�binomial�bincoeff�ceiling�ceil� chebyshevu� chebyshevU� chebyshevt� chebyshevT�Chi�coshint�Ci�cosint� conjugate�conj� DiracDelta�dirac� Heaviside� heaviside�im�imag�laguerre� laguerreL�LambertW�lambertw�li�logint�loggamma�gammaln�Max�max�min�mod�psi�real� pochhammer�sinhint�sinint)�Min�Mod� polygamma�re�RisingFactorial�Shi�Sic ���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�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd �Zd!�Zd"�Z e Z!e 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.d.�Z/d/�Z0d0�Z1d1�Z2d2�Z3d3�Z4d4�Z5d5�Z6d6�Z7d7�Z8d8�Z9d9�Z:d:�Z;d;�Z<d<�Z=e=xZ>Z?d=�Z@e@xZAZBd>�ZCd?�ZDd@�ZE�xZFS)A�OctaveCodePrinterzL A printer to convert expressions to strings of Octave/Matlab code. �_octave�Octave�&�|�~)�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 ��e/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/printing/octave.pyr�zOctaveCodePrinter.__init__Ys���� ������"�"�"�#�C���$I�$I�J�J��� ��#�#�D��$9�$9�:�:�:��L�L�!1�2�6�6� � ��#�#�I�.�.�.�.�.�c� �|dzS)N��)r��ps r��_rate_index_positionz&OctaveCodePrinter._rate_index_positionas ����s� r�c� �d|zS)Nz%s;r�)r�� codestrings r��_get_statementz OctaveCodePrinter._get_statementes ���z�!�!r�c�,�d�|��S)Nz% {}��format)r��texts r�� _get_commentzOctaveCodePrinter._get_commentis���}�}�T�"�"�"r�c�.�d�||��S)Nz{} = {};r�)r��name�values r��_declare_number_constz'OctaveCodePrinter._declare_number_constms��� � ��u�-�-�-r�c�,�|�|��S�N)� indent_code)r��liness r�� _format_codezOctaveCodePrinter._format_codeqs������&�&�&r�c�N��|j\�}�fd�t|��D��S)Nc3�D�K�|]}t���D]}||fV�� �dSr�)�range)�.0�j�i�rowss �r�� <genexpr>z=OctaveCodePrinter._traverse_matrix_indices.<locals>.<genexpr>xs:�����A�A�1�U�4�[�[�A�A���A��A�A�A�A�A�A�Ar�)�shaper�)r��mat�colsr�s @r��_traverse_matrix_indicesz*OctaveCodePrinter._traverse_matrix_indicesus.����Y� ��d�A�A�A�A��d� � �A�A�A�Ar�c ���g}g}|D]f}t|j|j|jdz|jdzg��\}}}|�d|�d|�d|����|�d���g||fS)N�zfor � = �:�end)�map�_print�label�lower�upper�append)r��indices� open_lines� close_linesr��var�start�stops r��_get_loop_opening_endingz*OctaveCodePrinter._get_loop_opening_ending{s���� �� �� &� &�A�"�4�;��W�a�g��k�1�7�Q�;�7� 9� 9� �C��� � � � �#�#�#�u�u�u�d�d�C� D� D� D� � � �u� %� %� %� %��;�&�&r�c����|jrA|jr:tj|zjr&d��tj |z��zSt |���|���\}}|dkrt| |��}d}nd}g}g}g}�j dvr|� ��}ntj |��}|D�]v} | j r�| jr�| jjr�| jjr�| jdkr1|�t'| j| j d������et+| jdj��d kr/t/| jt��r|�| ��|�t'| j| j ������| jrt| tjurf| jd kr'|�t5| j����| jd kr'|�t5| j������a|�| ����x|p tjg}��fd �|D��} ��fd �|D��} |D]I} | j|vr>d | |�| j��z| |�| j��<�Jd �} |s|| || ��zSt+|��d kr,|djrdnd} || || ��z| z| dzSt=d�|D����rdnd} || || ��z| zd | || ��zzS)Nz%sir�-�)�old�none�����F)�evaluater�c�<��g|]}��|�����Sr��� parenthesize�r��x�precr�s ��r�� <listcomp>z0OctaveCodePrinter._print_Mul.<locals>.<listcomp>��)���7�7�7���"�"�1�d�+�+�7�7�7r�c�<��g|]}��|�����Sr�r�r�s ��r�r�z0OctaveCodePrinter._print_Mul.<locals>.<listcomp>�r�r�z(%s)c��|d}tdt|����D]$}||dz jrdnd}||z||z}�%|S)Nrr��*�.*)r��len� is_number)�a�a_str�rr��mulsyms r��multjoinz.OctaveCodePrinter._print_Mul.<locals>.multjoin�s\���a��A��1�c�!�f�f�%�%� *� *�� !�!�A�#�� 0�:���d����J��q��)����Hr��/�./c3�$K�|] }|jV�� dSr��r�)r��bis r�r�z/OctaveCodePrinter._print_Mul.<locals>.<genexpr>�s$����9�9��� �9�9�9�9�9�9r�)r�� is_imaginaryr� ImaginaryUnit� is_Integerr�r � as_coeff_Mulr �order�as_ordered_factorsr� make_args�is_commutative�is_Powr5� is_Rational� is_negativer�r�baser��args� isinstance�Infinityr�r�q�One�index�all)r��expr�c�er>r��b� pow_parenr��itemr��b_strr��divsymr�s` @r�� _print_MulzOctaveCodePrinter._print_Mul�s����� �N� >�t�0� >����%�1� >��4�;�;���'7��'<�=�=�=� =��$����� � �"�"���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�!�*�&<�&<��6�Q�;�;��H�H�X�d�f�-�-�.�.�.��6�Q�;�;��H�H�X�d�f�-�-�.�.�.���������� �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�(�(�)�� � � �� :��(�(�1�e�,�,�,� ,� ��V�V�q�[�[��a�D�N�4�S�S��F��(�(�1�e�,�,�,�v�5��a��@� @��9�9�q�9�9�9�9�9�C�S�S�t�F��8�8�A�u�-�-�-���#�h�h�q�%�&8�&8�8�9� :r�c��|�|j��}|�|j��}|j}d�|||��S)Nz{} {} {})r��lhs�rhs�rel_opr�)r�r��lhs_code�rhs_code�ops r��_print_Relationalz#OctaveCodePrinter._print_Relational�sG���;�;�t�x�(�(���;�;�t�x�(�(�� �[��� � ��2�x�8�8�8r�c�l�td�|jD����rdnd}t|��}t|jd��rd|�|j��zS|jr�t|jd��r3|jjrdnd}d |zd|�|j��zzSt|jd ��r4|jjrdnd}d |zd |� |j|��zzS|� |j|���|�|� |j|����S) Nc3�$K�|] }|jV�� dSr�r�)r�r�s r�r�z/OctaveCodePrinter._print_Pow.<locals>.<genexpr>�s$����>�>�q�q�{�>�>�>�>�>�>r��^z.^g�?zsqrt(%s)g�r�r��1r��%s) r�r�r r r5r�r�r�r�r�)r�r�� powsymbol�PREC�syms r�� _print_PowzOctaveCodePrinter._print_Pow�sE���>�>�D�I�>�>�>�>�>�H�C�C�D� ��$���� ���#� &� &� 7��� � �D�I� 6� 6�6� 6� � � M��D�H�d�+�+� G�!�Y�0�:�c�c�d���S�y�:�� � �D�I�0F�0F�#F�F�F��D�H�b�)�)� M�!�Y�0�:�c�c�d���S�y�4�$�*;�*;�D�I�t�*L�*L�#L�L�L��,�,�T�Y��=�=�=�y�y��,�,�T�X�t�<�<�<�>� >r�c��t|��}|�|j|���d|�|j|����S)Nr )r r�r�r5�r�r�rs r�� _print_MatPowzOctaveCodePrinter._print_MatPow�sL���$�����+�+�D�I�t�<�<�<�<��+�+�D�H�d�;�;�;�=� =r�c��t|��}|�|j|���d|�|j|����S)Nz \ )r r��matrix�vectorrs r��_print_MatrixSolvez$OctaveCodePrinter._print_MatrixSolve�sN���$����!�.�.�t�{�D�A�A�A�A�!�.�.�t�{�D�A�A�A�C� Cr�c��dS)N�pir��r�r�s r�� _print_PizOctaveCodePrinter._print_Pi�����tr�c��dS)N�1ir�rs r��_print_ImaginaryUnitz&OctaveCodePrinter._print_ImaginaryUnit�rr�c��dS)Nzexp(1)r�rs r�� _print_Exp1zOctaveCodePrinter._print_Exp1�s���xr�c��dS)Nz (1+sqrt(5))/2r�rs r��_print_GoldenRatioz$OctaveCodePrinter._print_GoldenRatios ���r�c��ddlm}ddlm}ddlm}|j}|j}|jds�t|j|��rmg}g}|j D]9\} } |� ||| ����|� | ���:|t||���} |� | ��S|jdr@|�|��s|�|��r|�||��S|� |��} |� |��} |�| �d| ����S)Nr)� Assignment)� Piecewise)� IndexedBaserr~r�)�sympy.codegen.astr%�$sympy.functions.elementary.piecewiser&�sympy.tensor.indexedr'rr� _settingsr�r�r�r�r��has�_doprint_loopsr�)r�r�r%r&r'rr� expressions� conditionsr�r��temprrs r��_print_Assignmentz#OctaveCodePrinter._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� Hr�c��dS)N�infr�rs r��_print_Infinityz!OctaveCodePrinter._print_Infinity$����ur�c��dS)Nz-infr�rs r��_print_NegativeInfinityz)OctaveCodePrinter._print_NegativeInfinity(����vr�c��dS)N�NaNr�rs r�� _print_NaNzOctaveCodePrinter._print_NaN,r5r�c�R��dd��fd�|D����zdzS)N�{�, c3�B�K�|]}��|��V��dSr��r��r�r�r�s �r�r�z0OctaveCodePrinter._print_list.<locals>.<genexpr>1s-�����<�<�!�t�{�{�1�~�~�<�<�<�<�<�<r��}��joinrs` r�� _print_listzOctaveCodePrinter._print_list0s4����T�Y�Y�<�<�<�<�t�<�<�<�<�<�<�s�B�Br�c��dS)N�truer�rs r��_print_BooleanTruez$OctaveCodePrinter._print_BooleanTrue7r8r�c��dS)N�falser�rs r��_print_BooleanFalsez%OctaveCodePrinter._print_BooleanFalse;s���wr�c�D�t|�����Sr�)�strr�rs r�� _print_boolzOctaveCodePrinter._print_bool?s���4�y�y��� � � r�c�B����j�jfdkrdStj�jvrd�j�d�j�d�S�j�jfdkr���d��Sdd���fd �t�j��D����zS) N)rrz[]zzeros(r>�))r�r�z[%s]z; c3�n�K�|]/}d��fd��|dd�fD����V��0dS)� c�:��g|]}��|����Sr�r@rAs �r�r�zAOctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>.<listcomp>Ps#���+L�+L�+L�q�D�K�K��N�N�+L�+L�+Lr�NrC)r�r��Ar�s ��r�r�z6OctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>Psd�����":�":�&'�#&�(�(�+L�+L�+L�+L�A�a����d�G�+L�+L�+L�"M�"M�":�":�":�":�":�":r�)r�r�r�Zeror�r�rDr�)r�rTs``r��_print_MatrixBasez#OctaveCodePrinter._print_MatrixBaseGs����� �F�A�F� �v� %� %��4� �V�q�w� � � �&'�f�f�f�a�f�f�f�5� 5��f�a�f� �� '� '��;�;�q��w�'�'� '��� � �":�":�":�":�":�+0���=�=�":�":�":�:�:�:� :r�c �d�ddlm}|���}|d�|D��g��}|d�|D��g��}|d�|D��g��}d|�|���d|�|���d|�|���d|j�d|j�d� S) Nr)�Matrixc�$�g|] }|ddz��S)rr�r��r��ks r�r�z<OctaveCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>X� ��)�)�)�!�Q�q�T�A�X�)�)�)r�c�$�g|] }|ddz��S)r�r�rZs r�r�z<OctaveCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>Yr\r�c��g|] }|d�� S)�r�rZs r�r�z<OctaveCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>Zs��'�'�'��q��t�'�'�'r�zsparse(r>rP)�sympy.matricesrX�col_listr�r�r�)r�rTrX�L�I�J�AIJs r��_print_SparseRepMatrixz(OctaveCodePrinter._print_SparseRepMatrixTs���)�)�)�)�)�)� �J�J�L�L�� �F�)�)�q�)�)�)�*� +� +�� �F�)�)�q�)�)�)�*� +� +���f�'�'�Q�'�'�'�(�)�)���/3�{�{�1�~�~�~�~�t�{�{�1�~�~�~�~�,0�K�K��,<�,<�,<�,<�a�f�f�f�a�f�f�f�N� Nr�c��|�|jtdd���d|jdz�d|jdz�d�zS)N�AtomT)�strict�(r�r>rP)r��parentr r�r�rs r��_print_MatrixElementz&OctaveCodePrinter._print_MatrixElement_sJ��� � ���j��.@�� �N�N�N� �F�Q�J�J�J���� � � �3�4� 4r�c�����fd�}��|j��dz||j|jjd��zdz||j|jjd��zdzS)Nc�<��|ddz}|d}|d}��|��}||krdn��|��}|dkr|dkr||krdS||kr|S|dz|zSd�|��|��|f��S)Nrr�r_r�r�)r�rD)r��lim�l�h�step�lstr�hstrr�s �r��strslicez6OctaveCodePrinter._print_MatrixSlice.<locals>.strslicees�����!��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�$� ?�@�@�@r�rjrr>r�rP)r�rk�rowslicer��colslice)r�r�rus` r��_print_MatrixSlicez$OctaveCodePrinter._print_MatrixSliceds���� A� A� A� A� A�� � �D�K�(�(�3�.������ �(9�!�(<�=�=�>�@D�E������ �(9�!�(<�=�=�>�@C�D� Er�c����fd�|jD��}��|jj���dd�|���d�S)Nc�:��g|]}��|����Sr�r@)r�r�r�s �r�r�z4OctaveCodePrinter._print_Indexed.<locals>.<listcomp>zs#���7�7�7�A����Q���7�7�7r�rjr>rP)r�r�r�r�rD)r�r��indss` r��_print_Indexedz OctaveCodePrinter._print_IndexedysM���7�7�7�7���7�7�7���;�;�t�y��7�7�7�7����4�����I�Ir�c�6�|�|j��Sr�)r�r�rs r�� _print_IdxzOctaveCodePrinter._print_Idx~s���{�{�4�:�&�&�&r�c�h���td�dt��fd�|jD����zS)Nrzdouble(%s == %s)c3�D�K�|]}��|���V��dSr�r�r�s ��r�r�z:OctaveCodePrinter._print_KroneckerDelta.<locals>.<genexpr>�sG�����*>�*>�./�+/�*;�*;�A�t�*D�*D�*>�*>�*>�*>�*>�*>r�)r �tupler�)r�r�r�s` @r��_print_KroneckerDeltaz'OctaveCodePrinter._print_KroneckerDelta�sT�����%� ��!�E�*>�*>�*>�*>�*>�37�9�*>�*>�*>�%>�%>�>� >r�c�T���d���fd��jD����S)Nr�c�V��g|]%}��|t�������&Sr�)r�r )r�rCr�r�s ��r�r�z<OctaveCodePrinter._print_HadamardProduct.<locals>.<listcomp>�sA���0�0�0�!��+�+�C��D�1A�1A�B�B�0�0�0r�)rDr�rs``r��_print_HadamardProductz(OctaveCodePrinter._print_HadamardProduct�sG�����y�y�0�0�0�0�0�%)�Y�0�0�0�1�1� 1r�c��t|��}d�|�|j|��|�|j|��g��S)Nz.**)r rDr�r�r5rs r��_print_HadamardPowerz&OctaveCodePrinter._print_HadamardPower�sT���$�����z�z� � � �d�i�� .� .� � � �d�h�� -� -���� r�c����|j}t|��dkr|d|dkr |dg}d��fd�|D����}d|zdzS)Nr_rr�r>c3�B�K�|]}��|��V��dSr�r@)r��nr�s �r�r�z4OctaveCodePrinter._print_Identity.<locals>.<genexpr>�s-�����4�4��d�k�k�!�n�n�4�4�4�4�4�4r�zeye(rP)r�r�rD)r�r�r��ss` r��_print_Identityz!OctaveCodePrinter._print_Identity�sk���� �� �u�:�:��?�?�u�Q�x�5��8�3�3��1�X�J�E� �I�I�4�4�4�4�e�4�4�4� 4� 4����z�C��r�c��d�|�|jd��|�|jd����S)Nz (gammainc({1}, {0}).*gamma({0}))rr��r�r�r�rs r��_print_lowergammaz#OctaveCodePrinter._print_lowergamma�sF��1�8�8� �K�K�� �!� � %� %�t�{�{�4�9�Q�<�'@�'@�B�B� Br�c��d�|�|jd��|�|jd����S)Nz)(gammainc({1}, {0}, 'upper').*gamma({0}))rr�r�rs r��_print_uppergammaz#OctaveCodePrinter._print_uppergamma�sF��:�A�A� �K�K�� �!� � %� %�t�{�{�4�9�Q�<�'@�'@�B�B� Br�c�b�d|�|jdtjz ��zS)Nzsinc(%s)r)r�r�r�Pirs r�� _print_sinczOctaveCodePrinter._print_sinc�s&���D�K�K�� �!� �Q�T�(9�:�:�:�:r�c�t�d|�|j���d|�|j���d�S)N�besselh(z, 1, rP�r�r��argumentrs r��_print_hankel1z OctaveCodePrinter._print_hankel1��>���'+�{�{�4�:�'>�'>�'>�'>�'+�{�{�4�=�'A�'A�'A�'A�C� Cr�c�t�d|�|j���d|�|j���d�S)Nr�z, 2, rPr�rs r��_print_hankel2z OctaveCodePrinter._print_hankel2�r�r�c��ddlm}m}|j}|tjd|zz ��||jtjz|��z}|�|��S)Nr)�sqrtr/r_) �sympy.functionsr�r/r�rr�r��Halfr�)r�r�r�r/r��expr2s r�� _print_jnzOctaveCodePrinter._print_jn��f��1�1�1�1�1�1�1�1� �M����Q�T�1�Q�3�Z� � �����a�f�)<�a�!@�!@�@���{�{�5�!�!�!r�c��ddlm}m}|j}|tjd|zz ��||jtjz|��z}|�|��S)Nr)r�r1r_) r�r�r1r�rr�r�r�r�)r�r�r�r1r�r�s r�� _print_ynzOctaveCodePrinter._print_yn�r�r�c�H�d|�|jd��zS)Nz airy(0, %s)r�r�r�rs r�� _print_airyaizOctaveCodePrinter._print_airyai�����t�{�{�4�9�Q�<�8�8�8�8r�c�H�d|�|jd��zS)Nz airy(1, %s)rr�rs r��_print_airyaiprimez$OctaveCodePrinter._print_airyaiprime�r�r�c�H�d|�|jd��zS)Nz airy(2, %s)rr�rs r�� _print_airybizOctaveCodePrinter._print_airybi�r�r�c�H�d|�|jd��zS)Nz airy(3, %s)rr�rs r��_print_airybiprimez$OctaveCodePrinter._print_airybiprime�r�r�c�|�|j\}}|dkr|�|��Sd|�|��zS)Nr�z expint(%s))r��_print_not_supportedr�)r�r��mur�s r�� _print_expintzOctaveCodePrinter._print_expint�s?��� ���A� ��7�7��,�,�T�2�2� 2��d�k�k�!�n�n�,�,r�c ����t|j��dksJ�d��j|jjd��fd�t|j��D�������S)Nr_z{name}({args})r>c�:��g|]}��|����Sr�r@)r�r�r�s �r�r�z?OctaveCodePrinter._one_or_two_reversed_args.<locals>.<listcomp>�s#���H�H�H�q�D�K�K��N�N�H�H�Hr�)r�r�)r�r�r�r�r��__name__rD�reversedrs` r��_one_or_two_reversed_argsz+OctaveCodePrinter._one_or_two_reversed_args�sw����4�9�~�~��"�"�"�"��&�&��%�d�n�&=�>����H�H�H�H�H�T�Y�4G�4G�H�H�H�I�I�'� � � r�c ���d�|j|jj|�|jd��|�|j|jdd�������S)Nz{name}({arg1}, {arg2})rr�)r��arg1�arg2)r�r�r�r�r�r��funcrs r��_nested_binary_math_funcz*OctaveCodePrinter._nested_binary_math_func�sf��'�.�.��%�d�n�&=�>����T�Y�q�\�*�*����Y�T�Y�� �!�"�"� �6�7�7�/��� r�c���|jdjdkrtd���g}�jdrv�fd�|jdd�D��}d��|jdj��z}d�|��|zdt|��zz}d |zdzSt|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.rc���g|]A\}}d���|����|������BS)z({0}).*({1}) + (~({0})).*()r�r�)r�r�r�r�s �r�r�z6OctaveCodePrinter._print_Piecewise.<locals>.<listcomp>�sV���3�3�3��1�a�4�:�:�� � �A���� � �A���8�8�3�3�3r�r z ... rPrjrzif (%s)r��elsez elseif (%s)r�� ) r��cond� ValueErrorr+r�r�rDr�� enumerater�) r�r�r��ecpairs�elast�pwr�r�r��code0s ` r��_print_Piecewisez"OctaveCodePrinter._print_Piecewise�s���� �9�R�=� �� %� %��/�0�0� 0� �� �>�(� #� $�3�3�3�3�#'�9�S�b�S�>�3�3�3�G��4�;�;�t�y��}�'9�:�:�:�E����w�'�'�%�/�#�c�'�l�l�2B�B�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�#�#� #r�c��t|j��dkr#d|�|jd��zS|�|��S)Nr�zzeta(%s)r)r�r�r�r�rs r�� _print_zetazOctaveCodePrinter._print_zetasH�� �t�y�>�>�Q� � ��� � �D�I�a�L� 9� 9�9� 9��,�,�T�2�2� 2r�c��� � �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 r�r�z1OctaveCodePrinter.indent_code.<locals>.<listcomp>(s$��6�6�6�����U�#�#�6�6�6r�c�b���g|]*�tt�fd��D��������+S)c3�8�K�|]}t|���V��dSr�r�r�rmr�s �r�r�z;OctaveCodePrinter.indent_code.<locals>.<listcomp>.<genexpr>*�-�����B�B�"�V�B��-�-�B�B�B�B�B�Br���int�any)r�r�� inc_regexs @�r�r�z1OctaveCodePrinter.indent_code.<locals>.<listcomp>*�O����(�(�(����B�B�B�B� �B�B�B�B�B�C�C�(�(�(r�c�b���g|]*�tt�fd��D��������+S)c3�8�K�|]}t|���V��dSr�rr�s �r�r�z;OctaveCodePrinter.indent_code.<locals>.<listcomp>.<genexpr>,r�r�r�)r�r�� dec_regexs @�r�r�z1OctaveCodePrinter.indent_code.<locals>.<listcomp>,r�r�r)r�r�)r�rMr�� splitlinesrDr�r�) r��code� code_lines�tab�increase�decrease�pretty�levelr�r�r�r�s @@r�r�zOctaveCodePrinter.indent_codesD���� �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�� r�)Gr�� __module__� __qualname__�__doc__� printmethod�language� _operatorsr�r r��__annotations__r�r�r�r�r�r�r�r�r�rrrrrrr!r#r1r4r7r;rE� _print_tuple� _print_Tuple� _print_ListrHrKrNrVrfrlrxr|r~r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��_print_DiracDelta�_print_LambertWr�� _print_Max� _print_Minr�r�r�� __classcell__)r�s@r�rrrrAs �����������K��H������J� )-��[�-J�)�)����� O�O�)�)������!#�/�/�/�/�/�/����"�"�"�#�#�#�.�.�.�'�'�'�B�B�B� '� '� '�H:�H:�H:�T9�9�9� >�>�>�(=�=�=� C�C�C� ������������ I�I�I�:���������C�C�C��L��L��K�������!�!�!� :� :� :�N�N�N�4�4�4� E�E�E�*J�J�J� '�'�'�>�>�>� 1�1�1���� � � �B�B�B� B�B�B� ;�;�;� C�C�C� C�C�C� "�"�"�"�"�"�9�9�9�9�9�9�9�9�9�9�9�9�-�-�-� � � �+D�C������7�6�J��"$�"$�"$�J3�3�3�������r�rrNc �H�t|���||��S)a�Converts `expr` to a string of Octave (or Matlab) code. The string uses a subset of the Octave language for Matlab compatibility. 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 octave_code, symbols, sin, pi >>> x = symbols('x') >>> octave_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") >>> octave_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 very common in Octave to write "vectorized" code. It is harmless if the values are scalars. >>> octave_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) >>> octave_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: >>> octave_code(x**2*y*A**3) '(x.^2.*y)*A^3' Matrices are supported using Octave 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)]]) >>> octave_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)) >>> octave_code(pw, assign_to=tau) 'tau = ((x > 0).*(x + 1) + (~(x > 0)).*(x));' Note that any expression that can be generated normally can also exist inside a Matrix: >>> mat = Matrix([[x**2, pw, sin(x)]]) >>> octave_code(mat, assign_to='A') 'A = [x.^2 ((x > 0).*(x + 1) + (~(x > 0)).*(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 Octave function. >>> from sympy import Function >>> f = Function('f') >>> g = Function('g') >>> custom_functions = { ... "f": "existing_octave_fcn", ... "g": [(lambda x: x.is_Matrix, "my_mat_fcn"), ... (lambda x: not x.is_Matrix, "my_fcn")] ... } >>> mat = Matrix([[1, x]]) >>> octave_code(f(x) + g(x) + g(mat), user_functions=custom_functions) 'existing_octave_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])) >>> octave_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_tor�s r�� octave_coder�;s#��P �X� &� &� .� .�t�Y� ?� ?�?r�c �:�tt|fi|����dS)z�Prints the Octave (or Matlab) representation of the given expression. See `octave_code` for the meaning of the optional arguments. N)�printr�)r�r�s r��print_octave_coder��s(��  �+�d� '� '�h� '� '�(�(�(�(�(r�r�)r�� __future__r�typingr� sympy.corerrrr�sympy.core.mulr �sympy.core.numbersr �sympy.printing.codeprinterr �sympy.printing.precedencer r rmrr�r�rrr�r�r�r�r��<module>rs(�� � �#�"�"�"�"�"�������,�,�,�,�,�,�,�,�,�,�,�,�&�&�&�&�&�&�+�+�+�+�+�+�2�2�2�2�2�2�<�<�<�<�<�<�<�<�������:�:�:��� �5�� �7��� ���v� � �,� � �,� � �9�� �(������'����� �&��� ��� �� �(�� � �!�" �5�#�$ � �� �#� � �1����8w�w�w�w�w� �w�w�w�tH@�H@�H@�H@�V)�)�)�)�)r�
Memory