ó
¡¼™\c           @  s@  d  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 l m Z 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/ 6d0 d1 6Z d2 e f d3 „  ƒ  YZ d6 d4 „ Z d5 „  Z d6 S(7   sÅ   
Javascript code printer

The JavascriptCodePrinter converts single sympy expressions into single
Javascript expressions, using the functions defined in the Javascript
Math object where possible.

iÿÿÿÿ(   t   print_functiont   division(   t
   Assignment(   t   S(   t   string_typest   range(   t   CodePrinter(   t
   precedencet
   PRECEDENCEs   Math.abst   Abss	   Math.acost   acoss
   Math.acosht   acoshs	   Math.asint   asins
   Math.asinht   asinhs	   Math.atant   atans
   Math.atan2t   atan2s
   Math.atanht   atanhs	   Math.ceilt   ceilings   Math.cost   coss	   Math.cosht   coshs   Math.expt   exps
   Math.floort   floors   Math.logt   logs   Math.maxt   Maxs   Math.mint   Mins	   Math.signt   signs   Math.sint   sins	   Math.sinht   sinhs   Math.tant   tans	   Math.tanht   tanht   JavascriptCodePrinterc           B  sÿ   e  Z d  Z d Z d Z i d d 6d d 6d d 6i  d 6e d	 6e d
 6e d 6Z 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 RS(    sK   "A Printer to convert python expressions to strings of javascript code
    t   _javascriptt
   Javascriptt   ordert   autot	   full_preci   t	   precisiont   user_functionst   humant   allow_unknown_functionst   contractc         C  sE   t  j |  | ƒ t t ƒ |  _ | j d i  ƒ } |  j j | ƒ d  S(   NR%   (   R   t   __init__t   dictt   known_functionst   gett   update(   t   selft   settingst	   userfuncs(    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyR)   >   s    c         C  s   | d S(   Ni   (    (   R.   t   p(    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _rate_index_positionD   s    c         C  s   d | S(   Ns   %s;(    (   R.   t
   codestring(    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _get_statementG   s    c         C  s   d j  | ƒ S(   Ns   // {0}(   t   format(   R.   t   text(    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _get_commentJ   s    c         C  s    d j  | | j |  j d ƒ ƒ S(   Ns   var {0} = {1};R$   (   R5   t   evalft	   _settings(   R.   t   namet   value(    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _declare_number_constM   s    c         C  s   |  j  | ƒ S(   N(   t   indent_code(   R.   t   lines(    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _format_codeP   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(    s4   lib/python2.7/site-packages/sympy/printing/jscode.pys	   <genexpr>U   s    (   t   shapeR   (   R.   t   matt   rows(    (   RC   s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _traverse_matrix_indicesS   s    c         C  sˆ   g  } g  } d } xi | D]a } | j  | i |  j | j ƒ d 6|  j | j ƒ d 6|  j | j d ƒ d 6ƒ | j  d ƒ q W| | f S(   NsA   for (var %(varble)s=%(start)s; %(varble)s<%(end)s; %(varble)s++){t   varblet   starti   t   endt   }(   t   appendt   _printt   labelt   lowert   upper(   R.   t   indicest
   open_linest   close_linest	   loopstartRA   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _get_loop_opening_endingW   s    c         C  s¬   t  | ƒ } | j d k r2 d |  j | j | ƒ S| j d k rU d |  j | j ƒ S| j t d ƒ d k r‚ d |  j | j ƒ Sd |  j | j ƒ |  j | j ƒ f Sd  S(	   Niÿÿÿÿs   1/%sg      à?s   Math.sqrt(%s)i   i   s   Math.cbrt(%s)s   Math.pow(%s, %s)(   R   R   t   parenthesizet   baseRM   R   (   R.   t   exprt   PREC(    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt
   _print_Powd   s    c         C  s-   t  | j ƒ t  | j ƒ } } d | | f S(   Ns   %d/%d(   t   intR1   t   q(   R.   RX   R1   R\   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _print_Rationalp   s    c         C  s„   | j  } t j } t j } x@ t t | j ƒ ƒ D]) } | | j | | 7} | | | 9} q1 Wd |  j | j	 j
 ƒ |  j | ƒ f S(   Ns   %s[%s](   RD   R   t   Zerot   Onet   reversedR   t   rankRQ   RM   RW   RN   (   R.   RX   t   dimst   elemt   offsetRA   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _print_Indexedt   s    			c         C  s   |  j  | j ƒ S(   N(   RM   RN   (   R.   RX   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt
   _print_Idx~   s    c         C  s   d S(   Ns   Math.E(    (   R.   RX   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _print_Exp1   s    c         C  s   d S(   Ns   Math.PI(    (   R.   RX   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt	   _print_Pi„   s    c         C  s   d S(   Ns   Number.POSITIVE_INFINITY(    (   R.   RX   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _print_Infinity‡   s    c         C  s   d S(   Ns   Number.NEGATIVE_INFINITY(    (   R.   RX   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _print_NegativeInfinityŠ   s    c   	      C  sœ  | j  d j t k r% t d ƒ ‚ n  g  } | j t ƒ rxÄ t | j  ƒ D]³ \ } \ } } | d k r… | j d |  j | ƒ ƒ nO | t	 | j  ƒ d k rº | t k rº | j d ƒ n | j d |  j | ƒ ƒ |  j | ƒ } | j | ƒ | j d ƒ qJ Wd	 j
 | ƒ Sg  | j  d  D]. \ } } d
 |  j | ƒ |  j | ƒ f ^ q} d |  j | j  d j ƒ } d j
 | ƒ | d j
 d t	 | ƒ g ƒ Sd  S(   Niÿÿÿÿs¼   All Piecewise expressions must contain an (expr, True) statement to be used as a default condition. Without one, the generated expression may not evaluate to anything under some condition.i    s	   if (%s) {i   s   else {s   else if (%s) {RK   s   
s   ((%s) ? (
%s
)
s   : (
%s
)s   : t    t   )(   t   argst   condt   Truet
   ValueErrort   hasR   t	   enumerateRL   RM   t   lent   joinRX   (	   R.   RX   R>   RA   t   et   ct   code0t   ecpairst	   last_line(    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _print_Piecewise   s$    "%?c         C  sA   d j  |  j | j t d d t ƒ| j | j | j j d ƒ S(   Ns   {0}[{1}]t   Atomt   stricti   (   R5   RV   t   parentR   Ro   RB   RA   RD   (   R.   RX   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   _print_MatrixElement­   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   (
RK   Rl   s    	i    s   
s   %s%s(   R€   R   s   {
s   (
(   RK   Rl   (   t
   isinstanceR   R=   t
   splitlinesRo   Rt   t   lstripR[   t   anyt   mapt   endswitht
   startswithRr   RL   (   R.   t   codet
   code_linest   tabt	   inc_tokent	   dec_tokent   linet   increaset   decreaset   prettyt   levelt   n(    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyR=   ²   s(    "1.N(   t   __name__t
   __module__t   __doc__t   printmethodt   languaget   NoneRo   t   Falset   _default_settingsR)   R2   R4   R7   R<   R?   RG   RU   RZ   R]   Re   Rf   Rg   Rh   Ri   Rj   Rz   R~   R=   (    (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyR   .   s<   
										
						 	c         K  s   t  | ƒ j |  | ƒ S(   sÈ  Converts an expr to a string of javascript 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 keys are ``FunctionClass`` instances and values are
        their string representations. Alternatively, the dictionary value can
        be a list of tuples i.e. [(argument_test, js_function_string)]. 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 jscode, symbols, Rational, sin, ceiling, Abs
    >>> x, tau = symbols("x, tau")
    >>> jscode((2*tau)**Rational(7, 2))
    '8*Math.sqrt(2)*Math.pow(tau, 7/2)'
    >>> jscode(sin(x), assign_to="s")
    's = Math.sin(x);'

    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,
    js_function_string)].

    >>> custom_functions = {
    ...   "ceiling": "CEIL",
    ...   "Abs": [(lambda x: not x.is_integer, "fabs"),
    ...           (lambda x: x.is_integer, "ABS")]
    ... }
    >>> jscode(Abs(x) + ceiling(x), user_functions=custom_functions)
    'fabs(x) + CEIL(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(jscode(expr, tau))
    if (x > 0) {
       tau = x + 1;
    }
    else {
       tau = 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]))
    >>> jscode(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(jscode(mat, A))
    A[0] = Math.pow(x, 2);
    if (x > 0) {
       A[1] = x + 1;
    }
    else {
       A[1] = x;
    }
    A[2] = Math.sin(x);
    (   R   t   doprint(   RX   t	   assign_toR/   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   jscodeÏ   s    ic         K  s   t  t |  |  ƒ d S(   s€   Prints the Javascript representation of the given expression.

       See jscode for the meaning of the optional arguments.
    N(   t   printRž   (   RX   R/   (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   print_jscode;  s    N(   R–   t
   __future__R    R   t   sympy.codegen.astR   t
   sympy.coreR   t   sympy.core.compatibilityR   R   t   sympy.printing.codeprinterR   t   sympy.printing.precedenceR   R   R+   R   R™   Rž   R    (    (    (    s4   lib/python2.7/site-packages/sympy/printing/jscode.pyt   <module>   s>   
¡l