B
    [[9                 @   s   d Z ddlmZmZ ddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZmZ ddlmZ 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d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8gZG d9d: d:eZd@d<d=Zd>d? Zd;S )Az
R code printer

The RCodePrinter converts single sympy expressions into single R expressions,
using the functions defined in math.h where possible.



    )print_functiondivision)S)string_typesrange)
Assignment)CodePrinter)
precedence
PRECEDENCE)Range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)ZAbsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ZMaxZMinr"   r#   r$   r%   r&   ifelserepeatwhileZfunctionforinnextbreakZTRUEZFALSEZNULLInfZNaNZNAZNA_integer_ZNA_real_ZNA_complex_ZNA_character_Zvolatilec            
       s  e Zd ZdZdZdZdddi dde dd	d
	ZddddZi Z	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"dAdB Z#  Z$S )CRCodePrinterz<A printer to convert python expressions to strings of R codeZ_rcodeRNauto   TF_)	orderZ	full_precZ	precisionuser_functionsZhumancontractdereferenceZerror_on_reservedZreserved_word_suffix&|!)andornotc             C   sN   t | | tt| _|di }| j| t|dg | _tt| _d S )Nr6   r8   )	r   __init__dictknown_functionsgetupdateset_dereferencereserved_words)selfsettingsZ	userfuncs rI   3lib/python3.7/site-packages/sympy/printing/rcode.pyr?   g   s    
zRCodePrinter.__init__c             C   s   |d S )N   rI   )rG   prI   rI   rJ   _rate_index_positiono   s    z!RCodePrinter._rate_index_positionc             C   s   d| S )Nz%s;rI   )rG   Z
codestringrI   rI   rJ   _get_statementr   s    zRCodePrinter._get_statementc             C   s
   d |S )Nz// {0})format)rG   textrI   rI   rJ   _get_commentu   s    zRCodePrinter._get_commentc             C   s   d ||S )Nz
{0} = {1};)rO   )rG   namevaluerI   rI   rJ   _declare_number_constx   s    z"RCodePrinter._declare_number_constc             C   s
   |  |S )N)indent_code)rG   linesrI   rI   rJ   _format_code{   s    zRCodePrinter._format_codec                s    |j \}  fddt|D S )Nc             3   s$   | ]}t  D ]}||fV  qqd S )N)r   ).0ij)colsrI   rJ   	<genexpr>   s    z8RCodePrinter._traverse_matrix_indices.<locals>.<genexpr>)shaper   )rG   ZmatZrowsrI   )r[   rJ   _traverse_matrix_indices~   s    
z%RCodePrinter._traverse_matrix_indicesc          
   C   sb   g }g }d}xL|D ]D}| || |j| |jd | |jd d  | d qW ||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)rG   indicesZ
open_linesZclose_linesZ	loopstartrY   rI   rI   rJ   _get_loop_opening_ending   s    

z%RCodePrinter._get_loop_opening_endingc             C   sv   d| j kr| |S t|}|jdkr8d| |j| S |jdkrRd| |j S d| |j|| |j|f S d S )NZPowz1.0/%sg      ?zsqrt(%s)z%s^%s)rA   Z_print_Functionr	   r   parenthesizebasere   )rG   exprZPRECrI   rI   rJ   
_print_Pow   s    



zRCodePrinter._print_Powc             C   s"   t |jt |j }}d||f S )Nz	%d.0/%d.0)intrL   q)rG   rn   rL   rq   rI   rI   rJ   _print_Rational   s    zRCodePrinter._print_Rationalc                s0    fdd|j D }d |jjd|f S )Nc                s   g | ]}  |qS rI   )re   )rX   rY   )rG   rI   rJ   
<listcomp>   s    z/RCodePrinter._print_Indexed.<locals>.<listcomp>z%s[%s]z, )ri   re   rm   rf   join)rG   rn   ZindsrI   )rG   rJ   _print_Indexed   s    zRCodePrinter._print_Indexedc             C   s   |  |jS )N)re   rf   )rG   rn   rI   rI   rJ   
_print_Idx   s    zRCodePrinter._print_Idxc             C   s   dS )Nzexp(1)rI   )rG   rn   rI   rI   rJ   _print_Exp1   s    zRCodePrinter._print_Exp1c             C   s   dS )NZpirI   )rG   rn   rI   rI   rJ   	_print_Pi   s    zRCodePrinter._print_Pic             C   s   dS )Nr/   rI   )rG   rn   rI   rI   rJ   _print_Infinity   s    zRCodePrinter._print_Infinityc             C   s   dS )Nz-InfrI   )rG   rn   rI   rI   rJ   _print_NegativeInfinity   s    z$RCodePrinter._print_NegativeInfinityc             C   s   ddl m} ddlm} ddlm} |j}|j}t||rg }xD| 	|D ]6\}}	t
|||	f |||	f }
| |
}|| qJW d|S | jd r||s||r| ||S | |}| |}| d||f S d S )Nr   )	Piecewise)MatrixSymbol)IndexedBase
r7   z%s = %s)Z$sympy.functions.elementary.piecewiser{   Z"sympy.matrices.expressions.matexprr|   Zsympy.tensor.indexedr}   lhsrhs
isinstancer^   r   re   rd   rt   Z	_settingsZhasZ_doprint_loopsrN   )rG   rn   r{   r|   r}   r   r   rV   rY   rZ   ZtempZcode0lhs_coderhs_coderI   rI   rJ   _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 }|}x>t|j d d D ](\}}d| || |f | d }qhW |S )Nrk   Tz%szifelse(%s,%s,NA)zifelse(%s,%s,))argsZcondre   rn   reversed)rG   rn   	last_linecodeecrI   rI   rJ   _print_Piecewise   s    ($zRCodePrinter._print_Piecewisec             C   s:   ddl m} ||jd |jd f|jd df}| |S )Nr   )r{   r_      T)sympy.functionsr{   r   re   )rG   rn   r{   
_piecewiserI   rI   rJ   
_print_ITE   s    $zRCodePrinter._print_ITEc             C   s2   d | j|jtd dd|j|j|jjd   S )Nz{0}[{1}]ZAtomT)strictr_   )rO   rl   parentr
   rZ   rY   r]   )rG   rn   rI   rI   rJ   _print_MatrixElement   s    z!RCodePrinter._print_MatrixElementc                s,   t t| |}|| jkr$d|S |S d S )Nz(*{0}))superr0   _print_SymbolrE   rO   )rG   rn   rR   )	__class__rI   rJ   r      s    

zRCodePrinter._print_Symbolc             C   s,   |  |j}|  |j}|j}d|||S )Nz{0} {1} {2})re   r   r   Zrel_oprO   )rG   rn   r   r   oprI   rI   rJ   _print_Relational   s    zRCodePrinter._print_Relationalc             C   s\   ddl m} ddlm} ddlm} |||jd |jd  ||jd dfd}| |S )Nr   )r   )Ne)r{   )r_   T)Z(sympy.functions.elementary.trigonometricr   Zsympy.core.relationalr   r   r{   r   re   )rG   rn   r   r   r{   r   rI   rI   rJ   _print_sinc   s    ,zRCodePrinter._print_sincc             C   s,   |  |j}|j}|  |j}d|||S )Nz{0} {1} {2};)re   r   r   r   rO   )rG   rn   r   r   r   rI   rI   rJ   _print_AugmentedAssignment  s    z'RCodePrinter._print_AugmentedAssignmentc             C   sP   |  |j}t|jtr(|jj\}}}ntd|  |j}dj|||||dS )Nz*Only iterable currently supported is RangezLfor ({target} = {start}; {target} < {stop}; {target} += {step}) {{
{body}
}})targetra   stopstepbody)	re   r   r   iterabler   r   NotImplementedErrorr   rO   )rG   rn   r   ra   r   r   r   rI   rI   rJ   
_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
}x^t|D ]R\}}	|	dks|	dkr||	 qt||| 8 }|d|| |	f  ||| 7 }qtW |S )z0Accepts a string of code or a list of code linesT z   ){(z{
z(
)rc   r   c             S   s   g | ]}| d qS )z 	)lstrip)rX   linerI   rI   rJ   rs   !  s    z,RCodePrinter.indent_code.<locals>.<listcomp>c                s    g | ]}t tt|j qS rI   )rp   anymapendswith)rX   r   )	inc_tokenrI   rJ   rs   #  s    c                s    g | ]}t tt|j qS rI   )rp   r   r   
startswith)rX   r   )	dec_tokenrI   rJ   rs   $  s   r   r~   z%s%s)r   r   rU   
splitlinesrt   	enumeraterd   )
rG   r   Z
code_linesZtabZincreaseZdecreaseZprettylevelnr   rI   )r   r   rJ   rU     s(    



zRCodePrinter.indent_code)%__name__
__module____qualname____doc__ZprintmethodZlanguagerD   Z_default_settingsZ
_operatorsZ_relationalsr?   rM   rN   rQ   rT   rW   r^   rj   ro   rr   ru   rv   rw   rx   ry   rz   r   r   r   r   r   r   r   r   r   rU   __classcell__rI   rI   )r   rJ   r0   N   sT   %r0   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);

    )r0   Zdoprint)rn   Z	assign_torH   rI   rI   rJ   rcode3  s    kr   c             K   s   t t| f| dS )z0Prints R representation of the given expression.N)printr   )rn   rH   rI   rI   rJ   print_rcode  s    r   )N)r   Z
__future__r   r   Z
sympy.corer   Zsympy.core.compatibilityr   r   Zsympy.codegen.astr   Zsympy.printing.codeprinterr   Zsympy.printing.precedencer	   r
   Zsympy.sets.fancysetsr   rA   rF   r0   r   r   rI   rI   rI   rJ   <module>	   st    f
n