
    wg#p                       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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/m0Z0m1Z1 dd
l2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z=m>Z> ddl?m@Z@ ddl:mAZAmBZB 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&ZC G d' d(e;      ZDy))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_nonestderrstdout)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                  F    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 eej2                  fi di 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*d- Z+d. Z,d/ Z-d0 Z.d1 Z/d2 Z0d3 Z1d4 Z2d5 Z3d6 Z4d7 Z5d8 Z6d9 Z7d: Z8d; Z9d< Z:d= Z;d> Z<d? Z=d@ Z>dA Z?dB Z@dC ZAdD ZBdE ZCdF ZDdG ZEdH ZFdI ZGdJ ZHdK ZIdL ZJdM ZKdN ZLdO ZMdP ZNdQ ZOdR ZPdS ZQdT ZRdU ZSdV ZTdW ZUdX ZVdY ZWdZ ZXd[ ZYd\ ZZ xZ[S )^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_int   fixedTM   )	precisionuser_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   rT   Z   _     rY   zUnknown Fortran standard: %s)mangled_symbols	used_namedictr   rb   itemspoprc   super__init__known_functionsgetupdate	_settings
ValueErrorr   setmodule_uses)selfsettings	userfuncs	standards	__class__s       [/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/printing/fortran.pyro   zFCodePrinter.__init__x   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        )NrW   rS   z      z     @ zC     )codecontcommentfree z! zUnknown source format: %s)rs   rt   )rw   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 )NrZ   T_)
rs   ri   namelowerrj   appendr+   xreplacern   _print_Symbol)rw   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    )rw   ps     r|   _rate_index_positionz!FCodePrinter._rate_index_position   s    r!tr}   c                    |S Nr   )rw   
codestrings     r|   _get_statementzFCodePrinter._get_statement   s    r}   c                $    dj                  |      S )Nz! {})format)rw   texts     r|   _get_commentzFCodePrinter._get_comment   s    }}T""r}   c                D    dj                  || j                  |            S )Nzparameter ({} = {}))r   _print)rw   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*   evalfrs   strrw   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)rw   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   )rw   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   )rw   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   )rC   r   )$sympy.functions.elementary.complexesrC   args
is_integerr    r   r.   
is_complexis_infiniter   r!   r   r   )rw   r   rC   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
rY   rg   zmerge({T}, {F}, {COND}))TFCONDzmUsing Piecewise as an expression using inline operators is not supported in standards earlier than Fortran95.)r   condrt   hasr   	enumerater   r   lenjoinrs   r   listrm   r   NotImplementedError)
rw   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parentr2   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_imaginaryr1   r(   r   
startswithr'   ImaginaryUnitr0   
_print_Add)
rw   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   )	rs   r   r)   func
isinstancer,   r   r0   _print_Function)rw   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 )NrY   )re   rT   z]Python % operator and SymPy's Mod() function are not supported by Fortran 66 or 77 standards.z      modulo({}, {}))rs   r   r   r   r   )rw   r   msgxys        r|   
_print_ModzFCodePrinter._print_Mod5  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_ImaginaryUnitB  s    r}   c                    t        |      S r   r   r   s     r|   
_print_intzFCodePrinter._print_intF  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   r0   
_print_Mulr   s     r|   r  zFCodePrinter._print_MulI  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))r1   r-   rA   r   r!   r   baser   	is_Numberr0   
_print_Pow)rw   r   r   s      r|   r  zFCodePrinter._print_PowR  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)rw   r   r   r  s       r|   _print_RationalzFCodePrinter._print_Rationale  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)r0   _print_Floatfind)rw   r   printedr   s       r|   r  zFCodePrinter._print_Floati  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   )rw   r   lhs_coderhs_codeops        r|   _print_RelationalzFCodePrinter._print_Relationalp  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   )rw   r   r   indss       r|   _print_IndexedzFCodePrinter._print_Indexedw  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)rw   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)rw   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   )r7  )rw   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   rw   s    r|   <lambda>z(FCodePrinter._print_Do.<locals>.<lambda>  s    $++c*: r}   )applyexcluder   )r=  r   r   r<  kwargs)rw   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   r@  rA  s    r|   rB  z3FCodePrinter._print_ImpliedDoLoop.<locals>.<lambda>  s    4;;s+; r}   rC  r   )r=  r   rE  )rw   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   rO  r   iterabler/   r   r   rP  r   )rw   r   rO  r   r   r=  rP  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   )rb   rq   rc   r   type_modulesrv   r   )rw   type_type_strrv   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   r@  r   r   rw   s     r|   r   z.FCodePrinter._print_Element.<locals>.<genexpr>       EDKK,E   symbolidxs)r   r   ra  r   r   rw   elems   ` r|   _print_ElementzFCodePrinter._print_Element  sA    !((;;t{{+EEF ) 
 	
r}   c                    t        |      S r   r  )rw   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.rY   rf   z"{t}{vc}{dim}{intent}{alloc} :: {s}z, parameterz, dimension(%s)r   c              3  @   K   | ]  }j                  |        y wr   r@  r]  s     r|   r   z2FCodePrinter._print_Declaration.<locals>.<genexpr>  s     2S4;;s3C2Sr_  z, allocatable)r   vcr1  intentallocsz = %sz0F77 init./parameter statem. req. multiple lines.r   c              3  @   K   | ]  }j                  |        y wr   r@  r]  s     r|   r   z2FCodePrinter._print_Declaration.<locals>.<genexpr>  s     RCt{{3/Rr_  )variabler   attr_paramsr$   r%   r&   attrscountindexrt   r   r	   r   rs   r   r   typer
   r   r   ra  )rw   r   r   valr1  rp  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   r@  rA  s    r|   rB  z+FCodePrinter._print_While.<locals>.<lambda>  s    dkk#. r}   rK  r   r   rE  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)rw   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|   rB  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}   rW   r   z &r   r   H   r      r   Nr   re   r   A   r   )ru   stringdigitsascii_lettersrs   r   r   r   rfindr  r   r4  )
rw   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   rW   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   rs   r  r  anyr   r   endswithr   r   r  r   )rw   r   
code_linesr   r  inc_keyworddec_keywordincreasedecreasecontinuationlevelcont_paddingtabwidthnew_coder   paddings                   r|   r   zFCodePrinter.indent_codeB  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   r@  r]  s     r|   r   z+FCodePrinter._print_GoTo.<locals>.<genexpr>w  s     !Js$++c"2!Jr_  )labelsr   zgo to %s)r   r   r   r  r   )rw   gotolbls   `  r|   _print_GoTozFCodePrinter._print_GoTot  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   r@  rA  s    r|   rB  z-FCodePrinter._print_Program.<locals>.<lambda>  s    S1A r}   rK  r   r  )rw   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   r@  rA  s    r|   rB  z,FCodePrinter._print_Module.<locals>.<lambda>  s    C0@ r}   rK  r   r  )rw   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 )Nr   rY   rh   rP   zstdint=>input_unit
input_unitr   zstdint=>error_unit
error_unit*)r   rs   rv   r   )rw   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                   t        k(  rd}d}nHddt        dt        dij	                  |j
                  d      iz  } j                  |j                         }|j                  |dj                   fd|j                  D              	      S )
Nzprint {fmt}, {iolist}r  z3write(%(out)s, fmt="{fmt}", advance="no"), {iolist}rl  06r   c              3  @   K   | ]  }j                  |        y wr   r@  r]  s     r|   r   z,FCodePrinter._print_Print.<locals>.<genexpr>  s     7#T[[7r_  )fmtiolist)
format_stringr   r   r   rq   filer   r   r   
print_args)rw   pstemplater  s   `   r|   _print_PrintzFCodePrinter._print_Print  s    t#.HCLVS155bggsCP H ++b../C3tyy7709 : 	:r}   c                    |j                   \  }dj                  | j                  j                  dd      | j	                  |            S )Nz{result_name} = {arg}result_namesympy_result)r  r   )r   r   _contextrq   r   )rw   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   )rw   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)	ru  rs   rq   r   r   r   r   
parametersra  )rw   r  fprE  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  )rw   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  rP  )
r"   rv  r#   r   r   r  r3   r   r  rP  )rw   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_headrP  )r   r  r   rP  )rw   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   r@  r]  s     r|   r   z5FCodePrinter._print_SubroutineCall.<locals>.<genexpr>  s     NDKK,Nr_  )r   r   )r   r   r   r   subroutine_args)rw   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   r@  r]  s     r|   r   z1FCodePrinter._print_use_rename.<locals>.<genexpr>  s     "H4;;s#3"Hr_  )tupler   )rw   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)rw   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   rw   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 )NrY   rh   z[%s]z(/%s/)r   c              3  @   K   | ]  }j                  |        y wr   r@  r]  s     r|   r   z7FCodePrinter._print_ArrayConstructor.<locals>.<genexpr>  s     "K4;;s#3"Kr_  )rs   r   elements)rw   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   r@  r]  s     r|   r   z3FCodePrinter._print_ArrayElement.<locals>.<genexpr>  r^  r_  r`  )r   r   r   r   r   rc  s   ` r|   _print_ArrayElementz FCodePrinter._print_ArrayElement
  sA    !((;;tyy)EEF ) 
 	
r}   r   )\r3  
__module____qualname____doc__printmethodlanguager   r   r   r   r   r   rb   r   r   r   r   r   r   r   r   rc   rT  rk   r0   r[   __annotations__
_operatorsr!  ro   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+  r.  r7  r:  rH  rM  rR  rY  re  rh  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|   rL   rL   F   s0   KKH 	g*L 	;Lk{{{yM 	(L )-[-J-J ) O )~  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}   rL   )Er  
__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   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.codeprinterr0   sympy.printing.precedencer1   r2   sympy.printing.printerr3   r4   r5   rp   rL   r   r}   r|   <module>r/     sL  & #  #       
    0 / ( + $  2 < 2 :	5	5 
5 F	
 F F W F F F 
5 
5 
5 
5   
5!" 
5#*H
; H
r}   