
    g#p                       d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	Z	ddl
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ dd	l,m-Z-m.Z.m/Z/m0Z0m1Z1 dd
l2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z=m>Z> ddl?m@Z@ ddl:mAZAmBZB i dddddddddddddddddddddddddddd d!d"d#d$d%d&ZC G d' d(e;          ZDd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_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                  B    e Zd ZU dZdZdZeeee	e
eiZedede	dededed	ed
edededediZeddiiZ eej        fi di dddddZded<   ddddddZddiZd^ fd!	Zed"             Z fd#Z d$ Z!d% Z"d& Z#d' Z$d( Z%d) Z&d* Z'd+ Z(d, Z)d- Z*d. Z+d/ Z,d0 Z-d1 Z.d2 Z/d3 Z0d4 Z1d5 Z2d6 Z3d7 Z4d8 Z5d9 Z6d: Z7d; Z8d< Z9d= Z:d> Z;d? Z<d@ Z=dA Z>dB Z?dC Z@dD ZAdE ZBdF ZCdG ZDdH ZEdI ZFdJ ZGdK ZHdL ZIdM ZJdN ZKdO ZLdP ZMdQ ZNdR ZOdS ZPdT ZQdU ZRdV ZSdW ZTdX ZUdY ZVdZ ZWd[ ZXd\ ZYd] ZZ xZ[S )_FCodePrinterzAA printer to convert SymPy expressions to strings of Fortran code_fcodeFortranzinteger(c_int)zreal*4zreal*8zreal*10z	complex*8z
complex*16z	integer*1z	integer*2z	integer*4z	integer*8logicaliso_c_bindingc_int   fixedTM   )	precisionuser_functionssource_formatcontractstandardname_manglingzdict[str, Any]_default_settingsz.and.z.or.z.neqv.z.eqv.z.not. )andorxor
equivalentnotz!=z/=Nc           	        |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   rT   Z   _     rY   zUnknown Fortran standard: %s)mangled_symbols	used_namedictr   rb   itemspoprc   super__init__known_functionsgetupdate	_settings
ValueErrorr   setmodule_uses)selfsettings	userfuncs	standards	__class__s       R/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/printing/fortran.pyro   zFCodePrinter.__init__x   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            )NrW   rS   z      z     @ zC     )codecontcommentfree z! zUnknown source format: %s)rs   rt   )rw   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 )NrZ   T_)
rs   ri   namelowerrj   appendr+   xreplacern   _print_Symbol)rw   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    )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 )NrU   )_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   )rC   r   )$sympy.functions.elementary.complexesrC   args
is_integerr    r   r.   
is_complexis_infiniter   r!   r   r   )rw   r   rC   argnew_exprs        r|   _print_signzFCodePrinter._print_sign   s   <<<<<<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
rY   rg   zmerge({T}, {F}, {COND}))TFCONDzmUsing Piecewise as an expression using inline operators is not supported in standards earlier than Fortran95.)r   condrt   hasr   	enumerater   r   lenjoinrs   r   listrm   r   NotImplementedError)
rw   r   r   r   ecpatternr   termsr   s
             r|   _print_PiecewisezFCodePrinter._print_Piecewise   s   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parentr2   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_imaginaryr1   r(   r   
startswithr'   ImaginaryUnitr0   
_print_Add)
rw   r   	pure_realpure_imaginarymixedr   PRECtermtsigns
             r|   r   zFCodePrinter._print_Add  s    	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>.  s!    ...q!T

...r}   )rs   r   func
isinstancer,   r   r0   _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 )NrY   )re   rT   z]Python % operator and SymPy's Mod() function are not supported by Fortran 66 or 77 standards.z      modulo({}, {}))rs   r   r   r   r   )rw   r   msgxys        r|   
_print_ModzFCodePrinter._print_Mod5  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_ImaginaryUnitB  s    |r}   c                     t          |          S r   r   r   s     r|   
_print_intzFCodePrinter._print_intF  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   r0   
_print_Mulr   s     r|   r  zFCodePrinter._print_MulI  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))r1   r-   rA   r   r!   r   baser   	is_Numberr0   
_print_Pow)rw   r   r   s      r|   r  zFCodePrinter._print_PowR  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)rw   r   r   r  s       r|   _print_RationalzFCodePrinter._print_Rationale  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)r0   _print_Floatfind)rw   r   printedr   s       r|   r  zFCodePrinter._print_Floati  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   )rw   r   lhs_coderhs_codeops        r|   _print_RelationalzFCodePrinter._print_Relationalp  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   rw   s     r|   r   z/FCodePrinter._print_Indexed.<locals>.<listcomp>x  s#    777AQ777r}   (r   r   )r   r   r  r   r   )rw   r   indss   `  r|   _print_IndexedzFCodePrinter._print_Indexedw  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)rw   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)rw   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:  )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)  r   rw   s    r|   <lambda>z(FCodePrinter._print_Do.<locals>.<lambda>  s    $++c*:*: r}   )applyexcluder   )r@  r   r   r?  kwargs)rw   doexclr@  s   `   r|   	_print_DozFCodePrinter._print_Do  s    ~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   rA  z$({expr}, {counter} = {first}, {last}r   c                .                         |           S r   r)  rC  s    r|   rD  z3FCodePrinter._print_ImpliedDoLoop.<locals>.<lambda>  s    4;;s+;+; r}   rE  r   )r@  r   rG  )rw   idlr@  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   r@  body)	r   rQ  r  iterabler/   r   r   rR  r   )rw   r   rQ  r   r   r@  rR  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                     fd|j        D                                 S )N{symbol}({idxs})r   c              3  B   K   | ]}                     |          V  d S r   r)  r   r   rw   s     r|   r   z.FCodePrinter._print_Element.<locals>.<genexpr>  /      EEDKK,,EEEEEEr}   symbolidxs)r   r   rb  r   r   rw   elems   ` r|   _print_ElementzFCodePrinter._print_Element  sW    !((;;t{++EEEEEEEFF ) 
 
 	
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                     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.rY   rf   z"{t}{vc}{dim}{intent}{alloc} :: {s}z, parameterz, dimension(%s)r   c              3  B   K   | ]}                     |          V  d S r   r)  r_  s     r|   r   z2FCodePrinter._print_Declaration.<locals>.<genexpr>  s/      2S2S4;;s3C3C2S2S2S2S2S2Sr}   z, allocatable)r   vcr4  rn  allocsz = %sz0F77 init./parameter statem. req. multiple lines.r   c              3  B   K   | ]}                     |          V  d S r   r)  r_  s     r|   r   z2FCodePrinter._print_Declaration.<locals>.<genexpr>  s/      RRCt{{3//RRRRRRr}   )variabler   attr_paramsr$   r%   r&   countindexrt   r  r	   r   rs   r   r   typer
   rm  r   r   rb  )rw   r   valr4  intentsrn  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)  rC  s    r|   rD  z+FCodePrinter._print_While.<locals>.<lambda>  s    dkk#.. r}   rM  r   r   rG  r   s   ` r|   _print_WhilezFCodePrinter._print_While  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|   rD  zDFCodePrinter._wrap_fortran.<locals>.split_pos_code.<locals>.<lambda>  s    ch&H4a=+H Jc(*HtC!G}/HJch&H4a=+HJ c(*HtC!G}/H	 r}   r   r   )r   )r  endposr  splitr  r  s   `   r|   split_pos_codez2FCodePrinter._wrap_fortran.<locals>.split_pos_code  s    4yyF""4yy CJ J J J J JE
 eCjj "q!88!M eCjj " Jr}   rW   r   z &r   r   H   r      r   Nr   re   r   A   r   )ru   stringdigitsascii_lettersrs   r   r   r   rfindr  r   r7  )
rw   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   rW   r   c                8    g | ]}|                     d           S )z 	)r  r   r  s     r|   r   z,FCodePrinter.indent_code.<locals>.<listcomp>I  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>N  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>P  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>R  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_codeB  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)  r_  s     r|   r   z+FCodePrinter._print_GoTo.<locals>.<genexpr>w  s/      !J!Js$++c"2"2!J!J!J!J!J!Jr}   )labelsr   zgo to %s)r   r   r   r  r   )rw   gotolbls   `  r|   _print_GoTozFCodePrinter._print_GoTot  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)  rC  s    r|   rD  z-FCodePrinter._print_Program.<locals>.<lambda>  s    S1A1A r}   rM  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)  rC  s    r|   rD  z,FCodePrinter._print_Module.<locals>.<lambda>  s    C0@0@ r}   rM  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 )Nr   rY   rh   rP   zstdint=>input_unit
input_unitr   zstdint=>error_unit
error_unit*)r   rs   rv   r   )rw   strms     r|   _print_StreamzFCodePrinter._print_Stream  s    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                4    |j         t          k    rd}d}nHddt          dt          di                    |j        d          iz  }                     |j                   }|                    |d                     fd|j	        D                       	          S )
Nzprint {fmt}, {iolist}r  z3write(%(out)s, fmt="{fmt}", advance="no"), {iolist}rp  06r   c              3  B   K   | ]}                     |          V  d S r   r)  r_  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   rq   filer   r   r   
print_args)rw   pstemplater  s   `   r|   _print_PrintzFCodePrinter._print_Print  s    t##.HCCLVS155bgsCCP H ++b.//C3tyy777777709 09 : : 	: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                     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   rb  r_  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   r_  s     r|   r   z%FCodePrinter._head.<locals>.<genexpr>  s7      '_'_#K4D4D(E(E'_'_'_'_'_'_r}   )entityr   	arg_namesr~  bindarg_declarations)rx  rs   rq   r   r   r   r   
parameters)rw   r  fprG  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  )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  rR  )
r"   rm  r#   r   r   r  r3   r   r  rR  )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_headrR  )r   r  r   rR  )rw   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)  r_  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)rw   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)  r_  s     r|   r   z1FCodePrinter._print_use_rename.<locals>.<genexpr>  s/      "H"H4;;s#3#3"H"H"H"H"H"Hr}   )tupler   )rw   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	  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: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   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                     fd|j        D                       z  S )NrY   rh   z[%s]z(/%s/)r   c              3  B   K   | ]}                     |          V  d S r   r)  r_  s     r|   r   z7FCodePrinter._print_ArrayConstructor.<locals>.<genexpr>  s/      "K"K4;;s#3#3"K"K"K"K"K"Kr}   )rs   r   elements)rw   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)  r_  s     r|   r   z3FCodePrinter._print_ArrayElement.<locals>.<genexpr>  r`  r}   ra  )r   r   r   r   r   rd  s   ` r|   _print_ArrayElementz FCodePrinter._print_ArrayElement
  sW    !((;;ty))EEEEEEEFF ) 
 
 	
r}   r   )\r6  
__module____qualname____doc__printmethodlanguager   r   r   r   r   r   rb   r   r   r   r   r   r   r   r   rc   rV  rk   r0   r[   __annotations__
_operatorsr"  ro   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r
  r  r  r  r  r  r&  r,  r.  r1  r:  r=  rJ  rO  rT  r[  rf  ri  r  r  r  r  r  r  r   r   r  r  r  r  r  r  r  r  r  r  r   r  r
  r  r  r  r   r#  __classcell__)r{   s   @r|   rL   rL   F   s        KKKH 	g*L 	;Lk{{{yM 	(L )-[-J ) ) O O ) )      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}   rL   )Er&  
__future__r   typingr   collectionsr   	itertoolsr   r  sympy.codegen.astr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   sympy.codegen.fnodesr   r   r   r   r    r!   r"   r#   r$   r%   r&   
sympy.corer'   r(   r)   r*   r+   sympy.core.functionr,   sympy.core.numbersr-   sympy.core.relationalr.   
sympy.setsr/   sympy.printing.codeprinterr0   sympy.printing.precedencer1   r2   sympy.printing.printerr3   r4   r5   rp   rL   r   r}   r|   <module>r;     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}   