
~9\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	 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 m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% m& Z& m' Z' m( Z( d d	 l) m* Z* m+ Z+ d d
 l, m- Z- m. Z. d d l/ m0 Z0 i d   d f d   d f g 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 6Z1 e2 e1 i d  d  6d! d! 6d" d" 6d# d# 6d$ d% 6 Z3 e3 Z4 e2 e3 i d& d& 6d' d' 6d' d' 6d( d( 6d) d) 6d* d* 6d+ d, 6d- d- 6d. d. 6d/ d0 6d1 d1 6d2 d3 6d4 d5 6 Z5 d6 d7 d8 d9 d: d; d< d= d> d? d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ dR dS dT dU dV g! Z6 dW dX g Z7 dY   Z8 dZ   Z9 d[ e* f d\     YZ: d] e; f d^     YZ< e d_ d` da db dc dd de df  dg e< e: f dh     Y Z= di e< e: f dj     YZ> x+ dk j?   D] Z@ eA e> dl e@ e> jB  qWdm e> f dn     YZC i e: do 6e> dp 6eC dq 6ZD eE dp dr  ZF ds   ZG dt S(u   s  
C code printer

The C89CodePrinter & C99CodePrinter converts single sympy expressions into
single C expressions, using the functions defined in math.h where possible.

A complete code generator, which uses ccode extensively, can be found in
sympy.utilities.codegen. The codegen module can be used to generate complete
source code files that are compilable without further modifications.


i(   t   print_functiont   division(   t   wraps(   t   chain(   t   S(   t   string_typest   range(   t
   deprecated(   t
   Assignmentt   Pointert   Variablet   Declarationt   realt   complex_t   integert   bool_t   float32t   float64t   float80t	   complex64t
   complex128t   intct   value_constt   pointer_constt   int8t   int16t   int32t   int64t   uint8t   uint16t   uint32t   uint64t   untyped(   t   CodePrintert   requires(   t
   precedencet
   PRECEDENCE(   t   Rangec         C  s   |  j  S(   N(   t
   is_integer(   t   x(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   <lambda>#   t    t   fabsc         C  s   |  j  S(   N(   R&   (   R'   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR(   #   R)   t   abst   Abst   sint   cost   tant   asint   acost   atant   atan2t   expt   logt   sinht   cosht   tanht   floort   ceilt   ceilingt   asinht   acosht   atanht   erft   tgammat   gammat   exp2t   expm1t   log10t   log2t   log1pt   cbrtt   Cbrtt   hypott   fmat   lgammat   loggammat   erfct   fmaxt   Maxt   fmint   Mint   autot   breakt   caset   chart   constt   continuet   defaultt   dot   doublet   elset   enumt   externt   floatt   fort   gotot   ift   intt   longt   registert   returnt   shortt   signedt   sizeoft   statict   structt   entryt   switcht   typedeft   uniont   unsignedt   voidt   volatilet   whilet   inlinet   restrictc          C  s%  d d l  m }  m } d d l m } d d l m } i d t j 6d |  t j  6d d | d  6d	 | d  6d
 | d  6d t j	 6d t j	 d 6d t j	 d 6d d t j	 6d d t j	 6d d | t j	  6d d | t j	  6d | d  6d | d  6d d | d  6d d | d  6S(   sV   Returns a dictionary with math-related macros from math.h/cmath

    Note that these macros are not strictly required by the C/C++-standard.
    For MSVC they are enabled by defining "_USE_MATH_DEFINES" (preferably
    via a compilation flag).

    Returns
    =======

    Dictionary mapping sympy expressions to strings (macro names)

    i(   RE   t   Sqrt(   R5   (   t   sqrtt   M_Et   M_LOG2Ei   i   t   M_LN2t   M_LN10i
   t   M_PIt   M_PI_2t   M_PI_4i   t   M_1_PIt   M_2_PIt
   M_2_SQRTPIt   M_SQRT2t	   M_SQRT1_2(
   t   sympy.codegen.cfunctionsRE   Ru   t&   sympy.functions.elementary.exponentialR5   t(   sympy.functions.elementary.miscellaneousRv   R   t   Exp1t   Pi(   RE   Ru   R5   Rv   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   get_math_macros[   s(    

c           s   t       f d    } | S(   s   Decorator for printer methods

    When a Printer's method is decorated using this decorator the expressions printed
    will first be looked for in the attribute ``math_macros``, and if present it will
    print the macro name in ``math_macros`` followed by a type suffix for the type
    ``real``. e.g. printing ``sympy.pi`` would print ``M_PIl`` if real is mapped to float80.

    c           sA   | |  j  k r- d |  j  | |  j t  f S  |  | |  Sd  S(   Ns   %s%s(   t   math_macrost   _get_math_macro_suffixR   (   t   selft   exprt   kwargs(   t   meth(    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _meth_wrapper   s    (   R   (   R   R   (    (   R   s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _as_macro_if_defined   s    	t   C89CodePrinterc           B  s  e  Z d  Z d Z d Z d Z e e  Z i
 dW d 6d d 6d d 6i  d	 6e	 d
 6e
 d 6e	 d 6e   d 6e
 d 6d d 6Z i e e 6e e 6e e 6Z i d e 6d e 6d e 6d e 6d e 6d e 6d e 6d e 6d e 6d e 6d e 6d e 6d e 6d e 6Z i	 d h e 6d h e 6d h e 6d h e 6d h e 6d h e 6d h e 6d h e 6d h e 6Z i  Z i d e 6d e 6d e  6Z! i d e 6d e 6d e  6Z" i d e  6Z# dW Z$ d Z% e& Z' i  d   Z( d!   Z) d"   Z* d#   Z+ d$   Z, d%   Z- d&   Z. e/ d'    Z0 e/ d(    Z1 d)   Z2 d*   Z3 d+   Z4 d,   Z5 e/ d-    Z6 d.   Z7 d/   Z8 d0   Z9 d1   Z: d2   Z; d3   Z< d4   Z= d5   Z> d6   Z? d7   Z@ d8   ZA d9   ZB d:   ZC d;   ZD d<   ZE d=   ZF d>   ZG d?   ZH d@   ZI eJ dA d h  dB    ZK eJ dA d h  dC    ZL dD   ZM dE   ZN dF   ZO dG   ZP eJ dA dH h  dI    ZQ dJ   ZR dK   ZS dL   ZT dM   ZU dN   ZV dO   ZW dP   ZX dQ   ZY dR   ZZ dS   Z[ dT   Z\ dU   Z] dV   Z^ e\ Z_ RS(X   s<   A printer to convert python expressions to strings of c codet   _ccodet   Ct   C89t   orderRR   t	   full_preci   t	   precisiont   user_functionst   humant   allow_unknown_functionst   contractt   dereferencet   error_on_reservedt   _t   reserved_word_suffixRZ   Rb   R^   t   boolt   int8_tt   int16_tt   int32_tt   int64_ts	   stdbool.hs   stdint.ht   fR)   t   lt   Ft   Lc         C  s	  |  j  d  k r* | j d t    |  _  n  t t |  j j   | j d i   j     |  _ t t |  j j   | j d i   j     |  _ t t |  j	 j   | j d i   j     |  _	 t t |  j
 j   | j d i   j     |  _
 t t |  j j   | j d i   j     |  _ t t |  j j   | j d i   j     |  _ t t |  j j   | j d i   j     |  _ t t |   j |  t |  j | j d	 i    |  _ t | j d
 g    |  _ t   |  _ t   |  _ t   |  _ d  S(   NR   t   type_aliasest   type_mappingst   type_headerst   type_macrost   type_func_suffixest   type_literal_suffixest   type_math_macro_suffixesR   R   (   R   t   Nonet   popR   t   dictR   R   t   itemsR   R   R   R   R   R   t   superR   t   __init__t   _kft   gett   known_functionst   sett   _dereferencet   headerst	   librariest   macros(   R   t   settings(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR      s,    !!!!!!!!c         C  s   | d S(   Ni   (    (   R   t   p(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _rate_index_position   s    c         C  s   | j  d  r | S| d S(   s@    Get code string as a statement - i.e. ending with a semicolon. t   ;(   t   endswith(   R   t
   codestring(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _get_statement   s    c         C  s   d j  |  S(   Ns   // {0}(   t   format(   R   t   text(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _get_comment   s    c         C  s\   |  j  t } t | d | d | j | j  d t h } t |  } |  j |  j |   S(   Nt   typet   valuet   attrs(	   R   R   R
   t   evalft   decimal_digR   R   R   t   _print(   R   t   nameR   t   type_t   vart   decl(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _declare_number_const  s    -c         C  s   |  j  |  S(   N(   t   indent_code(   R   t   lines(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _format_code  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/ccode.pys	   <genexpr>  s    (   t   shapeR   (   R   t   matt   rows(    (   R   s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _traverse_matrix_indices  s    c         K  s   t  t |   j | |  S(   N(   R   R   t
   _print_Mul(   R   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR     s    c         C  s  d |  j  k r |  j |  St |  } |  j t  } | j d k ri d | j   |  j | j |  f S| j d k r d |  j	 | |  j
 | j  f S| j t j d k r |  j d k r d |  j	 | |  j
 | j  f Sd	 |  j	 | |  j
 | j  |  j
 | j  f Sd  S(
   Nt   Powis   1.0%s/%sg      ?s   %ssqrt%s(%s)i   R   s   %scbrt%s(%s)s   %spow%s(%s, %s)(   R   t   _print_FunctionR#   t   _get_func_suffixR   R4   t   uppert   parenthesizet   baset   _nsR   R   t   Onet   standard(   R   R   t   PRECt   suffix(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt
   _print_Pow  s    # % c         C  sZ   | j  \ } } | j rC | j rC d j |  j |  |  j |   S|  j | d d Sd  S(   Ns   (({}) % ({}))t   knownt   fmod(   t   argsR&   R   R   t   _print_math_func(   R   R   t   numt   den(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt
   _print_Mod#  s    "c         C  sB   t  | j  t  | j  } } |  j t  } d | | | | f S(   Ns   %d.0%s/%d.0%s(   Rb   R   t   qt   _get_literal_suffixR   (   R   R   R   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_Rational*  s    c         C  s`  t  | j d t j  } t  | j d d   } | j } | d  k sQ t | t  r| j } t j	 } t
   } | d k s | d  k r t t | j   } | d  d  d  } n | d k r t | j  } n  x) | D]! }	 | | f 7} | | |	 9} q W| } n  t g  t | |  D] }
 |
 d |
 d ^ q | } d |  j | j j  |  j |  f S(	   Nt   offsett   stridesR   iR   i    i   s   %s[%s](   t   getattrR   R   t   ZeroR   t   indicest
   isinstanceR   R   R   t   tuplet   reversedR   t   rankt   sumt   zipR   t   label(   R   R   R   R   R   t   dimst   shiftt   tempt	   traversalR   R'   t
   flat_index(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_Indexed/  s&    					8c         C  s   |  j  | j  S(   N(   R   R   (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt
   _print_IdxG  s    c         C  s   t  t |   j |  S(   N(   R   R   t   _print_NumberSymbol(   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR  J  s    c         C  s   d S(   Nt   HUGE_VAL(    (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_InfinityN  s    c         C  s   d S(   Ns	   -HUGE_VAL(    (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_NegativeInfinityQ  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(   Nis   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) {t   }s   
s   ((%s) ? (
%s
)
s   : (
%s
)s   : t    t   )(   R   t   condt   Truet
   ValueErrort   hasR   t	   enumeratet   appendR   t   lent   joinR   (	   R   R   R   R   t   et   ct   code0t   ecpairst	   last_line(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_PiecewiseT  s$    "%?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  R   (   R   R   R  t
   _piecewise(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt
   _print_ITEu  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  R   R   R   (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_MatrixElementz  s    c         C  s@   t  t |   j |  } | |  j d k r8 d j |  S| Sd  S(   NR   s   (*{0})(   R   R   t   _print_Symbolt	   _settingsR   (   R   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR%  ~  s    c         C  s@   |  j  | j  } |  j  | j  } | j } d j | | |  S(   Ns   {0} {1} {2}(   R   t   lhst   rhst   rel_opR   (   R   R   t   lhs_codet   rhs_codet   op(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.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  R   (   R   R   R-   R.  R  R  (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_sinc  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   targett   startt   stopt   stept   body(	   R   R2  R   t   iterableR%   R   t   NotImplementedErrorR6  R   (   R   R   R2  R3  R4  R5  R6  (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt
   _print_For  s    	c         C  s   d j  |  j | j d   S(   Ns   ((({0}) > 0) - (({0}) < 0))i    (   R   R   R   (   R   t   func(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_sign  s    c           s;   d  j  k r  j |  S   f d       | j  S(   NRO   c           s]   t  |   d k r#  j |  d  St  |   d } d i   |  |   d 6  |  |  d 6S(   Ni   i    i   s!   ((%(a)s > %(b)s) ? %(a)s : %(b)s)t   at   b(   R  R   (   R   t   half(   t   inner_print_maxR   (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR?    s    (   R   R   R   (   R   R   (    (   R?  R   s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt
   _print_Max  s    c           s;   d  j  k r  j |  S   f d       | j  S(   NRQ   c           s]   t  |   d k r#  j |  d  St  |   d } d i   |  |   d 6  |  |  d 6S(   Ni   i    i   s!   ((%(a)s < %(b)s) ? %(a)s : %(b)s)R<  R=  (   R  R   (   R   R>  (   t   inner_print_minR   (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyRA    s    (   R   R   R   (   R   R   (    (   RA  R   s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt
   _print_Min  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 linesR)   s      t   {t   (s   {
s   (
R  R  s    	i    s   
s   %s%s(   RC  RD  s   {
s   (
(   R  R  (   R   R   R   t
   splitlinesR  R  t   lstripRb   t   anyt   mapR   t
   startswithR  R  (   R   t   codet
   code_linest   tabt	   inc_tokent	   dec_tokent   linet   increaset   decreaset   prettyt   levelt   n(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR     s&    "11c         C  s   |  j  |  j j | |  S(   N(   R   R   R   (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR     s    c         C  s   |  j  |  j j | |  S(   N(   R   R   R   (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR     s    c         C  s=   |  j  j | |  } |  j j | d  } |  j j | |  S(   NR)   (   R   R   R   (   R   R   t   aliast   dflt(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR     s    c         C  sc   |  j  j |  j j | t     |  j j |  j j | t     |  j |  j j | | j	   S(   N(
   R   t   updateR   R   R   R   R   R   R   R   (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_Type  s    ""c         C  sp  d d l  m } | j } | j } | j t k r@ t d   n  t | t  r d j	 d t
 | j k rm d n d d |  j | j  d	 t | j k r d
 n d d | | j k r d n d d |  j | j   } ns t | t  r0d j	 d t
 | j k r d n d d |  j | j  d |  j | j   } n t d t |    | d  k rl| d |  j |  7} n  | S(   Ni(   Rt   s$   C does not support untyped variabless   {vc}{t} *{pc} {r}{s}t   vcs   const R)   t   tt   pcs    constt   rs	   restrict t   ss   {vc}{t} {s}s   Unknown type of var: %ss    = %s(   t   sympy.codegen.cnodesRt   t   variableR   R   R    R  R   R	   R   R   R   R   R   t   symbolR
   R8  R   (   R   R   Rt   R   t   valt   result(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_Declaration  s*    				c         C  s   |  j  j t t  } |  j j |  j j | t     |  j |  } t | j	 | j
   } d | k r d | k r | d 7} n  | j d  } | d j d  | d <| d j d  r | d c d 7<n  d j |  | S(   NR  t   .s   .0i    t   0(   R   R   R   R   RW  R   R   R   t   strR   R   t   splitt   rstripR   R  (   R   t   fltR   R   R   t	   num_parts(    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_Float  s    "R   c         C  s   d S(   Nt   true(    (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_BooleanTrue  s    c         C  s   d S(   Nt   false(    (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_BooleanFalse  s    c           s   | j  d  k rT | j d  k r- t d   n  d j t   f d   | j   } nf t g  t | j | j   D] \ } } | | ^ qm  } | j d  k r | | j 7} n    j	 |  } d j
 d   j	 | j  d |  S(   Ns%   Expected strides when offset is givens   ][c           s     j  |   S(   N(   R   (   t   arg(   R   (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR(     R)   s   {symb}[{idxs}]t   symbt   idxs(   R   R   R   R  R  RH  R   R   R   R   R   R`  (   R   t   elemRr  R   R]  t
   global_idx(    (   R   s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_Element  s    8	c         C  s5   d j  g  | j D] } |  j |  j |   ^ q  S(   s0    Elements of code blocks printed as statements. s   
(   R  R   R   R   (   R   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_CodeBlock'  s    c           s"   d j  | j d   f d      S(   Ns    while ({condition}) {{
{body}
}}t   applyc           s     j  |   S(   N(   R   (   Rp  (   R   (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR(   -  R)   (   R   R   (   R   R   (    (   R   s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_While+  s    c         C  s   d |  j  | j  S(   Ns   {
%s
}(   Rv  R6  (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_Scope/  s    s   stdio.hc           s@   d j  d   j | j  d d j t   f d   | j    S(   Ns   printf({fmt}, {pargs})t   fmtt   pargss   , c           s     j  |   S(   N(   R   (   Rp  (   R   (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR(   6  R)   (   R   R   t   format_stringR  RH  t
   print_args(   R   R   (    (   R   s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_Print2  s    	c           sW   d j  t   f d   | j   } d t t   f d   | j | j f   | f S(   Ns   , c           s     j  t |    S(   N(   R   R   (   Rp  (   R   (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR(   :  R)   s	   %s %s(%s)c           s     j  |   S(   N(   R   (   Rp  (   R   (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR(   =  R)   (   R  RH  t
   parametersR   t   return_typeR   (   R   R   t   pars(    (   R   s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_FunctionPrototype9  s
    c         C  s    d |  j  |  |  j |  f S(   Ns   %s%s(   R  Ry  (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_FunctionDefinitionA  s    c         C  s   | j  \ } d |  j |  S(   Ns	   return %s(   R   R   (   R   R   Rp  (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_ReturnE  s    c           s&   d d j  t   f d   | j   S(   Ns   (%s)s   , c           s     j  |   S(   N(   R   (   Rp  (   R   (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR(   J  R)   (   R  RH  R   (   R   R   (    (   R   s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_CommaOperatorI  s    c         C  s   d t  |  S(   Ns   %s:(   Rf  (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_LabelL  s    c         C  s   d | j  S(   Ns   goto %s(   R   (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_gotoO  s    c         C  s   | j  \ } d |  j |  S(   Ns   ++(%s)(   R   R   (   R   R   Rp  (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_PreIncrementR  s    c         C  s   | j  \ } d |  j |  S(   Ns   (%s)++(   R   R   (   R   R   Rp  (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_PostIncrementV  s    c         C  s   | j  \ } d |  j |  S(   Ns   --(%s)(   R   R   (   R   R   Rp  (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_PreDecrementZ  s    c         C  s   | j  \ } d |  j |  S(   Ns   (%s)--(   R   R   (   R   R   Rp  (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_PostDecrement^  s    c         C  sU   d t  d | j j d | j d d j g  | j D] } |  j |  ^ q. d g   S(   Ns!   %(keyword)s %(name)s {
%(lines)s}t   keywordR   R   s   ;
R)   (   R   t	   __class__t   __name__R   R  t   declarationsR   (   R   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_structb  s    	c         C  s   d S(   NRS   (    (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_BreakTokenh  s    c         C  s   d S(   NRW   (    (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_ContinueTokenk  s    N(`   R  t
   __module__t   __doc__t   printmethodt   languageR   R   t   reserved_wordsR   R  t   Falset   _default_settingsR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   known_functions_C89R   R   R   R   R   R   R   R   R   R   R   R   R   R  R  R  R
  R  R  R   R$  R%  R-  R1  R9  R;  R@  RB  R   R   R   R   RX  Rc  Rk  R"   Rm  Ro  Ru  Rv  Rx  Ry  R~  R  R  R  R  R  R  R  R  R  R  R  R  R  t   _print_union(    (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR      s   














													!																																	t   _C9XCodePrinterc           B  s   e  Z d    Z RS(   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(   Ns8   for (int %(var)s=%(start)s; %(var)s<%(end)s; %(var)s++){R   R3  i   t   endR  (   R  R   R   t   lowerR   (   R   R   t
   open_linest   close_linest	   loopstartR   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _get_loop_opening_endingt  s    (   R  R  R  (    (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR  r  s   t   last_supported_versions   1.0t
   useinsteadsA   C89CodePrinter or C99CodePrinter, e.g. ccode(..., standard='C99')t   issuei/  t   deprecated_since_versions   1.1t   CCodePrinterc           B  s   e  Z d  Z e Z RS(   sQ   
    Deprecated.

    Alias for C89CodePrinter, for backwards compatibility.
    (   R  R  R  t   _known_functions_C9XR   (    (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR    s   
t   C99CodePrinterc           B  s   e  Z d  Z e e e  Z e e e j	 j
   i d e 6d e 6j
     Z	 e e e j j
   i d h e 6d h e 6j
     Z e Z d j   Z d   Z d   Z d   Z e d d	 h d
 d h  e e d d    Z d   Z d   Z RS(   t   C99s   float complexs   double complexs	   complex.hs  fabs fmod remainder remquo fma fmax fmin fdim nan exp exp2 expm1 log log10 log2 log1p pow sqrt cbrt hypot sin cos tan asin acos atan atan2 sinh cosh tanh asinh acosh atanh erf erfc tgamma lgamma ceil floor trunc round nearbyint rint frexp ldexp modf scalbn ilogb logb nextafter copysignc         C  s   d S(   Nt   INFINITY(    (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR
    s    c         C  s   d S(   Ns	   -INFINITY(    (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR    s    c         C  s   d S(   Nt   NAN(    (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt
   _print_NaN  s    R   s   math.hR   t   mc   
        s  | d  k r"   j | j j } n  t | t  sp x< | D]% \ } } | | j   r8 | } Pq8 q8 Wt d   n  y |   | j  SWn< t k
 r   j	 |   j
 k r   j t  n d } n X| r  j | j d  } t | j  d k rd } xV | j d d !D]D }	 | d 7} | d j d   j	 d	 | d
 | d   j |	   7} qW| d   j | j | j d   | f 7} qn$ d j t   f d   | j   } d j d   j	 d	 | d
 | d |  S(   Ns   No matching printerR)   i    i   iR  s   , {ns}{name}{suffix}({next}t   nsR   R   t   nexts   , %s%ss   , c           s     j  |   S(   N(   R   (   Rp  (   R   (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR(     R)   s   {ns}{name}{suffix}({args})R   (   R   R   R  R  R   R   R   R  t	   TypeErrorR   t   _prec_funcsR   R   R   R  R   R:  R  RH  (
   R   R   t   nestR   t   cbR   R   R   t
   paren_pilet   curr_arg(    (   R   s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR     s@    /
	$		c         C  s   |  j  | d t S(   NR  (   R   R  (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR@    s    c         C  s   |  j  | d t S(   NR  (   R   R  (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyRB    s    N(   R  R  R   R   R  t   reserved_words_c99R   R   R   R   R   R   R   R   t   known_functions_C99R   Rg  R  R
  R  R  R"   R   R  R   R   R@  RB  (    (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR    s$   
			(	s   Abs Sqrt exp exp2 expm1 log log10 log2 log1p Cbrt hypot fma loggamma sin cos tan asin acos atan atan2 sinh cosh tanh asinh acosh atanh erf erfc loggamma gamma ceiling floors	   _print_%st   C11CodePrinterc           B  s#   e  Z e d  d h  d    Z RS(   R   s
   stdalign.hc         C  s   | j  \ } d |  j |  S(   Ns   alignof(%s)(   R   R   (   R   R   Rp  (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   _print_alignof  s    (   R  R  R"   R  (    (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyR    s   t   c89t   c99t   c11c         K  s    t  | j   |  j |  |  S(   s  Converts an expr to a string of c 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.
    standard : str, optional
        String specifying the standard. If your compiler supports a more modern
        standard you may set this to 'c99' to allow the printer to use more math
        functions. [default='c89'].
    precision : integer, optional
        The precision for numbers such as pi [default=17].
    user_functions : dict, optional
        A dictionary where the keys are string representations of either
        ``FunctionClass`` or ``UndefinedFunction`` instances and the values
        are their desired C string representations. Alternatively, the
        dictionary value can be a list of tuples i.e. [(argument_test,
        cfunction_string)] or [(argument_test, cfunction_formater)]. See below
        for examples.
    dereference : iterable, optional
        An iterable of symbols that should be dereferenced in the printed code
        expression. These would be values passed by address to the function.
        For example, if ``dereference=[a]``, the resulting code would print
        ``(*a)`` instead of ``a``.
    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 ccode, symbols, Rational, sin, ceiling, Abs, Function
    >>> x, tau = symbols("x, tau")
    >>> expr = (2*tau)**Rational(7, 2)
    >>> ccode(expr)
    '8*M_SQRT2*pow(tau, 7.0/2.0)'
    >>> ccode(expr, math_macros={})
    '8*sqrt(2)*pow(tau, 7.0/2.0)'
    >>> ccode(sin(x), assign_to="s")
    's = sin(x);'
    >>> from sympy.codegen.ast import real, float80
    >>> ccode(expr, type_aliases={real: float80})
    '8*M_SQRT2l*powl(tau, 7.0L/2.0L)'

    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')
    >>> ccode(func(Abs(x) + ceiling(x)), standard='C89', user_functions=custom_functions)
    'f(fabs(x) + CEIL(x))'

    or if the C-function takes a subset of the original arguments:

    >>> ccode(2**x + 3**x, standard='C99', 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(ccode(expr, tau, standard='C89'))
    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]))
    >>> ccode(e.rhs, assign_to=e.lhs, contract=False, standard='C89')
    '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(ccode(mat, A, standard='C89'))
    A[0] = pow(x, 2);
    if (x > 0) {
       A[1] = x + 1;
    }
    else {
       A[1] = x;
    }
    A[2] = sin(x);
    (   t   c_code_printersR  t   doprint(   R   t	   assign_toR   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   ccode  s    c         K  s   t  t |  |   d S(   s0   Prints C representation of the given expression.N(   t   printR  (   R   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   print_ccodez  s    N(H   R  t
   __future__R    R   t	   functoolsR   t	   itertoolsR   t
   sympy.coreR   t   sympy.core.compatibilityR   R   t   sympy.core.decoratorsR   t   sympy.codegen.astR   R	   R
   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R    t   sympy.printing.codeprinterR!   R"   t   sympy.printing.precedenceR#   R$   t   sympy.sets.fancysetsR%   R  R   R  R   R  R  R  R   R   R   t   objectR  R  R  Rg  t   kt   setattrR   R  R  R   R  R  (    (    (    s3   lib/python2.7/site-packages/sympy/printing/ccode.pyt   <module>   s   
			%	 		P
