
    Rieo                       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          ZAdS ))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                  2    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                                        |                    di                                                               | _        t          t          | j                                        |                    di                                                               | _        t                      	                    |           t          t                    | _
        |                    di           }| j
                            |           h d}| j        d         |vrt          d| j        d         z            t          t                     | _        d S )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       6lib/python3.11/site-packages/sympy/printing/fortran.pyrq   zFCodePrinter.__init__|   sk    	H! t'8'>'>'@'@'/||NB'G'G'M'M'O'O"Q "Q R R!%(:(@(@(B(B(0_b(I(I(O(O(Q(Q#S #S T T"""#O44LL!1266	##I...000	>*%Y66;dn'?) ) * * *&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   sa    >/*g55$iHMMM^O,66TBBB84>/;ZZ[[[r   c                   | j         d         dk    r|| j        vr|j        }|                                | j        v r |dz  }|                                | j        v  | j                            |                                           ||j        k    r|| j        |<   nt          |          | 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24///yjjlldn44CKD jjlldn44%%djjll33349$$15D(..17D(.==!566Dww$$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                     |          S )Nz! {})format)ry   texts     r~   _get_commentzFCodePrinter._get_comment   s    }}T"""r   c                T    d                     ||                     |                    S )Nzparameter ({} = {}))r   _print)ry   r   values      r~   _declare_number_constz"FCodePrinter._declare_number_const   s$    $++D$++e2D2DEEEr   c           	         | j                             |t          |                    | j        d                             f           t          |          S )NrU   )_number_symbolsaddr'   evalfru   strry   r   s     r~   _print_NumberSymbolz FCodePrinter._print_NumberSymbol   sG     	  $djj9T.U.U(V(V!WXXX4yyr   c                R    |                      |                     |                    S r   )_wrap_fortranindent_code)ry   liness     r~   _format_codezFCodePrinter._format_code   s$    !!$"2"25"9"9:::r   c                N    |j         \  }fdt          |          D             S )Nc              3  D   K   | ]}t                    D ]}||fV  	d S r   )range).0jirowss      r~   	<genexpr>z8FCodePrinter._traverse_matrix_indices.<locals>.<genexpr>   s:      AA1U4[[AAAAAAAAAAr   )shaper   )ry   matcolsr   s      @r~   _traverse_matrix_indicesz%FCodePrinter._traverse_matrix_indices   s.    Y
dAAAAdAAAAr   c           	         g }g }|D ]f}t          | j        |j        |j        dz   |j        dz   g          \  }}}|                    d|d|d|           |                    d           g||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;Wagk17Q;7 9  9C###uuuddCDDDx((((;&&r   c           
     L   ddl m} |j        \  }|j        r.t	          dt          d|          t          |d                    }n|j        s|j        rjt	          t          t          d          t          d                    | ||          z  t           ||          t          d                              }nTt	          t          d          t          t          d          |          t          |t          d                              }|                     |          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   <<<<<<y> 	_Qar#qzz::HHn 	_ 	_U:a==*Q--@@#cc#hh,PRSVSVWZS[S[]ghi]j]jPkPkllHHZ]]E*Q--,E,Er#zZ[}}G]G]^^H{{8$$$r   c                   |j         d         j        dk    rt          d          g }|                    t                    rt          |j                   D ]\  }\  }}|dk    r,|                    d|                     |          z             nb|t          |j                   dz
  k    r|dk    r|                    d           n+|                    d|                     |          z             |                    |                     |                     |                    d	           d
	                    |          S | j
        d         dk    rd}|                     |j         d         j                  }t          |j         d d                   }|r[|                                \  }}|                     |          }|                     |          }	|                    |||	          }|[|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   9R=%%  / 0 0 0
 88J 	K&ty11 - -	6Aq66LL$++a..!@AAAA#di..1,,,dLL((((LL!4t{{1~~!EFFFT[[^^,,,,LL"""99U###^J'2-- 0G;;ty}122D3B3((E Ayy{{1{{1~~{{1~~~~4~@@	  A
 K & 'J K K Kr   c                    d                     |                     |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  sS    ""4#4#4T[6"4 $5 $1 $126&1*dfqjJ J 	Jr   c           	        g }g }g }|j         D ]_}|j        r|j        r|                    |           &|j        r|j        r|                    |           J|                    |           `|r|rt          |          }t          | }|                     |          }|                    d          rd}	|dd          }nd}	t          |          |k     rd|z  }d|                     t          |           d|                     t          j
         t          | z            d|	d|S d|                     t          |           d|                     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    	9 	" 	"C} " "  %%%% "3#3 "%%c****S!!!! 	6 !$''E{KK%%<<$$ D!""AADd##d**
A KKY0000KK 0n1E EFFFFDD!! & KKY0000KK 0n1E EFFFF 
 )$555r   c                    | j         d         fd|j        D             } |j        | }t          |t                    s|                     |          S t          j        |  |j        |           S )NrU   c                0    g | ]}t          |          S r   )r&   )r   aprecs     r~   
<listcomp>z0FCodePrinter._print_Function.<locals>.<listcomp>2  s!    ...q!T

...r   )ru   r   func
isinstancer)   r   r-   _print_Function)ry   r   r   	eval_exprr   s       @r~   r  zFCodePrinter._print_Function/  sy    {+....DI...DIt$	)X.. 	G;;y))).tYTY5EFFFr   c                    | j         d         dv rd}t          |          |j        \  }}d                    |                     |          |                     |                    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  s`    
 >*%11>C%c***9DAq)00QQPPPr   c                    dS )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    4yyr   c                    |j         r-|j        r&d|                     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  sQ    > 	6d/ 	6 Q_,T122  )$555r   c                   t          |          }t          |j        d          r@|                     t	          d                    d|                     |j        |          S t          |j        d          ro|j        j        rF|j        j        rd|                     |j                  z  S d|                     |j                  z  S d|                     |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    $"%% 	6JqMM****!!$)T222  $(C(( 
	6y# ;9& E)DKK	,B,BBB+dkk$).D.DDD!DKK	$:$:::)$555r   c                `    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*    46{{CKK1!Q''r   c                    t          j        | |          }|                    d          }|dk    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  s^    *466LLr66%bqbkkk71q566??;;r   c                    |                      |j                  }|                      |j                  }|j        }|| j        vr|n| j        |         }d                    |||          S )Nz{} {} {})r   lhsrhsrel_op_relationalsr   )ry   r   lhs_coderhs_codeops        r~   _print_RelationalzFCodePrinter._print_Relationalt  se    ;;tx((;;tx(([T...RRD4Eb4I  2x888r   c                      fd|j         D             }                     |j        j                  dd                    |          dS )Nc                :    g | ]}                     |          S r   r   )r   r   ry   s     r~   r  z/FCodePrinter._print_Indexed.<locals>.<listcomp>|  s#    777AQ777r   (r   r   )r   r   r  r   r   )ry   r   indss   `  r~   _print_IndexedzFCodePrinter._print_Indexed{  sM    7777777;;ty77774IIr   c                6    |                      |j                  S r   )r   r   r   s     r~   
_print_IdxzFCodePrinter._print_Idx  s    {{4:&&&r   c           	     :   |                      |j                  }|                      |j                  }|                     d                    |                      |          |                      |j                  |                      |                              S )Nz{0} = {0} {1} {2})r   r!  r"  r   r   binop)ry   r   r%  r&  s       r~   _print_AugmentedAssignmentz'FCodePrinter._print_AugmentedAssignment  s    ;;tx((;;tx((""#6#=#=KK!!4;;tz#:#:DKK<Q<Q$S $S T T 	Tr   c                ,   |                      |j                  }|j        d k    r |d|                      |j                  z   z  }|j        d k    r |d|                      |j                  z   z  }|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    RX&&6T>>dT[[0000F7d??i$++bg"6"666F<077<<<<fffEEr   c                ,    |                      |          S r   )r<  )ry   prods     r~   _print_product_zFCodePrinter._print_product_  s    %%%r   c                     dg}|j         dk    r|                    d           d}nd} d|z   dz   j        dd|j        rdndi|                     fd	|
          S )N
concurrentr   stepr   , {step}z*do {concurrent}{counter} = {first}, {last}z
{body}
end do
zconcurrent c                .                         |           S r   r+  r   ry   s    r~   <lambda>z(FCodePrinter._print_Do.<locals>.<lambda>  s    $++c*:*: r   )applyexcluder   )rB  r   r   rA  kwargs)ry   doexclrB  s   `   r~   	_print_DozFCodePrinter._print_Do  s    ~7a<<KKDDD8= ?  	
 

 )+=}}2
 ii::::DiII
 
 	
r   c                v     |j         dk    rdnd} d|z   dz   j        di |                     fd          S )	Nr   r   rC  z$({expr}, {counter} = {first}, {last}r   c                .                         |           S r   r+  rE  s    r~   rF  z3FCodePrinter._print_ImpliedDoLoop.<locals>.<lambda>  s    4;;s+;+; r   rG  r   )rB  r   rI  )ry   idlrB  s   `  r~   _print_ImpliedDoLoopz!FCodePrinter._print_ImpliedDoLoop  s]    X]]rr
G6t;C?G 
 
jj;;;;j<<
 
 	
r   c                   |                      |j                  }t          |j        t                    r|j        j        \  }}}nt          d          |                      |j                  }d                    |||dz
  ||          S )Nz*Only iterable currently supported is Rangez3do {target} = {start}, {stop}, {step}
{body}
end dor   )targetr   r   rB  body)	r   rS  r  iterabler,   r   r   rT  r   )ry   r   rS  r   r   rB  rT  s          r~   
_print_ForzFCodePrinter._print_For  s    T[))dmU++ 	T $ 2E4%&RSSS{{49%% &e$(! ! . .	.r   c                   | j                             ||          }| j                            ||j                  }| j                            |          }|r(|D ]%\  }}| 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3%))%<<'++E22 	+# + +1 #''****r   c                     d                                          |j                  d                     fd|j        D                                 S )N{symbol}({idxs})r   c              3  B   K   | ]}                     |          V  d S r   r+  r   r   ry   s     r~   r   z.FCodePrinter._print_Element.<locals>.<genexpr>  /      EEDKK,,EEEEEEr   symbolidxs)r   r   rd  r   r   ry   elems   ` r~   _print_ElementzFCodePrinter._print_Element  sW    !((;;t{++EEEEEEEFF ) 
 
 	
r   c                     t          |          S r   r  )ry   exts     r~   _print_ExtentzFCodePrinter._print_Extent  s    3xxr   c           
         |j         j        }                    d          }fdt          t          t
          fD             }|                    d          dk    rd}nL|                    d          dk    r!dg d|                    d                   z  }nt          d	 z            t          t                    rt          d
           j        d         dk    rd                                         j                  t           j        v rdnd|r$dd                     fd|D                       z  nd|t&          j        v rdnd                     j                            }|d k    r|d                     |          z  z  }nLt           j        v s|rt          d          d                     fdj        j        fD                       }|S )N	dimensionc                "    g | ]}|j         v S r   )attrs)r   intentr   s     r~   r  z3FCodePrinter._print_Declaration.<locals>.<listcomp>  s     [[[66SY&[[[r   Tr   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  B   K   | ]}                     |          V  d S r   r+  ra  s     r~   r   z2FCodePrinter._print_Declaration.<locals>.<genexpr>  s/      2S2S4;;s3C3C2S2S2S2S2S2Sr   z, allocatable)r   vcr6  rp  allocsz = %sz0F77 init./parameter statem. req. multiple lines.r   c              3  B   K   | ]}                     |          V  d S r   r+  ra  s     r~   r   z2FCodePrinter._print_Declaration.<locals>.<genexpr>  s/      RRCt{{3//RRRRRRr   )variabler   attr_paramsr!   r"   r#   countindexrv   r  r	   r   ru   r   r   typer
   ro  r   r   rd  )ry   r   valr6  intentsrp  resultr   s   `      @r~   _print_DeclarationzFCodePrinter._print_Declaration  s   miook**[[[[i\5Z[[[==!##FF]]4  A%%#&<&<&<W]]4=P=P&QQFF@4GHHHc7## 	[%&YZZZ>*%++9@@++ch''$/39$<$<=="X[c%		2S2S2S2Ss2S2S2S(T(TTTac)4	)A)Aoor++cj)) A  F d{{'DKK$4$444ci''3')*\]]]XXRRRRCHcj;QRRRSSFr   c                L    d|                      t          d                    z  S )Nz(huge(%s) + 1)r   )r   r   r   s     r~   _print_InfinityzFCodePrinter._print_Infinity  s    $++jmm"<"<<<r   c                L      dj         di |                     fd          S )Nz$do while ({condition})
{body}
end doc                .                         |           S r   r+  rE  s    r~   rF  z+FCodePrinter._print_While.<locals>.<lambda>  s    dkk#.. r   rO  r   r   rI  r   s   ` r~   _print_WhilezFCodePrinter._print_While  sJ    >7> 1 1.... BM B0 B0 1 1 	1r   c                    dS )Nz.true.r   r   s     r~   _print_BooleanTruezFCodePrinter._print_BooleanTrue  s    xr   c                    dS )Nz.false.r   r   s     r~   _print_BooleanFalsez FCodePrinter._print_BooleanFalse  s    yr   c                    g }|D ]x}|                     d          r>|                    | j        d         |dd                                          z              U|                    | j        d         |z              y|S )N!r   r   r   )r   r   r   lstrip)ry   r   r  lines       r~   _pad_leading_columnsz!FCodePrinter._pad_leading_columns  s     	9 	9Ds## 9dj3d122hoo6G6GGHHHHdj0478888r   c                &  	 t          dt          j        z   t          j        z             t          d          		fd}g }| j        d         dk    rd}nd}|D ]4}|                    | j        d                   rt          |          d	k    r|                    d
dd	          }|dk    rd	}|d|         }||d         	                                }|
                    |           |r~|                    d
dd          }|dk    st          |          dk     rd}|d|         }||d         	                                }|
                    | j        d         |           |~|
                    |           *|                    | j        d                   r ||d	          }|d|                                         }||d         	                                }|r||z  }|
                    |           |rq ||d          }|d|                                         }||d         	                                }|r||z  }|
                    | j        d         |           |q|
                    |           6|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 )Nc                    |          v r| dz
           vpD|          vo| dz
           v p-|          v o| dz
           vp|          vo| dz
           v S )Nr   r   )posr  my_alnummy_whites    r~   rF  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  s    4yyF""4yy CJ J J J J JE
 eCjj "q!88!M eCjj " Jr   rY   r   z &r   r   H   r      r   Nr   rg   r   A   r   )rw   stringdigitsascii_lettersru   r   r   r   rfindr  r   r9  )
ry   r   r  r  trailingr  r  hunkr  r  s
           @@r~   r   zFCodePrinter._wrap_fortran  s    v-0DDEEw<<	 	 	 	 	 	 >/*f44HHH #	$ #	$Dtz)455 "$t99r>>**S!R00Cbyy :D:,,..DMM$''' N"jja44"99D		B"$C#DSDz#CDDz0022
90E0Ett&LMMM  N MM$''''F!344 $$nT2..DSDz((**CDDz((** %H$Dd### G(.r22C:,,..D:,,..D )(MMDJv,>,>"EFFF  G d####r   c                   t          |t                    r=|                     |                    d                    }d                    |          S | j        d         dk    }d |D             }ddfd|D             }fd	|D             }d
 |D             }d}d}d}	g }
t          |          D ]\  }}|dv r|
                    |           |||         z  }|rd||	z  |z   z  }nd|z  |	z  }||}|s|                     |g          d         }|
                    |           ||         rd|	z  }nd}|||         z  }|s| 	                    |
          S |
S )z0Accepts a string of code or a list of code linesTr   rY   r   c                8    g | ]}|                     d           S )z 	)r  r   r  s     r~   r  z,FCodePrinter.indent_code.<locals>.<listcomp>M  s$    666U##666r   )r   zif(zif zdo
r   program	interface)r   enddor   endifr   zend programzend interfacec           
     n    g | ]1}t          t          t          |j                                      2S r   r  anyr   r   )r   r  inc_keywords     r~   r  z,FCodePrinter.indent_code.<locals>.<listcomp>R  E     ( ( ( S+>>??@@ ( ( (r   c           
     n    g | ]1}t          t          t          |j                                      2S r   r  )r   r  dec_keywords     r~   r  z,FCodePrinter.indent_code.<locals>.<listcomp>T  r  r   c                p    g | ]3}t          t          t          |j        d dg                              4S )&z&
)r  r  r   endswithr  s     r~   r  z,FCodePrinter.indent_code.<locals>.<listcomp>V  sH     , , ,! ST]S%L!A!ABBCC , , ,r   r      )r   r   r      )
r  r   r   
splitlinesr   ru   r   r   r  r   )ry   r   
code_linesr   increasedecreasecontinuationlevelcont_paddingtabwidthnew_coder   r  paddingr  r  s                 @@r~   r   zFCodePrinter.indent_codeF  s   dC   	'))$//$*?*?@@J77:&&&~o.&866666Sd( ( ( (!%( ( (( ( ( (!%( ( (, ,%), , ,   	! 	!GAtz!!%%%Xa[ E -uX~<=e)H,$Wdd+D <00$88;OOD!!!A ! z Xa[ EE 	0%%h///r   c                     |j         rSd                    d                     fd|j        D                                            |j                             S |j        \  }d                     |          z  S )Nzgo to ({labels}), {expr}r   c              3  B   K   | ]}                     |          V  d S r   r+  ra  s     r~   r   z+FCodePrinter._print_GoTo.<locals>.<genexpr>{  s/      !J!Js$++c"2"2!J!J!J!J!J!Jr   )labelsr   zgo to %s)r   r   r   r  r   )ry   gotolbls   `  r~   _print_GoTozFCodePrinter._print_GoTox  s    9 	1-44yy!J!J!J!Jdk!J!J!JKK[[++ 5   
 ;DCC 0 000r   c                L      dj         di |                     fd          S )Nz"program {name}
{body}
end program
c                .                         |           S r   r+  rE  s    r~   rF  z-FCodePrinter._print_Program.<locals>.<lambda>  s    S1A1A r   rO  r   r  )ry   progs   ` r~   _print_ProgramzFCodePrinter._print_Program  sM     	D D ;;%A%A%A%A;BB	D D 	Dr   c                L      dj         di |                     fd          S )NzAmodule {name}
{declarations}

contains

{definitions}
end module
c                .                         |           S r   r+  rE  s    r~   rF  z,FCodePrinter._print_Module.<locals>.<lambda>  s    C0@0@ r   rO  r   r  )ry   mods   ` r~   _print_ModulezFCodePrinter._print_Module  sM    
 C C ::$@$@$@$@:AAC C 	Cr   c                "   |j         dk    r3| j        d         dk    r"| j        d                             d           dS |j         dk    r3| j        d         dk    r"| j        d                             d           d	S |j         dk    rd
S |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    9  T^J%?4%G%G_-112FGGG<Y(""t~j'AT'I'I_-112FGGG<yH$$sy r   c                     |j         d k    r                     |j                   }nd}d                    |d                     fd|j        D                                 S )Nr  zprint {fmt}, {iolist}r   c              3  B   K   | ]}                     |          V  d S r   r+  ra  s     r~   r   z,FCodePrinter._print_Print.<locals>.<genexpr>  s/      77#T[[777777r   )fmtiolist)format_stringr   r   r   
print_args)ry   psr  s   `  r~   _print_PrintzFCodePrinter._print_Print  sr    t##++b.//CCC&--#dii7777777?9 ?9- : : 	:r   c                    |j         \  }d                    | j                            dd          |                     |                    S )Nz{result_name} = {arg}result_namesympy_result)r  r   )r   r   _contextrs   r   )ry   rsr   s      r~   _print_ReturnzFCodePrinter._print_Return  sL    w&--))-HHC   . 
 
 	
r   c                L    |j         \  }|rd|                     |          z  S dS )Nz	return %sreturn)r   r   )ry   frsr   s      r~   _print_FortranReturnz!FCodePrinter._print_FortranReturn  s/    x 	S!1!1118r   c                    |                     d          }|d}n|rd|d         z  nd} j                            dd           }d                    |                     |j                  d                     fd	|j        D                       |rd
|z  nd|d                     fd|j        D                                 S )Nbind_Cr   z bind(C, name="%s")r   z bind(C)r  z<{entity}{name}({arg_names}){result}{bind}
{arg_declarations}r   c                D    g | ]}                     |j                  S r   )r   rd  ra  s     r~   r  z&FCodePrinter._head.<locals>.<listcomp>  s'     R R RSSZ!8!8 R R Rr   z result(%s)r   c              3  \   K   | ]&}                     t          |                    V  'd S r   )r   r   ra  s     r~   r   z%FCodePrinter._head.<locals>.<genexpr>  s7      '_'_#K4D4D(E(E'_'_'_'_'_'_r   )entityr   	arg_namesr  bindarg_declarations)rz  ru   rs   r   r   r   r   
parameters)ry   r  fprI  bind_C_paramsr  r  s   `      r~   _headzFCodePrinter._head  s    x00 DD?L\(=+;;;R\Dn((==!
&RW%%ii R R R RBM R R RSS4?GMK//R!YY'_'_'_'_QSQ^'_'_'_``  
 

	
r   c                    d                     |                     |j                            }d                     |                     ||                    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  sL    &&t{{2>'B'BCC &tzz&"55&
6
6	7r   c                   t           |j        v rd}nt          |j        v rd}nd}d                    |                     |j                            }t          | |j                  5  d                    ||                     ||          |                     |j	                            cd d d            S # 1 swxY w Y   d S )Nz
elemental zpure r   r  )r  z,{prefix}{function_head}
{body}
end function
)prefixr  rT  )
r   ro  r    r   r   r  r0   r   r  rT  )ry   fdr  r  s       r~   _print_FunctionDefinitionz&FCodePrinter._print_FunctionDefinition  s	     !FFRXFFF&&t{{2>'B'BCCTrw777 		 		! f"jj44[[))   		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		s   (AB99B= B=c                    d                     |                     d|          |                     |j                            S )Nz({subroutine_head}
{body}
end subroutine
zsubroutine )subroutine_headrT  )r   r  r   rT  )ry   subs     r~   _print_SubroutinezFCodePrinter._print_Subroutine  sD     & JJ}c::SX&&  
 
		
r   c                     d                                          |j                  d                     fd|j        D                                 S )Nzcall {name}({args})r   c              3  B   K   | ]}                     |          V  d S r   r+  ra  s     r~   r   z5FCodePrinter._print_SubroutineCall.<locals>.<genexpr>  s/      NNDKK,,NNNNNNr   )r   r   )r   r   r   r   subroutine_args)ry   scalls   ` r~   _print_SubroutineCallz"FCodePrinter._print_SubroutineCall  sX    $++UZ((NNNN8MNNNOO , 
 
 	
r   c                J     dt           fd|j        D                       z  S )Nz%s => %sc              3  B   K   | ]}                     |          V  d S r   r+  ra  s     r~   r   z1FCodePrinter._print_use_rename.<locals>.<genexpr>  s/      "H"H4;;s#3#3"H"H"H"H"H"Hr   )tupler   )ry   rnms   ` r~   _print_use_renamezFCodePrinter._print_use_rename  s-    E"H"H"H"Hsx"H"H"HIIIIr   c                    d                      |j                  z  }|j        d k    r,|dd                     fd|j        D                       z   z  }|j        d k    r,|dd                     fd|j        D                       z   z  }|S )Nzuse %sr   c                :    g | ]}                     |          S r   r+  )r   r	  ry   s     r~   r  z+FCodePrinter._print_use.<locals>.<listcomp>  s%    'O'O'OSC(8(8'O'O'Or   z, only: c                :    g | ]}                     |          S r   r+  )r   nlyry   s     r~   r  z+FCodePrinter._print_use.<locals>.<listcomp>  s%    -S-S-S3dkk#.>.>-S-S-Sr   )r   	namespacerenamer   only)ry   user  s   `  r~   
_print_usezFCodePrinter._print_use  s    DKK666:dTYY'O'O'O'OCJ'O'O'OPPPPF8tj499-S-S-S-S#(-S-S-S#T#TTTFr   c                    dS )Nexitr   ry   r   s     r~   _print_BreakTokenzFCodePrinter._print_BreakToken   s    vr   c                    dS )Ncycler   r  s     r~   _print_ContinueTokenz!FCodePrinter._print_ContinueToken  s    wr   c                      j         d         dk    rdnd}|d                     fd|j        D                       z  S )Nr[   rj   z[%s]z(/%s/)r   c              3  B   K   | ]}                     |          V  d S r   r+  ra  s     r~   r   z7FCodePrinter._print_ArrayConstructor.<locals>.<genexpr>  s/      "K"K4;;s#3#3"K"K"K"K"K"Kr   )ru   r   elements)ry   acfmtstrs   `  r~   _print_ArrayConstructorz$FCodePrinter._print_ArrayConstructor  sK    >*5==8		"K"K"K"Kr{"K"K"KLLLLr   c                     d                                          |j                  d                     fd|j        D                                 S )Nr_  r   c              3  B   K   | ]}                     |          V  d S r   r+  ra  s     r~   r   z3FCodePrinter._print_ArrayElement.<locals>.<genexpr>  rb  r   rc  )r   r   r   r   r   rf  s   ` r~   _print_ArrayElementz FCodePrinter._print_ArrayElement
  sW    !((;;ty))EEEEEEEFF ) 
 
 	
r   r   )Zr8  
__module____qualname____doc__printmethodlanguager   r   r   r   r   r   rd   r   r   r   r   r   r   r   r   re   rX  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.  r0  r3  r<  r?  rL  rQ  rV  r]  rh  rk  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KH 	g*L 	;Lk{{{yM 	(L
 #( ) )      J 	dL, , , , , ,( \ \ X\    "    # # #F F F  ; ; ;B B B	' 	' 	'	% 	% 	%(K (K (KTJ J J%6 %6 %6NG G GQ Q Q    6 6 66 6 6&( ( (     9 9 9J J J' ' 'T T TF F F& & &
 
 
"
 
 

. 
. 
.  
 
 
    B= = =1 1 1      E E EN0 0 0d1 1 1D D DC C C! ! !: : :
 
 
  
 
 
&7 7 7  (
 
 

 
 
J J J      M M 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;     s   & # " " " " "       # # # # # #                                              
                          0 / / / / / / / / / / / / / ( ( ( ( ( ( + + + + + + $ $ $ $ $ $       2 2 2 2 2 2 < < < < < < < < 2 2 2 2 2 2 : 9 9 9 9 9 9 9	5	5 
5 F	
 F F W F F F 
5 
5 
5 
5   
5!" 
5#*H
 H
 H
 H
 H
; H
 H
 H
 H
 H
r   