o
    §ƒðjÛo  ã                   @  sÎ  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&“ZCG d'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                      s˜  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!d"„	Zed#d$„ ƒZ‡ fd%d&„Z d'd(„ Z!d)d*„ Z"d+d,„ Z#d-d.„ Z$d/d0„ Z%d1d2„ Z&d3d4„ Z'd5d6„ Z(d7d8„ Z)d9d:„ Z*d;d<„ Z+d=d>„ Z,d?d@„ Z-dAdB„ Z.dCdD„ Z/dEdF„ Z0dGdH„ Z1dIdJ„ Z2dKdL„ Z3dMdN„ Z4dOdP„ Z5dQdR„ Z6dSdT„ Z7dUdV„ Z8dWdX„ Z9dYdZ„ Z:d[d\„ Z;d]d^„ Z<d_d`„ Z=dadb„ Z>dcdd„ Z?dedf„ Z@dgdh„ ZAdidj„ ZBdkdl„ ZCdmdn„ ZDdodp„ ZEdqdr„ ZFdsdt„ ZGdudv„ ZHdwdx„ ZIdydz„ ZJd{d|„ ZKd}d~„ ZLdd€„ ZMd�d‚„ ZNdƒd„„ ZOd…d†„ ZPd‡dˆ„ ZQd‰dŠ„ ZRd‹dŒ„ ZSd�dŽ„ ZTd�d�„ ZUd‘d’„ ZVd“d”„ ZWd•d–„ ZXd—d˜„ ZY‡  ZZS )šÚ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é   ÚfixedTéM   )Ú	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¼   |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rWtd| jd  ƒ‚ttƒ| _d S )NÚtype_aliasesÚtype_mappingsrT   >   éØ  éB   rR   éZ   é_   éÓ  rW   zUnknown Fortran standard: %s)Úmangled_symbolsÚ	used_nameÚdictr   r_   ÚitemsÚpopr`   ÚsuperÚ__init__Úknown_functionsÚgetÚupdateÚ	_settingsÚ
ValueErrorr   ÚsetÚmodule_uses)ÚselfÚsettingsÚ	userfuncsÚ	standards©Ú	__class__© ú˜/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/printing/fortran.pyrl   x   s(   ÿÿ
ÿzFCodePrinter.__init__c                 C  sF   | j d dkrddddœS | j d dkrddd	dœS td
| j d  ƒ‚)NrU   rQ   z      z     @ zC     )ÚcodeÚcontÚcommentÚfreeÚ z! zUnknown source format: %s)rp   rq   ©rt   rz   rz   r{   Ú_leadŒ   s
   zFCodePrinter._leadc                   s’   | j d dkrA|| jvr;|j}| ¡ | jv r!|d7 }| ¡ | jv s| j | ¡ ¡ ||jkr4|| j|< nt|ƒ| j|< | | j¡}tƒ  	|¡}|S )NrX   TÚ_)
rp   rf   ÚnameÚlowerrg   Úappendr*   Úxreplacerk   Ú_print_Symbol)rt   Úexprr„   rx   rz   r{   rˆ   •   s   
ÿ
zFCodePrinter._print_Symbolc                 C  s
   | d S )Né   rz   )rt   Úprz   rz   r{   Ú_rate_index_position¦   ó   
z!FCodePrinter._rate_index_positionc                 C  s   |S ©Nrz   )rt   Ú
codestringrz   rz   r{   Ú_get_statement©   ó   zFCodePrinter._get_statementc                 C  s
   d  |¡S )Nz! {})Úformat)rt   Útextrz   rz   r{   Ú_get_comment¬   r�   zFCodePrinter._get_commentc                 C  s   d  ||  |¡¡S )Nzparameter ({} = {}))r’   Ú_print)rt   r„   Úvaluerz   rz   r{   Ú_declare_number_const¯   ó   z"FCodePrinter._declare_number_constc                 C  s(   | j  |t| | jd ¡ƒf¡ t|ƒS )NrS   )Ú_number_symbolsÚaddr)   Úevalfrp   Ústr©rt   r‰   rz   rz   r{   Ú_print_NumberSymbol²   s    z FCodePrinter._print_NumberSymbolc                 C  s   |   |  |¡¡S rŽ   )Ú_wrap_fortranÚindent_code)rt   Úlinesrz   rz   r{   Ú_format_code¸   s   zFCodePrinter._format_codec                   s    |j \‰ }‡ fdd„t|ƒD ƒS )Nc                 3  s&   � | ]}t ˆ ƒD ]}||fV  qqd S rŽ   )Úrange)Ú.0ÚjÚi©Úrowsrz   r{   Ú	<genexpr>½   s   €$ z8FCodePrinter._traverse_matrix_indices.<locals>.<genexpr>)Úshaper£   )rt   ÚmatÚcolsrz   r§   r{   Ú_traverse_matrix_indices»   s   
z%FCodePrinter._traverse_matrix_indicesc                 C  s^   g }g }|D ]$}t | j|j|jd |jd gƒ\}}}| d|||f ¡ | d¡ q||fS )Né   zdo %s = %s, %súend do)Úmapr•   Úlabelr…   Úupperr†   )rt   ÚindicesÚ
open_linesÚclose_linesr¦   ÚvarÚstartÚstoprz   rz   r{   Ú_get_loop_opening_ending¿   s   
ÿz%FCodePrinter._get_loop_opening_endingc                 C  s    ddl m} |j\}|jrtdtd|ƒt|dƒƒ}n1|js |jr9tt	t
dƒt
dƒƒ|||ƒ t||ƒt
dƒƒƒ}ntt
dƒtt
dƒ|ƒt|t
dƒƒƒ}|  |¡S )Nr   )rB   r®   )Ú$sympy.functions.elementary.complexesrB   ÚargsÚ
is_integerr   r   r-   Ú
is_complexÚis_infiniter   r    r   r•   )rt   r‰   rB   ÚargÚnew_exprrz   rz   r{   Ú_print_signÊ   s   2$
zFCodePrinter._print_signc           
      C  s4  |j d jdkrtdƒ‚g }| t¡r]t|j ƒD ]:\}\}}|dkr-| d|  |¡ ¡ n|t|j ƒd kr@|dkr@| d¡ n
| d|  |¡ ¡ | |  |¡¡ q| d	¡ d
 	|¡S | j
d dkr–d}|  |j d j¡}t|j d d… ƒ}|r”| ¡ \}}|  |¡}|  |¡}	|j|||	d�}|sz|S tdƒ‚)NéÿÿÿÿTz¼All 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Ú
rW   rd   zmerge({T}, {F}, {COND}))ÚTÚFÚCONDzmUsing Piecewise as an expression using inline operators is not supported in standards earlier than Fortran95.)r»   Úcondrq   Úhasr   Ú	enumerater†   r•   ÚlenÚjoinrp   r‰   Úlistrj   r’   ÚNotImplementedError)
rt   r‰   r¡   r¦   ÚeÚcÚpatternr|   ÚtermsrÉ   rz   rz   r{   Ú_print_PiecewiseÖ   s2   




üzFCodePrinter._print_Piecewisec                 C  s,   d  | j|jtd dd�|jd |jd ¡S )Nz
{}({}, {})ÚAtomT)Ústrictr®   )r’   ÚparenthesizeÚparentr1   r¦   r¥   r�   rz   rz   r{   Ú_print_MatrixElement   s
   ÿÿz!FCodePrinter._print_MatrixElementc           
      C  s  g }g }g }|j D ]}|jr|jr| |¡ q	|jr#|jr#| |¡ q	| |¡ q	|r~|rjt|ƒ}t|Ž }|  |¡}| d¡rHd}	|dd … }nd}	t|ƒ|k rTd| }d|  t|Ž ¡|  t	j
 t|Ž  ¡|	|f S d|  t|Ž ¡|  t	j
 t|Ž  ¡f S t | |¡S )Nú-r®   ú+z(%s)zcmplx(%s,%s) %s %szcmplx(%s,%s))r»   Ú	is_numberÚis_realr†   Úis_imaginaryr0   r'   r•   Ú
startswithr&   ÚImaginaryUnitr/   Ú
_print_Add)
rt   r‰   Ú	pure_realÚpure_imaginaryÚmixedr¿   ÚPRECÚtermÚtÚsignrz   rz   r{   rá     s<   


ýþzFCodePrinter._print_Addc                   sN   | j d ‰ ‡ fdd„|jD ƒ}|j|Ž }t|tƒs|  |¡S t | |j|Ž ¡S )NrS   c                   s   g | ]}t |ˆ ƒ‘qS rz   )r(   )r¤   Úa©Úprecrz   r{   Ú
<listcomp>.  ó    z0FCodePrinter._print_Function.<locals>.<listcomp>)rp   r»   ÚfuncÚ
isinstancer+   r•   r/   Ú_print_Function)rt   r‰   r»   Ú	eval_exprrz   rê   r{   rð   +  s   



zFCodePrinter._print_Functionc                 C  s<   | j d dv rd}t|ƒ‚|j\}}d |  |¡|  |¡¡S )NrW   )rb   rR   z]Python % operator and SymPy's Mod() function are not supported by Fortran 66 or 77 standards.z      modulo({}, {}))rp   rÏ   r»   r’   r•   )rt   r‰   ÚmsgÚxÚyrz   rz   r{   Ú
_print_Mod5  s
   
zFCodePrinter._print_Modc                 C  ó   dS )Nz
cmplx(0,1)rz   r�   rz   rz   r{   Ú_print_ImaginaryUnitB  s   z!FCodePrinter._print_ImaginaryUnitc                 C  ó   t |ƒS rŽ   ©rœ   r�   rz   rz   r{   Ú
_print_intF  ó   zFCodePrinter._print_intc                 C  s.   |j r|jrd|  tj | ¡ S t | |¡S )Nzcmplx(0,%s))rÜ   rÞ   r•   r&   rà   r/   Ú
_print_Mulr�   rz   rz   r{   rü   I  s
   ÿzFCodePrinter._print_Mulc                 C  sŒ   t |ƒ}t|jdƒrd|  tdƒ¡|  |j|¡f S t|jdƒr@|jjr8|jjr0d|  |j¡ S d|  |j¡ S d|  |j¡ S t	 
| |¡S )NrÂ   z%s/%sr®   g      à?zsqrt(%s.0d0)zsqrt(dble(%s))zsqrt(%s))r0   r,   r@   r•   r    r×   Úbaser¼   Ú	is_Numberr/   Ú
_print_Pow)rt   r‰   rå   rz   rz   r{   rÿ   R  s   þzFCodePrinter._print_Powc                 C  s"   t |jƒt |jƒ}}d||f S )Nz%d.0d0/%d.0d0)Úintr‹   Úq)rt   r‰   r‹   r  rz   rz   r{   Ú_print_Rationale  s   zFCodePrinter._print_Rationalc                 C  sF   t  | |¡}| d¡}|dkrd|d |… ||d d … f S d| S )NrÐ   rÂ   z%sd%sr®   z%sd0)r/   Ú_print_FloatÚfind)rt   r‰   ÚprintedrÐ   rz   rz   r{   r  i  s
   
 zFCodePrinter._print_Floatc                 C  sD   |   |j¡}|   |j¡}|j}|| jvr|n| j| }d |||¡S )Nz{} {} {})r•   ÚlhsÚrhsÚrel_opÚ_relationalsr’   )rt   r‰   Úlhs_codeÚrhs_codeÚoprz   rz   r{   Ú_print_Relationalp  s
   zFCodePrinter._print_Relationalc                   s0   ‡ fdd„|j D ƒ}dˆ  |jj¡d |¡f S )Nc                   ó   g | ]}ˆ   |¡‘qS rz   ©r•   )r¤   r¦   r�   rz   r{   rì   x  rí   z/FCodePrinter._print_Indexed.<locals>.<listcomp>ú%s(%s)ú, )r³   r•   rý   r±   rÍ   )rt   r‰   Úindsrz   r�   r{   Ú_print_Indexedw  s   zFCodePrinter._print_Indexedc              	   C  s@   |   |j¡}|   |j¡}|  d |   |¡|   |j¡|   |¡¡¡S )Nz{0} = {0} {1} {2})r•   r  r  r�   r’   Úbinop)rt   r‰   r
  r  rz   rz   r{   Ú_print_AugmentedAssignment{  s
   ÿz'FCodePrinter._print_AugmentedAssignmentc                 C  s^   |   |j¡}|jd kr|d|   |j¡ 7 }|jd kr$|d|   |j¡ 7 }d|jj d¡|f S )Nr  z, mask=r  rƒ   )r•   ÚarrayÚdimÚmaskry   Ú__name__Úrstrip)rt   ÚsmÚparamsrz   rz   r{   Ú_print_sum_�  s   

zFCodePrinter._print_sum_c                 C  s
   |   |¡S rŽ   )r  )rt   Úprodrz   rz   r{   Ú_print_product_‰  r�   zFCodePrinter._print_product_c                   s\   dg}|j dkr| d¡ d}nd}d| d jdd|jrdndi|j‡ fd	d
„|d�¤ŽS )NÚ
concurrentr®   Ústepr€   ú, {step}z*do {concurrent}{counter} = {first}, {last}z
{body}
end do
zconcurrent c                   ó
   ˆ   | ¡S rŽ   r  ©r¿   r�   rz   r{   Ú<lambda>š  ó   
 z(FCodePrinter._print_Do.<locals>.<lambda>)ÚapplyÚexcluderz   )r!  r†   r’   r   Úkwargs)rt   ÚdoÚexclr!  rz   r�   r{   Ú	_print_DoŒ  s   


üûúzFCodePrinter._print_Doc                   s:   |j dkrdnd}d| d jd	i |j‡ fdd„d�¤ŽS )
Nr®   r€   r"  z$({expr}, {counter} = {first}, {last}ú)c                   r#  rŽ   r  r$  r�   rz   r{   r%     r&  z3FCodePrinter._print_ImpliedDoLoop.<locals>.<lambda>©r'  rz   )r!  r’   r)  )rt   Úidlr!  rz   r�   r{   Ú_print_ImpliedDoLoop�  s   ÿz!FCodePrinter._print_ImpliedDoLoopc                 C  sT   |   |j¡}t|jtƒr|jj\}}}ntdƒ‚|   |j¡}dj|||d ||d�S )Nz*Only iterable currently supported is Rangez3do {target} = {start}, {stop}, {step}
{body}
end dor®   )Útargetr·   r¸   r!  Úbody)	r•   r1  rï   Úiterabler.   r»   rÏ   r2  r’   )rt   r‰   r1  r·   r¸   r!  r2  rz   rz   r{   Ú
_print_For£  s   ýzFCodePrinter._print_Forc                 C  sP   | j  ||¡}| j ||j¡}| j |¡}|r&|D ]\}}| j|  |¡ q|S rŽ   )r_   rn   r`   r„   Útype_modulesrs   rš   )rt   Útype_Útype_strrs   ÚkÚvrz   rz   r{   Ú_print_Type¯  s   zFCodePrinter._print_Typec                   ó,   dj ˆ  |j¡d ‡ fdd„|jD ƒ¡d�S )Nú{symbol}({idxs})r  c                 3  ó   � | ]}ˆ   |¡V  qd S rŽ   r  ©r¤   r¿   r�   rz   r{   r©   »  ó   € z.FCodePrinter._print_Element.<locals>.<genexpr>©ÚsymbolÚidxs)r’   r•   rA  rÍ   r³   ©rt   Úelemrz   r�   r{   Ú_print_Element¸  ó   
þzFCodePrinter._print_Elementc                 C  rø   rŽ   rù   )rt   Úextrz   rz   r{   Ú_print_Extent¾  rû   zFCodePrinter._print_Extentc              	     sN  |j ‰ˆj}ˆ d¡}‡fdd„tttfD ƒ}| d¡dkr!d}n| d¡dkr4dg d	¢| d¡  }ntd
ˆ  ƒ‚t	ˆt
ƒrCtdƒ‚ˆ jd dkrŠdjˆ  ˆj¡tˆjv rXdnd|ridd ‡ fdd„|D ƒ¡ nd|tˆjv rrdndˆ  ˆj¡d�}|d krˆ|dˆ  |¡ 7 }|S tˆjv s‘|r•tdƒ‚d ‡ fdd„ˆjˆjfD ƒ¡}|S )NÚ	dimensionc                   s   g | ]}|ˆ j v ‘qS rz   )Úattrs)r¤   Úintent)r¶   rz   r{   rì   Å  rí   z3FCodePrinter._print_Declaration.<locals>.<listcomp>Tr   r€   r®   z, intent(%s))ÚinÚoutÚinoutz!Multiple intents specified for %sz1Pointers are not available by default in Fortran.rW   rc   z"{t}{vc}{dim}{intent}{alloc} :: {s}z, parameterz, dimension(%s)r  c                 3  r=  rŽ   r  r>  r�   rz   r{   r©   Ó  r?  z2FCodePrinter._print_Declaration.<locals>.<genexpr>z, allocatable)rç   Úvcr  rK  ÚallocÚsz = %sz0F77 init./parameter statem. req. multiple lines.ú c                 3  r=  rŽ   r  r>  r�   rz   r{   r©   Ý  r?  )Úvariabler–   Úattr_paramsr#   r$   r%   ÚcountÚindexrq   rï   r   rÏ   rp   r’   r•   Útyper	   rJ  rÍ   r   rA  )rt   r‰   Úvalr  ÚintentsrK  Úresultrz   )rt   r¶   r{   Ú_print_DeclarationÁ  s6   


"
úü zFCodePrinter._print_Declarationc                 C  s   d|   tdƒ¡ S )Nz(huge(%s) + 1)r   )r•   r    r�   rz   rz   r{   Ú_print_Infinityâ  r˜   zFCodePrinter._print_Infinityc                   ó    dj di |j‡ fdd„d�¤ŽS )Nz$do while ({condition})
{body}
end doc                   r#  rŽ   r  r$  r�   rz   r{   r%  ç  r&  z+FCodePrinter._print_While.<locals>.<lambda>r.  rz   ©r’   r)  r�   rz   r�   r{   Ú_print_Whileå  s   

ÿzFCodePrinter._print_Whilec                 C  rö   )Nz.true.rz   r�   rz   rz   r{   Ú_print_BooleanTrueé  r‘   zFCodePrinter._print_BooleanTruec                 C  rö   )Nz.false.rz   r�   rz   rz   r{   Ú_print_BooleanFalseì  r‘   z FCodePrinter._print_BooleanFalsec                 C  sR   g }|D ]"}|  d¡r| | jd |dd …  ¡  ¡ q| | jd | ¡ q|S )Nú!r~   r®   r|   )rß   r†   r‚   Úlstrip)rt   r¡   rZ  Úlinerz   rz   r{   Ú_pad_leading_columnsï  s   
"z!FCodePrinter._pad_leading_columnsc                   sÒ  t dtj tj ƒ‰ t dƒ‰‡ ‡fdd„}g }| jd dkr!d}nd}|D ]Á}| | jd	 ¡r�t|ƒd
kr‡| ddd
¡}|dkrBd
}|d|… }||d…  	¡ }| 
|¡ |r†| ddd¡}|dksht|ƒdk rjd}|d|… }||d…  	¡ }| 
d| jd	 |f ¡ |sWq%| 
|¡ q%| | jd ¡rá||d
ƒ}|d|…  ¡ }||d…  	¡ }|r°||7 }| 
|¡ |rà||dƒ}|d|…  ¡ }||d…  	¡ }|rÒ||7 }| 
d| jd |f ¡ |s·q%| 
|¡ q%|S )zøWrap 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                   sP   t ˆ ƒ|kr
t ˆ ƒS |}‡ ‡‡fdd„}||ƒs&|d8 }|dkr"|S ||ƒr|S )Nc                   sp   ˆ |  ˆv rˆ | d  ˆvp7ˆ |  ˆvoˆ | d  ˆv p7ˆ |  ˆv o)ˆ | d  ˆvp7ˆ |  ˆvo7ˆ | d  ˆv S )Nr®   rz   )Úpos)rd  Úmy_alnumÚmy_whiterz   r{   r%  	  s   ÿþzDFCodePrinter._wrap_fortran.<locals>.split_pos_code.<locals>.<lambda>r®   r   )rÌ   )rd  Úendposrf  Úsplit©rg  rh  )rd  r{   Úsplit_pos_code  s   ýz2FCodePrinter._wrap_fortran.<locals>.split_pos_coderU   r   z &r€   r~   éH   rR  é   rÂ   Nr   rb   ú%s%sr|   éA   r}   )rr   ÚstringÚdigitsÚascii_lettersrp   rß   r‚   rÌ   Úrfindrc  r†   r  )rt   r¡   rl  rZ  Útrailingrd  rf  Úhunkrz   rk  r{   rŸ   ø  sX   

ú€


ú€zFCodePrinter._wrap_fortranc                   s<  t |tƒr|  | d¡¡}d |¡S | jd dk}dd„ |D ƒ}d‰d‰ ‡fd	d„|D ƒ}‡ fd
d„|D ƒ}dd„ |D ƒ}d}d}d}	g }
t|ƒD ]K\}}|dv rW|
 |¡ qI||| 8 }|rhd||	 |  }nd| |	 }d||f }|s~|  |g¡d }|
 |¡ || rŒd|	 }nd}||| 7 }qI|sœ|  	|
¡S |
S )z0Accepts a string of code or a list of code linesTr€   rU   r   c                 S  s   g | ]}|  d ¡‘qS )z 	)rc  ©r¤   rd  rz   rz   r{   rì   F  rí   z,FCodePrinter.indent_code.<locals>.<listcomp>)zdo zif(zif zdo
rÃ   ÚprogramÚ	interface)r¯   ÚenddorÄ   ÚendifrÃ   zend programzend interfacec                   ó    g | ]}t tt|jˆ ƒƒƒ‘qS rz   ©r   Úanyr°   rß   rw  )Úinc_keywordrz   r{   rì   K  ó    ÿc                   r|  rz   r}  rw  )Údec_keywordrz   r{   rì   M  r€  c                 S  s$   g | ]}t tt|jd dgƒƒƒ‘qS )ú&z&
)r   r~  r°   Úendswithrw  rz   rz   r{   rì   O  s    ÿr   é   )r€   rÅ   rR  ro  é   )
rï   rœ   r    Ú
splitlinesrÍ   rp   rË   r†   re  rŸ   )rt   r|   Ú
code_linesr   ÚincreaseÚdecreaseÚcontinuationÚlevelÚcont_paddingÚtabwidthÚnew_coder¦   rd  Úpaddingrz   )r�  r  r{   r    ?  sN   


ÿ
ÿÿ



zFCodePrinter.indent_codec                   sH   |j rdjd ‡ fdd„|jD ƒ¡ˆ  |j ¡d�S |j\}dˆ  |¡ S )Nzgo to ({labels}), {expr}r  c                 3  r=  rŽ   r  r>  r�   rz   r{   r©   t  r?  z+FCodePrinter._print_GoTo.<locals>.<genexpr>)Úlabelsr‰   zgo to %s)r‰   r’   rÍ   r�  r•   )rt   ÚgotoÚlblrz   r�   r{   Ú_print_GoToq  s   
þzFCodePrinter._print_GoToc                   r]  )Nz"program {name}
{body}
end program
c                   r#  rŽ   r  r$  r�   rz   r{   r%  €  r&  z-FCodePrinter._print_Program.<locals>.<lambda>r.  rz   r^  )rt   Úprogrz   r�   r{   Ú_print_Program{  s
   üüzFCodePrinter._print_Programc                   r]  )NzAmodule {name}
{declarations}

contains

{definitions}
end module
c                   r#  rŽ   r  r$  r�   rz   r{   r%  ‰  r&  z,FCodePrinter._print_Module.<locals>.<lambda>r.  rz   r^  )rt   Úmodrz   r�   r{   Ú_print_Module‚  s
   úúzFCodePrinter._print_Modulec                 C  sl   |j dkr| jd dkr| jd  d¡ dS |j dkr,| jd dkr,| jd  d¡ d	S |j dkr3d
S |j S )Nr   rW   re   rN   zstdint=>input_unitÚ
input_unitr   zstdint=>error_unitÚ
error_unitÚ*)r„   rp   rs   rš   )rt   Ústrmrz   rz   r{   Ú_print_Stream‹  s   
zFCodePrinter._print_Streamc                   sb   |j tkr
d}d}nddtdtdi |jd¡i }ˆ  |j ¡}|j|d ‡ fdd	„|j	D ƒ¡d
�S )Nzprint {fmt}, {iolist}rš  z3write(%(out)s, fmt="{fmt}", advance="no"), {iolist}rM  Ú0Ú6r  c                 3  r=  rŽ   r  r>  r�   rz   r{   r©   ¢  r?  z,FCodePrinter._print_Print.<locals>.<genexpr>)ÚfmtÚiolist)
Úformat_stringr   r   r   rn   Úfiler•   r’   rÍ   Ú
print_args)rt   ÚpsÚtemplaterŸ  rz   r�   r{   Ú_print_Print˜  s   
ÿ
ÿzFCodePrinter._print_Printc                 C  s&   |j \}dj| j dd¡|  |¡d�S )Nz{result_name} = {arg}Úresult_nameÚsympy_result)r§  r¿   )r»   r’   Ú_contextrn   r•   )rt   Úrsr¿   rz   rz   r{   Ú_print_Return¤  s
   þzFCodePrinter._print_Returnc                 C  s   |j \}|rd|  |¡ S dS )Nz	return %sÚreturn)r»   r•   )rt   Úfrsr¿   rz   rz   r{   Ú_print_FortranReturn«  s   z!FCodePrinter._print_FortranReturnc                   s�   |  d¡}|d u rd}n
|rd|d  nd}ˆ j dd ¡}dj|ˆ  |j¡d ‡ fd	d
„|jD ƒ¡|r7d| nd|d ‡ fdd„|jD ƒ¡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                   s   g | ]}ˆ   |j¡‘qS rz   )r•   rA  r>  r�   rz   r{   rì   ¿  s    z&FCodePrinter._head.<locals>.<listcomp>z result(%s)rÅ   c                 3  s   � | ]
}ˆ   t|ƒ¡V  qd S rŽ   )r•   r   r>  r�   rz   r{   r©   Â  s   € z%FCodePrinter._head.<locals>.<genexpr>)Úentityr„   Ú	arg_namesrZ  ÚbindÚarg_declarations)rT  rp   rn   r’   r•   r„   rÍ   Ú
parameters)rt   r°  Úfpr)  Úbind_C_paramsr²  r§  rz   r�   r{   Ú_head²  s   

÷zFCodePrinter._headc                 C  s&   d  |  |j¡¡}dj |  ||¡d�S )Nú{} function z4interface
{function_head}
end function
end interface)Úfunction_head)r’   r•   Úreturn_typer·  )rt   rµ  r°  rz   rz   r{   Ú_print_FunctionPrototypeÅ  s   ûz%FCodePrinter._print_FunctionPrototypec                 C  sˆ   t |jv rd}n
t|jv rd}nd}d |  |j¡¡}t| |jd�� dj||  ||¡|  |j	¡d�W  d   ƒ S 1 s=w   Y  d S )Nz
elemental zpure r€   r¸  )r§  z,{prefix}{function_head}
{body}
end function
)Úprefixr¹  r2  )
r!   rJ  r"   r’   r•   rº  r2   r„   r·  r2  )rt   Úfdr¼  r°  rz   rz   r{   Ú_print_FunctionDefinitionÎ  s   



ù$ÿz&FCodePrinter._print_FunctionDefinitionc                 C  s   dj |  d|¡|  |j¡d�S )Nz({subroutine_head}
{body}
end subroutine
zsubroutine )Úsubroutine_headr2  )r’   r·  r•   r2  )rt   Úsubrz   rz   r{   Ú_print_Subroutineâ  s
   

úzFCodePrinter._print_Subroutinec                   r;  )Nzcall {name}({args})r  c                 3  r=  rŽ   r  r>  r�   rz   r{   r©   ï  r?  z5FCodePrinter._print_SubroutineCall.<locals>.<genexpr>)r„   r»   )r’   r•   r„   rÍ   Úsubroutine_args)rt   Úscallrz   r�   r{   Ú_print_SubroutineCallì  rF  z"FCodePrinter._print_SubroutineCallc                   s   dt ‡ fdd„|jD ƒƒ S )Nz%s => %sc                 3  r=  rŽ   r  r>  r�   rz   r{   r©   ó  r?  z1FCodePrinter._print_use_rename.<locals>.<genexpr>)Útupler»   )rt   Úrnmrz   r�   r{   Ú_print_use_renameò  s   zFCodePrinter._print_use_renamec                   sl   dˆ   |j¡ }|jd kr|dd ‡ fdd„|jD ƒ¡ 7 }|jd kr4|dd ‡ fdd„|jD ƒ¡ 7 }|S )Nzuse %sr  c                   r  rz   r  )r¤   rÆ  r�   rz   r{   rì   ø  rí   z+FCodePrinter._print_use.<locals>.<listcomp>z, only: c                   r  rz   r  )r¤   Únlyr�   rz   r{   rì   ú  rí   )r•   Ú	namespaceÚrenamerÍ   Úonly)rt   ÚuserZ  rz   r�   r{   Ú
_print_useõ  s   
"
"zFCodePrinter._print_usec                 C  rö   )NÚexitrz   ©rt   rƒ   rz   rz   r{   Ú_print_BreakTokený  r‘   zFCodePrinter._print_BreakTokenc                 C  rö   )NÚcyclerz   rÏ  rz   rz   r{   Ú_print_ContinueToken   r‘   z!FCodePrinter._print_ContinueTokenc                   s4   ˆ j d dkr	dnd}|d ‡ fdd„|jD ƒ¡ S )NrW   re   z[%s]z(/%s/)r  c                 3  r=  rŽ   r  r>  r�   rz   r{   r©     r?  z7FCodePrinter._print_ArrayConstructor.<locals>.<genexpr>)rp   rÍ   Úelements)rt   ÚacÚfmtstrrz   r�   r{   Ú_print_ArrayConstructor  s   z$FCodePrinter._print_ArrayConstructorc                   r;  )Nr<  r  c                 3  r=  rŽ   r  r>  r�   rz   r{   r©   
  r?  z3FCodePrinter._print_ArrayElement.<locals>.<genexpr>r@  )r’   r•   r„   rÍ   r³   rC  rz   r�   r{   Ú_print_ArrayElement  rF  z FCodePrinter._print_ArrayElementrŽ   )[r  Ú
__module__Ú__qualname__Ú__doc__ÚprintmethodÚlanguager   r   r   r   r   r   r_   r   r
   r   r   r   r   r   r   r`   r5  rh   r/   rY   Ú__annotations__Ú
_operatorsr	  rl   Úpropertyr‚   rˆ   rŒ   r�   r”   r—   rž   r¢   r­   r¹   rÁ   rÔ   rÙ   rá   rð   rõ   r÷   rú   rü   rÿ   r  r  r  r  r  r  r  r,  r0  r4  r:  rE  rH  r[  r\  r_  r`  ra  re  rŸ   r    r“  r•  r—  rœ  r¦  r«  r®  r·  r»  r¾  rÁ  rÄ  rÇ  rÍ  rÐ  rÒ  rÖ  r×  Ú__classcell__rz   rz   rx   r{   rJ   F   sÆ   
 ýõÿ
ú
û	ÿ
*'
		!	G2
		
rJ   )ErÚ  Ú
__future__r   Útypingr   Úcollectionsr   Ú	itertoolsr   rq  Ú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.codeprinterr/   Úsympy.printing.precedencer0   r1   Úsympy.printing.printerr2   r3   r4   rm   rJ   rz   rz   rz   r{   Ú<module>   sj    \4ÿþýüûúùø	÷
öõôóòñðï