
\c           @  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 m Z d d l m Z i d d	 6d
 d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6d! d! 6d" d" 6d# d# 6d$ d$ 6d% d% 6Z d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 g Z d: e
 f d;     YZ d> d<  Z d=   Z d> S(?   s   
R code printer

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



i(   t   print_functiont   division(   t
   Assignment(   t   string_typest   range(   t   CodePrinter(   t
   precedencet
   PRECEDENCE(   t   Ranget   abst   Abst   sint   cost   tant   asint   acost   atant   atan2t   expt   logt   erft   sinht   cosht   tanht   asinht   acosht   atanht   floort   ceilingt   signt   maxt   Maxt   mint   Mint	   factorialt   gammat   digammat   trigammat   betat   ift   elset   repeatt   whilet   functiont   fort   int   nextt   breakt   TRUEt   FALSEt   NULLt   Inft   NaNt   NAt   NA_integer_t   NA_real_t   NA_complex_t   NA_character_t   volatilet   RCodePrinterc           B  sp  e  Z d  Z d Z d Z i	 d/ d 6d d 6d d 6i  d 6e d	 6e d
 6e   d 6e d 6d d 6Z	 i d d 6d d 6d d 6Z
 i  Z i  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 d(   Z d)   Z  d*   Z! d+   Z" d,   Z# d-   Z$ d.   Z% RS(0   s<   A printer to convert python expressions to strings of R codet   _rcodet   Rt   ordert   autot	   full_preci   t	   precisiont   user_functionst   humant   contractt   dereferencet   error_on_reservedt   _t   reserved_word_suffixt   &t   andt   |t   ort   !t   notc         C  so   t  j |  |  t t  |  _ | j d i   } |  j j |  t | j d g    |  _ t t  |  _ d  S(   NRB   RE   (	   R   t   __init__t   dictt   known_functionst   gett   updatet   sett   _dereferencet   reserved_words(   t   selft   settingst	   userfuncs(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyRO   f   s    c         C  s   | d S(   Ni   (    (   RW   t   p(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _rate_index_positionn   s    c         C  s   d | S(   Ns   %s;(    (   RW   t
   codestring(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _get_statementq   s    c         C  s   d j  |  S(   Ns   // {0}(   t   format(   RW   t   text(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _get_commentt   s    c         C  s   d j  | |  S(   Ns
   {0} = {1};(   R^   (   RW   t   namet   value(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _declare_number_constw   s    c         C  s   |  j  |  S(   N(   t   indent_code(   RW   t   lines(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _format_codez   s    c           s)   | j  \ }     f d   t |  D S(   Nc         3  s.   |  ]$ } t     D] } | | f Vq q d  S(   N(   R   (   t   .0t   it   j(   t   cols(    s3   lib/python2.7/site-packages/sympy/printing/rcode.pys	   <genexpr>   s    (   t   shapeR   (   RW   t   matt   rows(    (   Rj   s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _traverse_matrix_indices}   s    c         C  s   g  } g  } d } xm | D]e } | j  | i |  j | j  d 6|  j | j d  d 6|  j | j d  d 6 | j  d  q W| | f S(   sP   Returns a tuple (open_lines, close_lines) containing lists of codelines
        s#   for (%(var)s in %(start)s:%(end)s){t   vari   t   startt   endt   }(   t   appendt   _printt   labelt   lowert   upper(   RW   t   indicest
   open_linest   close_linest	   loopstartRh   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _get_loop_opening_ending   s    c         C  s   d |  j  k r |  j |  St |  } | j d k rN d |  j | j |  S| j d k rq d |  j | j  Sd |  j | j |  |  j | j |  f Sd  S(   Nt   Powis   1.0/%sg      ?s   sqrt(%s)s   %s^%s(   RQ   t   _print_FunctionR   R   t   parenthesizet   baseRt   (   RW   t   exprt   PREC(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt
   _print_Pow   s    c         C  s-   t  | j  t  | j  } } d | | f S(   Ns	   %d.0/%d.0(   t   intRZ   t   q(   RW   R   RZ   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_Rational   s    c         C  sK   g  | j  D] } |  j |  ^ q
 } d |  j | j j  d j |  f S(   Ns   %s[%s]s   , (   Rx   Rt   R   Ru   t   join(   RW   R   Rh   t   inds(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_Indexed   s    %c         C  s   |  j  | j  S(   N(   Rt   Ru   (   RW   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt
   _print_Idx   s    c         C  s   d S(   Ns   exp(1)(    (   RW   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_Exp1   s    c         C  s   d S(   Nt   pi(    (   RW   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt	   _print_Pi   s    c         C  s   d S(   NR3   (    (   RW   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_Infinity   s    c         C  s   d S(   Ns   -Inf(    (   RW   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_NegativeInfinity   s    c         C  s'  d d l  m } d d l m } | j } | j } t | |  r g  } x\ |  j |  D]K \ } } t | | | f | | | f  }	 |  j	 |	  }
 | j
 |
  qW Wd j |  S|  j d r | j |  s | j |  r |  j | |  S|  j	 |  } |  j	 |  } |  j d | | f  Sd  S(   Ni(   t   MatrixSymbol(   t   IndexedBases   
RD   s   %s = %s(   t"   sympy.matrices.expressions.matexprR   t   sympy.tensor.indexedR   t   lhst   rhst
   isinstanceRn   R   Rt   Rs   R   t	   _settingst   hast   _doprint_loopsR]   (   RW   R   R   R   R   R   Re   Rh   Ri   t   tempt   code0t   lhs_codet   rhs_code(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_Assignment   s"    		#c         C  s   | j  d j t k r6 d |  j | j  d j  } n6 d |  j | j  d j  |  j | j  d j  f } | } xK t | j  d   D]6 \ } } d |  j |  |  j |  f | d } q W| S(   Nis   %ss   ifelse(%s,%s,NA)s   ifelse(%s,%s,t   )(   t   argst   condt   TrueRt   R   t   reversed(   RW   R   t	   last_linet   codet   et   c(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_Piecewise   s     6 .c         C  sM   d d l  m } | | j d | j d f | j d t f  } |  j |  S(   Ni(   t	   Piecewisei   i    i   (   t   sympy.functionsR   R   R   Rt   (   RW   R   R   t
   _piecewise(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt
   _print_ITE   s    0c         C  sA   d j  |  j | j t d d t | j | j | j j d  S(   Ns   {0}[{1}]t   Atomt   stricti   (   R^   R   t   parentR   R   Ri   Rh   Rk   (   RW   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_MatrixElement   s    c         C  s<   t  t |   j |  } | |  j k r4 d j |  S| Sd  S(   Ns   (*{0})(   t   superR;   t   _print_SymbolRU   R^   (   RW   R   Ra   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyR      s    c         C  s@   |  j  | j  } |  j  | j  } | j } d j | | |  S(   Ns   {0} {1} {2}(   Rt   R   R   t   rel_opR^   (   RW   R   R   R   t   op(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_Relational   s    	c         C  s   d d l  m } d d l m } d d l m } | | | j d  | j d | | j d d  f d t f  } |  j |  S(   Ni(   R   (   t   Ne(   R   i    i   (	   t(   sympy.functions.elementary.trigonometricR   t   sympy.core.relationalR   R   R   R   R   Rt   (   RW   R   R   R   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_sinc   s    @c         C  s@   |  j  | j  } | j } |  j  | j  } d j | | |  S(   Ns   {0} {1} {2};(   Rt   R   R   R   R^   (   RW   R   R   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   _print_AugmentedAssignment  s    	c         C  s   |  j  | j  } t | j t  r< | j j \ } } } n t d   |  j  | j  } d j d | d | d | d | d |  S(   Ns*   Only iterable currently supported is RangesL   for ({target} = {start}; {target} < {stop}; {target} += {step}) {{
{body}
}}t   targetRp   t   stopt   stept   body(	   Rt   R   R   t   iterableR   R   t   NotImplementedErrorR   R^   (   RW   R   R   Rp   R   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt
   _print_For	  s    	c         C  sY  t  | t  r4 |  j | j t   } d j |  Sd } d } d } g  | D] } | j d	  ^ qM } g  | D]$ } t t t	 | j
 |    ^ qo } g  | D]$ } t t t	 | j |    ^ q } g  }	 d
 }
 x| t |  D]n \ } } | d k s| d k r|	 j |  q n  |
 | | 8}
 |	 j d | |
 | f  |
 | | 7}
 q W|	 S(   s0   Accepts a string of code or a list of code linest    s      t   {t   (s   {
s   (
Rr   R   s    	i    s   
s   %s%s(   R   R   s   {
s   (
(   Rr   R   (   R   R   Rd   t
   splitlinesR   R   t   lstripR   t   anyt   mapt   endswitht
   startswitht	   enumerateRs   (   RW   R   t
   code_linest   tabt	   inc_tokent	   dec_tokent   linet   increaset   decreaset   prettyt   levelt   n(    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyRd     s(    "1.N(&   t   __name__t
   __module__t   __doc__t   printmethodt   languaget   NoneR   RT   t   Falset   _default_settingst
   _operatorst   _relationalsRO   R[   R]   R`   Rc   Rf   Rn   R|   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   Rd   (    (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyR;   M   sX   


																%								c         K  s   t  |  j |  |  S(   s  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;   t   doprint(   R   t	   assign_toRX   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   rcode2  s    kc         K  s   t  t |  |   d S(   s0   Prints R representation of the given expression.N(   t   printR   (   R   RX   (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   print_rcode  s    N(   R   t
   __future__R    R   t   sympy.codegen.astR   t   sympy.core.compatibilityR   R   t   sympy.printing.codeprinterR   t   sympy.printing.precedenceR   R   t   sympy.sets.fancysetsR   RQ   RV   R;   R   R   R   (    (    (    s3   lib/python2.7/site-packages/sympy/printing/rcode.pyt   <module>	   sr   
	n