
    Edn                        d Z ddlmZmZ ddlmZ ddlmZ ddl	Z	ddl
mZmZmZmZmZ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l7m8Z8m9Z9 ddl:m;Z; ddl5m<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&e6          Z?dS )'aq  
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:

http://www.star.le.ac.uk/~cgp/prof77.html

Fortran is a case-insensitive language. This might cause trouble because
SymPy is case sensitive. So, fcode adds underscores to variable names when
it is necessary to make them different for Fortran.
    )Dict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)Eq)Range)CodePrinter)
precedence
PRECEDENCE)printer_context)fcodeprint_fcodesincostanasinacosatanatan2sinhcoshtanhlogexperfAbsabs	conjugateconjgMaxmaxMinminc                        e Zd ZdZdZdZeeee	e
eiZedede	dededed	ed
edededediZeddiiZdddi ddddddd
ZddddddZdd iZ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)d0 Z*d1 Z+d2 Z,d3 Z-d4 Z.d5 Z/d6 Z0d7 Z1d8 Z2d9 Z3d: Z4d; Z5d< Z6d= Z7d> Z8d? Z9d@ Z:dA Z;dB Z<dC Z=dD Z>dE Z?dF Z@dG ZAdH ZBdI ZCdJ ZDdK ZEdL ZFdM ZGdN ZHdO ZIdP ZJdQ ZKdR ZLdS ZMdT ZNdU ZOdV ZPdW ZQdX ZRdY ZSdZ ZTd[ ZUd\ ZV xZWS )^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.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_mappingsrU   >     B   rQ   Z   _     rZ   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       6lib/python3.11/site-packages/sympy/printing/fortran.pyro   zFCodePrinter.__init__z   sm    	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 	*;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            )NrX   rP   z      z     @ zC     )codecontcommentfree z! zUnknown source format: %s)rs   rt   )rw   s    r|   _leadzFCodePrinter._lead   sg    >/*g5 	\$iHMMM^O,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_)
rs   ri   namelowerrj   appendr(   xreplacern   _print_Symbol)rw   exprr   r{   s      r|   r   zFCodePrinter._print_Symbol   s    >/*d2 	74// >yjjlldn4  CKD jjlldn4  %%djjll33349$ >15D(..17D(.==!566Dww$$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                     |          S )Nz! {})format)rw   texts     r|   _get_commentzFCodePrinter._get_comment   s    }}T"""r}   c                 T    d                     ||                     |                    S )Nzparameter ({} = {}))r   _print)rw   r   values      r|   _declare_number_constz"FCodePrinter._declare_number_const   s$    $++D$++e2D2DEEEr}   c           	          | j                             |t          |                    | j        d                             f           t          |          S )NrT   )_number_symbolsaddr'   evalfrs   strrw   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)rw   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   )rw   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   )rw   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   )rw   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
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   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   9R=% 	0  / 0 0 0
 88J 	K&ty11 - -	6Aq6 GLL$++a..!@AAAA#di..1,, Gd GLL((((LL!4t{{1~~!EFFFT[[^^,,,,LL"""99U###^J'2- 	K 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)
rw   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 )NrT   c                 0    g | ]}t          |          S r   )r&   )r   aprecs     r|   
<listcomp>z0FCodePrinter._print_Function.<locals>.<listcomp>0  s!    ...q!T

...r}   )rs   r   func
isinstancer)   r   r,   _print_Function)rw   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 )NrZ   )re   rQ   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_Mod7  sc    
 >*%1 	Q>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_ImaginaryUnitD  s    |r}   c                      t          |          S r   r   r   s     r|   
_print_intzFCodePrinter._print_intH  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_MulK  sQ    > 	6d/ 	6 Q_,T122  )$555r}   c                    t          |          }|j        dk    r@|                     t          d                    d|                     |j        |          S |j        dk    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   baser   	is_Numberr,   
_print_Pow)rw   r   r   s      r|   r  zFCodePrinter._print_PowT  s    $8r> 	6JqMM****!!$)T222  X_ 
	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)rw   r   r   r  s       r|   _print_RationalzFCodePrinter._print_Rationalg  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)rw   r   printedr   s       r|   r  zFCodePrinter._print_Floatk  sa    *466LLr6 	<%bqbkkk71q566??;;r}   c                     |                      |j                  }|                      |j                  }|j        }|| j        vr|n| j        |         }d                    |||          S )Nz{} {} {})r   lhsrhsrel_op_relationalsr   )rw   r   lhs_coderhs_codeops        r|   _print_RelationalzFCodePrinter._print_Relationalr  se    ;;tx((;;tx(([T..IRRD4Eb4I  2x888r}   c                       fd|j         D             }                     |j        j                  dd                    |          dS )Nc                 :    g | ]}                     |          S r   r   )r   r   rw   s     r|   r   z/FCodePrinter._print_Indexed.<locals>.<listcomp>z  s#    777AQ777r}   (r   r   )r   r   r  r   r   )rw   r   indss   `  r|   _print_IndexedzFCodePrinter._print_Indexedy  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        t           fd||j        |g                     S )Nz{0} = {0} {1} {2}c                 .                         |           S r   r)  r   rw   s    r|   <lambda>z9FCodePrinter._print_AugmentedAssignment.<locals>.<lambda>  s    T[[-- r}   )r   r  r   r   r   r   binop)rw   r   r#  r$  s   `   r|   _print_AugmentedAssignmentz'FCodePrinter._print_AugmentedAssignment  st    ;;tx((;;tx((""#=#6#=----DJ13 3$4 5 5 	5r}   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)rw   smparamss      r|   _print_sum_zFCodePrinter._print_sum_  s    RX&&6T> 	1dT[[0000F7d? 	7i$++bg"6"666F<077<<<<fffEEr}   c                 ,    |                      |          S r   )r=  )rw   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)  r1  s    r|   r2  z(FCodePrinter._print_Do.<locals>.<lambda>  s    $++c*:*: r}   )applyexcluder   )rC  r   r   rB  kwargs)rw   doexclrC  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   rD  z$({expr}, {counter} = {first}, {last}r   c                 .                         |           S r   r)  r1  s    r|   r2  z3FCodePrinter._print_ImpliedDoLoop.<locals>.<lambda>      4;;s+;+; r}   rF  r   )rC  r   rH  )rw   idlrC  s   `  r|   _print_ImpliedDoLoopz!FCodePrinter._print_ImpliedDoLoop  s^    X]2rr
G6t;C?G 
 
jj;;;;j<<
 
 	
r}   c                    |                      |j                  }t          |j        t                    r|j        j        \  }}}nt          d          |                      |j                  }d                    |||||          S )Nz*Only iterable currently supported is Rangez3do {target} = {start}, {stop}, {step}
{body}
end do)targetr   r   rC  body)	r   rS  r  iterabler+   r   r   rT  r   )rw   r   rS  r   r   rC  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   )rb   rq   rc   r   type_modulesrv   r   )rw   type_type_strrv   kvs         r|   _print_TypezFCodePrinter._print_Type  s    !%%eU33%))%<<'++E22 	+# + +1 #''****r}   c           	           d                                          |j                  d                    t	           fd|j                                      S )Nz{symbol}({idxs})r   c                 .                         |           S r   r)  r1  s    r|   r2  z-FCodePrinter._print_Element.<locals>.<lambda>  rN  r}   )symbolidxs)r   r   r`  r   r   r   )rw   elems   ` r|   _print_ElementzFCodePrinter._print_Element  sV    !((;;t{++3;;;;T\JJKK ) 
 
 	
r}   c                      t          |          S r   r  )rw   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                    t'           fd|                    z  nd|t(          j        v rdnd                     j                            }|d k    r|d                     |          z  z  }nQt           j        v s|rt          d          d                    t'           fdj        j        g                    }|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.rZ   rf   z"{t}{vc}{dim}{intent}{alloc} :: {s}z, parameterz, dimension(%s)r   c                 .                         |           S r   r)  r1  s    r|   r2  z1FCodePrinter._print_Declaration.<locals>.<lambda>  s    $++cBRBR r}   z, allocatable)r   vcr7  rk  allocsz = %sz0F77 init./parameter statem. req. multiple lines.r   c                 .                         |           S r   r)  r1  s    r|   r2  z1FCodePrinter._print_Declaration.<locals>.<lambda>  s    dkk#.>.> r}   )variabler   attr_paramsr!   r"   r#   countindexrt   r  r	   r   rs   r   r   typer
   rj  r   r   r   r`  )rw   r   valr7  intentsrk  resultr   s   `      @r|   _print_DeclarationzFCodePrinter._print_Declaration  s   miook**[[[[i\5Z[[[==!# 	IFF]]4  A% 	I#&<&<&<W]]4=P=P&QQFF@4GHHHc7## 	[%&YZZZ>*%+ 	Y9@@++ch''$/39$<D=="]`h%		#6R6R6R6RTW2X2X(Y(YYYfh)4	)AIoor++cj)) A  F d{ 5'DKK$4$444ci' ^3 ^)*\]]]XXc">">">">3:@VWWXXF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)  r1  s    r|   r2  z+FCodePrinter._print_While.<locals>.<lambda>  s    dkk#.. r}   rO  r   r   rH  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)rw   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|   r2  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!8 "!M eCjj " Jr}   rX   r   z &r   r   H   r      r   Nr   re   r   A   r   )ru   stringdigitsascii_lettersrs   r   r   r   rfindr  r   r:  )
rw   r   r  r{  trailingr  r  hunkr  r  s
           @@r|   r   zFCodePrinter._wrap_fortran  s    v-0DDEEw<<	 	 	 	 	 	 >/*f4 	HHH #	$ #	$Dtz)455 "$t99r> (**S!R00Cby ! :D:,,..DMM$''' N"jja44"9 %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   rX   r   c                 8    g | ]}|                     d           S )z 	)r  r   r  s     r|   r   z,FCodePrinter.indent_code.<locals>.<listcomp>L  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>Q  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>S  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>U  sH     , , ,! ST]S%L!A!ABBCC , , ,r}   r      )r   r   r      )
r  r   r   
splitlinesr   rs   r   r   r  r   )rw   r   
code_linesr   increasedecreasecontinuationlevelcont_paddingtabwidthnew_coder   r  paddingr  r  s                 @@r|   r   zFCodePrinter.indent_codeE  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         rXd                    d                    t           fd|j                                                 |j                             S |j        \  }d                     |          z  S )Nzgo to ({labels}), {expr}r   c                 .                         |           S r   r)  r1  s    r|   r2  z*FCodePrinter._print_GoTo.<locals>.<lambda>z      S1A1A r}   )labelsr   zgo to %s)r   r   r   r   r  r   )rw   gotolbls   `  r|   _print_GoTozFCodePrinter._print_GoTow  s    9 	1-44yy%A%A%A%A4;!O!OPP[[++ 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)  r1  s    r|   r2  z-FCodePrinter._print_Program.<locals>.<lambda>  r  r}   rO  r   r  )rw   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)  r1  s    r|   r2  z,FCodePrinter._print_Module.<locals>.<lambda>  s    C0@0@ r}   rO  r   r  )rw   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stdoutrZ   rh   rL   zstdint=>input_unit
input_unitstderrzstdint=>error_unit
error_unit*)r   rs   rv   r   )rw   strms     r|   _print_StreamzFCodePrinter._print_Stream  s    9  
	!T^J%?4%G 
	!_-112FGGG<Y(" 	!t~j'AT'I 	!_-112FGGG<yH$ !sy r}   c           	           |j         d k    r                     |j                   }nd}d                    |d                    t	           fd|j                                      S )Nr  zprint {fmt}, {iolist}r   c                 .                         |           S r   r)  r1  s    r|   r2  z+FCodePrinter._print_Print.<locals>.<lambda>  s    DKK,, r}   )fmtiolist)format_stringr   r   r   r   
print_args)rw   psr  s   `  r|   _print_PrintzFCodePrinter._print_Print  st    t# 	++b.//CCC&--#dii,,,,bm<<?> ?>- ? ? 	?r}   c                     |j         \  }d                    | j                            dd          |                     |                    S )Nz{result_name} = {arg}result_namesympy_result)r  r   )r   r   _contextrq   r   )rw   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   )rw   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                    t           fd|j                                      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   r`  )r   r   rw   s     r|   r   z&FCodePrinter._head.<locals>.<listcomp>  s'     R R RSSZ!8!8 R R Rr}   z result(%s)r   c                 H                         t          |                     S r   )r   r   r1  s    r|   r2  z$FCodePrinter._head.<locals>.<lambda>  s    t{{;sCSCS7T7T r}   )entityr   	arg_namesr{  bindarg_declarations)	ru  rs   rq   r   r   r   r   
parametersr   )rw   r  fprH  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!YYs+T+T+T+TVXVc'd'dee  
 

	
r}   c                     d                     |                     |j                            }d                     |                     ||                    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  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   rj  r    r   r   r  r/   r   r  rT  )rw   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  )rw   subs     r|   _print_SubroutinezFCodePrinter._print_Subroutine  sD     & JJ}c::SX&&  
 
		
r}   c           	           d                                          |j                  d                    t	           fd|j                                      S )Nzcall {name}({args})r   c                 .                         |           S r   r)  r1  s    r|   r2  z4FCodePrinter._print_SubroutineCall.<locals>.<lambda>  rN  r}   )r   r   )r   r   r   r   r   subroutine_args)rw   scalls   ` r|   _print_SubroutineCallz"FCodePrinter._print_SubroutineCall  sW    $++UZ((3;;;;U=RSSTT , 
 
 	
r}   c                 T     dt          t           fd|j                            z  S )Nz%s => %sc                 .                         |           S r   r)  r1  s    r|   r2  z0FCodePrinter._print_use_rename.<locals>.<lambda>      $++c2B2B r}   )tupler   r   )rw   rnms   ` r|   _print_use_renamezFCodePrinter._print_use_rename  s,    E#&B&B&B&BCH"M"MNNNNr}   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  rw   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   nlyrw   s     r|   r   z+FCodePrinter._print_use.<locals>.<listcomp>  s%    -S-S-S3dkk#.>.>-S-S-Sr}   )r   	namespacerenamer   only)rw   user{  s   `  r|   
_print_usezFCodePrinter._print_use  s    DKK666: 	QdTYY'O'O'O'OCJ'O'O'OPPPPF8t 	Uj499-S-S-S-S#(-S-S-S#T#TTTFr}   c                     dS )Nexitr   rw   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                    t           fd|j                            z  S )NrZ   rh   z[%s]z(/%s/)r   c                 .                         |           S r   r)  r1  s    r|   r2  z6FCodePrinter._print_ArrayConstructor.<locals>.<lambda>  r  r}   )rs   r   r   elements)rw   acfmtstrs   `  r|   _print_ArrayConstructorz$FCodePrinter._print_ArrayConstructor  sJ    >*5=K8		#&B&B&B&BBK"P"PQQQQr}   r   )Xr9  
__module____qualname____doc__printmethodlanguager   r   r   r   r   r   rb   r   r   r   r   r   r   r   r   rc   rX  _default_settings
_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.  r4  r=  r@  rK  rQ  rV  r]  rc  rf  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|   rH   rH   D   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' ' '5 5 5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  (
 
 

 
 
O O O      R R R R R R Rr}   rH   )@r   typingr   tDictr   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.relationalr*   
sympy.setsr+   sympy.printing.codeprinterr,   sympy.printing.precedencer-   r.   sympy.printing.printerr/   r0   r1   rp   rH   r   r}   r|   <module>r4     so   & & % % % % % % % # # # # # #                                              
                          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#*CR CR CR CR CR; CR CR CR CR CRr}   