� ��g]7� ��dZddlmZddlmZddlmZddlmZddl m Z m Z ddl m Z idd �d d �d d �d d �d d �dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�ddddddd d!d"d#d$d%� �Zgd&�ZGd'�d(e��Zd,d*�Zd+�Zd)S)-z� R code printer The RCodePrinter converts single SymPy expressions into single R expressions, using the functions defined in math.h where possible. �)� annotations)�Any)� equal_valued)� CodePrinter)� precedence� PRECEDENCE)�Range�Abs�abs�sin�cos�tan�asin�acos�atan�atan2�exp�log�erf�sinh�cosh�tanh�asinh�acosh�atanh�floor�ceiling�sign�max�min� factorial�gamma�digamma�trigamma�beta�sqrt) rrr�Max�Minr!r"r#r$r%r&)�if�else�repeat�while�function�for�in�next�break�TRUE�FALSE�NULL�Inf�NaN�NA� NA_integer_�NA_real_� NA_complex_� NA_character_�volatilec ���eZdZUdZdZdZeejfidide ��d���Zde d<d d d d �Z iZ d e d<ifd�Z d�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd �Zd!�Zd"�Z �fd#�Z!d$�Z"d%�Z#d&�Z$d'�Z%�xZ&S)(� RCodePrinterz;A printer to convert SymPy expressions to strings of R code�_rcode�R�T)� precision�user_functions�contract� dereferencezdict[str, Any]�_default_settings�&�|�!)�and�or�notzdict[str, str]� _relationalsc�D�tj||��tt��|_|�di��}|j�|��t |�dg����|_t t��|_dS)NrCrE) r�__init__�dict�known_functions�get�update�set� _dereference�reserved_words)�self�settings� userfuncss �d/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/printing/rcode.pyrOzRCodePrinter.__init__as�����T�8�,�,�,�#�O�4�4����L�L�!1�2�6�6� � ��#�#�I�.�.�.��� � �]�B� ?� ?�@�@���!�.�1�1�����c� �|dzS)N��)rW�ps rZ�_rate_index_positionz!RCodePrinter._rate_index_positionis ����s� r[c� �d|zS)Nz%s;r^)rW� codestrings rZ�_get_statementzRCodePrinter._get_statementls ���z�!�!r[c�,�d�|��S)Nz// {}��format)rW�texts rZ� _get_commentzRCodePrinter._get_commentos���~�~�d�#�#�#r[c�.�d�||��S)Nz{} = {};re)rW�name�values rZ�_declare_number_constz"RCodePrinter._declare_number_constrs��� � ��u�-�-�-r[c�,�|�|��S�N)� indent_code)rW�liness rZ� _format_codezRCodePrinter._format_codeus������&�&�&r[c�N��|j\}��fd�t|��D��S)Nc3�D�K�|]}t���D]}||fV�� �dSrn)�range)�.0�i�j�colss �rZ� <genexpr>z8RCodePrinter._traverse_matrix_indices.<locals>.<genexpr>zs:�����A�A�1�U�4�[�[�A�A���A��A�A�A�A�A�A�Ar[)�shapert)rW�mat�rowsrxs @rZ�_traverse_matrix_indicesz%RCodePrinter._traverse_matrix_indicesxs.����Y� ��d�A�A�A�A��d� � �A�A�A�Ar[c ��g}g}d}|D]�}|�||�|j��|�|jdz��|�|jdz��d�z��|�d����||fS)zPReturns a tuple (open_lines, close_lines) containing lists of codelines z#for (%(var)s in %(start)s:%(end)s){�)�var�start�end�})�append�_print�label�lower�upper)rW�indices� open_lines� close_lines� loopstartrvs rZ�_get_loop_opening_endingz%RCodePrinter._get_loop_opening_ending|s���� �� �9� �� $� $�A� � � �i��{�{�1�7�+�+����Q�W�Q�Y�/�/��{�{�1�7�Q�;�/�/�+1�+1�1� 2� 2� 2� � � �s� #� #� #� #��;�&�&r[c��d|jvr|�|��St|��}t|jd��rd|�|j|��zSt|jd��rd|�|j��zS|�|j|���d|�|j|����S)N�Pow�����z1.0/%sg�?zsqrt(%s)�^)rQ�_print_Functionrrr� parenthesize�baser�)rW�expr�PRECs rZ� _print_PowzRCodePrinter._print_Pow�s��� �D�(� (� (��'�'��-�-� -��$���� ���"� %� %� D��t�0�0���D�A�A�B� B� �$�(�C� (� (� D��� � �D�I� 6� 6�6� 6�"�/�/�� �4�@�@�@�@�!%�!2�!2�4�8�T�!B�!B�!B�D� Dr[c�`�t|j��t|j��}}d||fzS)Nz %d.0/%d.0)�intr_�q)rWr�r_r�s rZ�_print_RationalzRCodePrinter._print_Rational�s*���4�6�{�{�C���K�K�1���a��V�#�#r[c����fd�|jD��}��|jj���dd�|���d�S)Nc�:��g|]}��|����Sr^)r�)rurvrWs �rZ� <listcomp>z/RCodePrinter._print_Indexed.<locals>.<listcomp>�s#���7�7�7�A����Q���7�7�7r[�[z, �])r�r�r�r��join)rWr��indss` rZ�_print_IndexedzRCodePrinter._print_Indexed�sM���7�7�7�7���7�7�7���;�;�t�y��7�7�7�7����4�����I�Ir[c�6�|�|j��Srn)r�r��rWr�s rZ� _print_IdxzRCodePrinter._print_Idx�s���{�{�4�:�&�&�&r[c��dS)Nzexp(1)r^r�s rZ� _print_Exp1zRCodePrinter._print_Exp1�s���xr[c��dS)N�pir^r�s rZ� _print_PizRCodePrinter._print_Pi�s���tr[c��dS)Nr5r^r�s rZ�_print_InfinityzRCodePrinter._print_Infinity�s���ur[c��dS)Nz-Infr^r�s rZ�_print_NegativeInfinityz$RCodePrinter._print_NegativeInfinity�s���vr[c�t�ddlm}ddlm}ddlm}|j}|j}t||��rxg}|� |��D]K\}} |||| f||| f��} |� | ��} |� | ���Ld� |��S|j dr@|�|��s|�|��r|�||��S|� |��} |� |��} |�| �d| ����S)Nr)� Assignment)� MatrixSymbol)� IndexedBase� rDz = )�sympy.codegen.astr��"sympy.matrices.expressions.matexprr��sympy.tensor.indexedr��lhs�rhs� isinstancer}r�r�r�� _settings�has�_doprint_loopsrc)rWr�r�r�r�r�r�rprvrw�temp�code0�lhs_code�rhs_codes rZ�_print_AssignmentzRCodePrinter._print_Assignment�sn��0�0�0�0�0�0�C�C�C�C�C�C�4�4�4�4�4�4��h���h�� �c�<� (� (� I��E��7�7��<�<� $� $���A�!�z�#�a��d�)�S��A��Y�7�7��� � �D�)�)��� � �U�#�#�#�#��9�9�U�#�#� #� �^�J� '� I�S�W�W�[�-A�-A� I���� �$�$� I��&�&�s�C�0�0� 0��{�{�3�'�'�H��{�{�3�'�'�H��&�&�H�H�H�h�h�'G�H�H� Hr[c���|jdjdkr)d|�|jdj��z}nOd|�|jdj���d|�|jdj���d�}|}t |jdd���D]:\}}d|�|���d|�|���d�|zdz}�;|S)Nr�Tz%szifelse(�,z,NA)�))�args�condr�r��reversed)rWr�� last_line�code�e�cs rZ�_print_PiecewisezRCodePrinter._print_Piecewise�s��� �9�R�=� �� %� %��t�{�{�4�9�R�=�+=�>�>�>�I�I� &�/3�k�k�$�)�B�-�:L�.M�.M�.M�.M�t�{�{�[_�[d�eg�[h�[m�On�On�On�On�o�I� ���T�Y�s��s�^�,�,� N� N�D�A�q�q�%)�[�[��^�^�^�^�T�[�[��^�^�^�^�D�T�I�#�M�D�D�� r[c�^�ddlm}|�|�|����S)Nr)� Piecewise)�sympy.functionsr�r��rewrite)rWr�r�s rZ� _print_ITEzRCodePrinter._print_ITE�s2��-�-�-�-�-�-��{�{�4�<�<� �2�2�3�3�3r[c��d�|�|jtdd���|j|j|jjdzz��S)Nz{}[{}]�AtomT)�strictr)rfr��parentrrwrvrzr�s rZ�_print_MatrixElementz!RCodePrinter._print_MatrixElement�s[�����t�0�0���j��>P�� 1� � ��&�4�6�$�+�*;�A�*>�#>�>�@�@� @r[c���t���|��}||jvrd�|��S|S)Nz(*{}))�super� _print_SymbolrUrf)rWr�rj� __class__s �rZr�zRCodePrinter._print_Symbol�s?����w�w�$�$�T�*�*�� �4�$� $� $��>�>�$�'�'� '��Kr[c��|�|j��}|�|j��}|j}d�|||��S)Nz{} {} {})r�r�r��rel_oprf)rWr�r�r��ops rZ�_print_RelationalzRCodePrinter._print_Relational�sG���;�;�t�x�(�(���;�;�t�x�(�(�� �[��� � ��2�x�8�8�8r[c��|�|j��}|j}|�|j��}d�|||��S)Nz {} {} {};)r�r�r�r�rf)rWr�r�r�r�s rZ�_print_AugmentedAssignmentz'RCodePrinter._print_AugmentedAssignment�sG���;�;�t�x�(�(�� �W���;�;�t�x�(�(���!�!�(�B��9�9�9r[c��|�|j��}t|jt��r|jj\}}}nt d���|�|j��}d�|||dz ||���S)Nz*Only iterable currently supported is RangezCfor({target} in seq(from={start}, to={stop}, by={step}){{ {body} }}r)�targetr��stop�step�body) r�r�r��iterabler r��NotImplementedErrorr�rf)rWr�r�r�r�r�r�s rZ� _print_ForzRCodePrinter._print_For�s������T�[�)�)�� �d�m�U� +� +� T� $� � 2� �E�4���%�&R�S�S� S��{�{�4�9�%�%��V�]�]�ek�sx��!�V�$�T�^�3�3� 3r[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 linesT�z )�{�(z{ z( )r�r�c�8�g|]}|�d����S)z )�lstrip)ru�lines rZr�z,RCodePrinter.indent_code.<locals>.<listcomp>s$��6�6�6�����U�#�#�6�6�6r[c �n��g|]1}ttt|j���������2Sr^)r��any�map�endswith)rur�� inc_tokens �rZr�z,RCodePrinter.indent_code.<locals>.<listcomp>s5���O�O�O��S��S��� �:�:�;�;�<�<�O�O�Or[c �n��g|]1}ttt|j���������2Sr^)r�r�r�� startswith)rur�� dec_tokens �rZr�z,RCodePrinter.indent_code.<locals>.<listcomp>sE���(�(�(����S���)�<�<�=�=�>�>�(�(�(r[r)r�r�)r��strro� splitlinesr�� enumerater�) rWr�� code_lines�tab�increase�decrease�pretty�level�nr�r�r�s @@rZrozRCodePrinter.indent_codes3���� �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[)'�__name__� __module__� __qualname__�__doc__� printmethod�languagerPrrFrT�__annotations__� _operatorsrMrOr`rcrhrlrqr}r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�ro� __classcell__)r�s@rZr>r>NsG��������E�E��K��H�(,��[�-J�)�)�����s�u�u� O�O�)�)�����������J� $&�L�%�%�%�%� "�2�2�2�2����"�"�"�$�$�$�.�.�.�'�'�'�B�B�B� '� '� '� D� D� D�$�$�$�J�J�J�'�'�'�������������%I�%I�%I�N � � �4�4�4�@�@�@������9�9�9� :�:�:� 3�3�3�������r[r>Nc �H�t|���||��S)aConverts an expr to a string of r 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 R string representations. Alternatively, the dictionary value can be a list of tuples i.e. [(argument_test, rfunction_string)] or [(argument_test, rfunction_formater)]. 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 rcode, symbols, Rational, sin, ceiling, Abs, Function >>> x, tau = symbols("x, tau") >>> rcode((2*tau)**Rational(7, 2)) '8*sqrt(2)*tau^(7.0/2.0)' >>> rcode(sin(x), assign_to="s") 's = sin(x);' 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') >>> rcode(func(Abs(x) + ceiling(x)), user_functions=custom_functions) 'f(fabs(x) + CEIL(x))' or if the R-function takes a subset of the original arguments: >>> rcode(2**x + 3**x, 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(rcode(expr, assign_to=tau)) tau = ifelse(x > 0,x + 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])) >>> rcode(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(rcode(mat, A)) A[0] = x^2; A[1] = ifelse(x > 0,x + 1,x); A[2] = sin(x); )r>�doprint)r�� assign_torXs rZ�rcoder%s#��V �� !� !� )� )�$� � :� :�:r[c �:�tt|fi|����dS)z0Prints R representation of the given expression.N)�printr)r�rXs rZ� print_rcoder�s&�� �%�� !� !�� !� !�"�"�"�"�"r[rn)r� __future__r�typingr�sympy.core.numbersr�sympy.printing.codeprinterr�sympy.printing.precedencerr�sympy.sets.fancysetsr rQrVr>rrr^r[rZ�<module>rs�����#�"�"�"�"�"�������+�+�+�+�+�+�2�2�2�2�2�2�<�<�<�<�<�<�<�<�&�&�&�&�&�&�� �5�� �5�� �5� �  �5� �  �F� � �F�� �F�� �W�� �5�� �5�� �5�� �F�� �F�� �F��  �W�!�" �W�#�$ �W�%�&�� � � �� ��� � �;����F����,T�T�T�T�T�;�T�T�T�nk;�k;�k;�k;�\#�#�#�#�#r[
Memory