B
    }[                 @   s  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 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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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= ddl>m?Z?m@Z@ ddddddddddddddddd d!ZAG d"d# d#e<ZBd(d$d%ZCd&d' ZDdS ))ap  
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:

http://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.
    )print_functiondivision)defaultdict)chainN)SAddNFloatSymbol)string_typesrange)Function)Eq)Range)
Assignment	AttributeDeclarationPointerTypevalue_constfloat32float64float80	complex64
complex128int8int16int32int64intcrealintegerbool_complex_)allocatableisigndsigncmplxmerge
literal_dp	elementalpure	intent_in
intent_outintent_inout)printer_context)CodePrinterrequires)
precedence
PRECEDENCEsincostanasinacosatanatan2sinhcoshtanhlogexperfabsZconjgmaxmin)r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   Abs	conjugateZMaxZMinc                   s  e Zd ZdZdZdZeeee	e
eiZedede	dededed	ed
edededediZeddiiZdddi ddddddd
ZddddddZdd iZi f fd!d"	Zed#d$ Z fd%d&Zd'd( Zd)d* Zd+d, Z d-d. Z!d/d0 Z"d1d2 Z#d3d4 Z$d5d6 Z%d7d8 Z&d9d: Z'd;d< Z(d=d> Z)d?d@ Z*dAdB Z+dCdD Z,dEdF Z-dGdH Z.dIdJ Z/dKdL Z0dMdN Z1dOdP Z2dQdR Z3dSdT Z4dUdV Z5dWdX Z6dYdZ Z7d[d\ Z8d]d^ Z9d_d` Z:dadb Z;dcdd Z<dedf Z=dgdh Z>didj Z?dkdl Z@dmdn ZAdodp ZBdqdr ZCdsdt ZDdudv ZEdwdx ZFdydz ZGd{d| ZHd}d~ ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUdd ZV  ZWS )FCodePrinterzAA printer to convert sympy expressions to strings of Fortran codeZ_fcodeZFortranzinteger(c_int)zreal*4zreal*8zreal*10z	complex*8z
complex*16z	integer*1z	integer*2z	integer*4z	integer*8Zlogicaliso_c_bindingZc_intNauto   TFfixedM   )
orderZ	full_prec	precisionuser_functionsZhumanZallow_unknown_functionssource_formatZcontractstandardname_manglingz.and.z.or.z.neqv.z.eqv.z.not. )andorxorZ
equivalentnotz!=z/=c                s   i | _ g | _tt| j |di  | _tt| j |di  | _tt	| 
| tt| _|di }| j| dddddd	h}| jd
 |krtd| jd
  tt| _d S )Ntype_aliasestype_mappingsrN   B   rK   Z   _   i  i  rP   zUnknown Fortran standard: %s)mangled_symbols	used_namedictr   rV   itemspoprW   superrF   __init__known_functionsgetupdate	_settings
ValueErrorr   setmodule_uses)selfsettingsZ	userfuncsZ	standards)	__class__ 3lib/python3.7/site-packages/sympy/printing/fcode.pyra   x   s    

zFCodePrinter.__init__c             C   sJ   | j d dkrddddS | j d dkr4ddd	dS td
| j d  d S )NrO   rJ   z      z     @ zC     )codecontcommentfree z! zUnknown source format: %s)re   rf   )ri   rl   rl   rm   _lead   s
    zFCodePrinter._leadc                s   | j d dkrz|| jkrn|j}x| | jkr8|d7 }q W | j|  ||jkr`|| j|< nt|| j|< || j}tt	| 
|}|S )NrQ   T_)re   r[   namelowerr\   appendr
   Zxreplacer`   rF   _print_Symbol)ri   exprru   )rk   rl   rm   rx      s    

zFCodePrinter._print_Symbolc             C   s
   | d S )N   rl   )ri   prl   rl   rm   _rate_index_position   s    z!FCodePrinter._rate_index_positionc             C   s   |S )Nrl   )ri   Z
codestringrl   rl   rm   _get_statement   s    zFCodePrinter._get_statementc             C   s
   d |S )Nz! {0})format)ri   textrl   rl   rm   _get_comment   s    zFCodePrinter._get_commentc             C   s   d || |S )Nzparameter ({0} = {1}))r~   _print)ri   ru   valuerl   rl   rm   _declare_number_const   s    z"FCodePrinter._declare_number_constc             C   s(   | j |t|| jd f t|S )NrM   )Z_number_symbolsaddr	   Zevalfre   str)ri   ry   rl   rl   rm   _print_NumberSymbol   s     z FCodePrinter._print_NumberSymbolc             C   s   |  | |S )N)_wrap_fortranindent_code)ri   linesrl   rl   rm   _format_code   s    zFCodePrinter._format_codec                s    |j \ } fddt|D S )Nc             3   s$   | ]}t  D ]}||fV  qqd S )N)r   ).0ji)rowsrl   rm   	<genexpr>   s    z8FCodePrinter._traverse_matrix_indices.<locals>.<genexpr>)shaper   )ri   ZmatZcolsrl   )r   rm   _traverse_matrix_indices   s    
z%FCodePrinter._traverse_matrix_indicesc             C   sb   g }g }xP|D ]H}t | j|j|jd |jd g\}}}|d|||f  |d qW ||fS )N   zdo %s = %s, %szend do)mapr   labelrv   upperrw   )ri   indicesZ
open_linesZclose_linesr   varstartstoprl   rl   rm   _get_loop_opening_ending   s    
 z%FCodePrinter._get_loop_opening_endingc             C   s   ddl m} |j\}|jr4tdtd|t|d}n\|jrlttt	dt	d||| t||t	d}n$tt	dt
t	d|t|t	d}| |S )Nr   )rD   r   )ZsympyrD   args
is_integerr(   r%   r   Z
is_complexr'   r)   r&   r   )ri   ry   rD   argZnew_exprrl   rl   rm   _print_sign   s    2$zFCodePrinter._print_signc       
      C   sB  |j d jdkrtdg }|trxt|j D ]t\}\}}|dkr\|d| |  n:|t|j d kr|dkr|d n|d| |  || | q2W |d	 d
	|S | j
d dkr6d}| |j d j}t|j d d }x:|r0| \}}| |}| |}	|j|||	d}qW |S tdd S )NTzAll 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.r   zif (%s) thenr   elsezelse if (%s) thenzend if
rP   rZ   zmerge({T}, {F}, {COND}))TFZCONDzmUsing Piecewise as an expression using inline operators is not supported in standards earlier than Fortran95.)r   condrf   Zhasr   	enumeraterw   r   lenjoinre   ry   listr_   r~   NotImplementedError)
ri   ry   r   r   ecpatternrn   Ztermsr   rl   rl   rm   _print_Piecewise   s0    




zFCodePrinter._print_Piecewisec             C   s,   d | j|jtd dd|jd |jd S )Nz{0}({1}, {2})ZAtomT)strictr   )r~   parenthesizeparentr3   r   r   )ri   ry   rl   rl   rm   _print_MatrixElement   s    z!FCodePrinter._print_MatrixElementc       
      C   s$  g }g }g }xH|j D ]>}|jr0|jr0|| q|jrH|jrH|| q|| qW t|dkrt|dkrt|}t| }| |}|	drd}	|dd  }nd}	t||k rd| }d| t| | t
j t|  |	|f S d| t| | t
j t|  f S nt| |S d S )Nr   -r   +z(%s)zcmplx(%s,%s) %s %szcmplx(%s,%s))r   	is_numberZis_realrw   is_imaginaryr   r2   r   r   
startswithr   ImaginaryUnitr0   
_print_Add)
ri   ry   Z	pure_realZpure_imaginaryZmixedr   PRECZtermtZsignrl   rl   rm   r     s8    


zFCodePrinter._print_Addc                sR   | j d   fdd|jD }|j| }t|ts<| |S t| |j| S d S )NrM   c                s   g | ]}t | qS rl   )r   )r   a)precrl   rm   
<listcomp>,  s    z0FCodePrinter._print_Function.<locals>.<listcomp>)re   r   func
isinstancer   r   r0   _print_Function)ri   ry   r   Z	eval_exprrl   )r   rm   r   )  s    



zFCodePrinter._print_Functionc             C   s   dS )Nz
cmplx(0,1)rl   )ri   ry   rl   rl   rm   _print_ImaginaryUnit3  s    z!FCodePrinter._print_ImaginaryUnitc             C   s   t |S )N)r   )ri   ry   rl   rl   rm   
_print_int7  s    zFCodePrinter._print_intc             C   s2   |j r"|jr"d| tj |  S t| |S d S )Nzcmplx(0,%s))r   r   r   r   r   r0   
_print_Mul)ri   ry   rl   rl   rm   r   :  s    zFCodePrinter._print_Mulc             C   s   t |}|jdkr2d| td| |j|f S |jdkr|jjrn|jjr\d| |j S d| |j S qd| |j S nt	| |S d S )Nr   z%s/%sr   g      ?zsqrt(%s.0d0)zsqrt(dble(%s))zsqrt(%s))
r2   r?   r   r)   r   baser   Z	is_Numberr0   
_print_Pow)ri   ry   r   rl   rl   rm   r   C  s    

zFCodePrinter._print_Powc             C   s"   t |jt |j }}d||f S )Nz%d.0d0/%d.0d0)intr{   q)ri   ry   r{   r   rl   rl   rm   _print_RationalV  s    zFCodePrinter._print_Rationalc             C   sF   t | |}|d}|dkr>d|d | ||d d  f S d| S )Nr   r   z%sd%sr   z%sd0)r0   _print_Floatfind)ri   ry   Zprintedr   rl   rl   rm   r   Z  s
    
 zFCodePrinter._print_Floatc                s0    fdd|j D }d |jjd|f S )Nc                s   g | ]}  |qS rl   )r   )r   r   )ri   rl   rm   r   b  s    z/FCodePrinter._print_Indexed.<locals>.<listcomp>z%s(%s)z, )r   r   r   r   r   )ri   ry   Zindsrl   )ri   rm   _print_Indexeda  s    zFCodePrinter._print_Indexedc             C   s   |  |jS )N)r   r   )ri   ry   rl   rl   rm   
_print_Idxe  s    zFCodePrinter._print_Idxc                s>     |j}  |j} djt fdd||j|g S )Nz{0} = {0} {1} {2}c                s
     | S )N)r   )r   )ri   rl   rm   <lambda>l  s    z9FCodePrinter._print_AugmentedAssignment.<locals>.<lambda>)r   lhsrhsr}   r~   r   Zbinop)ri   ry   Zlhs_codeZrhs_coderl   )ri   rm   _print_AugmentedAssignmenth  s
    z'FCodePrinter._print_AugmentedAssignmentc             C   s^   |  |j}|jd kr*|d|  |j 7 }|jd krH|d|  |j 7 }d|jjd|f S )Nz, z, mask=z%s(%s)rt   )r   Zarraydimmaskrk   __name__rstrip)ri   ZsmZparamsrl   rl   rm   _print_sum_o  s    

zFCodePrinter._print_sum_c             C   s
   |  |S )N)r   )ri   Zprodrl   rl   rm   _print_product_w  s    zFCodePrinter._print_product_c                s\   dg}|j dkr |d d}nd}d| d jf d|jr>dndi|j fd	d
|dS )N
concurrentr   steprr   z, {step}z*do {concurrent}{counter} = {first}, {last}z
{body}
end do
zconcurrent c                s
     | S )N)r   )r   )ri   rl   rm   r     s    z(FCodePrinter._print_Do.<locals>.<lambda>)applyZexclude)r   rw   r~   r   kwargs)ri   ZdoZexclr   rl   )ri   rm   	_print_Doz  s    

zFCodePrinter._print_Doc                s6   |j dkrdnd}d| d jf |j fdddS )	Nr   rr   z, {step}z$({expr}, {counter} = {first}, {last})c                s
     | S )N)r   )r   )ri   rl   rm   r     s    z3FCodePrinter._print_ImpliedDoLoop.<locals>.<lambda>)r   )r   r~   r   )ri   Zidlr   rl   )ri   rm   _print_ImpliedDoLoop  s    z!FCodePrinter._print_ImpliedDoLoopc             C   sP   |  |j}t|jtr(|jj\}}}ntd|  |j}dj|||||dS )Nz*Only iterable currently supported is Rangez3do {target} = {start}, {stop}, {step}
{body}
end do)targetr   r   r   body)	r   r   r   iterabler   r   r   r   r~   )ri   ry   r   r   r   r   r   rl   rl   rm   
_print_For  s    zFCodePrinter._print_Forc                s&   |j \}}dt fdd||fS )Nz == c                s
     | S )N)r   )r   )ri   rl   rm   r     s    z.FCodePrinter._print_Equality.<locals>.<lambda>)r   r   r   )ri   ry   r   r   rl   )ri   rm   _print_Equality  s    
zFCodePrinter._print_Equalityc                s&   |j \}}dt fdd||fS )Nz /= c                s
     | S )N)r   )r   )ri   rl   rm   r     s    z0FCodePrinter._print_Unequality.<locals>.<lambda>)r   r   r   )ri   ry   r   r   rl   )ri   rm   _print_Unequality  s    
zFCodePrinter._print_Unequalityc             C   sT   | j ||}| j||j}| j|}|rPx |D ]\}}| j| | q4W |S )N)rV   rc   rW   ru   type_modulesrh   r   )ri   Ztype_Ztype_strrh   kvrl   rl   rm   _print_Type  s    zFCodePrinter._print_Typec                s,   dj  |jdt fdd|jdS )Nz{symbol}({idxs})z, c                s
     | S )N)r   )r   )ri   rl   rm   r     s    z-FCodePrinter._print_Element.<locals>.<lambda>)symbolZidxs)r~   r   r   r   r   r   )ri   elemrl   )ri   rm   _print_Element  s    
zFCodePrinter._print_Elementc             C   s   t |S )N)r   )ri   Zextrl   rl   rm   _print_Extent  s    zFCodePrinter._print_Extentc          
      sV  |j j}d}fddtttfD }|ddkrBd}n4|ddkrjdd	d
dg|d  }ntd  t	t
rtd jd dkrdj jtjkrdnd|rddt fdd| nd|tjkrdnd jd}|d krR|d | 7 }n:tjks*|r2tddt fddjjg}|S )NZ	dimensionc                s   g | ]}| j kqS rl   )attrs)r   intent)r   rl   rm   r     s    z3FCodePrinter._print_Declaration.<locals>.<listcomp>Tr   rr   r   z, intent(%s)inoutZinoutz!Multiple intents specified for %sz1Pointers are not available by default in Fortran.rP   rY   z"{t}{vc}{dim}{intent}{alloc} :: {s}z, parameterz, dimension(%s)z, c                s
     | S )N)r   )r   )ri   rl   rm   r     s    z1FCodePrinter._print_Declaration.<locals>.<lambda>z, allocatable)r   Zvcr   r   Zallocsz = %sz0F77 init./parameter statem. req. multiple lines. c                s
     | S )N)r   )r   )ri   rl   rm   r     s    )Zvariabler   attr_paramsr,   r-   r.   countindexrf   r   r   r   re   r~   r   typer   r   r   r   r$   r   )ri   ry   valr   Zintentsr   resultrl   )ri   r   rm   _print_Declaration  s2    


"
 zFCodePrinter._print_Declarationc             C   s   d|  td S )Nz(huge(%s) + 1)r   )r   r)   )ri   ry   rl   rl   rm   _print_Infinity  s    zFCodePrinter._print_Infinityc                s   dj f |j fdddS )Nz$do while ({condition})
{body}
end doc                s
     | S )N)r   )r   )ri   rl   rm   r     s    z+FCodePrinter._print_While.<locals>.<lambda>)r   )r~   r   )ri   ry   rl   )ri   rm   _print_While  s    
zFCodePrinter._print_Whilec             C   s   dS )Nz.true.rl   )ri   ry   rl   rl   rm   _print_BooleanTrue  s    zFCodePrinter._print_BooleanTruec             C   s   dS )Nz.false.rl   )ri   ry   rl   rl   rm   _print_BooleanFalse  s    z FCodePrinter._print_BooleanFalsec             C   sV   g }xL|D ]D}| dr:|| jd |dd     q
|| jd |  q
W |S )N!rp   r   rn   )r   rw   rs   lstrip)ri   r   r   linerl   rl   rm   _pad_leading_columns  s    

"z!FCodePrinter._pad_leading_columnsc                s  t dtj tj  t d fdd}g }| jd dkrBd}nd}x|D ]}|| jd	 r0t|d
kr$|ddd
}|dkrd
}|d| }||d 	 }|
| xxt|dkr |ddd}|dkst|dk rd}|d| }||d 	 }|
d| jd	 |f  qW n
|
| qN|| jd r||d
}|d|  }||d 	 }|rz||7 }|
| xpt|dkr||d}|d|  }||d 	 }|r||7 }|
d| jd |f  qW qN|
| qNW |S )zWrap 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.
        z_+-.z 	()c                sN   t  |krt  S |} fdd}x ||sH|d8 }|dkr*|S q*W |S )Nc                sp    |  kr | d  kpn |  kr8 | d  kpn |  krT | d  kpn |  kon | d  kS )Nr   rl   )pos)r   my_alnummy_whiterl   rm   r     s    zDFCodePrinter._wrap_fortran.<locals>.split_pos_code.<locals>.<lambda>r   r   )r   )r   endposr  split)r  r  )r   rm   split_pos_code  s    
z2FCodePrinter._wrap_fortran.<locals>.split_pos_coderO   rq   z &rr   rp   H   r      r   Nr   rX   z%s%srn   A   ro   )rg   stringZdigitsZascii_lettersre   r   rs   r   rfindr   rw   r   )ri   r   r  r   Ztrailingr   r  Zhunkrl   )r  r  rm   r     sP    




 zFCodePrinter._wrap_fortranc                sN  t |tr$| |d}d|S | jd dk}dd |D }dd fd	d|D } fd
d|D }dd |D }d}d}d}	g }
xt|D ]\}}|dks|dkr|
| q||| 8 }|rd||	 |  }nd| |	 }d||f }|s| |gd }|
| || r&d|	 }nd}||| 7 }qW |sJ| 	|
S |
S )z0Accepts a string of code or a list of code linesTrr   rO   rq   c             S   s   g | ]}| d qS )z 	)r   )r   r   rl   rl   rm   r   <  s    z,FCodePrinter.indent_code.<locals>.<listcomp>)zdo zif(zif zdo
r   ZprogramZ	interface)zend doZenddozend ifZendifr   zend programzend interfacec                s    g | ]}t tt|j qS rl   )r   anyr   r   )r   r   )inc_keywordrl   rm   r   A  s   c                s    g | ]}t tt|j qS rl   )r   r  r   r   )r   r   )dec_keywordrl   rm   r   C  s   c             S   s$   g | ]}t tt|jd dgqS )&z&
)r   r  r   endswith)r   r   rl   rl   rm   r   E  s   r      r   r   z%s%s   )
r   r   r   
splitlinesr   re   r   rw   r   r   )ri   rn   Z
code_linesrq   ZincreaseZdecreaseZcontinuationlevelZcont_paddingZtabwidthZnew_coder   r   Zpaddingrl   )r  r  rm   r   5  sH    








zFCodePrinter.indent_codec                sL   |j r2djdt fdd|j |j dS |j\}d | S d S )Nzgo to ({labels}), {expr}z, c                s
     | S )N)r   )r   )ri   rl   rm   r   j  s    z*FCodePrinter._print_GoTo.<locals>.<lambda>)labelsry   zgo to %s)ry   r~   r   r   r  r   )ri   ZgotoZlblrl   )ri   rm   _print_GoTog  s    zFCodePrinter._print_GoToc                s   dj f |j fdddS )Nz"program {name}
{body}
end program
c                s
     | S )N)r   )r   )ri   rl   rm   r   v  s    z-FCodePrinter._print_Program.<locals>.<lambda>)r   )r~   r   )ri   progrl   )ri   rm   _print_Programq  s    zFCodePrinter._print_Programc                s   dj f |j fdddS )NzAmodule {name}
{declarations}

contains

{definitions}
end module
c                s
     | S )N)r   )r   )ri   rl   rm   r     s    z,FCodePrinter._print_Module.<locals>.<lambda>)r   )r~   r   )ri   modrl   )ri   rm   _print_Modulex  s    zFCodePrinter._print_Modulec             C   sp   |j dkr,| jd dkr,| jd d dS |j dkrX| jd dkrX| jd d d	S |j dkrfd
S |j S d S )NstdoutrP   i  rG   zstdint=>input_unitZ
input_unitstderrzstdint=>error_unitZ
error_unit*)ru   re   rh   r   )ri   Zstrmrl   rl   rm   _print_Stream  s    
zFCodePrinter._print_Streamc                s@   |j d kr |j }nd}dj|dt fdd|jdS )Nr  zprint {fmt}, {iolist}z, c                s
     | S )N)r   )r   )ri   rl   rm   r     s    z+FCodePrinter._print_Print.<locals>.<lambda>)fmtZiolist)format_stringr   r~   r   r   Z
print_args)ri   Zpsr  rl   )ri   rm   _print_Print  s
    

zFCodePrinter._print_Printc             C   s&   |j \}dj| jdd| |dS )Nz{result_name} = {arg}result_nameZsympy_result)r"  r   )r   r~   Z_contextrc   r   )ri   Zrsr   rl   rl   rm   _print_Return  s    zFCodePrinter._print_Returnc             C   s"   |j \}|rd| | S dS d S )Nz	return %sreturn)r   r   )ri   Zfrsr   rl   rl   rm   _print_FortranReturn  s    z!FCodePrinter._print_FortranReturnc                s   | d}|d krd}n|r(d|d  nd} jdd }dj| |jd fd	d
|jD |rnd| nd|dt fdd|jdS )NZbind_Crr   z bind(C, name="%s")r   z bind(C)r"  z<{entity}{name}({arg_names}){result}{bind}
{arg_declarations}z, c                s   g | ]}  |jqS rl   )r   r   )r   r   )ri   rl   rm   r     s    z&FCodePrinter._head.<locals>.<listcomp>z result(%s)r   c                s     t| S )N)r   r   )r   )ri   rl   rm   r     s    z$FCodePrinter._head.<locals>.<lambda>)entityru   Z	arg_namesr   bindZarg_declarations)	r   re   rc   r~   r   ru   r   Z
parametersr   )ri   r&  fpr   Zbind_C_paramsr'  r"  rl   )ri   rm   _head  s    

zFCodePrinter._headc             C   s,   d | |j}dj | j||ft dS )Nz{0} function z4interface
{function_head}
end function
end interface)function_head)r~   r   return_typer)  r   )ri   r(  r&  rl   rl   rm   _print_FunctionPrototype  s    z%FCodePrinter._print_FunctionPrototypec          	   C   sp   t |jkrd}nt|jkr d}nd}d| |j}t| |jd" dj|| ||| |j	dS Q R X d S )Nz
elemental zpure rr   z{0} function )r"  z,{prefix}{function_head}
{body}
end function
)prefixr*  r   )
r*   r   r+   r~   r   r+  r/   ru   r)  r   )ri   fdr-  r&  rl   rl   rm   _print_FunctionDefinition  s    


z&FCodePrinter._print_FunctionDefinitionc             C   s   dj | d|| |jdS )Nz({subroutine_head}
{body}
end subroutine
zsubroutine )Zsubroutine_headr   )r~   r)  r   r   )ri   subrl   rl   rm   _print_Subroutine  s    
zFCodePrinter._print_Subroutinec                s,   dj  |jdt fdd|jdS )Nzcall {name}({args})z, c                s
     | S )N)r   )r   )ri   rl   rm   r     s    z4FCodePrinter._print_SubroutineCall.<locals>.<lambda>)ru   r   )r~   r   ru   r   r   Zsubroutine_args)ri   Zscallrl   )ri   rm   _print_SubroutineCall  s    
z"FCodePrinter._print_SubroutineCallc                s   dt t fdd|j S )Nz%s => %sc                s
     | S )N)r   )r   )ri   rl   rm   r     s    z0FCodePrinter._print_use_rename.<locals>.<lambda>)tupler   r   )ri   rnmrl   )ri   rm   _print_use_rename  s    zFCodePrinter._print_use_renamec                sl   d  |j }|jd kr<|dd fdd|jD  7 }|jd krh|dd fdd|jD  7 }|S )Nzuse %sz, c                s   g | ]}  |qS rl   )r   )r   r4  )ri   rl   rm   r     s    z+FCodePrinter._print_use.<locals>.<listcomp>z, only: c                s   g | ]}  |qS rl   )r   )r   Znly)ri   rl   rm   r     s    )r   	namespacerenamer   Zonly)ri   Zuser   rl   )ri   rm   
_print_use  s    
"
"zFCodePrinter._print_usec             C   s   dS )Nexitrl   )ri   rt   rl   rl   rm   _print_BreakToken  s    zFCodePrinter._print_BreakTokenc             C   s   dS )Ncyclerl   )ri   rt   rl   rl   rm   _print_ContinueToken  s    z!FCodePrinter._print_ContinueTokenc                s4    j d dkrdnd}|dt fdd|j S )NrP   i  z[%s]z(/%s/)z, c                s
     | S )N)r   )r   )ri   rl   rm   r     s    z6FCodePrinter._print_ArrayConstructor.<locals>.<lambda>)re   r   r   elements)ri   ZacZfmtstrrl   )ri   rm   _print_ArrayConstructor  s    z$FCodePrinter._print_ArrayConstructor)Xr   
__module____qualname____doc__ZprintmethodZlanguager!   r   r    r   r#   r   rV   r   r   r   r   r   r   r   r"   rW   r   Z_default_settingsZ
_operatorsZ_relationalsra   propertyrs   rx   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   r   r  r  r  r  r!  r#  r%  r)  r,  r/  r1  r2  r5  r8  r:  r<  r>  __classcell__rl   rl   )rk   rm   rF   B   s   	*'
		!	G2
		
rF   c             K   s   t || |S )aH  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)
    )rF   Zdoprint)ry   Z	assign_torj   rl   rl   rm   fcode  s    urD  c             K   s   t t| f| dS )z|Prints the Fortran representation of the given expression.

       See fcode for the meaning of the optional arguments.
    N)printrD  )ry   rj   rl   rl   rm   print_fcoder  s    rF  )N)ErA  Z
__future__r   r   collectionsr   	itertoolsr   r
  Z
sympy.corer   r   r   r	   r
   Zsympy.core.compatibilityr   r   Zsympy.core.functionr   Zsympy.core.relationalr   Z
sympy.setsr   Zsympy.codegen.astr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   Zsympy.codegen.fnodesr$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   Zsympy.printing.printerr/   Zsympy.printing.codeprinterr0   r1   Zsympy.printing.precedencer2   r3   rb   rF   rD  rF  rl   rl   rl   rm   <module>   sN   X4     =
x