Ë
    7^(h7  ã                  ó  — d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ i dd	“d
d
“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“ddddddd d!d"d#d$d%œ¥Zg d&¢Z G d'„ d(e«      Zd,d*„Zd+„ Zy))-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)r   r   r   Ú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            	      ó  ‡ — e Zd ZU dZdZdZ eej                  fi di d e	«       dœ¤ŽZde
d<   d	d
ddœZi Zde
d<   i f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„ Zd„ Zd„ Zd„ Zd„ Zd „ Zd!„ Zˆ fd"„Z d#„ Z!d$„ Z"d%„ Z#d&„ Z$ˆ xZ%S )'ÚRCodePrinterz;A printer to convert SymPy expressions to strings of R codeÚ_rcodeÚRé   T)Ú	precisionÚuser_functionsÚcontractÚdereferencezdict[str, Any]Ú_default_settingsú&ú|ú!)ÚandÚorÚnotzdict[str, str]Ú_relationalsc                ó  — t        j                  | |«       t        t        «      | _        |j	                  di «      }| j                  j                  |«       t        |j	                  dg «      «      | _        t        t        «      | _        y )NrC   rE   )	r   Ú__init__ÚdictÚknown_functionsÚgetÚupdateÚsetÚ_dereferenceÚreserved_words)ÚselfÚsettingsÚ	userfuncss      úR/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/printing/rcode.pyrO   zRCodePrinter.__init__a   si   € Ü×Ñ˜T 8Ô,Ü#¤OÓ4ˆÔØ—L‘LÐ!1°2Ó6ˆ	Ø×Ñ×#Ñ# IÔ.Ü §¡¨]¸BÓ ?Ó@ˆÔÜ!¤.Ó1ˆÕó    c                ó   — |dz  S )Né   © )rW   Úps     rZ   Ú_rate_index_positionz!RCodePrinter._rate_index_positioni   s   € Ø�‰sˆ
r[   c                ó   — d|z  S )Nz%s;r^   )rW   Ú
codestrings     rZ   Ú_get_statementzRCodePrinter._get_statementl   s   € Ø�zÑ!Ð!r[   c                ó$   — dj                  |«      S )Nz// {}©Úformat)rW   Útexts     rZ   Ú_get_commentzRCodePrinter._get_commento   s   € Ø�~‰~˜dÓ#Ð#r[   c                ó&   — dj                  ||«      S )Nz{} = {};re   )rW   ÚnameÚvalues      rZ   Ú_declare_number_constz"RCodePrinter._declare_number_constr   s   € Ø× Ñ   uÓ-Ð-r[   c                ó$   — | j                  |«      S ©N)Úindent_code)rW   Úliness     rZ   Ú_format_codezRCodePrinter._format_codeu   s   € Ø×Ñ Ó&Ð&r[   c                óJ   ‡— |j                   \  }Šˆfd„t        |«      D «       S )Nc              3  óF   •K  — | ]  }t        ‰«      D ]  }||f–— Œ
 Œ y ­wrn   )Úrange)Ú.0ÚiÚjÚcolss      €rZ   ú	<genexpr>z8RCodePrinter._traverse_matrix_indices.<locals>.<genexpr>z   s%   øè ø€ ÒA˜1´U¸4³[ÒA°��A”ÐA�ÑAùs   ƒ!)Úshapert   )rW   ÚmatÚrowsrx   s      @rZ   Ú_traverse_matrix_indicesz%RCodePrinter._traverse_matrix_indicesx   s   ø€ Ø—Y‘Y‰
ˆˆdÛA¤ d£ÔAÐAr[   c           
     ó  — g }g }d}|D ]|  }|j                  || j                  |j                  «      | j                  |j                  dz   «      | j                  |j                  dz   «      dœz  «       |j                  d«       Œ~ ||fS )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Ú	loopstartrv   s         rZ   Ú_get_loop_opening_endingz%RCodePrinter._get_loop_opening_ending|   s‘   € ð ˆ
ØˆØ9ˆ	Øò 	$ˆAà×Ñ˜iØ—{‘{ 1§7¡7Ó+ØŸ™ Q§W¡W¨Q¡YÓ/Ø—{‘{ 1§7¡7¨Q¡;Ó/ñ+1ñ 1ô 2ð ×Ñ˜sÕ#ð	$ð ˜;Ð&Ð&r[   c                óž  — d| j                   v r| j                  |«      S t        |«      }t        |j                  d«      rd| j                  |j                  |«      z  S t        |j                  d«      rd| j                  |j                  «      z  S | j                  |j                  |«      ›d| j                  |j                  |«      ›�S )NÚPowéÿÿÿÿz1.0/%sg      à?zsqrt(%s)ú^)rQ   Ú_print_Functionr   r   r   ÚparenthesizeÚbaser…   )rW   ÚexprÚPRECs      rZ   Ú
_print_PowzRCodePrinter._print_Pow‹   s¯   € Ø�D×(Ñ(Ñ(Ø×'Ñ'¨Ó-Ð-Ü˜$ÓˆÜ˜Ÿ™ "Ô%Ø˜t×0Ñ0°·±¸DÓAÑBÐBÜ˜$Ÿ(™( CÔ(Ø §¡¨D¯I©IÓ 6Ñ6Ð6à"×/Ñ/°·	±	¸4Õ@Ø!%×!2Ñ!2°4·8±8¸TÔ!BðDð Dr[   c                ód   — t        |j                  «      t        |j                  «      }}d||fz  S )Nz	%d.0/%d.0)Úintr_   Úq)rW   r•   r_   rš   s       rZ   Ú_print_RationalzRCodePrinter._print_Rational˜   s*   € Ü�4—6‘6‹{œC §¡›Kˆ1ˆØ˜a ˜VÑ#Ð#r[   c                óÐ   — |j                   D �cg c]  }| j                  |«      ‘Œ }}| j                  |j                  j                  «      ›ddj	                  |«      ›d�S c c}w )Nú[z, ú])r‰   r…   r”   r†   Újoin)rW   r•   rv   Úindss       rZ   Ú_print_IndexedzRCodePrinter._print_Indexedœ   sJ   € Ø)-¯©Ö7 A�—‘˜Q•Ð7ˆÐ7ØŸ;™; t§y¡y§¡Õ7¸¿¹À4½ÐIÐIùò 8s   �A#c                 ó   — y)Nzexp(1)r^   ©rW   r•   s     rZ   Ú_print_Exp1zRCodePrinter._print_Exp1    s   € Ør[   c                 ó   — y)NÚpir^   r£   s     rZ   Ú	_print_PizRCodePrinter._print_Pi£   s   € Ør[   c                 ó   — y)Nr5   r^   r£   s     rZ   Ú_print_InfinityzRCodePrinter._print_Infinity¦   s   € Ør[   c                 ó   — y)Nz-Infr^   r£   s     rZ   Ú_print_NegativeInfinityz$RCodePrinter._print_NegativeInfinity©   s   € Ør[   c                ó&  — ddl m} ddlm} ddlm} |j                  }|j                  }t        ||«      rag }| j                  |«      D ]:  \  }}	 ||||	f   |||	f   «      }
| j                  |
«      }|j                  |«       Œ< dj                  |«      S | j                  d   r4|j                  |«      s|j                  |«      r| j                  ||«      S | j                  |«      }| j                  |«      }| j!                  |›d|›�«      S )Nr   )Ú
Assignment)ÚMatrixSymbol)ÚIndexedBaseú
rD   z = )Úsympy.codegen.astr­   Ú"sympy.matrices.expressions.matexprr®   Úsympy.tensor.indexedr¯   ÚlhsÚrhsÚ
isinstancer}   r…   r„   rŸ   Ú	_settingsÚhasÚ_doprint_loopsrc   )rW   r•   r­   r®   r¯   r´   rµ   rp   rv   rw   ÚtempÚcode0Úlhs_codeÚrhs_codes                 rZ   Ú_print_AssignmentzRCodePrinter._print_Assignment¬   sÿ   € Ý0åCÝ4Ø�h‰hˆØ�h‰hˆô �c˜<Ô(ð ˆEØ×7Ñ7¸Ó<ò $‘��AÙ! # a¨ d¡)¨S°°A°©YÓ7�ØŸ™ DÓ)�Ø—‘˜UÕ#ð$ð —9‘9˜UÓ#Ð#Ø�^‰^˜JÒ'¨S¯W©W°[Ô-AØ—‘˜Ô$ð ×&Ñ& s¨CÓ0Ð0à—{‘{ 3Ó'ˆHØ—{‘{ 3Ó'ˆHØ×&Ñ&²H¹hÐ'GÓHÐHr[   c                óÞ  — |j                   d   j                  dk(  r,d| j                  |j                   d   j                  «      z  }nUd| j                  |j                   d   j                  «      ›d| j                  |j                   d   j                  «      ›d�}|}t	        |j                   d d «      D ]2  \  }}d| j                  |«      ›d| j                  |«      ›d�|z   dz   }Œ4 |S )Nr�   Tz%szifelse(ú,z,NA)ú))ÚargsÚcondr…   r•   Úreversed)rW   r•   Ú	last_lineÚcodeÚeÚcs         rZ   Ú_print_PiecewisezRCodePrinter._print_PiecewiseÓ   sÑ   € ð �9‰9�R‰=×Ñ Ò%Ø˜tŸ{™{¨4¯9©9°R©=×+=Ñ+=Ó>Ñ>‰Ið &ð /3¯k©k¸$¿)¹)ÀB¹-×:LÑ:LÕ.MÈtÏ{É{Ð[_×[dÑ[dÐegÑ[h×[mÑ[mÕOnÐoˆIØˆÜ˜TŸY™Y s¨˜^Ó,ò 	N‰DˆA‰qØ%)§[¡[°¥^°T·[±[Àµ^ÐDÀTÑIÈ#ÑM‰Dð	Nàˆr[   c                óN   — ddl m} | j                  |j                  |«      «      S )Nr   )Ú	Piecewise)Úsympy.functionsrË   r…   Úrewrite)rW   r•   rË   s      rZ   Ú
_print_ITEzRCodePrinter._print_ITEß   s   € Ý-Ø�{‰{˜4Ÿ<™<¨	Ó2Ó3Ð3r[   c                óÎ   — dj                  | j                  |j                  t        d   d¬«      |j                  |j
                  |j                  j                  d   z  z   «      S )Nz{}[{}]ÚAtomT)Ústrictr   )rf   r“   Úparentr   rw   rv   rz   r£   s     rZ   Ú_print_MatrixElementz!RCodePrinter._print_MatrixElementã   s[   € Ø�‰˜t×0Ñ0°·±¼jÈÑ>PØð  1ó  ØŸ&™& 4§6¡6¨$¯+©+×*;Ñ*;¸AÑ*>Ñ#>Ñ>ó@ð 	@r[   c                ód   •— t         ‰| �  |«      }|| j                  v rdj                  |«      S |S )Nz(*{}))ÚsuperÚ_print_SymbolrU   rf   )rW   r•   rj   Ú	__class__s      €rZ   rÖ   zRCodePrinter._print_Symbolç   s4   ø€ Ü‰wÑ$ TÓ*ˆØ�4×$Ñ$Ñ$Ø—>‘> $Ó'Ð'àˆKr[   c                ó¬   — | j                  |j                  «      }| j                  |j                  «      }|j                  }dj	                  |||«      S )Nz{} {} {})r…   r´   rµ   Úrel_oprf   )rW   r•   r¼   r½   Úops        rZ   Ú_print_RelationalzRCodePrinter._print_Relationalî   sD   € Ø—;‘;˜tŸx™xÓ(ˆØ—;‘;˜tŸx™xÓ(ˆØ�[‰[ˆØ× Ñ  ¨2¨xÓ8Ð8r[   c                ó¬   — | j                  |j                  «      }|j                  }| j                  |j                  «      }dj	                  |||«      S )Nz	{} {} {};)r…   r´   rÚ   rµ   rf   )rW   r•   r¼   rÚ   r½   s        rZ   Ú_print_AugmentedAssignmentz'RCodePrinter._print_AugmentedAssignmentô   sD   € Ø—;‘;˜tŸx™xÓ(ˆØ�W‰WˆØ—;‘;˜tŸx™xÓ(ˆØ×!Ñ! (¨B°Ó9Ð9r[   c                ó   — | j                  |j                  «      }t        |j                  t        «      r|j                  j
                  \  }}}nt        d«      ‚| j                  |j                  «      }dj                  |||dz
  ||¬«      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   )rW   r•   rß   r�   rà   rá   râ   s          rZ   Ú
_print_ForzRCodePrinter._print_Forú   s€   € Ø—‘˜TŸ[™[Ó)ˆÜ�d—m‘m¤UÔ+Ø $§¡× 2Ñ 2ÑˆE�4™ä%Ð&RÓSÐSØ�{‰{˜4Ÿ9™9Ó%ˆØV×]Ñ]ÐekÐsxØ˜!‘V $¨Tð ^ó 3ð 	3r[   c                ól  — t        |t        «      r1| j                  |j                  d«      «      }dj	                  |«      S d}d}d}|D �cg c]  }|j                  d«      ‘Œ }}|D �cg c]*  }t        t        t        |j                  |«      «      «      ‘Œ, }}|D �cg c]*  }t        t        t        |j                  |«      «      «      ‘Œ, }}g }	d}
t        |«      D ]C  \  }}|dv r|	j                  |«       Œ|
||   z  }
|	j                  ||
z  ›|›�«       |
||   z  }
ŒE |	S c c}w c c}w c c}w )	z0Accepts a string of code or a list of code linesTÚ z   )ú{ú(z{
z(
)rƒ   rÁ   z 	r   )rç   r°   )r¶   Ústrro   Ú
splitlinesrŸ   Úlstripr™   ÚanyÚmapÚendswithÚ
startswithÚ	enumerater„   )rW   rÆ   Ú
code_linesÚtabÚ	inc_tokenÚ	dec_tokenÚlineÚincreaseÚdecreaseÚprettyÚlevelÚns               rZ   ro   zRCodePrinter.indent_code  s:  € ô �dœCÔ Ø×)Ñ)¨$¯/©/¸$Ó*?Ó@ˆJØ—7‘7˜:Ó&Ð&àˆØ,ˆ	Øˆ	à04Ö6¨�—‘˜UÕ#Ð6ˆÐ6àIMÖOÀ”SœœS §¡°	Ó:Ó;Õ<ÐOˆÐOà!%ö(Øô œœS §¡°)Ó<Ó=Õ>ð (ˆð (ð ˆØˆÜ  “ò 	!‰GˆAˆtØ�zÑ!Ø—‘˜dÔ#ØØ�X˜a‘[Ñ ˆEØ�M‰M C¨¢I©tÐ4Ô5Ø�X˜a‘[Ñ ‰Eð	!ð ˆùò 7ùâOùò(s   ÁD'Á*/D,Â/D1)&Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚprintmethodÚlanguagerP   r   rF   rT   Ú__annotations__Ú
_operatorsrM   rO   r`   rc   rh   rl   rq   r}   r�   r—   r›   r¡   r¤   r§   r©   r«   r¾   rÉ   rÎ   rÓ   rÖ   rÛ   rÝ   rå   ro   Ú__classcell__)r×   s   @rZ   r>   r>   N   sâ   ø… ÙEØ€KØ€Há(,¨[×-JÑ-Jñ )ØØØÙ“uñ	Oñ )Ð�~ó ð ØØñ€Jð $&€L�.Ó%à "ó 2òò"ò$ò.ò'òBò'ò
Dò$òJòòòòò%IòN
ò4ò@ôò9ò:ò3ör[   r>   Nc                ó8   — t        |«      j                  | |«      S )a  Converts 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_torX   s      rZ   Úrcoder  "  s   € ôV ˜Ó!×)Ñ)¨$°	Ó:Ð:r[   c                ó.   — t        t        | fi |¤Ž«       y)z0Prints R representation of the given expression.N)Úprintr  )r•   rX   s     rZ   Úprint_rcoder  �  s   € ä	Œ%�Ñ
!˜Ñ
!Õ"r[   rn   )rÿ   Ú
__future__r   Útypingr   Úsympy.core.numbersr   Úsympy.printing.codeprinterr   Úsympy.printing.precedencer   r   Úsympy.sets.fancysetsr	   rQ   rV   r>   r  r  r^   r[   rZ   ú<module>r     s:  ðñõ #Ý å +Ý 2ß <Ý &ðà	ˆ5ðð 
ˆ5ðð 
ˆ5ð	ð
 
ˆ5ðð ˆFðð ˆFðð ˆFðð ˆWðð 
ˆ5ðð 
ˆ5ðð 
ˆ5ðð ˆFðð ˆFðð ˆFðð  ˆWð!ð" ˆWð#ð$ ˆWð%ð& ØØØØØØØØØØò;€òF€ô,Q�;ô Qóhk;ó\#r[   