K iIdZddlmZddlmZmZmZmZmZm Z m Z m Z m Z ddl mZddlmZddlmZddlmZddlmZmZdd lmZdd lmZdd lmZmZdd lm Z ed Z!edZ"edZ#edZ$edZ%edZ&Gdde Z'Gdde Z(dZ)Gdde Z*Gdde Z+GddeZ,Gdde Z-Gd d!e Z.Gd"d#e Z/Gd$d%e Z0Gd&d'eZ1e1Z2d(Z3e3d)Z4dRd+d*d*d,d-Z5d.Z6d/Z7dSd0Z8dSd1Z9dRd2Z:dSd3Z;dSd4ZGd8d9e Z?Gd:d;eZ@Gd<d=e@ZAGd>d?e@ZBGd@dAe@ZCGdBdCe@ZDGdDdEe@ZEGdFdGeAZFGdHdIeZGGdJdKeGZHGdLdMeGZIGdNdOe eZJGdPdQe eZKy*)Tz AST nodes specific to Fortran. The functions defined in this module allows the user to express functions such as ``dsign`` as a SymPy function for symbolic manipulation. ) annotations) Attribute CodeBlock FunctionCallNodenoneStringToken _mk_TupleVariable)Basic)Tuple)Expr)Function)FloatInteger)Str)sympifytruefalse)iterablepure elemental intent_in intent_out intent_inout allocatablec.eZdZdZdxZZeZedZ y)Programaf Represents a 'program' block in Fortran. Examples ======== >>> from sympy.codegen.ast import Print >>> from sympy.codegen.fnodes import Program >>> prog = Program('myprogram', [Print([42])]) >>> from sympy import fcode >>> print(fcode(prog, source_format='free')) program myprogram print *, 42 end program )namebodyct|SNrr"s Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/codegen/fnodes.pyzProgram.4 40@N) __name__ __module__ __qualname____doc__ __slots___fieldsr _construct_name staticmethod_construct_bodyr*r'r r "s#+*IO"#@AOr*r c eZdZdZdxZZeZeZy) use_renamea Represents a renaming in a use statement in Fortran. Examples ======== >>> from sympy.codegen.fnodes import use_rename, use >>> from sympy import fcode >>> ren = use_rename("thingy", "convolution2d") >>> print(fcode(ren, source_format='free')) thingy => convolution2d >>> full = use('signallib', only=['snr', ren]) >>> print(fcode(full, source_format='free')) use signallib, only: snr, thingy => convolution2d )localoriginalN) r+r,r-r.r/r0r _construct_local_construct_originalr4r*r'r6r67s0/I r*r6cHt|dr |jSt|S)Nr!)hasattrr!r args r'_namer?KssFxxc{r*cVeZdZdZdxZZeedZee Z edZ edZ y)usea Represents a use statement in Fortran. Examples ======== >>> from sympy.codegen.fnodes import use >>> from sympy import fcode >>> fcode(use('signallib'), source_format='free') 'use signallib' >>> fcode(use('signallib', [('metric', 'snr')]), source_format='free') 'use signallib, metric => snr' >>> fcode(use('signallib', only=['snr', 'convolution2d']), source_format='free') 'use signallib, only: snr, convolution2d' ) namespacerenameonly)rCrDc ht|Dcgc]}t|tr|nt|c}Scc}wr$)r isinstancer6argsr>s r'r(z use.dsA%C:DwzCQ[A\#blnqbr:r:D3E:Ds!/c nt|Dcgc]}t|tr|n t|!c}Scc}wr$)rrFr6r?rGs r'r(z use.es0vz7{orz#z?Z`efi`j8j7{0|7{s$2N) r+r,r-r.r/r0rdefaultsr2r?_construct_namespace_construct_rename_construct_onlyr4r*r'rArAQsE:9I-H'.$&EF"#|}Or*rAcPeZdZdZdxZZdeiZeZ e dZ e dZ y)Modulea\ Represents a module in Fortran. Examples ======== >>> from sympy.codegen.fnodes import Module >>> from sympy import fcode >>> print(fcode(Module('signallib', ['implicit none'], []), source_format='free')) module signallib implicit none contains end module )r! declarations definitionsrPcp|Dcgc]}t|tr t|n|!}}t|Scc}wr$)rFstrrr)clsrHr>s r'_construct_declarationszModule._construct_declarations~s8EIJcJsC0Cc9JJ$Ks$3ct|Sr$r%r=s r'r(zModule.s ior*N)r+r,r-r.r/r0rrJr r1 classmethodrUr2_construct_definitionsr4r*r'rOrOhsE"BAI(HO  **EFr*rOcXeZdZdZdZeej zZeZe dZ e dZ y) Subroutinea Represents a subroutine in Fortran. Examples ======== >>> from sympy import fcode, symbols >>> from sympy.codegen.ast import Print >>> from sympy.codegen.fnodes import Subroutine >>> x, y = symbols('x y', real=True) >>> sub = Subroutine('mysub', [x, y], [Print([x**2 + y**2, x*y])]) >>> print(fcode(sub, source_format='free', standard=2003)) subroutine mysub(x, y) real*8 :: x real*8 :: y print *, x**2 + y**2, x*y end subroutine )r! parametersr"cBtttj|Sr$)rmapr deduced)paramss r'r(zSubroutine.ss8CSCSU[?\8]r*c6t|tr|St|Sr$)rFr)rTitrs r'r3zSubroutine._construct_bodys c9 %Jc? "r*N) r+r,r-r.r/rr0r r1r2_construct_parametersrWr3r4r*r'rZrZs?$/I$,,&GO()]^##r*rZc8eZdZdZdxZZeeZee Z y)SubroutineCallz Represents a call to a subroutine in Fortran. Examples ======== >>> from sympy.codegen.fnodes import SubroutineCall >>> from sympy import fcode >>> fcode(SubroutineCall('mysub', 'x y'.split())) ' call mysub(x, y)' )r!subroutine_argsN) r+r,r-r.r/r0r2r?r1r _construct_subroutine_argsr4r*r'rdrds( 65I"5)O!-i!8r*rdceZdZdZdxZZededZe dZ e e Z e e Z e e Ze e Ze dZy)Doa Represents a Do loop in in Fortran. Examples ======== >>> from sympy import fcode, symbols >>> from sympy.codegen.ast import aug_assign, Print >>> from sympy.codegen.fnodes import Do >>> i, n = symbols('i n', integer=True) >>> r = symbols('r', real=True) >>> body = [aug_assign(r, '+', 1/i), Print([i, r])] >>> do1 = Do(body, i, 1, n) >>> print(fcode(do1, source_format='free')) do i = 1, n r = r + 1d0/i print *, i, r end do >>> do2 = Do(body, i, 1, n, 2) >>> print(fcode(do2, source_format='free')) do i = 1, n, 2 r = r + 1d0/i print *, i, r end do )r"counterfirstlaststep concurrent)rlrmct|Sr$r%r&s r'r(z Do.r)r*c|rtStSr$rr=s r'r(z Do.s STer*N)r+r,r-r.r/r0rrrJr2r3r_construct_counter_construct_first_construct_last_construct_step_construct_concurrentr4r*r'rhrhsc4UTI %8H"#@AO%g.#G,"7+O"7+O()KLr*rhc(eZdZdZdxZZeeZy)ArrayConstructoraT Represents an array constructor. Examples ======== >>> from sympy import fcode >>> from sympy.codegen.fnodes import ArrayConstructor >>> ac = ArrayConstructor([1, 2, 3]) >>> fcode(ac, standard=95, source_format='free') '(/1, 2, 3/)' >>> fcode(ac, standard=2003, source_format='free') '[1, 2, 3]' )elementsN) r+r,r-r.r/r0r2r _construct_elementsr4r*r'rwrws ('I&y1r*rwc|eZdZdZdxZZdediZee Z ee Z ee Z ee Z ee Zy) ImpliedDoLoopa Represents an implied do loop in Fortran. Examples ======== >>> from sympy import Symbol, fcode >>> from sympy.codegen.fnodes import ImpliedDoLoop, ArrayConstructor >>> i = Symbol('i', integer=True) >>> idl = ImpliedDoLoop(i**3, i, -3, 3, 2) # -27, -1, 1, 27 >>> ac = ArrayConstructor([-28, idl, 28]) # -28, -27, -1, 1, 27, 28 >>> fcode(ac, standard=2003, source_format='free') '[-28, (i**3, i = -3, 3, 2), 28]' )exprrirjrkrlrlrnN)r+r,r-r.r/r0rrJr2r_construct_exprrqrrrsrtr4r*r'r{r{sV GFI #H"7+O%g.#G,"7+O"7+Or*r{ceZdZdZdZdZy)ExtentaC Represents a dimension extent. Examples ======== >>> from sympy.codegen.fnodes import Extent >>> e = Extent(-3, 3) # -3, -2, -1, 0, 1, 2, 3 >>> from sympy import fcode >>> fcode(e, source_format='free') '-3:3' >>> from sympy.codegen.ast import Variable, real >>> from sympy.codegen.fnodes import dimension, intent_out >>> dim = dimension(e, e) >>> arr = Variable('x', real, attrs=[dim, intent_out]) >>> fcode(arr.as_Declaration(), source_format='free', standard=2003) 'real*8, dimension(-3:3, -3:3), intent(out) :: x' ct|dk(r.|\}}tj|t|t|St|dk(st|dk(r|ddvrtj|St d)Nrrn):Nz5Expected 0 or 2 args (or one argument == None or ':'))lenr __new__r ValueError)rTrHlowhighs r'rzExtent.__new__sl t9>IC==gclGDMB B Y!^D Q47k3I==% %TU Ur*cxt|jdk(rydjd|jDS)Nrrc32K|]}t|ywr$)rS).0r>s r' z#Extent._sympystr..%s6SC6s)rrHjoin)selfprinters r' _sympystrzExtent._sympystr"s. tyy>Q xx6DII666r*N)r+r,r-r.rrr4r*r'rrs$V7r*rct|dkDr tdg}|D]}t|tr|j |%t|t r:|dk(r|j tT|j t |ot|r|j t||j t|t|dk(r tdtd|S)a Creates a 'dimension' Attribute with (up to 7) extents. Examples ======== >>> from sympy import fcode >>> from sympy.codegen.fnodes import dimension, intent_in >>> dim = dimension('2', ':') # 2 rows, runtime determined number of columns >>> from sympy.codegen.ast import Variable, integer >>> arr = Variable('a', integer, attrs=[dim, intent_in]) >>> fcode(arr.as_Declaration(), source_format='free', standard=2003) 'integer*4, dimension(2, :), intent(in) :: a' z0Fortran only supports up to 7 dimensional arraysrrzNeed at least one dimension dimension) rrrFrappendrSr rrr)rHr[r>s r'rr*s 4y1}KLLJ , c6 "   c " S !cz!!&(+!!&+. c]   fcl +   gcl + , 4yA~677 [* --r**Nr4)attrsvaluetypecvt|tr/t|jdk7rt dt|zt |}t ||gz}|9|tttfvrtttd|}|j||tj|||St||||S)a Convenience function for creating a Variable instance for a Fortran array. Parameters ========== symbol : symbol dim : Attribute or iterable If dim is an ``Attribute`` it need to have the name 'dimension'. If it is not an ``Attribute``, then it is passed to :func:`dimension` as ``*dim`` intent : str One of: 'in', 'out', 'inout' or None \*\*kwargs: Keyword arguments for ``Variable`` ('type' & 'value') Examples ======== >>> from sympy import fcode >>> from sympy.codegen.ast import integer, real >>> from sympy.codegen.fnodes import array >>> arr = array('a', '*', 'in', type=integer) >>> print(fcode(arr.as_Declaration(), source_format='free', standard=2003)) integer*4, dimension(*), intent(in) :: a >>> x = array('x', [3, ':', ':'], intent='out', type=real) >>> print(fcode(x.as_Declaration(value=1), source_format='free', standard=2003)) real*8, dimension(3, :, :), intent(out) :: x = 1 rz/Got an unexpected Attribute argument as dim: %s)inoutinout)rr) rFrrSr!rrlistrrrrr r^)symboldimintentrrrs r'arrayrOs:#y! sxx=K 'NQTUXQYYZ Zo K3% E  )Z> >%j<PQWXF V |e5AAE??r*cNt|tr t|St|Sr$)rFrSr rr=s r' _printabler|s$S#.6#;@GCL@r*c.tdt|gS)a Creates an AST node for a function call to Fortran's "allocated(...)" Examples ======== >>> from sympy import fcode >>> from sympy.codegen.fnodes import allocated >>> alloc = allocated('x') >>> fcode(alloc, source_format='free') 'allocated(x)' allocatedrr)rs r'rrs  j&7%8 99r*c~tdt|g|r t|gngz|rt|gzSgzS)ap Creates an AST node for a function call to Fortran's "lbound(...)" Parameters ========== array : Symbol or String dim : expr kind : expr Examples ======== >>> from sympy import fcode >>> from sympy.codegen.fnodes import lbound >>> lb = lbound('arr', dim=2) >>> fcode(lb, source_format='free') 'lbound(arr, 2)' lboundrrrkinds r'rrsX(  E !*S/ r +#*T   - *, - r*c~tdt|g|r t|gngz|rt|gzSgzS)Nuboundrrs r'rrsV  E !*S/ r +#*T   - *, - r*c\tdt|g|rt|gzSgzS)aR Creates an AST node for a function call to Fortran's "shape(...)" Parameters ========== source : Symbol or String kind : expr Examples ======== >>> from sympy import fcode >>> from sympy.codegen.fnodes import shape >>> shp = shape('x') >>> fcode(shp, source_format='free') 'shape(x)' shaper)sourcers r'rrsE&  F #*T   - *, - r*c~tdt|g|r t|gngz|rt|gzSgzS)a Creates an AST node for a function call to Fortran's "size(...)" Examples ======== >>> from sympy import fcode, Symbol >>> from sympy.codegen.ast import FunctionDefinition, real, Return >>> from sympy.codegen.fnodes import array, sum_, size >>> a = Symbol('a', real=True) >>> body = [Return((sum_(a**2)/size(a))**.5)] >>> arr = array(a, dim=[':'], intent='in') >>> fd = FunctionDefinition(real, 'rms', [arr], body) >>> print(fcode(fd, source_format='free', standard=2003)) real*8 function rms(a) real*8, dimension(:), intent(in) :: a rms = sqrt(sum(a**2)*1d0/size(a)) end function sizerrs r'rrsX(  E !*S/ r +#*T   - *, - r*ctdt|t|g|r t|gngz|rt|gzSgzS)z Creates an AST node for a function call to Fortran's "reshape(...)" Parameters ========== source : Symbol or String shape : ArrayExpr reshaper)rrpadorders r'rrs_  F Z./!*S/ r + #*U   - *, - r*c>td|rt|gSgS)a Creates an Attribute ``bind_C`` with a name. Parameters ========== name : str Examples ======== >>> from sympy import fcode, Symbol >>> from sympy.codegen.ast import FunctionDefinition, real, Return >>> from sympy.codegen.fnodes import array, sum_, bind_C >>> a = Symbol('a', real=True) >>> s = Symbol('s', integer=True) >>> arr = array(a, dim=[s], intent='in') >>> body = [Return((sum_(a**2)/s)**.5)] >>> fd = FunctionDefinition(real, 'rms', [arr, s], body, attrs=[bind_C('rms')]) >>> print(fcode(fd, source_format='free', standard=2003)) real*8 function rms(a, s) bind(C, name="rms") real*8, dimension(s), intent(in) :: a integer*4 :: s rms = sqrt(sum(a**2)/s) end function bind_C)rr )r!s r'rrs!6 Xt ~ >>2 >>r*c@eZdZdZdxZZdeiZee Z ee Z y)GoToa Represents a goto statement in Fortran Examples ======== >>> from sympy.codegen.fnodes import GoTo >>> go = GoTo([10, 20, 30], 'i') >>> from sympy import fcode >>> fcode(go, source_format='free') 'go to (10, 20, 30), i' )labelsr|r|N) r+r,r-r.r/r0rrJr2r _construct_labelsrr}r4r*r'rrs1 -,I~H$Y/"7+Or*rc0eZdZdZdxZZdeiZee Z y) FortranReturnaK AST node explicitly mapped to a fortran "return". Explanation =========== Because a return statement in fortran is different from C, and in order to aid reuse of our codegen ASTs the ordinary ``.codegen.ast.Return`` is interpreted as assignment to the result variable of the function. If one for some reason needs to generate a fortran RETURN statement, this node should be used. Examples ======== >>> from sympy.codegen.fnodes import FortranReturn >>> from sympy import fcode >>> fcode(FortranReturn('x')) ' return x' ) return_valuerN) r+r,r-r.r/r0rrJr2r_construct_return_valuer4r*r'rr-s((,+I%H*73r*rceZdZdZdZy) FFunctionMc |jj}|jd|jkrt d||jfzdj |dj t|j|jS)Nstandardz%s requires Fortran %d or newerz{}({})z, ) __class__r+ _settings_required_standardNotImplementedErrorformatrr]_printrH)rrr!s r'_fcodezFFunction._fcodeJsy~~&&   Z (4+B+B B%&G'+T-D-D&E'FG GtTYYs7>>499/M%NOOr*N)r+r,r-rrr4r*r'rrGsPr*rceZdZdZy) F95Function_N)r+r,r-rr4r*r'rrRsr*rceZdZdZdZy)isignz/ Fortran sign intrinsic for integer arguments. rNr+r,r-r.nargsr4r*r'rrVs 9 Er*rceZdZdZdZy)dsignz8 Fortran sign intrinsic for double precision arguments. rNrr4r*r'rr[s B Er*rceZdZdZdZy)cmplxz& Fortran complex conversion function. rNrr4r*r'rr`s 0 Er*rceZdZdZdZy)rz Fortran kind function. rnNrr4r*r'rre " Er*rceZdZdZdZy)mergez Fortran merge function Nrr4r*r'rrjrr*rc(eZdZUded<ded<dZy)_literalrS_tokenint _decimalsc dj|j|zjd\}}|jdj d}|d|ddj d}}|dk(rdn|}|xsd|j z|z|xsdzS) Nz%.{}ee0.rrn+)rrsplitstriprstriplstripr)rrrHkwargsmantissasgnd_exex_sgnex_nums r'rz_literal._fcodess$^^DNN;dBII#N'>>#&--c2 WQR[%7%7%<}&C4;;.76=SIIr*N)r+r,r-__annotations__rr4r*r'rros KNJr*rceZdZdZdZdZy) literal_spz' Fortran single precision real literal r Nr+r,r-r.rrr4r*r'rr{s1 FIr*rceZdZdZdZdZy) literal_dpz' Fortran double precision real literal dNrr4r*r'rrs1 FIr*rc>eZdZdxZZeedZeeZ eeZ y)sum_rrmaskrrN r+r,r-r/r0rrJr2r_construct_array_construct_dimr4r*r'rr+22IT*H#G,!'*Nr*rc>eZdZdxZZeedZeeZ eeZ y)product_rrNrr4r*r'rrrr*rr$)NN)Lr. __future__rsympy.codegen.astrrrrrr r r r sympy.core.basicr sympy.core.containersrsympy.core.exprrsympy.core.functionrsympy.core.numbersrrsympy.core.symbolrsympy.core.sympifyr sympy.logicrrsympy.utilities.iterablesrrrrrrrr r6r?rArOrZrdrhrwr{rassumed_extentr assumed_sizerrrrrrrrrrrrrrrrrrrrrrrr4r*r'rs##' (-!&#. k " k " | $ (  & BeB*!!( ~%~.GUG<##>9U9""M"MJ2u2&,E,07U7B .F~ +@Rt$+@ZA : 848$?:,5,&4E44PP)I I I 9 K Ju J  +5$++ud+r*