
    \ho                       S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	r	SSK
JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  SSK J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+  SS	K,J-r-J.r.J/r/J0r0J1r1  SS
K2J3r3  SSK4J5r5  SSK6J7r7  SSK8J9r9  SSK:J;r;  SSK<J=r=J>r>  SSK?J@r@  SSK:JArAJBrB  0 SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS _S!S"_S#S$_S%S&_rC " S' S(\;5      rDg))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                  N  ^  \ rS rSr% SrSrSr\\\	\
\\0r\S\S\
S\S\S	\S
\S\S\S\S\S0r\SS00r\" \R4                  40 S0 SSSSS.D6rS\S'   SSSSSS.rSS 0rS^U 4S! jjr\S" 5       r U 4S# jr!S$ r"S% r#S& r$S' r%S( r&S) r'S* r(S+ r)S, r*S- r+S. r,S/ r-S0 r.S1 r/S2 r0S3 r1S4 r2S5 r3S6 r4S7 r5S8 r6S9 r7S: r8S; r9S< r:S= r;S> r<S? r=S@ r>SA r?SB r@SC rASD rBSE rCSF rDSG rESH rFSI rGSJ rHSK rISL rJSM rKSN rLSO rMSP rNSQ rOSR rPSS rQST rRSU rSSV rTSW rUSX rVSY rWSZ rXS[ rYS\ rZS]r[U =r\$ )_FCodePrinterF   zAA 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           	       > U(       d  0 n0 U l         / U l        [        [        U R                  R                  5       UR                  S0 5      R                  5       5      5      U l        [        [        U R                  R                  5       UR                  S0 5      R                  5       5      5      U l        [        TU ]%  U5        [        [        5      U l
        UR                  S0 5      nU R                  R                  U5        1 SknU R                  S   U;  a  [        SU R                  S   -  5      e[        [         5      U l        g )Ntype_aliasestype_mappingsrW   >   B   rU   Z   _       rZ   zUnknown Fortran standard: %s)mangled_symbols	used_namedictr   rc   itemspoprd   super__init__known_functionsgetupdate	_settings
ValueErrorr   setmodule_uses)selfsettings	userfuncs	standards	__class__s       N/var/www/auris/envauris/lib/python3.13/site-packages/sympy/printing/fortran.pyrp   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                    U R                   S   S:X  a  SSSS.$ U R                   S   S:X  a  SSS	S.$ [        S
U R                   S   -  5      e)NrX   rT   z      z     @ zC     )codecontcommentfree z! zUnknown source format: %s)rt   ru   )rx   s    r}   _leadFCodePrinter._lead   sX    >>/*g5$iHMM^^O,6TBB84>>/;ZZ[[r   c                  > U R                   S   S:X  a  XR                  ;  a  UR                  nUR                  5       U R                  ;   a%  US-  nUR                  5       U R                  ;   a  M%  U R                  R                  UR                  5       5        X!R                  :X  a  XR                  U'   O[        U5      U R                  U'   UR                  U R                  5      n[        TU ]%  U5      nU$ )Nr[   T_)
rt   rj   namelowerrk   appendr+   xreplacero   _print_Symbol)rx   exprr   r|   s      r}   r   FCodePrinter._print_Symbol   s    >>/*d2///yyjjldnn4CKD jjldnn4%%djjl399$15((.17D((.==!5!56Dw$T*r   c                    U* S-  $ )N    )rx   ps     r}   _rate_index_position!FCodePrinter._rate_index_position   s    r!tr   c                    U$ Nr   )rx   
codestrings     r}   _get_statementFCodePrinter._get_statement   s    r   c                $    SR                  U5      $ )Nz! {})format)rx   texts     r}   _get_commentFCodePrinter._get_comment   s    }}T""r   c                B    SR                  XR                  U5      5      $ )Nzparameter ({} = {}))r   _print)rx   r   values      r}   _declare_number_const"FCodePrinter._declare_number_const   s    $++D++e2DEEr   c           	         U R                   R                  U[        UR                  U R                  S   5      5      45        [        U5      $ NrV   )_number_symbolsaddr*   evalfrt   strrx   r   s     r}   _print_NumberSymbol FCodePrinter._print_NumberSymbol   s>     	  $djj9T.U(V!WX4yr   c                B    U R                  U R                  U5      5      $ r   )_wrap_fortranindent_code)rx   liness     r}   _format_codeFCodePrinter._format_code   s    !!$"2"25"9::r   c                L   ^ UR                   u  mnU4S j[        U5       5       $ )Nc              3  P   >#    U  H  n[        T5        H  o"U4v   M
     M     g 7fr   )range).0jirowss      r}   	<genexpr>8FCodePrinter._traverse_matrix_indices.<locals>.<genexpr>   s     A1U4[A[s   #&)shaper   )rx   matcolsr   s      @r}   _traverse_matrix_indices%FCodePrinter._traverse_matrix_indices   s    YY
dAdAAr   c           	         / n/ nU Hq  n[        U R                  UR                  UR                  S-   UR                  S-   /5      u  pVnUR                  SU< SU< SU< 35        UR                  S5        Ms     X#4$ )N   do z = , end do)mapr   labelr   upperr   )rx   indices
open_linesclose_linesr   varstartstops           r}   _get_loop_opening_ending%FCodePrinter._get_loop_opening_ending   sw    
A"4;;WWaggk177Q;7 9C#udCDx(  &&r   c           
        SSK Jn  UR                  u  nUR                  (       a"  [	        S[        SU5      [        US5      5      nOUR                  (       d  UR                  (       aK  [	        [        [        S5      [        S5      5      X2" U5      -  [        U" U5      [        S5      5      5      nO<[	        [        S5      [        [        S5      U5      [        U[        S5      5      5      nU R                  U5      $ )Nr   )rC   r   )$sympy.functions.elementary.complexesrC   args
is_integerr    r   r.   
is_complexis_infiniter   r!   r   r   )rx   r   rC   argnew_exprs        r}   _print_sign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                   UR                   S   R                  S:w  a  [        S5      e/ nUR                  [        5      (       a  [        UR                   5       H  u  nu  pEUS:X  a$  UR                  SU R                  U5      -  5        OWU[        UR                   5      S-
  :X  a  US:X  a  UR                  S5        O#UR                  SU R                  U5      -  5        UR                  U R                  U5      5        M     UR                  S	5        S
R                  U5      $ U R                  S   S:  a  SnU R                  UR                   S   R                  5      n[        UR                   S S 5      nU(       aM  UR                  5       u  pEU R                  U5      nU R                  U5      n	UR                  XU	S9nU(       a  MM  U$ [        S5      e)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
rZ   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   condru   hasr   	enumerater   r   lenjoinrt   r   listrn   r   NotImplementedError)
rx   r   r   r   ecpatternr   termsr   s
             r}   _print_PiecewiseFCodePrinter._print_Piecewise   s   99R=%  / 0 0
 88J&tyy1	6A6LL$++a.!@A#dii.1,,dLL(LL!4t{{1~!EFT[[^, 2 LL"99U##^^J'2- 0G;;tyy}112D3B(Eyy{{{1~{{1~~~4~@	 %
 K & 'J K Kr   c                    SR                  U R                  UR                  [        S   SS9UR                  S-   UR
                  S-   5      $ )Nz
{}({}, {})AtomT)strictr   )r   parenthesizeparentr2   r   r   r   s     r}   _print_MatrixElement!FCodePrinter._print_MatrixElement   sN    ""4#4#4T[[6"4 $5 $126&&1*dffqjJ 	Jr   c           	     L   / n/ n/ nUR                    H~  nUR                  (       a$  UR                  (       a  UR                  U5        M8  UR                  (       a$  UR                  (       a  UR                  U5        Mm  UR                  U5        M     U(       a  U(       a  [        U5      n[        U6 nU R                  U5      nUR                  S5      (       a  Sn	USS  nOSn	[        U5      U:  a  SU-  nSU R                  [        U6 5      < SU R                  [        R                  * [        U6 -  5      < SU	< SU< 3$ SU R                  [        U6 5      < SU R                  [        R                  * [        U6 -  5      < S	3$ [        R                  " X5      $ )
N-r   +z(%s)zcmplx(,z)  ))r   	is_numberis_realr   is_imaginaryr1   r(   r   
startswithr'   ImaginaryUnitr0   
_print_Add)
rx   r   	pure_realpure_imaginarymixedr   PRECtermtsigns
             r}   r  FCodePrinter._print_Add  sX    	99C}}  %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                $   U R                   S   nUR                   Vs/ s H  n[        X25      PM     nnUR                  " U6 n[	        U[
        5      (       d  U R                  U5      $ [        R                  " XR                  " U6 5      $ s  snf r   )	rt   r   r)   func
isinstancer,   r   r0   _print_Function)rx   r   precar   	eval_exprs         r}   r  FCodePrinter._print_Function+  su    {+$(II.Iq!
I.IIt$	)X..;;y))..tYY5EFF /s   Bc                    U R                   S   S;   a  Sn[        U5      eUR                  u  p4SR                  U R	                  U5      U R	                  U5      5      $ )NrZ   )re   rU   z]Python % operator and SymPy's Mod() function are not supported by Fortran 66 or 77 standards.z      modulo({}, {}))rt   r   r   r   r   )rx   r   msgxys        r}   
_print_ModFCodePrinter._print_Mod5  sU    
 >>*%1>C%c**99DA)00QQPPr   c                    g)Nz
cmplx(0,1)r   r   s     r}   _print_ImaginaryUnit!FCodePrinter._print_ImaginaryUnitB  s    r   c                    [        U5      $ r   r   r   s     r}   
_print_intFCodePrinter._print_intF  s    4yr   c                    UR                   (       a7  UR                  (       a&  SU R                  [        R                  * U-  5      -  $ [
        R                  " X5      $ )Nzcmplx(0,%s))r   r  r   r'   r  r0   
_print_Mulr   s     r}   r#  FCodePrinter._print_MulI  sI    >>d// Q__,T12  ))$55r   c                F   [        U5      n[        UR                  S5      (       a;  U R                  [	        S5      5      < SU R                  UR                  U5      < 3$ [        UR                  S5      (       a  UR                  R                  (       aW  UR                  R                  (       a  SU R                  UR                  5      -  $ SU R                  UR                  5      -  $ SU R                  UR                  5      -  $ [        R                  " X5      $ )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)rx   r   r  s      r}   r)  FCodePrinter._print_PowR  s    $"%%JqM*!!$))T2  $((C((yy##99&&)DKK		,BBB+dkk$)).DDD!DKK		$:::))$55r   c                `    [        UR                  5      [        UR                  5      p2SX#4-  $ )Nz%d.0d0/%d.0d0)intr   q)rx   r   r   r-  s       r}   _print_RationalFCodePrinter._print_Rationale  s&    466{CK1!''r   c                    [         R                  " X5      nUR                  S5      nUS:  a  US U < SX#S-   S  < 3$ SU-  $ )Nr   r   dr   z%sd0)r0   _print_Floatfind)rx   r   printedr   s       r}   r2  FCodePrinter._print_Floati  sK    **46LLr6%bqk7q56?;;r   c                    U R                  UR                  5      nU R                  UR                  5      nUR                  nX@R                  ;  a  UOU R                  U   nSR                  X$U5      $ )Nz{} {} {})r   lhsrhsrel_op_relationalsr   )rx   r   lhs_coderhs_codeops        r}   _print_RelationalFCodePrinter._print_Relationalp  s_    ;;txx(;;txx([[...RD4E4Eb4I  x88r   c                    UR                    Vs/ s H  o R                  U5      PM     nnU R                  UR                  R                  5      < SSR	                  U5      < S3$ s  snf )N(r   r   )r   r   r'  r   r   )rx   r   r   indss       r}   _print_IndexedFCodePrinter._print_Indexedw  sI    )-7AQ7;;tyy74II 8s   A'c           	         U R                  UR                  5      nU R                  UR                  5      nU R                  SR	                  U R                  U5      U R                  UR
                  5      U R                  U5      5      5      $ )Nz{0} = {0} {1} {2})r   r7  r8  r   r   binop)rx   r   r;  r<  s       r}   _print_AugmentedAssignment'FCodePrinter._print_AugmentedAssignment{  sp    ;;txx(;;txx(""#6#=#=KK!4;;tzz#:DKK<Q$S T 	Tr   c                V   U R                  UR                  5      nUR                  S :w  a!  USU R                  UR                  5      -   -  nUR                  S :w  a!  USU R                  UR                  5      -   -  nUR                  R
                  R                  S5      < SU< S3$ )Nr   z, mask=r   rA  r   )r   arraydimmaskr|   __name__rstrip)rx   smparamss      r}   _print_sum_FCodePrinter._print_sum_  s    RXX&66T>dT[[000F77d?i$++bgg"666F<<0077<fEEr   c                $    U R                  U5      $ r   )rQ  )rx   prods     r}   _print_product_FCodePrinter._print_product_  s    %%r   c           	        ^  S/nUR                   S:X  a  UR                  S5        SnOSnSU-   S-   R                  " SSUR                  (       a  SOS0UR	                  U 4S	 jUS
9D6$ )N
concurrentr   stepr   , {step}z*do {concurrent}{counter} = {first}, {last}z
{body}
end do
zconcurrent c                &   > TR                  U 5      $ r   r   r   rx   s    r}   <lambda>(FCodePrinter._print_Do.<locals>.<lambda>  s    $++c*:r   )applyexcluder   )rY  r   r   rX  kwargs)rx   doexclrY  s   `   r}   	_print_DoFCodePrinter._print_Do  s    ~77a<KKDD 9= ?  &	 

 )+}2
 ii:DiI
 	
r   c                ~   ^  UR                   S:X  a  SOSnSU-   S-   R                  " S0 UR                  U 4S jS9D6$ )	Nr   r   rZ  z$({expr}, {counter} = {first}, {last}r   c                &   > TR                  U 5      $ r   r\  r]  s    r}   r^  3FCodePrinter._print_ImpliedDoLoop.<locals>.<lambda>  s    4;;s+;r   r`  r   )rY  r   rb  )rx   idlrY  s   `  r}   _print_ImpliedDoLoop!FCodePrinter._print_ImpliedDoLoop  sE    XX]r
6t;C?GG 
jj;j<
 	
r   c                   U R                  UR                  5      n[        UR                  [        5      (       a  UR                  R
                  u  p4nO[        S5      eU R                  UR                  5      nSR                  X#US-
  XVS9$ )Nz*Only iterable currently supported is Rangez3do {target} = {start}, {stop}, {step}
{body}
end dor   )targetr   r   rY  body)	r   ro  r  iterabler/   r   r   rp  r   )rx   r   ro  r   r   rY  rp  s          r}   
_print_ForFCodePrinter._print_For  s{    T[[)dmmU++ $ 2 2E%&RSS{{499% &$(! ! .	.r   c                   U R                   R                  X5      nU R                  R                  XR                  5      nU R                  R                  U5      nU(       a)  U H#  u  pEU R
                  U   R                  U5        M%     U$ r   )rc   rr   rd   r   type_modulesrw   r   )rx   type_type_strrw   kvs         r}   _print_TypeFCodePrinter._print_Type  su    !!%%e3%%))%<''++E2#  #''* $r   c                   ^  SR                  T R                  UR                  5      SR                  U 4S jUR                   5       5      S9$ )N{symbol}({idxs})r   c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   r\  r   r   rx   s     r}   r   .FCodePrinter._print_Element.<locals>.<genexpr>       EDKK,,   !symbolidxs)r   r   r  r   r   rx   elems   ` r}   _print_ElementFCodePrinter._print_Element  sA    !((;;t{{+EEF ) 
 	
r   c                    [        U5      $ r   r  )rx   exts     r}   _print_ExtentFCodePrinter._print_Extent  s    3xr   c           
       ^  UR                   nUR                  nUR                  S5      n[        [        [
        4 Vs/ s H  oUUR                  ;   PM     nnUR                  S5      S:X  a  SnO=UR                  S5      S:X  a  S/ SQUR                  S5         -  nO[        ST -  5      e[        U[        5      (       a  [        S	5      eT R                  S
   S:  a  SR                  T R                  UR                   5      ["        UR                  ;   a  SOSU(       a  SSR%                  U 4S jU 5       5      -  OSU[&        UR                  ;   a  SOST R                  UR(                  5      S9nUS :w  a  UST R                  U5      -  -  nU$ ["        UR                  ;   d  U(       a  [        S5      eSR%                  U 4S jUR                   UR(                  4 5       5      nU$ s  snf )N	dimensionTr   r   r   z, intent(%s))inoutinoutz!Multiple intents specified for %sz1Pointers are not available by default in Fortran.rZ   rf   z"{t}{vc}{dim}{intent}{alloc} :: {s}z, parameterz, dimension(%s)r   c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   r\  r  s     r}   r   2FCodePrinter._print_Declaration.<locals>.<genexpr>  s     2Ss4;;s3C3Csr  z, allocatable)r
  vcrK  intentallocsz = %sz0F77 init./parameter statem. req. multiple lines.r   c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   r\  r  s     r}   r   r    s     R;QCt{{3//;Qr  )variabler   attr_paramsr$   r%   r&   attrscountindexru   r  r	   r   rt   r   r   typer
   r   r   r  )rx   r   r   valrK  r  intentsresults   `       r}   _print_DeclarationFCodePrinter._print_Declaration  s   mmiiook*6?\5Z[5Z6SYY&5Z[==!#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                <    SU R                  [        S5      5      -  $ )Nz(huge(%s) + 1)r   )r   r!   r   s     r}   _print_InfinityFCodePrinter._print_Infinity  s    $++jm"<<<r   c                J   ^  SR                   " S0 UR                  U 4S jS9D6$ )Nz$do while ({condition})
{body}
end doc                &   > TR                  U 5      $ r   r\  r]  s    r}   r^  +FCodePrinter._print_While.<locals>.<lambda>  s    dkk#.r   rj  r   r   rb  r   s   ` r}   _print_WhileFCodePrinter._print_While  s1    7>> 1. BM B0 1 	1r   c                    g)Nz.true.r   r   s     r}   _print_BooleanTrueFCodePrinter._print_BooleanTrue  s    r   c                    g)Nz.false.r   r   s     r}   _print_BooleanFalse FCodePrinter._print_BooleanFalse  s    r   c                    / nU Hn  nUR                  S5      (       a4  UR                  U R                  S   USS  R                  5       -   5        MM  UR                  U R                  S   U-   5        Mp     U$ )N!r   r   r   )r  r   r   lstrip)rx   r   r  lines       r}   _pad_leading_columns!FCodePrinter._pad_leading_columns  sh    Ds##djj3d12hoo6GGHdjj0478	 
 r   c                  ^^	 [        S[        R                  -   [        R                  -   5      m[        S5      m	UU	4S jn/ nU R                  S   S:X  a  SnOSnU GH  nUR                  U R                  S   5      (       a  [        U5      S	:  a  UR                  S
SS	5      nUS:X  a  S	nUSU nXVS R                  5       nUR                  U5        U(       ap  UR                  S
SS5      nUS:X  d  [        U5      S:  a  SnUSU nXVS R                  5       nUR                  U R                  S   < U< 35        U(       a  Mn  M  M  UR                  U5        GM  UR                  U R                  S   5      (       a  U" US	5      nUSU R                  5       nXVS R                  5       nU(       a  Xt-  nUR                  U5        U(       ai  U" US5      nUSU R                  5       nXVS R                  5       nU(       a  Xt-  nUR                  U R                  S   < U< 35        U(       a  Mf  GM  GM  UR                  U5        GM     U$ )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 5      U::  a  [        T 5      $ UnU UU4S jnU" U5      (       d  US-  nUS:X  a  U$ U" U5      (       d  M  U$ )Nc                   > TU    T;   =(       a    TU S-
     T;  =(       d\    TU    T;  =(       a    TU S-
     T;   =(       d;    TU    T;   =(       a    TU S-
     T;  =(       d    TU    T;  =(       a    TU S-
     T;   $ )Nr   r   )posr  my_alnummy_whites    r}   r^  DFCodePrinter._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Jr   r   r   )r   )r  endposr  splitr  r  s   `   r}   split_pos_code2FCodePrinter._wrap_fortran.<locals>.split_pos_code  sX    4yF"4y CJE
 Cjjq!8!M Cjj Jr   rX   r   z &r   r   H   r      r   Nr   re   r   A   r   )rv   stringdigitsascii_lettersrt   r  r   r   rfindr  r   rN  )
rx   r   r  r  trailingr  r  hunkr  r  s
           @@r}   r   FCodePrinter._wrap_fortran  s    v-0D0DDEw<	 >>/*f4HHDtzz)455t9r>**S!R0Cby :D:,,.DMM$'"jja4"9D	B"$C#DSz#Dz002

90Et&LM $ MM$'F!344$T2.DSz((*Dz((*$Dd#(r2C:,,.D:,,.D(MMDJJv,>"EF d d#G H r   c                   [        U[        5      (       a1  U R                  UR                  S5      5      nSR	                  U5      $ U R
                  S   S:H  nU Vs/ s H  oDR                  S5      PM     nnSnSnU Vs/ s H+  n[        [        [        UR                  U5      5      5      PM-     nnU Vs/ s H+  n[        [        [        UR                  U5      5      5      PM-     nnU Vs/ s H-  n[        [        [        UR                  SS	/5      5      5      PM/     n	nS
n
S
nSn/ n[        U5       H  u  pUS;   a  UR                  U5        M  XU   -  n
U(       a  SX-  U-   -  nOSU
-  U-  nU< U< 3nU(       d  U R                  U/5      S
   nUR                  U5        X   (       a  SU-  nOS
nXU   -  n
M     U(       d  U R                  U5      $ U$ s  snf s  snf s  snf s  snf )z0Accepts a string of code or a list of code linesTr   rX   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   rt   r  r,  anyr   r  endswithr   r   r  r   )rx   r   
code_linesr   r  inc_keyworddec_keywordincreasedecreasecontinuationlevelcont_paddingtabwidthnew_coder   paddings                   r}   r   FCodePrinter.indent_code?  s   dC  ))$//$*?@J77:&&~~o.&8046U#6Sd "&(!% S+>?@!% 	 ( "&(!% S+>?@!% 	 ( &*,%)T ST]]S%L!ABC%) 	 ,  GAz!%a[ Eu~<=e)H,$d+D00$8;OOD! z a[ E+ '. %%h//S 7
((,s   G$2G)92G.14G3c                  ^  UR                   (       aL  SR                  SR                  U 4S jUR                   5       5      T R	                  UR                   5      S9$ UR                  u  nST R	                  U5      -  $ )Nzgo to ({labels}), {expr}r   c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   r\  r  s     r}   r   +FCodePrinter._print_GoTo.<locals>.<genexpr>t  s     !Jks$++c"2"2kr  )labelsr   zgo to %s)r   r   r   r  r   )rx   gotolbls   `  r}   _print_GoToFCodePrinter._print_GoToq  si    99-44yy!Jdkk!JK[[+ 5  
 ;;DCC 000r   c                J   ^  SR                   " S0 UR                  U 4S jS9D6$ )Nz"program {name}
{body}
end program
c                &   > TR                  U 5      $ r   r\  r]  s    r}   r^  -FCodePrinter._print_Program.<locals>.<lambda>  s    S1Ar   rj  r   r  )rx   progs   ` r}   _print_ProgramFCodePrinter._print_Program{  s7     &	 D ;;%A;B	D 	Dr   c                J   ^  SR                   " S0 UR                  U 4S jS9D6$ )NzAmodule {name}
{declarations}

contains

{definitions}
end module
c                &   > TR                  U 5      $ r   r\  r]  s    r}   r^  ,FCodePrinter._print_Module.<locals>.<lambda>  s    C0@r   rj  r   r  )rx   mods   ` r}   _print_ModuleFCodePrinter._print_Module  s7    
 & C ::$@:AC 	Cr   c                D   UR                   S:X  a2  U R                  S   S:  a  U R                  S   R                  S5        gUR                   S:X  a2  U R                  S   S:  a  U R                  S   R                  S5        g	UR                   S:X  a  g
UR                   $ )Nr   rZ   rh   rQ   zstdint=>input_unit
input_unitr   zstdint=>error_unit
error_unit*)r   rt   rw   r   )rx   strms     r}   _print_StreamFCodePrinter._print_Stream  s    99 T^^J%?4%G_-112FGYY("t~~j'AT'I_-112FGyyH$yy r   c                ,  ^  UR                   [        :X  a  SnSnOHSS[        S[        S0R	                  UR
                  S5      0-  nT R                  UR                   5      nUR                  USR                  U 4S jUR                   5       5      S	9$ )
Nzprint {fmt}, {iolist}r  z3write(%(out)s, fmt="{fmt}", advance="no"), {iolist}r  06r   c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   r\  r  s     r}   r   ,FCodePrinter._print_Print.<locals>.<genexpr>  s     7#T[[r  )fmtiolist)
format_stringr   r   r   rr   filer   r   r   
print_args)rx   pstemplater  s   `   r}   _print_PrintFCodePrinter._print_Print  s    t#.HCLVS155bggsCP H ++b../C3tyy7709 : 	:r   c                    UR                   u  nSR                  U R                  R                  SS5      U R	                  U5      S9$ )Nz{result_name} = {arg}result_namesympy_result)r  r   )r   r   _contextrr   r   )rx   rsr   s      r}   _print_ReturnFCodePrinter._print_Return  sE    ww&--))-HC  . 
 	
r   c                V    UR                   u  nU(       a  SU R                  U5      -  $ g)Nz	return %sreturn)r   r   )rx   frsr   s      r}   _print_FortranReturn!FCodePrinter._print_FortranReturn  s'    xxS!111r   c                  ^  UR                  S5      nUc  SnOU(       a  SUS   -  OSnT R                  R                  SS 5      nSR                  UT R	                  UR
                  5      SR                  UR                   Vs/ s H  nT R	                  UR                  5      PM      sn5      U(       a  S	U-  OSUS
R                  U 4S jUR                   5       5      S9$ s  snf )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  X   >#    U  H  nTR                  [        U5      5      v   M!     g 7fr   )r   r   r  s     r}   r   %FCodePrinter._head.<locals>.<genexpr>  s$     '_Q^#K4D(E(EQ^s   '*)entityr   	arg_namesr  bindarg_declarations)	r  rt   rr   r   r   r   r   
parametersr  )rx   r  fprb  bind_C_paramsr!  r  r   s   `       r}   _headFCodePrinter._head  s    x0 D?L(=+;;R\Dnn((=!
&RWW%iiBMM RMSSZZ!8M RS4?MK/R!YY'_QSQ^Q^'_`  

	
 !Ss   %C%c                    SR                  U R                  UR                  5      5      nSR                  U R                  X!5      S9$ )N{} function z4interface
{function_head}
end function
end interface)function_head)r   r   return_typer&  )rx   r$  r  s      r}   _print_FunctionPrototype%FCodePrinter._print_FunctionPrototype  s@    &&t{{2>>'BC &tzz&5&
6	7r   c                   [         UR                  ;   a  SnO[        UR                  ;   a  SnOSnSR                  U R	                  UR
                  5      5      n[        XR                  S9   SR                  UU R                  X15      U R	                  UR                  5      S9sS S S 5        $ ! , (       d  f       g = f)Nz
elemental zpure r   r)  )r  z,{prefix}{function_head}
{body}
end function
)prefixr*  rp  )
r"   r  r#   r   r   r+  r3   r   r&  rp  )rx   fdr/  r  s       r}   _print_FunctionDefinition&FCodePrinter._print_FunctionDefinition  s     !FRXXFF&&t{{2>>'BCTww7! f"jj4[[)   877s   .9B11
B?c                t    SR                  U R                  SU5      U R                  UR                  5      S9$ )Nz({subroutine_head}
{body}
end subroutine
zsubroutine )subroutine_headrp  )r   r&  r   rp  )rx   subs     r}   _print_SubroutineFCodePrinter._print_Subroutine  s;     & JJ}c:SXX&  
		
r   c                   ^  SR                  T R                  UR                  5      SR                  U 4S jUR                   5       5      S9$ )Nzcall {name}({args})r   c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   r\  r  s     r}   r   5FCodePrinter._print_SubroutineCall.<locals>.<genexpr>  s     N8MDKK,,8Mr  )r   r   )r   r   r   r   subroutine_args)rx   scalls   ` r}   _print_SubroutineCall"FCodePrinter._print_SubroutineCall  sC    $++UZZ(N8M8MNO , 
 	
r   c                H   ^  S[        U 4S jUR                   5       5      -  $ )Nz%s => %sc              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   r\  r  s     r}   r   1FCodePrinter._print_use_rename.<locals>.<genexpr>  s     "Hx4;;s#3#3xr  )tupler   )rx   rnms   ` r}   _print_use_renameFCodePrinter._print_use_rename  s    E"Hsxx"HIIIr   c           
        SU R                  UR                  5      -  nUR                  S :w  a@  USSR                  UR                   Vs/ s H  o0R                  U5      PM     sn5      -   -  nUR                  S :w  a@  USSR                  UR                   Vs/ s H  o@R                  U5      PM     sn5      -   -  nU$ s  snf s  snf )Nzuse %sr   z, only: )r   	namespacerenamer   only)rx   user  rC  nlys        r}   
_print_useFCodePrinter._print_use  s    DKK66::dTYYCJJ'OJSC(8J'OPPPF88tj499#((-S(3kk#.>(-S#TTTF (P-Ss   
CCc                    g)Nexitr   rx   r   s     r}   _print_BreakTokenFCodePrinter._print_BreakToken  s    r   c                    g)Ncycler   rP  s     r}   _print_ContinueToken!FCodePrinter._print_ContinueToken   s    r   c                   ^  T R                   S   S:  a  SOSnUSR                  U 4S jUR                   5       5      -  $ )NrZ   rh   z[%s]z(/%s/)r   c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   r\  r  s     r}   r   7FCodePrinter._print_ArrayConstructor.<locals>.<genexpr>  s     "K{4;;s#3#3{r  )rt   r   elements)rx   acfmtstrs   `  r}   _print_ArrayConstructor$FCodePrinter._print_ArrayConstructor  s8    >>*5=8		"Kr{{"KLLLr   c                   ^  SR                  T R                  UR                  5      SR                  U 4S jUR                   5       5      S9$ )Nr}  r   c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   r\  r  s     r}   r   3FCodePrinter._print_ArrayElement.<locals>.<genexpr>
  r  r  r  )r   r   r   r   r   r  s   ` r}   _print_ArrayElement FCodePrinter._print_ArrayElement  sA    !((;;tyy)EEF ) 
 	
r   )rq   rj   rw   rc   rd   rk   r   )]rM  
__module____qualname____firstlineno____doc__printmethodlanguager   r   r   r   r   r   rc   r   r   r   r   r   r   r   r   rd   ru  rl   r0   r\   __annotations__
_operatorsr:  rp   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r#  r)  r.  r2  r>  rC  rG  rQ  rU  re  rl  rr  rz  r  r  r  r  r  r  r  r  r   r   r  r  r  r  r  r  r  r&  r,  r1  r6  r=  rD  rL  rQ  rU  r]  rb  __static_attributes____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   )Erg  
__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   rq   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#*E
; E
r   