ó
”¼\c           @  s   d  d l  m Z m Z d  d l m Z d  d l m Z m Z m Z m	 Z	 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 d  d	 l m Z d  d
 l m Z d  d l m Z d e f d     YZ d e f d     YZ  d e f d     YZ! d S(   i’’’’(   t   print_functiont   division(   t   wraps(   t   Addt   Mult   Powt   St   sympifyt   Float(   t   Basic(   t   default_sort_keyt   string_types(   t   Lambda(   t   _keep_coeff(   t   Symbol(   t
   StrPrinter(   t
   precedence(   t
   Assignmentt   requiresc           B  s    e  Z d  Z d   Z d   Z RS(   s:    Decorator for registering requirements on print methods. c         K  s   | |  _  d  S(   N(   t   _req(   t   selft   kwargs(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   __init__   s    c           s"      f d   } t     |  S(   Nc           sF   x3  j  j   D]" \ } } t |  |  j |  q W  |  | |  S(   N(   R   t   itemst   getattrt   update(   t   self_t   argsR   t   kt   v(   t   methodR   (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _method_wrapper   s    (   R   (   R   R   R   (    (   R   R   s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   __call__   s    (   t   __name__t
   __module__t   __doc__R   R    (    (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR      s   	t   AssignmentErrorc           B  s   e  Z d  Z RS(   sA   
    Raised if an assignment variable for a loop is missing.
    (   R!   R"   R#   (    (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR$      s   t   CodePrinterc           B  s|  e  Z d  Z i d d 6d d 6d d 6Z i d5 d 6d d	 6e d
 6d d 6e d 6e d 6e d 6Z d5 d  Z d5 d  Z	 d5 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 e Z d'   Z  d(   Z! d)   Z" d*   Z# d+   Z$ d,   Z% d-   Z& d.   Z' d/   Z( d0   Z) d1   Z* d2   Z+ d3   Z, d4   Z- e- Z. e- Z/ e- Z0 e- Z1 e- Z2 e- Z3 e- Z4 e- Z5 e- Z6 e- Z7 e- Z8 e- Z9 e- Z: e- Z; e- Z< e- Z= e- Z> e- Z? e- Z@ e- ZA e- ZB e- ZC e- ZD e- ZE e- ZF e- ZG e- ZH e- ZI e- ZJ e- ZK e- ZL e- ZM RS(6   s6   
    The base class for code-printing subclasses.
    s   &&t   ands   ||t   ort   !t   nott   ordert   autot	   full_prect   error_on_reservedt   _t   reserved_word_suffixt   humant   inlinet   allow_unknown_functionsc         C  s;   t  t |   j d |  t |  d  s7 t   |  _ n  d  S(   Nt   settingst   reserved_words(   t   superR%   R   t   hasattrt   setR4   (   R   R3   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR   ;   s    c         C  sI  d d l  m } t | t  rL | j r= | | | j  } q t |  } nE t | t t d  f  s t
 d j t |   j t |     n  | r© t | |  } n t |  } t   |  _ t   |  _ |  j |  j   } |  j d rĢg  } |  j rb| j |  j d j |  j    x? t |  j d t D]% } | j |  j t |  j   q6Wn  x< t |  j d t D]% \ } } | j |  j | |   qxW| | } |  j |  } d j |  } na |  j |  } t g  |  j D]! \ }	 }
 |	 |  j |
  f ^ qč } | |  j d j |  f } t   |  _ t   |  _ | S(	   sO  
        Print the expression as code.

        Parameters
        ----------
        expr : Expression
            The expression to be printed.

        assign_to : Symbol, MatrixSymbol, or string (optional)
            If provided, the printed code will set the expression to a
            variable with name ``assign_to``.
        i’’’’(   t   MatrixSymbols'   {0} cannot assign to object of type {1}R0   s   Not supported in {0}:t   keys   
N(   t"   sympy.matrices.expressions.matexprR8   t
   isinstanceR   t	   is_Matrixt   shapeR   R	   t   typet   Nonet	   TypeErrort   formatR!   R   R   R7   t   _not_supportedt   _number_symbolst   _printt
   splitlinest	   _settingst   appendt   _get_commentt   languaget   sortedt   strt   _declare_number_constt   _format_codet   join(   R   t   exprt	   assign_toR8   t   linest
   frontlinest   namet   valuet   resultR   R   t   num_syms(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   doprintA   s@    		!	&"
7c         C  s  |  j  j d t  rF d d l m } |  j | |  } | |  } n g  } i | f d  6} |  j |  \ } } d  | k r t j	 |  t
 | d     } n t j	 |  d  } |  j |  }	 g  }
 | |	 k r!|
 j |  | d  k	 r|  j d |	 | f  } n  |
 j |  |
 j |  n  xd| D]\} t | t  r(|  j | |  } |  j |  \ } } x| | D]} | | k rŚg  | | D] } t | j    ^ qg  | | D] } d  g ^ q²k rŚt d   qo| d  k rõt d   n  | j |  rt d   n  |
 j |  |
 j |  d |	 t j	 |  | |  f } |
 j |  j |   |
 j |  |
 j |  qoWq(q(Wd	 j |
  S(
   Nt   contracti’’’’(   t   get_contraction_structurei    s   %s = %ss0   FIXME: no support for contractions in factor yets"   need assignment variable for loopssZ   FIXME: lhs present in rhs,                                this is undefined in CodePrinters   
(   RF   t   gett   Truet   sympy.tensorRY   t   _get_expression_indicesR?   t   _get_loop_opening_endingR   RW   R   RD   t   extendt   _get_statementRG   R;   t   tuplet   _sort_optimizedt   listt   keyst   NotImplementedErrorR$   t   hast
   ValueErrorRN   (   R   RO   RP   RY   t   indicest   dummiest   openloopt	   closeloopt   textt   lhs_printedRQ   t   dt
   openloop_dt   closeloop_dt   termt   f(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _doprint_loops{   sT    2$c         C  sy   d d l  m } | |  \ } } | |  \ } } | rJ | rJ | } n  | | k ri t d |   n  |  j | |  S(   Ni’’’’(   t   get_indicess2   lhs indices must match non-dummy rhs indices in %s(   R\   Rt   Rg   Rb   (   R   RO   RP   Rt   t   rindst   junkt   linds(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR]   Ź   s    	c   	        sÅ   d d l  m } | s g  Si    x | D] } d   | <q' W| j |  } x_ | D]W } xN t | j  D]= \ } } y   | c |  j |  7<Wqg t k
 r£ qg Xqg WqQ Wt | d   f d   S(   Ni’’’’(   t   Indexedi    R9   c           s     |  S(   N(    (   t   x(   t   score_table(    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   <lambda>ķ   t    (   t   sympy.tensor.indexedRx   t   atomst	   enumerateRh   t   _rate_index_positiont   KeyErrorRJ   (	   R   Rh   RO   Rx   t   it   arrayst   arrt   pt   ind(    (   Rz   s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyRb   Ų   s    c         C  s   t  d   d S(   s°   function to calculate score based on position among indices

        This method is used to sort loops in an optimized order, see
        CodePrinter._sort_optimized()
        s=   This function must be implemented by subclass of CodePrinter.N(   Re   (   R   R   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR   ļ   s    c         C  s   t  d   d S(   s1   Formats a codestring with the proper line ending.s=   This function must be implemented by subclass of CodePrinter.N(   Re   (   R   t
   codestring(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR`   ų   s    c         C  s   t  d   d S(   s#   Formats a text string as a comment.s=   This function must be implemented by subclass of CodePrinter.N(   Re   (   R   Rl   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyRH   ż   s    c         C  s   t  d   d S(   s3   Declare a numeric constant at the top of a functions=   This function must be implemented by subclass of CodePrinter.N(   Re   (   R   RS   RT   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyRL     s    c         C  s   t  d   d S(   s~   Take in a list of lines of code, and format them accordingly.

        This may include indenting, wrapping long lines, etc...s=   This function must be implemented by subclass of CodePrinter.N(   Re   (   R   RQ   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyRM     s    c         C  s   t  d   d S(   sO   Returns a tuple (open_lines, close_lines) containing lists
        of codeliness=   This function must be implemented by subclass of CodePrinter.N(   Re   (   R   Rh   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR^     s    c         C  s5   | j  j d  r d | j  Sd | j  | j f Sd  S(   Nt   Dummy_R.   s   %s_%d(   RS   t
   startswitht   dummy_index(   R   RO   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_Dummy  s    c         C  s,   d j  g  | j D] } |  j |  ^ q  S(   Ns   
(   RN   R   RD   (   R   RO   R   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_CodeBlock  s    c         C  s
   t  |  S(   N(   RK   (   R   t   string(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_String  s    c         C  s   d | j  S(   Ns   "%s"(   Rl   (   R   t   arg(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_QuotedString   s    c         C  s   |  j  t |   S(   N(   RH   RK   (   R   R   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_Comment#  s    c         C  s¼  d d l  m } d d l m } d d l m } | j } | j } t | j |  ræ g  } g  } x: | j	 D]/ \ }	 }
 | j
 t | |	   | j
 |
  qj W| t | |    } |  j |  St | |  r@g  } x\ |  j |  D]K \ } } t | | | f | | | f  } |  j |  } | j
 |  qä Wd j |  S|  j j d t  r| j |  ss| j |  r|  j | |  S|  j |  } |  j |  } |  j d | | f  Sd  S(   Ni’’’’(   t	   Piecewise(   R8   (   t   IndexedBases   
RX   s   %s = %s(   t$   sympy.functions.elementary.piecewiseR   R:   R8   R}   R   t   lhst   rhsR;   R   RG   R   t   zipRD   t   _traverse_matrix_indicesRN   RF   RZ   t   FalseRf   Rs   R`   (   R   RO   R   R8   R   R   R   t   expressionst
   conditionst   et   ct   tempRQ   R   t   jt   code0t   lhs_codet   rhs_code(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_Assignment&  s4    		#$c           sX     j  | j  }   j  | j  }   j d j t   f d   | | j | g     S(   Ns   {0} {1} {2}c           s     j  |   S(   N(   RD   (   R   (   R   (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR{   N  R|   (   RD   R   R   R`   RA   t   mapt   op(   R   RO   R”   R¢   (    (   R   s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_AugmentedAssignmentJ  s
    c           s/   d | j  d j t   f d   | j   f S(   Ns   %s(%s)s   , c           s     j  |   S(   N(   RD   (   R   (   R   (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR{   T  R|   (   RS   RN   R¤   t   function_args(   R   RO   (    (   R   s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_FunctionCallQ  s    c         C  s   |  j  | j  S(   N(   RD   t   symbol(   R   RO   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_VariableW  s    c         C  s"   | j  \ } |  j |  j |   S(   N(   R   R`   RD   (   R   RO   R   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_StatementZ  s    c         C  si   t  t |   j |  } | |  j k ra |  j d rR d } t | j |    n  | |  j d S| Sd  S(   NR-   sV   This expression includes the symbol "{}" which is a reserved keyword in this language.R/   (   R5   R%   t   _print_SymbolR4   RF   Rg   RA   (   R   RO   RS   t   msg(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR¬   ^  s    c         C  s{  | j  j |  j k rą |  j | j  j } d  } t | t  rF | } n* x' | D] \ } } | | j   rM PqM qM W| d  k	 rwy0 | g  | j D] } |  j | d  ^ q   SWqŻ t k
 rŁ d | |  j	 | j d  f SXqwn t
 | d  rt | j t  r|  j | j | j    S| j rj|  j j d t  rjd |  j | j   d j t |  j | j   f S|  j |  Sd  S(   Ni    s   %s(%s)s   , t   _imp_R2   (   t   funcR!   t   known_functionsR?   R;   R   R   t   parenthesizeR@   t	   stringifyR6   R®   R   RD   t   is_FunctionRF   RZ   R   RN   R¤   t   _print_not_supported(   R   RO   t	   cond_funcRÆ   t   condt   item(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_Functionk  s$    	0$!2c         C  sr   |  j  j d t  r8 |  j t | j |  j  d    S|  j j | t | j |  j  d   f  t |  Sd  S(   NR1   t	   precision(	   RF   RZ   R   RD   R   t   evalfRC   t   addRK   (   R   RO   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_NumberSymbol  s
    # c         C  s   |  j  |  S(   N(   R¼   (   R   RO   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_Catalan  s    c         C  s   |  j  |  S(   N(   R¼   (   R   RO   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_EulerGamma  s    c         C  s   |  j  |  S(   N(   R¼   (   R   RO   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_GoldenRatio  s    c         C  s   |  j  |  S(   N(   R¼   (   R   RO   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_TribonacciConstant  s    c         C  s   |  j  |  S(   N(   R¼   (   R   RO   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_Exp1  s    c         C  s   |  j  |  S(   N(   R¼   (   R   RO   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt	   _print_Pi  s    c           sF   t  |    d  j d j    f d   t | j d t D  S(   Ns    %s R&   c         3  s!   |  ] }  j  |    Vq d  S(   N(   R±   (   t   .0t   a(   t   PRECR   (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pys	   <genexpr>  s   R9   (   R   t
   _operatorsRN   RJ   R   R
   (   R   RO   (    (   RÅ   R   s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt
   _print_And  s     c           sF   t  |    d  j d j    f d   t | j d t D  S(   Ns    %s R'   c         3  s!   |  ] }  j  |    Vq d  S(   N(   R±   (   RĆ   RÄ   (   RÅ   R   (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pys	   <genexpr>¢  s   R9   (   R   RĘ   RN   RJ   R   R
   (   R   RO   (    (   RÅ   R   s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt	   _print_Or   s     c           s_    j  j d  d  k r%  j |  St |    d  j  d j    f d   | j D  S(   Nt   xors    %s c         3  s!   |  ] }  j  |    Vq d  S(   N(   R±   (   RĆ   RÄ   (   RÅ   R   (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pys	   <genexpr>©  s   (   RĘ   RZ   R?   R“   R   RN   R   (   R   RO   (    (   RÅ   R   s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt
   _print_Xor„  s
     c           s_    j  j d  d  k r%  j |  St |    d  j  d j    f d   | j D  S(   Nt
   equivalents    %s c         3  s!   |  ] }  j  |    Vq d  S(   N(   R±   (   RĆ   RÄ   (   RÅ   R   (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pys	   <genexpr>°  s   (   RĘ   RZ   R?   R“   R   RN   R   (   R   RO   (    (   RÅ   R   s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   _print_Equivalent¬  s
     c         C  s.   t  |  } |  j d |  j | j d |  S(   NR)   i    (   R   RĘ   R±   R   (   R   RO   RÅ   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt
   _print_Not³  s    c         C  sx  t  |  } | j   \ } } | d k  rC t | |  } d } n d } g  } g  } g  } |  j d k ry | j   }	 n t j |  }	 xŲ |	 D]Š }
 |
 j rR|
 j rR|
 j	 j
 rR|
 j	 j rR|
 j	 d k rō | j t |
 j |
 j	 d t  q_t |
 j d j  d k r2t |
 j t  r2| j |
  n  | j t |
 j |
 j	   q | j |
  q W| prt j g } g  | D] } |  j | |  ^ q|} g  | D] } |  j | |  ^ q”} xJ | D]B }
 |
 j | k rĘd	 | | j |
 j  | | j |
 j  <qĘqĘW| s#| d
 j |  St |  d k rR| d
 j |  d | d S| d
 j |  d d
 j |  Sd  S(   Ni    t   -R|   t   oldt   nonei’’’’t   evaluatei   s   (%s)t   *t   /s   /(%s)(   RĻ   RŠ   (   R   t   as_coeff_MulR   R*   t   as_ordered_factorsR   t	   make_argst   is_commutativet   is_Powt   expt   is_Rationalt   is_negativeRG   R   t   baseR   t   lenR   R;   R   t   OneR±   t   indexRN   (   R   RO   t   precR   R   t   signRÄ   t   bt	   pow_parenR   R·   Ry   t   a_strt   b_str(    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt
   _print_Mul·  s>    	*&. %%1c         C  s   |  j  j |  |  j |  S(   N(   RB   R»   t   emptyPrinter(   R   RO   (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR“   ź  s    N(N   R!   R"   R#   RĘ   R?   R   R[   t   _default_settingsR   RW   Rs   R]   Rb   R   R`   RH   RL   RM   R^   R   R   R   R   R   R£   R¦   RØ   RŖ   R«   R¬   Rø   t   _print_ExprR¼   R½   R¾   Ræ   RĄ   RĮ   RĀ   RĒ   RČ   RŹ   RĢ   RĶ   Rę   R“   t   _print_Basict   _print_ComplexInfinityt   _print_Derivativet   _print_ExprCondPairt   _print_GeometryEntityt   _print_Infinityt   _print_Integralt   _print_Intervalt   _print_AccumulationBoundst   _print_Limitt   _print_Matrixt   _print_ImmutableMatrixt   _print_ImmutableDenseMatrixt   _print_MutableDenseMatrixt   _print_MatrixBaset   _print_DeferredVectort
   _print_NaNt   _print_NegativeInfinityt   _print_Normalt   _print_Ordert
   _print_PDFt   _print_RootOft   _print_RootsOft   _print_RootSumt   _print_Samplet   _print_SparseMatrixt   _print_MutableSparseMatrixt   _print_ImmutableSparseMatrixt   _print_Uniformt   _print_Unitt   _print_Wildt   _print_WildFunction(    (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyR%   &   s¦   

:O															$							
												3	N("   t
   __future__R    R   t	   functoolsR   t
   sympy.coreR   R   R   R   R   R   t   sympy.core.basicR	   t   sympy.core.compatibilityR
   R   t   sympy.core.functionR   t   sympy.core.mulR   t   sympy.core.symbolR   t   sympy.printing.strR   t   sympy.printing.precedenceR   t   sympy.codegen.astR   t   objectR   t	   ExceptionR$   R%   (    (    (    s9   lib/python2.7/site-packages/sympy/printing/codeprinter.pyt   <module>   s   .