ó
    Š*£h7  ã                  ó  • S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
Jr  SSKJr  0 SS	_S
S
_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SSSSSSS S!S"S#S$S%.Er/ S&Qr " S' S(\5      rS,S* jrS+ rg))-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            	      ó$  ^ • \ rS rSr% SrSrSr\" \R                  40 S0 S\
" 5       S.D6r	S\S	'   S
SSS.r0 rS\S'   0 4S jrS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS  rS! rS" r U 4S# jr!S$ r"S% r#S& r$S' r%S(r&U =r'$ ))ÚRCodePrinteréN   z;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                ó  • [         R                  " X5        [        [        5      U l        UR	                  S0 5      nU R                  R                  U5        [        UR	                  S/ 5      5      U l        [        [        5      U l        g )NrD   rF   )	r   Ú__init__ÚdictÚknown_functionsÚgetÚupdateÚsetÚ_dereferenceÚreserved_words)ÚselfÚsettingsÚ	userfuncss      ÚQ/home/mande/repo/quber/.venv/lib/python3.13/site-packages/sympy/printing/rcode.pyrP   ÚRCodePrinter.__init__a   sg   € Ü×Ò˜TÔ,Ü#¤OÓ4ˆÔØ—L‘LÐ!1°2Ó6ˆ	Ø×Ñ×#Ñ# IÔ.Ü §¡¨]¸BÓ ?Ó@ˆÔÜ!¤.Ó1ˆÕó    c                ó   • US-  $ )Né   © )rX   Úps     r[   Ú_rate_index_positionÚ!RCodePrinter._rate_index_positioni   s   € Ø�‰sˆ
r]   c                ó   • SU-  $ )Nz%s;r`   )rX   Ú
codestrings     r[   Ú_get_statementÚRCodePrinter._get_statementl   s   € Ø�zÑ!Ð!r]   c                ó$   • SR                  U5      $ )Nz// {}©Úformat)rX   Útexts     r[   Ú_get_commentÚRCodePrinter._get_commento   s   € Ø�~‰~˜dÓ#Ð#r]   c                ó$   • SR                  X5      $ )Nz{} = {};ri   )rX   ÚnameÚvalues      r[   Ú_declare_number_constÚ"RCodePrinter._declare_number_constr   s   € Ø× Ñ  Ó-Ð-r]   c                ó$   • U R                  U5      $ ©N)Úindent_code)rX   Úliness     r[   Ú_format_codeÚRCodePrinter._format_codeu   s   € Ø×Ñ Ó&Ð&r]   c                óL   ^• UR                   u  nmU4S j[        U5       5       $ )Nc              3  óP   >#   • U  H  n[        T5        H  o!U4v •  M
     M     g 7frt   )Úrange)Ú.0ÚiÚjÚcolss      €r[   Ú	<genexpr>Ú8RCodePrinter._traverse_matrix_indices.<locals>.<genexpr>z   s   øé € ÐA¢˜1´U¸4·[°�A•±[‘¢ùs   ƒ#&)Úshaper{   )rX   ÚmatÚrowsr   s      @r[   Ú_traverse_matrix_indicesÚ%RCodePrinter._traverse_matrix_indicesx   s   ø€ Ø—Y‘Y‰
ˆˆdÜA¤ d¤ÓAÐAr]   c           
     ó  • / n/ nSnU H}  nUR                  UU R                  UR                  5      U R                  UR                  S-   5      U R                  UR                  S-   5      S.-  5        UR                  S5        M     X#4$ )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)rX   ÚindicesÚ
open_linesÚclose_linesÚ	loopstartr}   s         r[   Ú_get_loop_opening_endingÚ%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                óº  • SU R                   ;   a  U R                  U5      $ [        U5      n[        UR                  S5      (       a  SU R                  UR                  U5      -  $ [        UR                  S5      (       a  SU R                  UR                  5      -  $ U R                  UR                  U5      < SU R                  UR                  U5      < 3$ )NÚPowéÿÿÿÿz1.0/%sg      à?zsqrt(%s)Ú^)rR   Ú_print_Functionr   r   r   ÚparenthesizeÚbaserŽ   )rX   ÚexprÚPRECs      r[   Ú
_print_PowÚ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                ó`   • [        UR                  5      [        UR                  5      p2SX#4-  $ )Nz	%d.0/%d.0)Úintra   Úq)rX   rŸ   ra   r¥   s       r[   Ú_print_RationalÚRCodePrinter._print_Rational˜   s&   € Ü�4—6‘6‹{œC §¡›Kˆ1Ø˜a˜VÑ#Ð#r]   c                óØ   • UR                    Vs/ s H  o R                  U5      PM     nnU R                  UR                  R                  5      < SSR	                  U5      < S3$ s  snf )NÚ[z, Ú])r’   rŽ   rž   r�   Újoin)rX   rŸ   r}   Úindss       r[   Ú_print_IndexedÚRCodePrinter._print_Indexedœ   sI   € Ø)-¯ªÓ7ª A—‘˜Q–©ˆÐ7ØŸ;™; t§y¡y§¡Ö7¸¿¹À4¾ÐIÐIùò 8s   �A'c                ó   • g)Nzexp(1)r`   ©rX   rŸ   s     r[   Ú_print_Exp1ÚRCodePrinter._print_Exp1    s   € Ør]   c                ó   • g)NÚpir`   r°   s     r[   Ú	_print_PiÚRCodePrinter._print_Pi£   s   € Ør]   c                ó   • g)Nr5   r`   r°   s     r[   Ú_print_InfinityÚRCodePrinter._print_Infinity¦   s   € Ør]   c                ó   • g)Nz-Infr`   r°   s     r[   Ú_print_NegativeInfinityÚ$RCodePrinter._print_NegativeInfinity©   s   € Ør]   c                óL  • SSK Jn  SSKJn  SSKJn  UR                  nUR                  n[        XS5      (       a`  / nU R                  U5       H8  u  p‰U" XXU	4   XhU	4   5      n
U R                  U
5      nUR                  U5        M:     SR                  U5      $ U R                  S   (       a=  UR                  U5      (       d  UR                  U5      (       a  U R                  Xe5      $ U R                  U5      nU R                  U5      nU R!                  U< SU< 35      $ )Nr   )Ú
Assignment)ÚMatrixSymbol)ÚIndexedBaseÚ
rE   z = )Úsympy.codegen.astr¾   Ú"sympy.matrices.expressions.matexprr¿   Úsympy.tensor.indexedrÀ   ÚlhsÚrhsÚ
isinstancer…   rŽ   r�   r«   Ú	_settingsÚhasÚ_doprint_loopsrf   )rX   rŸ   r¾   r¿   rÀ   rÅ   rÆ   rv   r}   r~   ÚtempÚcode0Úlhs_codeÚrhs_codes                 r[   Ú_print_AssignmentÚRCodePrinter._print_Assignment¬   sù   € Ý0åCÝ4Ø�h‰hˆØ�h‰hˆô �c×(Ñ(ð ˆEØ×7Ñ7¸Ö<‘�Ù! #¨ d¡)¨S°A°©YÓ7�ØŸ™ DÓ)�Ø—‘˜UÖ#ñ =ð —9‘9˜UÓ#Ð#Ø�^‰^˜J×'¨S¯W©W°[×-AÑ-AØ—‘˜×$Ñ$ð ×&Ñ& sÓ0Ð0à—{‘{ 3Ó'ˆHØ—{‘{ 3Ó'ˆHØ×&Ñ&³HºhÐ'GÓHÐHr]   c                óê  • UR                   S   R                  S:X  a,  SU R                  UR                   S   R                  5      -  nOWSU R                  UR                   S   R                  5      < SU R                  UR                   S   R                  5      < S3nUn[	        UR                   S S 5       H4  u  pESU R                  U5      < SU R                  U5      < S3U-   S-   nM6     U$ )Nrš   Tz%szifelse(Ú,z,NA)Ú))ÚargsÚcondrŽ   rŸ   Úreversed)rX   rŸ   Ú	last_lineÚcodeÚeÚcs         r[   Ú_print_PiecewiseÚ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¨˜^Ö,‰DˆAˆqØ%)§[¡[°¦^°T·[±[À¶^ÐDÀTÑIÈ#ÑMŠDñ -àˆr]   c                óN   • SSK Jn  U R                  UR                  U5      5      $ )Nr   )Ú	Piecewise)Úsympy.functionsrÞ   rŽ   Úrewrite)rX   rŸ   rÞ   s      r[   Ú
_print_ITEÚRCodePrinter._print_ITEß   s   € Ý-Ø�{‰{˜4Ÿ<™<¨	Ó2Ó3Ð3r]   c                óÈ   • SR                  U R                  UR                  [        S   SS9UR                  UR
                  UR                  R                  S   -  -   5      $ )Nz{}[{}]ÚAtomT)Ústrictrˆ   )rj   r�   Úparentr   r~   r}   r‚   r°   s     r[   Ú_print_MatrixElementÚ!RCodePrinter._print_MatrixElementã   s[   € Ø�‰˜t×0Ñ0°·±¼jÈÑ>PØð  1ð  ØŸ&™& 4§6¡6¨$¯+©+×*;Ñ*;¸AÑ*>Ñ#>Ñ>ó@ð 	@r]   c                óf   >• [         TU ]  U5      nXR                  ;   a  SR                  U5      $ U$ )Nz(*{}))ÚsuperÚ_print_SymbolrV   rj   )rX   rŸ   ro   Ú	__class__s      €r[   rë   ÚRCodePrinter._print_Symbolç   s2   ø€ Ü‰wÑ$ TÓ*ˆØ×$Ñ$Ó$Ø—>‘> $Ó'Ð'àˆKr]   c                óª   • U R                  UR                  5      nU R                  UR                  5      nUR                  nSR	                  X$U5      $ )Nz{} {} {})rŽ   rÅ   rÆ   Úrel_oprj   )rX   rŸ   rÍ   rÎ   Úops        r[   Ú_print_RelationalÚRCodePrinter._print_Relationalî   sB   € Ø—;‘;˜tŸx™xÓ(ˆØ—;‘;˜tŸx™xÓ(ˆØ�[‰[ˆØ× Ñ  ¨xÓ8Ð8r]   c                óª   • U R                  UR                  5      nUR                  nU R                  UR                  5      nSR	                  X#U5      $ )Nz	{} {} {};)rŽ   rÅ   rð   rÆ   rj   )rX   rŸ   rÍ   rð   rÎ   s        r[   Ú_print_AugmentedAssignmentÚ'RCodePrinter._print_AugmentedAssignmentô   sB   € Ø—;‘;˜tŸx™xÓ(ˆØ�W‰WˆØ—;‘;˜tŸx™xÓ(ˆØ×!Ñ! (°Ó9Ð9r]   c                ó  • U R                  UR                  5      n[        UR                  [        5      (       a  UR                  R
                  u  p4nO[        S5      eU R                  UR                  5      nSR                  X#US-
  XVS9$ )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ú   rj   )rX   rŸ   r÷   rŠ   rø   rù   rú   s          r[   Ú
_print_ForÚRCodePrinter._print_Forú   s|   € Ø—‘˜TŸ[™[Ó)ˆÜ�d—m‘m¤U×+Ñ+Ø $§¡× 2Ñ 2ÑˆE™ä%Ð&RÓSÐSØ�{‰{˜4Ÿ9™9Ó%ˆØV×]Ñ]ÐekØ˜!‘V $ð ^ð 3ð 	3r]   c                óŒ  • [        U[        5      (       a1  U R                  UR                  S5      5      nSR	                  U5      $ SnSnSnU Vs/ s H  ofR                  S5      PM     nnU Vs/ s H+  n[        [        [        UR                  U5      5      5      PM-     nnU Vs/ s H+  n[        [        [        UR                  U5      5      5      PM-     nn/ n	Sn
[        U5       HE  u  p¶US;   a  U	R                  U5        M  X¨U   -  n
U	R                  X:-  < U< 35        X§U   -  n
MG     U	$ s  snf s  snf s  snf )	z0Accepts a string of code or a list of code linesTÚ z   )Ú{Ú(z{
z(
)rŒ   rÓ   z 	r   )r   rÁ   )rÇ   Ústrru   Ú
splitlinesr«   Úlstripr¤   ÚanyÚmapÚendswithÚ
startswithÚ	enumerater�   )rX   rØ   Ú
code_linesÚtabÚ	inc_tokenÚ	dec_tokenÚlineÚincreaseÚdecreaseÚprettyÚlevelÚns               r[   ru   ÚRCodePrinter.indent_code  s3  € ô �dœC× Ñ Ø×)Ñ)¨$¯/©/¸$Ó*?Ó@ˆJØ—7‘7˜:Ó&Ð&àˆØ,ˆ	Øˆ	á04Ó6²¨—‘˜UÖ#±ˆÐ6áIMÓOÊÀ”SœœS §¡°	Ó:Ó;Ö<ÉˆÐOá!%ó(Ú!%˜ô œœS §¡°)Ó<Ó=Ö>Ù!%ð 	ð (ð ˆØˆÜ  –‰GˆAØ�zÓ!Ø—‘˜dÔ#ÙØ˜a‘[Ñ ˆEØ�M‰M C£IªtÐ4Ô5Ø˜a‘[Ñ ŠEñ 'ð ˆùò 7ùâOùò(s   ÁD7Á12D<Â)2E)rV   rR   rW   )(Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚprintmethodÚlanguagerQ   r   rG   rU   Ú__annotations__Ú
_operatorsrN   rP   rb   rf   rl   rq   rw   r…   r–   r¡   r¦   r­   r±   rµ   r¸   r»   rÏ   rÛ   rá   rç   rë   rñ   rô   rý   ru   Ú__static_attributes__Ú__classcell__)rì   s   @r[   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                ó6   • [        U5      R                  X5      $ )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_torY   s      r[   Úrcoder$  "  s   € ôV ˜Ó!×)Ñ)¨$Ó:Ð:r]   c                ó.   • [        [        U 40 UD65        g)z0Prints R representation of the given expression.N)Úprintr$  )rŸ   rY   s     r[   Úprint_rcoder'  �  s   € ä	Œ%�Ñ
!˜Ñ
!Õ"r]   rt   )r  Ú
__future__r   Útypingr   Úsympy.core.numbersr   Úsympy.printing.codeprinterr   Úsympy.printing.precedencer   r   Úsympy.sets.fancysetsr	   rR   rW   r>   r$  r'  r`   r]   r[   Ú<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]   