
    MZdo                       d Z ddlmZ ddl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/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z= ddl7m>Z>m?Z? i dddddddddddddddddddddddddddd d!d"d#d$d%d&Z@ G d' d(e8      ZAy))ar  
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:

https://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.
    )annotations)Any)defaultdict)chainN)
AssignmentDeclarationPointer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)SAddNFloatSymbol)Function)equal_valued)Eq)Range)CodePrinter)
precedence
PRECEDENCE)printer_context)fcodeprint_fcodesincostanasinacosatanatan2sinhcoshtanhlogexperfAbsabs	conjugateconjgMaxmaxMinminc                  ,    e Zd ZU 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ed<   dddddd Zd!d"iZd` fd#	Zed$        Z fd%Zd& Zd' Z d( Z!d) Z"d* Z#d+ Z$d, Z%d- Z&d. Z'd/ Z(d0 Z)d1 Z*d2 Z+d3 Z,d4 Z-d5 Z.d6 Z/d7 Z0d8 Z1d9 Z2d: Z3d; Z4d< Z5d= Z6d> Z7d? Z8d@ Z9dA Z:dB Z;dC Z<dD Z=dE Z>dF Z?dG Z@dH ZAdI ZBdJ ZCdK ZDdL ZEdM ZFdN ZGdO ZHdP ZIdQ ZJdR ZKdS ZLdT ZMdU ZNdV ZOdW ZPdX ZQdY ZRdZ ZSd[ ZTd\ ZUd] ZVd^ ZWd_ ZX xZYS )aFCodePrinterzAA printer to convert SymPy expressions to strings of Fortran code_fcodeFortranzinteger(c_int)zreal*4zreal*8zreal*10z	complex*8z
complex*16z	integer*1z	integer*2z	integer*4z	integer*8logicaliso_c_bindingc_intNauto   TFfixedM   )
order	full_prec	precisionuser_functionshumanallow_unknown_functionssource_formatcontractstandardname_manglingzdict[str, Any]_default_settingsz.and.z.or.z.neqv.z.eqv.z.not. )andorxor
equivalentnotz!=z/=c           	        |si }i | _         g | _        t        t        | j                  j                         |j                  di       j                                     | _        t        t        | j                  j                         |j                  di       j                                     | _        t        | %  |       t        t              | _
        |j                  di       }| j                  j                  |       h d}| j                  d   |vrt        d| j                  d   z        t        t               | _        y )Ntype_aliasestype_mappingsrV   >     B   rR   Z   _     r[   zUnknown Fortran standard: %s)mangled_symbols	used_namedictr   rd   itemspopre   super__init__known_functionsgetupdate	_settings
ValueErrorr   setmodule_uses)selfsettings	userfuncs	standards	__class__s       8/usr/lib/python3/dist-packages/sympy/printing/fortran.pyrq   zFCodePrinter.__init__|   s   H! t'8'8'>'>'@'/||NB'G'M'M'O"Q R!%(:(:(@(@(B(0_b(I(O(O(Q#S T"#O4LL!126	##I.0	>>*%Y6;dnn'?) ) * *&s+    c                    | j                   d   dk(  rddddS | j                   d   dk(  rddd	dS t        d
| j                   d   z        )NrY   rQ   z      z     @ zC     )codecontcommentfree z! zUnknown source format: %s)ru   rv   )ry   s    r~   _leadzFCodePrinter._lead   sX    >>/*g5$iHMM^^O,6TBB84>>/;ZZ[[r   c                   | j                   d   dk(  r|| j                  vr|j                  }|j                         | j                  v r"|dz  }|j                         | j                  v r"| j                  j                  |j                                ||j                  k(  r|| j                  |<   nt        |      | j                  |<   |j                  | j                        }t        | %  |      }|S )Nr\   T_)
ru   rk   namelowerrl   appendr(   xreplacerp   _print_Symbol)ry   exprr   r}   s      r~   r   zFCodePrinter._print_Symbol   s    >>/*d24///yyjjldnn4CKD jjldnn4%%djjl3499$15D((.17D((.==!5!56Dw$T*r   c                    | dz  S )N    )ry   ps     r~   _rate_index_positionz!FCodePrinter._rate_index_position   s    r!tr   c                    |S Nr   )ry   
codestrings     r~   _get_statementzFCodePrinter._get_statement   s    r   c                $    dj                  |      S )Nz! {})format)ry   texts     r~   _get_commentzFCodePrinter._get_comment   s    }}T""r   c                D    dj                  || j                  |            S )Nzparameter ({} = {}))r   _print)ry   r   values      r~   _declare_number_constz"FCodePrinter._declare_number_const   s    $++D$++e2DEEr   c           	         | j                   j                  |t        |j                  | j                  d               f       t        |      S NrU   )_number_symbolsaddr'   evalfru   strry   r   s     r~   _print_NumberSymbolz FCodePrinter._print_NumberSymbol   s>     	  $djj9T.U(V!WX4yr   c                B    | j                  | j                  |            S r   )_wrap_fortranindent_code)ry   liness     r~   _format_codezFCodePrinter._format_code   s    !!$"2"25"9::r   c                J    |j                   \  }fdt        |      D        S )Nc              3  F   K   | ]  }t              D ]  }||f 
  y wr   )range).0jirowss      r~   	<genexpr>z8FCodePrinter._traverse_matrix_indices.<locals>.<genexpr>   s%     A1U4[AAAAs   !)shaper   )ry   matcolsr   s      @r~   _traverse_matrix_indicesz%FCodePrinter._traverse_matrix_indices   s    YY
dAdAAr   c           	         g }g }|D ]n  }t        | j                  |j                  |j                  dz   |j                  dz   g      \  }}}|j                  d|d|d|       |j                  d       p ||fS )N   do z = , end do)mapr   labelr   upperr   )ry   indices
open_linesclose_linesr   varstartstops           r~   _get_loop_opening_endingz%FCodePrinter._get_loop_opening_ending   s    
 	)A"4;;WWaggk177Q;7 9C#udCDx(	) ;&&r   c           
        ddl m} |j                  \  }|j                  r"t	        dt        d|      t        |d            }n|j                  s|j                  rLt	        t        t        d      t        d            | ||      z  t         ||      t        d                  }n<t	        t        d      t        t        d      |      t        |t        d                  }| j                  |      S )Nr   )r@   r   )$sympy.functions.elementary.complexesr@   args
is_integerr   r   r+   
is_complexis_infiniter   r   r   r   )ry   r   r@   argnew_exprs        r~   _print_signzFCodePrinter._print_sign   s    <yy>>Qar#qz:HnnU:a=*Q-@#c#h,PRSVWZS[]ghi]jPklHZ]E*Q-,Er#zZ[}G]^H{{8$$r   c                   |j                   d   j                  dk7  rt        d      g }|j                  t              rt        |j                         D ]  \  }\  }}|dk(  r$|j                  d| j                  |      z         nU|t        |j                         dz
  k(  r|dk(  r|j                  d       n#|j                  d| j                  |      z         |j                  | j                  |              |j                  d	       d
j                  |      S | j                  d   dk\  rd}| j                  |j                   d   j                        }t        |j                   d d       }|rL|j                         \  }}| j                  |      }| j                  |      }	|j                  |||	      }|rL|S t        d      )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) thenend if
r[   ri   zmerge({T}, {F}, {COND}))TFCONDzmUsing Piecewise as an expression using inline operators is not supported in standards earlier than Fortran95.)r   condrv   hasr   	enumerater   r   lenjoinru   r   listro   r   NotImplementedError)
ry   r   r   r   ecpatternr   termsr   s
             r~   _print_PiecewisezFCodePrinter._print_Piecewise   s   99R=%  / 0 0
 88J&tyy1 -	6Aq6LL$++a.!@A#dii.1,,dLL(LL!4t{{1~!EFT[[^,- LL"99U##^^J'2- 0G;;tyy}112D3B(Eyy{1{{1~{{1~~~4~@	 
 K & 'J K Kr   c                    dj                  | j                  |j                  t        d   d      |j                  dz   |j
                  dz         S )Nz
{}({}, {})AtomT)strictr   )r   parenthesizeparentr/   r   r   r   s     r~   _print_MatrixElementz!FCodePrinter._print_MatrixElement  sN    ""4#4#4T[[6"4 $5 $126&&1*dffqjJ 	Jr   c           	        g }g }g }|j                   D ]g  }|j                  r|j                  r|j                  |       -|j                  r|j                  r|j                  |       W|j                  |       i |r|rt        |      }t        | }| j                  |      }|j                  d      rd}	|dd  }nd}	t        |      |k  rd|z  }d| j                  t        |       d| j                  t        j                   t        | z        d|	d|S d| j                  t        |       d| j                  t        j                   t        | z        d	S t        j                  | |      S )
N-r   +z(%s)zcmplx(,z)  ))r   	is_numberis_realr   is_imaginaryr.   r%   r   
startswithr$   ImaginaryUnitr-   
_print_Add)
ry   r   	pure_realpure_imaginarymixedr   PRECtermtsigns
             r~   r   zFCodePrinter._print_Add  s\    	99 	"C}}  %3#3#3%%c*S!	" !$'E{KK%<<$D!"ADd#d*
A KKY0KK 0n1E EF!  & KKY0KK 0n1E EF 
 ))$55r   c                   | j                   d   }|j                  D cg c]  }t        ||       }} |j                  | }t	        |t
              s| j                  |      S t        j                  |  |j                  |       S c c}w r   )	ru   r   r&   func
isinstancer)   r   r-   _print_Function)ry   r   precar   	eval_exprs         r~   r  zFCodePrinter._print_Function/  sy    {+$(II.q!T
..DIIt$	)X.;;y))..tYTYY5EFF /s   Bc                    | j                   d   dv rd}t        |      |j                  \  }}dj                  | j	                  |      | j	                  |            S )Nr[   )rg   rR   z]Python % operator and SymPy's Mod() function are not supported by Fortran 66 or 77 standards.z      modulo({}, {}))ru   r   r   r   r   )ry   r   msgxys        r~   
_print_ModzFCodePrinter._print_Mod9  sW    
 >>*%1>C%c**99DAq)00QQPPr   c                     y)Nz
cmplx(0,1)r   r   s     r~   _print_ImaginaryUnitz!FCodePrinter._print_ImaginaryUnitF  s    r   c                    t        |      S r   r   r   s     r~   
_print_intzFCodePrinter._print_intJ  s    4yr   c                    |j                   r2|j                  r&d| j                  t        j                   |z        z  S t        j                  | |      S )Nzcmplx(0,%s))r   r   r   r$   r   r-   
_print_Mulr   s     r~   r  zFCodePrinter._print_MulM  sK    >>d// Q__,T12  ))$55r   c                   t        |      }t        |j                  d      r9| j                  t	        d            d| j                  |j                  |      S t        |j                  d      r|j                  j                  rR|j                  j                  rd| j                  |j                        z  S d| j                  |j                        z  S d| j                  |j                        z  S t        j                  | |      S )Nr   r   /g      ?zsqrt(%s.0d0)zsqrt(dble(%s))zsqrt(%s))r.   r*   r>   r   r   r   baser   	is_Numberr-   
_print_Pow)ry   r   r   s      r~   r  zFCodePrinter._print_PowV  s    $"%JqM*!!$))T2  $((C(yy##99&&)DKK		,BBB+dkk$)).DDD!DKK		$:::))$55r   c                d    t        |j                        t        |j                        }}d||fz  S )Nz%d.0d0/%d.0d0)intr   q)ry   r   r   r  s       r~   _print_RationalzFCodePrinter._print_Rationali  s*    466{CK1!Q''r   c                    t        j                  | |      }|j                  d      }|dkD  r|d | d||dz   d  S d|z  S )Nr   r   dr   z%sd0)r-   _print_Floatfind)ry   r   printedr   s       r~   r  zFCodePrinter._print_Floatm  sO    **46LLr6%bqk71q56?;;r   c                    | j                  |j                        }| j                  |j                        }|j                  }|| j                  vr|n| j                  |   }dj                  |||      S )Nz{} {} {})r   lhsrhsrel_op_relationalsr   )ry   r   lhs_coderhs_codeops        r~   _print_RelationalzFCodePrinter._print_Relationalt  sc    ;;txx(;;txx([[T...RD4E4Eb4I  2x88r   c                    |j                   D cg c]  }| j                  |       }}| j                  |j                  j                        ddj	                  |      dS c c}w )N(r   r   )r   r   r  r   r   )ry   r   r   indss       r~   _print_IndexedzFCodePrinter._print_Indexed{  sJ    )-7AQ77;;tyy74II 8s   A#c                8    | j                  |j                        S r   )r   r   r   s     r~   
_print_IdxzFCodePrinter._print_Idx  s    {{4::&&r   c           	         | j                  |j                        }| j                  |j                        }| j                  dj	                  | j                  |      | j                  |j
                        | j                  |                  S )Nz{0} = {0} {1} {2})r   r   r!  r   r   binop)ry   r   r$  r%  s       r~   _print_AugmentedAssignmentz'FCodePrinter._print_AugmentedAssignment  sp    ;;txx(;;txx(""#6#=#=KK!4;;tzz#:DKK<Q$S T 	Tr   c                N   | j                  |j                        }|j                  d k7  r!|d| j                  |j                        z   z  }|j                  d k7  r!|d| j                  |j                        z   z  }|j                  j
                  j                  d      d|dS )Nr   z, mask=r   r)  r   )r   arraydimmaskr}   __name__rstrip)ry   smparamss      r~   _print_sum_zFCodePrinter._print_sum_  s    RXX&66T>dT[[000F77d?i$++bgg"666F<<0077<fEEr   c                $    | j                  |      S r   )r9  )ry   prods     r~   _print_product_zFCodePrinter._print_product_  s    %%r   c                     dg}|j                   dk(  r|j                  d       d}nd} d|z   dz   j                  dd|j                  rdndi|j	                   fd	|
      S )N
concurrentr   stepr   , {step}z*do {concurrent}{counter} = {first}, {last}z
{body}
end do
zconcurrent c                &    j                  |       S r   r   r   ry   s    r~   <lambda>z(FCodePrinter._print_Do.<locals>.<lambda>  s    $++c*: r   )applyexcluder   )r?  r   r   r>  kwargs)ry   doexclr?  s   `   r~   	_print_DozFCodePrinter._print_Do  s}    ~77a<KKDD8= ?  &	

 )+}2
 ii:DiI
 	
r   c                     |j                   dk(  rdnd} d|z   dz   j                  di |j                   fd      S )	Nr   r   r@  z$({expr}, {counter} = {first}, {last}r   c                &    j                  |       S r   rB  rC  s    r~   rD  z3FCodePrinter._print_ImpliedDoLoop.<locals>.<lambda>  s    4;;s+; r   rE  r   )r?  r   rG  )ry   idlr?  s   `  r~   _print_ImpliedDoLoopz!FCodePrinter._print_ImpliedDoLoop  sH    XX]r
G6t;C?GG 
jj;j<
 	
r   c                    | j                  |j                        }t        |j                  t              r|j                  j
                  \  }}}nt        d      | j                  |j                        }dj                  |||dz
  ||      S )Nz*Only iterable currently supported is Rangez3do {target} = {start}, {stop}, {step}
{body}
end dor   )targetr   r   r?  body)	r   rQ  r   iterabler,   r   r   rR  r   )ry   r   rQ  r   r   r?  rR  s          r~   
_print_ForzFCodePrinter._print_For  s~    T[[)dmmU+ $ 2 2E4%&RSS{{499% &e$(! ! .	.r   c                   | j                   j                  ||      }| j                  j                  ||j                        }| j                  j                  |      }|r(|D ]#  \  }}| j
                  |   j                  |       % |S r   )rd   rs   re   r   type_modulesrx   r   )ry   type_type_strrx   kvs         r~   _print_TypezFCodePrinter._print_Type  s    !!%%eU3%%))%<''++E2# +1  #''*+r   c                     dj                   j                  |j                        dj                   fd|j                  D                    S )N{symbol}({idxs})r   c              3  @   K   | ]  }j                  |        y wr   rB  r   r   ry   s     r~   r   z.FCodePrinter._print_Element.<locals>.<genexpr>       EDKK,E   symbolidxs)r   r   rc  r   r   ry   elems   ` r~   _print_ElementzFCodePrinter._print_Element  sA    !((;;t{{+EEF ) 
 	
r   c                    t        |      S r   r  )ry   exts     r~   _print_ExtentzFCodePrinter._print_Extent  s    3xr   c           
         |j                   }|j                  }|j                  d      }t        t        t
        fD cg c]  }||j                  v  }}|j                  d      dk(  rd}n<|j                  d      dk(  rdg d|j                  d         z  }nt        d z        t        |t              rt        d	       j                  d
   dk\  rdj                   j                  |j                         t"        |j                  v rdnd|rddj%                   fd|D              z  nd|t&        |j                  v rdnd j                  |j(                              }|d k7  r|d j                  |      z  z  }|S t"        |j                  v s|rt        d      dj%                   fd|j                   |j(                  fD              }|S c c}w )N	dimensionTr   r   r   z, intent(%s))inoutinoutz!Multiple intents specified for %sz1Pointers are not available by default in Fortran.r[   rh   z"{t}{vc}{dim}{intent}{alloc} :: {s}z, parameterz, dimension(%s)r   c              3  @   K   | ]  }j                  |        y wr   rB  r_  s     r~   r   z2FCodePrinter._print_Declaration.<locals>.<genexpr>  s     2S4;;s3C2Sra  z, allocatable)r   vcr3  intentallocsz = %sz0F77 init./parameter statem. req. multiple lines.r   c              3  @   K   | ]  }j                  |        y wr   rB  r_  s     r~   r   z2FCodePrinter._print_Declaration.<locals>.<genexpr>  s     RCt{{3/Rra  )variabler   attr_paramsr!   r"   r#   attrscountindexrv   r   r	   r   ru   r   r   typer
   r   r   rc  )ry   r   r   valr3  rr  intentsresults   `       r~   _print_DeclarationzFCodePrinter._print_Declaration  s   mmiiook*6?\5Z[66SYY&[[==!#F]]4 A%#&<W]]4=P&QQF@4GHHc7#%&YZZ>>*%+9@@++chh'$/399$<="X[%		2Ss2S(TTac)4		)Aor++cjj) A F d{'DKK$444 	 cii'3)*\]]XXRCHHcjj;QRSF5 \s   Gc                <    d| j                  t        d            z  S )Nz(huge(%s) + 1)r   )r   r   r   s     r~   _print_InfinityzFCodePrinter._print_Infinity  s    $++jm"<<<r   c                N      dj                   di |j                   fd      S )Nz$do while ({condition})
{body}
end doc                &    j                  |       S r   rB  rC  s    r~   rD  z+FCodePrinter._print_While.<locals>.<lambda>  s    dkk#. r   rM  r   r   rG  r   s   ` r~   _print_WhilezFCodePrinter._print_While  s4    >7>> 1. BM B0 1 	1r   c                     y)Nz.true.r   r   s     r~   _print_BooleanTruezFCodePrinter._print_BooleanTrue  s    r   c                     y)Nz.false.r   r   s     r~   _print_BooleanFalsez FCodePrinter._print_BooleanFalse  s    r   c                    g }|D ]g  }|j                  d      r3|j                  | j                  d   |dd  j                         z          G|j                  | j                  d   |z          i |S )N!r   r   r   )r   r   r   lstrip)ry   r   r~  lines       r~   _pad_leading_columnsz!FCodePrinter._pad_leading_columns  sj     	9Ds#djj3d12hoo6GGHdjj0478		9
 r   c                L  	 t        dt        j                  z   t        j                  z         t        d      		fd}g }| j                  d   dk(  rd}nd}|D ]  }|j                  | j                  d         rt        |      d	kD  r|j                  d
dd	      }|dk(  rd	}|d| }||d j                         }|j                  |       |sv|j                  d
dd      }|dk(  st        |      dk  rd}|d| }||d j                         }|j                  | j                  d   |       |re|j                  |       |j                  | j                  d         r ||d	      }|d| j                         }||d j                         }|r||z  }|j                  |       |sW ||d      }|d| j                         }||d j                         }|r||z  }|j                  | j                  d   |       |r[|j                  |        |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                     t               |k  rt               S |} fd} ||      s|dz  }|dk(  r|S  ||      s|S )Nc                    |    v xr
 | dz
     vxs= |    vxr
 | dz
     v xs( |    v xr
 | dz
     vxs |    vxr
 | dz
     v S )Nr   r   )posr  my_alnummy_whites    r~   rD  zDFCodePrinter._wrap_fortran.<locals>.split_pos_code.<locals>.<lambda>  s    ch&H4a=+H Jc(*HtC!G}/HJch&H4a=+HJ c(*HtC!G}/H	 r   r   r   )r   )r  endposr  splitr  r  s   `   r~   split_pos_codez2FCodePrinter._wrap_fortran.<locals>.split_pos_code  sT    4yF"4y CJE
 Cjq!8!M Cj Jr   rY   r   z &r   r   H   r      r   Nr   rg   r   A   r   )rw   stringdigitsascii_lettersru   r   r   r   rfindr  r   r6  )
ry   r   r  r~  trailingr  r  hunkr  r  s
           @@r~   r   zFCodePrinter._wrap_fortran  s    v-0D0DDEw<	 >>/*f4HH #	$Dtzz)45t9r>**S!R0Cby :D:,,.DMM$'"jja4"9D	B"$C#DSz#CDz002

90Et&LM  MM$'F!34$T2.DSz((*CDz((*H$Dd#(r2C:,,.D:,,.D(MMDJJv,>"EF  d#G#	$H r   c                   t        |t              r1| j                  |j                  d            }dj	                  |      S | j
                  d   dk(  }|D cg c]  }|j                  d       }}d}d}|D cg c]*  }t        t        t        |j                  |                  , }}|D cg c]*  }t        t        t        |j                  |                  , }}|D cg c],  }t        t        t        |j                  dd	g                  . }	}d
}
d
}d}g }t        |      D ]|  \  }}|dv r|j                  |       |
||   z  }
|rd|
|z  |z   z  }nd|
z  |z  }||}|s| j                  |g      d
   }|j                  |       |	|   rd|z  }nd
}|
||   z  }
~ |s| j                  |      S |S c c}w c c}w c c}w c c}w )z0Accepts a string of code or a list of code linesTr   rY   r   z 	)r   zif(zif zdo
r   program	interface)r   enddor   endifr   zend programzend interface&z&
r      )r   r   r      )r   r   r   
splitlinesr   ru   r  r  anyr   r   endswithr   r   r  r   )ry   r   
code_linesr   r  inc_keyworddec_keywordincreasedecreasecontinuationlevelcont_paddingtabwidthnew_coder   paddings                   r~   r   zFCodePrinter.indent_codeF  s   dC ))$//$*?@J77:&&~~o.&8046U#66Sd "&( S+>?@ ( ( "&( S+>?@ ( ( &*,! ST]]S%L!ABC , ,   	!GAtz!%Xa[ EuX~<=e)H,$d+D00$8;OOD!A z Xa[ E+	!. %%h//S 7
((,s   F>:/G//G$1Gc                     |j                   rNdj                  dj                   fd|j                  D               j	                  |j                               S |j                  \  }d j	                  |      z  S )Nzgo to ({labels}), {expr}r   c              3  @   K   | ]  }j                  |        y wr   rB  r_  s     r~   r   z+FCodePrinter._print_GoTo.<locals>.<genexpr>{  s     !Js$++c"2!Jra  )labelsr   zgo to %s)r   r   r   r  r   )ry   gotolbls   `  r~   _print_GoTozFCodePrinter._print_GoTox  si    99-44yy!Jdkk!JK[[+ 5  
 ;;DCC 000r   c                N      dj                   di |j                   fd      S )Nz"program {name}
{body}
end program
c                &    j                  |       S r   rB  rC  s    r~   rD  z-FCodePrinter._print_Program.<locals>.<lambda>  s    S1A r   rM  r   r  )ry   progs   ` r~   _print_ProgramzFCodePrinter._print_Program  s7     &	D ;;%A;B	D 	Dr   c                N      dj                   di |j                   fd      S )NzAmodule {name}
{declarations}

contains

{definitions}
end module
c                &    j                  |       S r   rB  rC  s    r~   rD  z,FCodePrinter._print_Module.<locals>.<lambda>  s    C0@ r   rM  r   r  )ry   mods   ` r~   _print_ModulezFCodePrinter._print_Module  s7    
 &C ::$@:AC 	Cr   c                :   |j                   dk(  r1| j                  d   dk\  r| j                  d   j                  d       y|j                   dk(  r1| j                  d   dk\  r| j                  d   j                  d       y	|j                   dk(  ry
|j                   S )Nstdoutr[   rj   rM   zstdint=>input_unit
input_unitstderrzstdint=>error_unit
error_unit*)r   ru   rx   r   )ry   strms     r~   _print_StreamzFCodePrinter._print_Stream  s    99 T^^J%?4%G_-112FGYY("t~~j'AT'I_-112FGyyH$yy r   c                     |j                   d k7  r j                  |j                         }nd}dj                  |dj                   fd|j                  D                    S )Nr  zprint {fmt}, {iolist}r   c              3  @   K   | ]  }j                  |        y wr   rB  r_  s     r~   r   z,FCodePrinter._print_Print.<locals>.<genexpr>  s     7#T[[7ra  )fmtiolist)format_stringr   r   r   
print_args)ry   psr  s   `  r~   _print_PrintzFCodePrinter._print_Print  sY    t#++b../CC&--#dii77?9- : 	:r   c                    |j                   \  }dj                  | j                  j                  dd      | j	                  |            S )Nz{result_name} = {arg}result_namesympy_result)r  r   )r   r   _contextrs   r   )ry   rsr   s      r~   _print_ReturnzFCodePrinter._print_Return  sE    ww&--))-HC  . 
 	
r   c                L    |j                   \  }|rd| j                  |      z  S y)Nz	return %sreturn)r   r   )ry   frsr   s      r~   _print_FortranReturnz!FCodePrinter._print_FortranReturn  s'    xxS!111r   c                    |j                  d      }|d}n|rd|d   z  nd} j                  j                  dd       }dj                  | j	                  |j
                        dj                  |j                  D cg c]  } j	                  |j                         c}      |rd	|z  nd|d
j                   fd|j                  D                    S c c}w )Nbind_Cr   z bind(C, name="%s")r   z bind(C)r  z<{entity}{name}({arg_names}){result}{bind}
{arg_declarations}r   z result(%s)r   c              3  R   K   | ]  }j                  t        |               y wr   )r   r   r_  s     r~   r   z%FCodePrinter._head.<locals>.<genexpr>  s     '_#K4D(E'_s   $')entityr   	arg_namesr~  bindarg_declarations)	rw  ru   rs   r   r   r   r   
parametersrc  )ry   r  fprG  bind_C_paramsr  r  r   s   `       r~   _headzFCodePrinter._head  s    x0 D?L(=+;;R\Dnn((=!
&RWW%iiBMM RSSZZ!8 RS4?MK/R!YY'_QSQ^Q^'_`  

	
 !Ss   ?"Cc                    dj                  | j                  |j                              }dj                  | j                  ||            S )N{} function z4interface
{function_head}
end function
end interface)function_head)r   r   return_typer  )ry   r  r  s      r~   _print_FunctionPrototypez%FCodePrinter._print_FunctionPrototype  sB    &&t{{2>>'BC &tzz&"5&
6	7r   c                   t         |j                  v rd}nt        |j                  v rd}nd}dj                  | j	                  |j
                              }t        | |j                        5  dj                  || j                  ||      | j	                  |j                              cd d d        S # 1 sw Y   y xY w)Nz
elemental zpure r   r  )r  z,{prefix}{function_head}
{body}
end function
)prefixr  rR  )
r   rx  r    r   r   r  r0   r   r  rR  )ry   fdr  r  s       r~   _print_FunctionDefinitionz&FCodePrinter._print_FunctionDefinition  s     !FRXXFF&&t{{2>>'BCTrww7 		! f"jj4[[)  		 		 		s   .=B55B>c                z    dj                  | j                  d|      | j                  |j                              S )Nz({subroutine_head}
{body}
end subroutine
zsubroutine )subroutine_headrR  )r   r  r   rR  )ry   subs     r~   _print_SubroutinezFCodePrinter._print_Subroutine  s;     & JJ}c:SXX&  
		
r   c                     dj                   j                  |j                        dj                   fd|j                  D                    S )Nzcall {name}({args})r   c              3  @   K   | ]  }j                  |        y wr   rB  r_  s     r~   r   z5FCodePrinter._print_SubroutineCall.<locals>.<genexpr>  s     NDKK,Nra  )r   r   )r   r   r   r   subroutine_args)ry   scalls   ` r~   _print_SubroutineCallz"FCodePrinter._print_SubroutineCall  sC    $++UZZ(N8M8MNO , 
 	
r   c                F     dt         fd|j                  D              z  S )Nz%s => %sc              3  @   K   | ]  }j                  |        y wr   rB  r_  s     r~   r   z1FCodePrinter._print_use_rename.<locals>.<genexpr>  s     "H4;;s#3"Hra  )tupler   )ry   rnms   ` r~   _print_use_renamezFCodePrinter._print_use_rename  s    E"Hsxx"HIIIr   c           
        d| j                  |j                        z  }|j                  d k7  r>|ddj                  |j                  D cg c]  }| j                  |       c}      z   z  }|j                  d k7  r>|ddj                  |j                  D cg c]  }| j                  |       c}      z   z  }|S c c}w c c}w )Nzuse %sr   z, only: )r   	namespacerenamer   only)ry   user~  r  nlys        r~   
_print_usezFCodePrinter._print_use  s    DKK66::dTYYCJJ'OSC(8'OPPPF88tj499#((-S3dkk#.>-S#TTTF (P-Ss   	B;C c                     y)Nexitr   ry   r   s     r~   _print_BreakTokenzFCodePrinter._print_BreakToken   s    r   c                     y)Ncycler   r
  s     r~   _print_ContinueTokenz!FCodePrinter._print_ContinueToken  s    r   c                ~      j                   d   dk\  rdnd}|dj                   fd|j                  D              z  S )Nr[   rj   z[%s]z(/%s/)r   c              3  @   K   | ]  }j                  |        y wr   rB  r_  s     r~   r   z7FCodePrinter._print_ArrayConstructor.<locals>.<genexpr>  s     "K4;;s#3"Kra  )ru   r   elements)ry   acfmtstrs   `  r~   _print_ArrayConstructorz$FCodePrinter._print_ArrayConstructor  s8    >>*5=8		"Kr{{"KLLLr   c                     dj                   j                  |j                        dj                   fd|j                  D                    S )Nr]  r   c              3  @   K   | ]  }j                  |        y wr   rB  r_  s     r~   r   z3FCodePrinter._print_ArrayElement.<locals>.<genexpr>  r`  ra  rb  )r   r   r   r   r   re  s   ` r~   _print_ArrayElementz FCodePrinter._print_ArrayElement
  sA    !((;;tyy)EEF ) 
 	
r   r   )Zr5  
__module____qualname____doc__printmethodlanguager   r   r   r   r   r   rd   r   r   r   r   r   r   r   r   re   rV  r]   __annotations__
_operatorsr#  rq   propertyr   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  r9  r<  rJ  rO  rT  r[  rg  rj  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  __classcell__)r}   s   @r~   rI   rI   F   s&   KKH 	g*L 	;Lk{{{yM 	(L
 #( )~  J 	dL,( \ \"#F;B	'	%(KTJ%6NGQ66&( 9J'TF&
"

.
B=1EN0d1DC!:

&7(

JM
r   rI   )Br  
__future__r   typingr   collectionsr   	itertoolsr   r  sympy.codegen.astr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   sympy.codegen.fnodesr   r   r   r   r   r   r   r    r!   r"   r#   
sympy.corer$   r%   r&   r'   r(   sympy.core.functionr)   sympy.core.numbersr*   sympy.core.relationalr+   
sympy.setsr,   sympy.printing.codeprinterr-   sympy.printing.precedencer.   r/   sympy.printing.printerr0   r1   r2   rr   rI   r   r   r~   <module>r/     sG  & #  #      
    0 / ( + $  2 < 2 :	5	5 
5 F	
 F F W F F F 
5 
5 
5 
5   
5!" 
5#*H
; H
r   