
~9\c           @  sZ  d  Z  d d l m Z m Z d d l m Z d d l m Z d d l 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 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' d d l( m) Z) m* Z* m+ Z+ m, Z, m- Z- d d	 l. m/ Z/ m0 Z0 d d
 l1 m2 Z2 d d l3 m4 Z4 d d l5 m6 Z6 d d l7 m8 Z8 d d l9 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$ 6Z> d% e8 f d&     YZ? e@ d'  ZA d(   ZB d S()   sq  
Fortran code printer

The FCodePrinter converts single sympy expressions into single Fortran
expressions, using the functions defined in the Fortran 77 standard where
possible. Some useful pointers to Fortran can be found on wikipedia:

https://en.wikipedia.org/wiki/Fortran

Most of the code below is based on the "Professional Programmer's Guide to
Fortran77" by Clive G. Page:

http://www.star.le.ac.uk/~cgp/prof77.html

Fortran is a case-insensitive language. This might cause trouble because
SymPy is case sensitive. So, fcode adds underscores to variable names when
it is necessary to make them different for Fortran.
i(   t   print_functiont   division(   t   defaultdict(   t   chainN(   t
   Assignmentt   Declarationt   Pointert   value_constt   float32t   float64t   float80t	   complex64t
   complex128t   int8t   int16t   int32t   int64t   intct   realt   integert   bool_t   complex_(   t   allocatablet   isignt   dsignt   cmplxt   merget
   literal_dpt	   elementalt   puret	   intent_int
   intent_outt   intent_inout(   t   St   Addt   Nt   Floatt   Symbol(   t   string_typest   range(   t   Function(   t   Eq(   t   Range(   t   CodePrinter(   t
   precedencet
   PRECEDENCE(   t   printer_contextt   sint   cost   tant   asint   acost   atant   atan2t   sinht   cosht   tanht   logt   expt   erft   abst   Abst   conjgt	   conjugatet   maxt   Maxt   mint   Mint   FCodePrinterc           B  sL  e  Z d  Z d Z d Z 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 6Z i i d d 6e 6Z i
 dg d 6d d 6d d 6i  d 6e d 6e d 6d d 6e d 6d d 6e d 6Z i d d 6d  d! 6d" d# 6d$ d% 6d& d' 6Z i d( d) 6Z i  d*  Z e d+    Z d,   Z d-   Z  d.   Z! d/   Z" d0   Z# d1   Z$ d2   Z% d3   Z& d4   Z' d5   Z( d6   Z) d7   Z* d8   Z+ d9   Z, d:   Z- d;   Z. d<   Z/ d=   Z0 d>   Z1 d?   Z2 d@   Z3 dA   Z4 dB   Z5 dC   Z6 dD   Z7 dE   Z8 dF   Z9 dG   Z: dH   Z; dI   Z< dJ   Z= dK   Z> dL   Z? dM   Z@ dN   ZA dO   ZB dP   ZC dQ   ZD dR   ZE dS   ZF dT   ZG dU   ZH dV   ZI dW   ZJ dX   ZK dY   ZL dZ   ZM d[   ZN d\   ZO d]   ZP d^   ZQ d_   ZR d`   ZS da   ZT db   ZU dc   ZV dd   ZW de   ZX df   ZY RS(h   sA   A printer to convert sympy expressions to strings of Fortran codet   _fcodet   Fortrans   integer(c_int)s   real*4s   real*8s   real*10s	   complex*8s
   complex*16s	   integer*1s	   integer*2s	   integer*4s	   integer*8t   logicalt   c_intt   iso_c_bindingt   ordert   autot	   full_preci   t	   precisiont   user_functionst   humant   allow_unknown_functionst   fixedt   source_formatt   contractiM   t   standardt   name_manglings   .and.t   ands   .or.t   ors   .neqv.t   xors   .eqv.t
   equivalents   .not. t   nots   /=s   !=c         C  s  i  |  _  g  |  _ t t |  j j   | j d i   j     |  _ t t |  j j   | j d i   j     |  _ t t	 |   j
 |  t t  |  _ | j d i   } |  j j |  d d d d d d	 h } |  j d
 | k rt d |  j d
   n  t t  |  _ d  S(   Nt   type_aliasest   type_mappingsRN   iB   iM   iZ   i_   i  i  RT   s   Unknown Fortran standard: %s(   t   mangled_symbolst	   used_namet   dictR   R[   t   itemst   popR\   t   superRD   t   __init__t   known_functionst   gett   updatet	   _settingst
   ValueErrorR   t   sett   module_uses(   t   selft   settingst	   userfuncst	   standards(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyRc   x   s    		!!c         C  ss   |  j  d d k r, i d d 6d d 6d d 6S|  j  d d	 k rX i d
 d 6d d 6d d 6St d |  j  d   d  S(   NRR   RQ   s         t   codes        @ t   conts   C     t   commentt   freet    s   ! s   Unknown source format: %s(   Rg   Rh   (   Rk   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _lead   s
    c         C  s   |  j  d t k r | |  j k r | j } x# | j   |  j k rP | d 7} q. W|  j j | j    | | j k r | |  j | <q t |  |  j | <n  | j |  j  } n  t	 t
 |   j |  } | S(   NRU   t   _(   Rg   t   TrueR]   t   namet   lowerR^   t   appendR%   t   xreplaceRb   RD   t   _print_Symbol(   Rk   t   exprRw   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR{      s    	c         C  s	   | d S(   Ni   (    (   Rk   t   p(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _rate_index_position   s    c         C  s   | S(   N(    (   Rk   t
   codestring(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _get_statement   s    c         C  s   d j  |  S(   Ns   ! {0}(   t   format(   Rk   t   text(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _get_comment   s    c         C  s   d j  | |  j |   S(   Ns   parameter ({0} = {1})(   R   t   _print(   Rk   Rw   t   value(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _declare_number_const   s    c         C  s6   |  j  j | t | j |  j d   f  t |  S(   NRM   (   t   _number_symbolst   addR$   t   evalfRg   t   str(   Rk   R|   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_NumberSymbol   s    ,c         C  s   |  j  |  j |   S(   N(   t   _wrap_fortrant   indent_code(   Rk   t   lines(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.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   jt   i(   t   rows(    s3   lib/python2.7/site-packages/sympy/printing/fcode.pys	   <genexpr>   s    (   t   shapeR'   (   Rk   t   matt   cols(    (   R   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _traverse_matrix_indices   s    c         C  s   g  } g  } xj | D]b } t  |  j | j | j d | j d g  \ } } } | j d | | | f  | j d  q W| | f S(   Ni   s   do %s = %s, %ss   end do(   t   mapR   t   labelRx   t   upperRy   (   Rk   t   indicest
   open_linest   close_linesR   t   vart   startt   stop(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _get_loop_opening_ending   s    	,c         C  s   d d l  m } | j \ } | j rL t d t d |  t | d   } n | j r t t t	 d  t	 d   | | |  t | |  t	 d    } n6 t t	 d  t
 t	 d  |  t | t	 d    } |  j |  S(   Ni(   R=   i    i   (   t   sympyR=   t   argst
   is_integerR   R   R)   t
   is_complexR   R   R   R   (   Rk   R|   R=   t   argt   new_expr(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_sign   s    	'	I6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 |   qJ W| j d  d	 j
 |  S|  j d
 d k rd } |  j | j  d j  } t | j  d   } xX | r| j   \ } } |  j |  } |  j |  }	 | j d | d | d |	  } qPW| St d   d  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) theni   t   elses   else if (%s) thens   end ifs   
RT   i_   s   merge({T}, {F}, {COND})t   Tt   Ft   CONDsm   Using Piecewise as an expression using inline operators is not supported in standards earlier than Fortran95.(   R   t   condRv   Rh   t   hasR   t	   enumerateRy   R   t   lent   joinRg   R|   t   listRa   R   t   NotImplementedError(
   Rk   R|   R   R   t   et   ct   patternRo   t   termsR   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Piecewise   s0    "%	"c         C  s:   d j  |  j | j t d d t | j d | j d  S(   Ns   {0}({1}, {2})t   Atomt   stricti   (   R   t   parenthesizet   parentR-   Rv   R   R   (   Rk   R|   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_MatrixElement   s    c   
      C  sv  g  } g  } g  } xb | j  D]W } | j rD | j rD | j |  q | j rf | j rf | j |  q | j |  q W| rb| r+t |  } t |   } |  j |  } | j d  r d }	 | d } n d }	 t |  | k  r d | } n  d |  j t |    |  j t	 j
 t |    |	 | f Sd |  j t |    |  j t	 j
 t |    f Sn t j |  |  Sd  S(   Nt   -i   t   +s   (%s)s   cmplx(%s,%s) %s %ss   cmplx(%s,%s)(   R   t	   is_numbert   is_realRy   t   is_imaginaryR,   R"   R   t
   startswithR!   t   ImaginaryUnitR+   t
   _print_Add(
   Rk   R|   t	   pure_realt   pure_imaginaryt   mixedR   t   PRECt   termt   tt   sign(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     s8    "c         C  sz   |  j  d } g  | j D] } t | |  ^ q } | j |   } t | t  s] |  j |  St j |  | j |    Sd  S(   NRM   (	   Rg   R   R#   t   funct
   isinstanceR(   R   R+   t   _print_Function(   Rk   R|   t   prect   aR   t	   eval_expr(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR   )  s    %c         C  s]   |  j  d d k r( d } t |   n1 | j \ } } d j |  j |  |  j |   Sd  S(   NRT   iB   iM   s]   Python % operator and SymPy's Mod() function are not supported by Fortran 66 or 77 standards.s         modulo({}, {})(   iB   iM   (   Rg   R   R   R   R   (   Rk   R|   t   msgt   xt   y(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt
   _print_Mod3  s
    c         C  s   d S(   Ns
   cmplx(0,1)(    (   Rk   R|   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_ImaginaryUnit@  s    c         C  s
   t  |  S(   N(   R   (   Rk   R|   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt
   _print_intD  s    c         C  s?   | j  r+ | j r+ d |  j t j |  St j |  |  Sd  S(   Ns   cmplx(0,%s)(   R   R   R   R!   R   R+   t
   _print_Mul(   Rk   R|   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR   G  s    c         C  s   t  |  } | j d k rG d |  j t d   |  j | j |  f S| j d k r | j j r | j j r d |  j | j  Sd |  j | j  Sq d |  j | j  Sn t j	 |  |  Sd  S(   Nis   %s/%si   g      ?s   sqrt(%s.0d0)s   sqrt(dble(%s))s   sqrt(%s)(
   R,   R:   R   R   R   t   baseR   t	   is_NumberR+   t
   _print_Pow(   Rk   R|   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR   P  s    c         C  s-   t  | j  t  | j  } } d | | f S(   Ns   %d.0d0/%d.0d0(   t   intR}   t   q(   Rk   R|   R}   R   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Rationalc  s    c         C  sO   t  j |  |  } | j d  } | d k rG d | |  | | d f Sd | S(   NR   is   %sd%si   s   %sd0(   R+   t   _print_Floatt   find(   Rk   R|   t   printedR   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR   g  s
    c         C  sK   g  | j  D] } |  j |  ^ q
 } d |  j | j j  d j |  f S(   Ns   %s(%s)s   , (   R   R   R   R   R   (   Rk   R|   R   t   inds(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Indexedn  s    %c         C  s   |  j  | j  S(   N(   R   R   (   Rk   R|   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt
   _print_Idxr  s    c           sX     j  | j  }   j  | j  }   j d j t   f d   | | j | g     S(   Ns   {0} = {0} {1} {2}c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   <lambda>y  Rs   (   R   t   lhst   rhsR   R   R   t   binop(   Rk   R|   t   lhs_codet   rhs_code(    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_AugmentedAssignmentu  s
    c         C  s   |  j  | j  } | j d  k r> | d |  j  | j  7} n  | j d  k rj | d |  j  | j  7} n  d | j j j d  | f S(   Ns   , s   , mask=s   %s(%s)Ru   (   R   t   arrayt   dimt   Nonet   maskt	   __class__t   __name__t   rstrip(   Rk   t   smt   params(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_sum_|  s    c         C  s   |  j  |  S(   N(   R   (   Rk   t   prod(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_product_  s    c           sy   d g } | j  d k r. | j d  d } n d } d | d j d | j rT d n d | j d	   f d
   d |   S(   Nt
   concurrenti   t   stepRs   s   , {step}s*   do {concurrent}{counter} = {first}, {last}s   
{body}
end do
s   concurrent t   applyc           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   t   exclude(   R   Ry   R   R   t   kwargs(   Rk   t   dot   exclR   (    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt	   _print_Do  s    		c           sE   | j  d k r d n d } d | d j | j d   f d      S(   Ni   Rs   s   , {step}s$   ({expr}, {counter} = {first}, {last}t   )R   c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   R   R   R   (   Rk   t   idlR   (    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_ImpliedDoLoop  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 Ranges3   do {target} = {start}, {stop}, {step}
{body}
end dot   targetR   R   R   t   body(	   R   R  R   t   iterableR*   R   R   R  R   (   Rk   R|   R  R   R   R   R  (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt
   _print_For  s    	c           s4   | j  \ } } d j t   f d   | | f   S(   Ns    == c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   R   R   R   (   Rk   R|   R   R   (    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Equality  s    c           s4   | j  \ } } d j t   f d   | | f   S(   Ns    /= c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   R   R   R   (   Rk   R|   R   R   (    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Unequality  s    c         C  sw   |  j  j | |  } |  j j | | j  } |  j j |  } | rs x+ | D]  \ } } |  j | j |  qL Wn  | S(   N(   R[   Re   R\   Rw   t   type_modulesRj   R   (   Rk   t   type_t   type_strRj   t   kt   v(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Type  s    c           s@   d j  d   j | j  d d j t   f d   | j    S(   Ns   {symbol}({idxs})t   symbolt   idxss   , c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   R   R   R  R   R   R   (   Rk   t   elem(    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Element  s    	c         C  s
   t  |  S(   N(   R   (   Rk   t   ext(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Extent  s    c           s  | j  } | j } | j d  } g  t t t f D] } | | j k ^ q1 } | j t  d k rj d } nH | j t  d k r d d d d g | j	 t  } n t
 d	     t | t  r t d
   n    j d d k rd j d   j | j  d t | j k rd n d d | rDd d j t   f d   |   n d d | d t | j k red n d d   j | j   } | d  k r| d   j |  7} qnQ t | j k s| rt d   n  d j t   f d   | j | j g   } | S(   Nt	   dimensioni    Rs   i   s   , intent(%s)t   int   outt   inouts!   Multiple intents specified for %ss1   Pointers are not available by default in Fortran.RT   iZ   s"   {t}{vc}{dim}{intent}{alloc} :: {s}R   t   vcs   , parameterR   s   , dimension(%s)s   , c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   t   intentt   allocs   , allocatablet   ss    = %ss0   F77 init./parameter statem. req. multiple lines.t    c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   t   variableR   t   attr_paramsR   R   R    t   attrst   countRv   t   indexRh   R   R   R   Rg   R   R   t   typeR   R   R   R   R  R   (   Rk   R|   R   t   valR   R  t   intentst   result(    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Declaration  s2    		+	#	1-c         C  s   d |  j  t d   S(   Ns   (huge(%s) + 1)i    (   R   R   (   Rk   R|   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Infinity  s    c           s"   d j  | j d   f d      S(   Ns$   do while ({condition})
{body}
end doR   c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   R   R   (   Rk   R|   (    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_While  s    c         C  s   d S(   Ns   .true.(    (   Rk   R|   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_BooleanTrue  s    c         C  s   d S(   Ns   .false.(    (   Rk   R|   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_BooleanFalse  s    c         C  sg   g  } xZ | D]R } | j  d  rG | j |  j d | d j    q | j |  j d |  q W| S(   Nt   !Rq   i   Ro   (   R   Ry   Rt   t   lstrip(   Rk   R   R'  t   line(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _pad_leading_columns  s    %c           sd  t  d t j t j    t  d      f d   } g  } |  j d d k rZ d } n d } x| D]} | j |  j d  rzt |  d	 k rj| j d
 d d	  } | d k r d	 } n  | |  } | | j	   } | j
 |  x | rf| j d
 d d  } | d k s"t |  d k  r+d } n  | |  } | | j	   } | j
 d |  j d | f  q Wq\| j
 |  qg | j |  j d  rO| | d	  } | |  j   } | | j	   } | r| | 7} n  | j
 |  xz | rK| | d  } | |  j   } | | j	   } | r*| | 7} n  | j
 d |  j d | f  qWqg | j
 |  qg W| S(   s   Wrap long Fortran lines

           Argument:
             lines  --  a list of lines (without \n character)

           A comment line is split at white space. Code lines are split with a more
           complex rule to give nice results.
        s   _+-.s    	()c           sh   t     | k r t     S| }     f d   } x* | |  sc | d 8} | d k r: | Sq: W| S(   Nc           s     |   k r$   |  d  k p   |   k rH   |  d  k p   |   k rl   |  d  k p   |   k o   |  d  k S(   Ni   (    (   t   pos(   R/  t   my_alnumt   my_white(    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     s    $$$i   i    (   R   (   R/  t   endposR1  t   split(   R2  R3  (   R/  s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   split_pos_code  s    

RR   Rr   s    &Rs   Rq   iH   R  i   ii    iB   s   %s%sRo   iA   Rp   (   Ri   t   stringt   digitst   ascii_lettersRg   R   Rt   R   t   rfindR.  Ry   R   (   Rk   R   R6  R'  t   trailingR/  R1  t   hunk(    (   R2  R3  s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     sP    
		
		
%	%c         C  s#  t  | t  r4 |  j | j t   } d j |  S|  j d d k } g  | D] } | j d  ^ qN } d } d } g  | D]$ } t t	 t
 | j |    ^ q| } g  | D]$ } t t	 t
 | j |    ^ q } g  | D]* } t t	 t
 | j d d g    ^ q }	 d }
 d } d } g  } x t |  D] \ } } | d k sW| d k rj| j |  q3n  |
 | | 8}
 | rd |
 | | } n d |
 | } d | | f } | s|  j | g  d } n  | j |  |	 | rd | } n d } |
 | | 7}
 q3W| s|  j |  S| S(   s0   Accepts a string of code or a list of code linesRs   RR   Rr   s    	s   do s   if(s   if s   do
R   t   programt	   interfaces   end dot   enddos   end ift   endifs   end programs   end interfacet   &s   &
i    i   s   
R  s   %s%si   (   s   do s   if(s   if s   do
R   R=  R>  (   s   end doR?  s   end ifR@  R   s   end programs   end interface(   R   R&   R   t
   splitlinesRv   R   Rg   R.  R   t   anyR   R   t   endswithR   Ry   R0  R   (   Rk   Ro   t
   code_linesRr   R/  t   inc_keywordt   dec_keywordt   increaset   decreaset   continuationt   levelt   cont_paddingt   tabwidtht   new_codeR   t   padding(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR   B  sH    "..4
c           sj   | j  rI d j d d j t   f d   | j   d   j | j    S| j \ } d   j |  Sd  S(   Ns   go to ({labels}), {expr}t   labelss   , c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR   w  Rs   R|   s   go to %s(   R|   R   R   R   RP  R   (   Rk   t   gotot   lbl(    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_GoTot  s    		$c           s"   d j  | j d   f d      S(   Ns"   program {name}
{body}
end program
R   c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   R   R   (   Rk   t   prog(    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Program~  s    c           s"   d j  | j d   f d      S(   NsA   module {name}
{declarations}

contains

{definitions}
end module
R   c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   R   R   (   Rk   t   mod(    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Module  s    c         C  s   | j  d k r: |  j d d k r: |  j d j d  d S| j  d k rt |  j d d k rt |  j d j d  d	 S| j  d k r d
 S| j  Sd  S(   Nt   stdoutRT   i  RI   s   stdint=>input_unitt
   input_unitt   stderrs   stdint=>error_unitt
   error_unitt   *(   Rw   Rg   Rj   R   (   Rk   t   strm(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Stream  s    ""c           s^   | j  d  k r$   j | j   } n d } d j d | d d j t   f d   | j    S(   NR\  s   print {fmt}, {iolist}t   fmtt   iolists   , c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   t   format_stringR   R   R   R   R   t
   print_args(   Rk   t   psR_  (    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Print  s
    c         C  s:   | j  \ } d j d |  j j d d  d |  j |   S(   Ns   {result_name} = {arg}t   result_namet   sympy_resultR   (   R   R   t   _contextRe   R   (   Rk   t   rsR   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Return  s    	c         C  s+   | j  \ } | r# d |  j |  Sd Sd  S(   Ns	   return %st   return(   R   R   (   Rk   t   frsR   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_FortranReturn  s    c           s   | j  d  } | d  k r$ d } n | r8 d | d n d }   j j d d   } d j d | d	   j | j  d
 d j g  | j D] }   j | j	  ^ q  d | r d | n d d | d d j t
   f d   | j    S(   Nt   bind_CRs   s    bind(C, name="%s")i    s    bind(C)Re  s<   {entity}{name}({arg_names}){result}{bind}
{arg_declarations}t   entityRw   t	   arg_namess   , R'  s    result(%s)t   bindt   arg_declarationss   
c           s     j  t |    S(   N(   R   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   R   R   Rg   Re   R   R   Rw   R   t
   parametersR  R   (   Rk   Rn  t   fpR   t   bind_C_paramsRp  Re  R   (    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _head  s    		1c         C  s7   d j  |  j | j   } d j  d |  j | |   S(   Ns   {0} function s4   interface
{function_head}
end function
end interfacet   function_head(   R   R   t   return_typeRu  (   Rk   Rs  Rn  (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_FunctionPrototype  s    	c         C  s   t  | j k r d } n t | j k r0 d } n d } d j |  j | j   } t |  d | j 9 d j d | d |  j | |  d	 |  j | j	   SWd  QXd  S(
   Ns
   elemental s   pure Rs   s   {0} function Re  s,   {prefix}{function_head}
{body}
end function
t   prefixRv  R  (
   R   R!  R   R   R   Rw  R.   Rw   Ru  R  (   Rk   t   fdRy  Rn  (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_FunctionDefinition  s    			c         C  s.   d j  d |  j d |  d |  j | j   S(   Ns(   {subroutine_head}
{body}
end subroutine
t   subroutine_heads   subroutine R  (   R   Ru  R   R  (   Rk   t   sub(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_Subroutine  s    	c           s@   d j  d   j | j  d d j t   f d   | j    S(   Ns   call {name}({args})Rw   R   s   , c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   R   R   Rw   R   R   t   subroutine_args(   Rk   t   scall(    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_SubroutineCall  s    	c           s#   d t  t   f d   | j   S(   Ns   %s => %sc           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   t   tupleR   R   (   Rk   t   rnm(    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_use_rename  s    c         C  s   d |  j  | j  } | j d  k r^ | d d j g  | j D] } |  j  |  ^ q;  7} n  | j d  k r | d d j g  | j D] } |  j  |  ^ q  7} n  | S(   Ns   use %ss   , s   , only: (   R   t	   namespacet   renameR   R   t   only(   Rk   t   useR'  R  t   nly(    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt
   _print_use  s    99c         C  s   d S(   Nt   exit(    (   Rk   Ru   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_BreakToken  s    c         C  s   d S(   Nt   cycle(    (   Rk   Ru   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_ContinueToken  s    c           sE     j  d d k r d n d } | d j t   f d   | j   S(   NRT   i  s   [%s]s   (/%s/)s   , c           s     j  |   S(   N(   R   (   R   (   Rk   (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyR     Rs   (   Rg   R   R   t   elements(   Rk   t   act   fmtstr(    (   Rk   s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   _print_ArrayConstructor  s    N(Z   R   t
   __module__t   __doc__t   printmethodt   languageR   R   R	   R   R   R   R[   R   R   R
   R   R   R   R   R   R\   R
  R   Rv   t   Falset   _default_settingst
   _operatorst   _relationalsRc   t   propertyRt   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   R   R   R  R  R  R	  R  R  R  R(  R)  R*  R+  R,  R0  R   R   RS  RU  RW  R^  Rd  Ri  Rl  Ru  Rx  R{  R~  R  R  R  R  R  R  (    (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyRD   B   s   




												*		'	
																							!							G	2	
												
					c         K  s   t  |  j |  |  S(   sH  Converts an expr to a string of fortran 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
        DEPRECATED. Use type_mappings instead. The precision for numbers such
        as pi [default=17].
    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, cfunction_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].
    source_format : optional
        The source format can be either 'fixed' or 'free'. [default='fixed']
    standard : integer, optional
        The Fortran standard to be followed. This is specified as an integer.
        Acceptable standards are 66, 77, 90, 95, 2003, and 2008. Default is 77.
        Note that currently the only distinction internally is between
        standards before 95, and those 95 and after. This may change later as
        more features are added.
    name_mangling : bool, optional
        If True, then the variables that would become identical in
        case-insensitive Fortran are mangled by appending different number
        of ``_`` at the end. If False, SymPy won't interfere with naming of
        variables. [default=True]

    Examples
    ========

    >>> from sympy import fcode, symbols, Rational, sin, ceiling, floor
    >>> x, tau = symbols("x, tau")
    >>> fcode((2*tau)**Rational(7, 2))
    '      8*sqrt(2.0d0)*tau**(7.0d0/2.0d0)'
    >>> fcode(sin(x), assign_to="s")
    '      s = 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,
    cfunction_string)].

    >>> custom_functions = {
    ...   "ceiling": "CEIL",
    ...   "floor": [(lambda x: not x.is_integer, "FLOOR1"),
    ...             (lambda x: x.is_integer, "FLOOR2")]
    ... }
    >>> fcode(floor(x) + ceiling(x), user_functions=custom_functions)
    '      CEIL(x) + FLOOR1(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(fcode(expr, tau))
          if (x > 0) then
             tau = x + 1
          else
             tau = x
          end if

    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]))
    >>> fcode(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(fcode(mat, A))
          A(1, 1) = x**2
             if (x > 0) then
          A(2, 1) = x + 1
             else
          A(2, 1) = x
             end if
          A(3, 1) = sin(x)
    (   RD   t   doprint(   R|   t	   assign_toRl   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   fcode  s    uc         K  s   t  t |  |   d S(   s|   Prints the Fortran representation of the given expression.

       See fcode for the meaning of the optional arguments.
    N(   t   printR  (   R|   Rl   (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   print_fcode  s    (C   R  t
   __future__R    R   t   collectionsR   t	   itertoolsR   R7  t   sympy.codegen.astR   R   R   R   R   R	   R
   R   R   R   R   R   R   R   R   R   R   R   t   sympy.codegen.fnodesR   R   R   R   R   R   R   R   R   R   R    t
   sympy.coreR!   R"   R#   R$   R%   t   sympy.core.compatibilityR&   R'   t   sympy.core.functionR(   t   sympy.core.relationalR)   t
   sympy.setsR*   t   sympy.printing.codeprinterR+   t   sympy.printing.precedenceR,   R-   t   sympy.printing.printerR.   Rd   RD   R   R  R  (    (    (    s3   lib/python2.7/site-packages/sympy/printing/fcode.pyt   <module>   sJ   vL(
  x