o
    oh]7                     @  s  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( d(eZd.d*d+Zd,d- Zd)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)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                      s,  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dZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Z  fd7d8Z!d9d: Z"d;d< Z#d=d> Z$d?d@ Z%  Z&S )A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                 C  sN   t | | tt| _|di }| j| t|dg | _tt| _d S )NrA   rC   )	r   __init__dictknown_functionsgetupdateset_dereferencereserved_words)selfsettings	userfuncs rW   h/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/printing/rcode.pyrL   a   s   
zRCodePrinter.__init__c                 C  s   |d S )N   rW   )rT   prW   rW   rX   _rate_index_positioni      z!RCodePrinter._rate_index_positionc                 C  s   d| S )Nz%s;rW   )rT   
codestringrW   rW   rX   _get_statementl   r\   zRCodePrinter._get_statementc                 C  s
   d |S )Nz// {}format)rT   textrW   rW   rX   _get_commento      
zRCodePrinter._get_commentc                 C  s   d ||S )Nz{} = {};r_   )rT   namevaluerW   rW   rX   _declare_number_constr      z"RCodePrinter._declare_number_constc                 C  s
   |  |S N)indent_code)rT   linesrW   rW   rX   _format_codeu   rc   zRCodePrinter._format_codec                   s    |j \}  fddt|D S )Nc                 3  s&    | ]}t  D ]}||fV  qqd S rh   )range).0ijcolsrW   rX   	<genexpr>z   s   $ z8RCodePrinter._traverse_matrix_indices.<locals>.<genexpr>)shaperl   )rT   matrowsrW   rp   rX   _traverse_matrix_indicesx   s   
z%RCodePrinter._traverse_matrix_indicesc              
   C  s^   g }g }d}|D ]"}| || |j| |jd | |jd d  | d q||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)rT   indices
open_linesclose_lines	loopstartrn   rW   rW   rX   _get_loop_opening_ending|   s   

z%RCodePrinter._get_loop_opening_endingc                 C  sv   d| j v r
| |S t|}t|jdrd| |j| S t|jdr+d| |j S d| |j|| |j|f S )NPowz1.0/%sg      ?zsqrt(%s)z%s^%s)rN   _print_Functionr   r   r   parenthesizebaser}   )rT   exprPRECrW   rW   rX   
_print_Pow   s   

zRCodePrinter._print_Powc                 C  s"   t |jt |j}}d||f S )Nz	%d.0/%d.0)intrZ   q)rT   r   rZ   r   rW   rW   rX   _print_Rational   s   zRCodePrinter._print_Rationalc                   s0    fdd|j D }d |jjd|f S )Nc                   s   g | ]}  |qS rW   )r}   )rm   rn   rT   rW   rX   
<listcomp>       z/RCodePrinter._print_Indexed.<locals>.<listcomp>z%s[%s]z, )r   r}   r   r~   join)rT   r   indsrW   r   rX   _print_Indexed   s   zRCodePrinter._print_Indexedc                 C  s   |  |jS rh   )r}   r~   rT   r   rW   rW   rX   
_print_Idx   rg   zRCodePrinter._print_Idxc                 C     dS )Nzexp(1)rW   r   rW   rW   rX   _print_Exp1      zRCodePrinter._print_Exp1c                 C  r   )NpirW   r   rW   rW   rX   	_print_Pi   r   zRCodePrinter._print_Pic                 C  r   )Nr4   rW   r   rW   rW   rX   _print_Infinity   r   zRCodePrinter._print_Infinityc                 C  r   )Nz-InfrW   r   rW   rW   rX   _print_NegativeInfinity   r   z$RCodePrinter._print_NegativeInfinityc                 C  s   ddl m} ddlm} ddlm} |j}|j}t||rEg }| 	|D ]\}}	||||	f |||	f }
| 
|
}|| q$d|S | jd rZ||sT||rZ| ||S | 
|}| 
|}| d||f S )Nr   )
Assignment)MatrixSymbol)IndexedBase
rB   z%s = %s)sympy.codegen.astr   "sympy.matrices.expressions.matexprr   sympy.tensor.indexedr   lhsrhs
isinstancerv   r}   r|   r   	_settingshas_doprint_loopsr^   )rT   r   r   r   r   r   r   rj   rn   ro   tempcode0lhs_coderhs_coderW   rW   rX   _print_Assignment   s&   




zRCodePrinter._print_Assignmentc                 C  s   |j d jdkrd| |j d j }nd| |j d j| |j d jf }|}t|j d d D ]\}}d| || |f | d }q3|S )Nr   Tz%szifelse(%s,%s,NA)zifelse(%s,%s,))argscondr}   r   reversed)rT   r   	last_linecodeecrW   rW   rX   _print_Piecewise   s   ("zRCodePrinter._print_Piecewisec                 C  s   ddl m} | ||S )Nr   )	Piecewise)sympy.functionsr   r}   rewrite)rT   r   r   rW   rW   rX   
_print_ITE   s   zRCodePrinter._print_ITEc                 C  s2   d | j|jtd dd|j|j|jjd   S )Nz{}[{}]AtomT)strictrw   )r`   r   parentr   ro   rn   rs   r   rW   rW   rX   _print_MatrixElement   s
   z!RCodePrinter._print_MatrixElementc                   s$   t  |}|| jv rd|S |S )Nz(*{}))super_print_SymbolrR   r`   )rT   r   rd   	__class__rW   rX   r      s   

zRCodePrinter._print_Symbolc                 C  s,   |  |j}|  |j}|j}d|||S )Nz{} {} {})r}   r   r   rel_opr`   )rT   r   r   r   oprW   rW   rX   _print_Relational   s   zRCodePrinter._print_Relationalc                 C  s,   |  |j}|j}|  |j}d|||S )Nz	{} {} {};)r}   r   r   r   r`   )rT   r   r   r   r   rW   rW   rX   _print_AugmentedAssignment   s   z'RCodePrinter._print_AugmentedAssignmentc                 C  sT   |  |j}t|jtr|jj\}}}ntd|  |j}dj|||d ||dS )Nz*Only iterable currently supported is RangezCfor({target} in seq(from={start}, to={stop}, by={step}){{
{body}
}}rw   )targetry   stopstepbody)	r}   r   r   iterabler   r   NotImplementedErrorr   r`   )rT   r   r   ry   r   r   r   rW   rW   rX   
_print_For   s   
zRCodePrinter._print_Forc           
        s   t |tr| |d}d|S d}dd dd |D }fdd|D } fd	d|D }g }d
}t|D ]%\}}	|	dv rG||	 q9||| 8 }|d|| |	f  ||| 7 }q9|S )z0Accepts a string of code or a list of code linesT z   ){(z{
z(
)r{   r   c                 S  s   g | ]}| d qS )z 	)lstriprm   linerW   rW   rX   r     r   z,RCodePrinter.indent_code.<locals>.<listcomp>c                       g | ]}t tt|j qS rW   )r   anymapendswithr   )	inc_tokenrW   rX   r     s     c                   r   rW   )r   r   r   
startswithr   )	dec_tokenrW   rX   r     s    r   )r   r   z%s%s)r   strri   
splitlinesr   	enumerater|   )
rT   r   
code_linestabincreasedecreaseprettylevelnr   rW   )r   r   rX   ri     s*   



zRCodePrinter.indent_code)'__name__
__module____qualname____doc__printmethodlanguagerM   r   rD   rQ   __annotations__
_operatorsrK   rL   r[   r^   rb   rf   rk   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ri   __classcell__rW   rW   r   rX   r<   N   sP   
 
'r<   Nc                 K  s   t || |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_torU   rW   rW   rX   rcode%  s   kr   c                 K  s   t t| fi | dS )z0Prints R representation of the given expression.N)printr   )r   rU   rW   rW   rX   print_rcode  s   r   rh   )r   
__future__r   typingr   sympy.core.numbersr   sympy.printing.codeprinterr   sympy.printing.precedencer   r   sympy.sets.fancysetsr   rN   rS   r<   r   r   rW   rW   rW   rX   <module>   sv    
	
# 
Xn