K i"ddlmZmZmZddlmZddlmZedZedddgiZ erdd l m Z e rdd l m Z eddg Gd d Zy))pycodeccodefcode) import_module)doctest_depends_onlfortranz clang.cindexfromlistcindex) import_kwargs) src_to_sympy)parse_c)modulescBeZdZdZdfd ZdZdZdZdZdZ xZ S) SymPyExpressiona' Class to store and handle SymPy expressions This class will hold SymPy Expressions and handle the API for the conversion to and from different languages. It works with the C and the Fortran Parser to generate SymPy expressions which are stored here and which can be converted to multiple language's source code. Notes ===== The module and its API are currently under development and experimental and can be changed during development. The Fortran parser does not support numeric assignments, so all the variables have been Initialized to zero. The module also depends on external dependencies: - LFortran which is required to use the Fortran parser - Clang which is required for the C parser Examples ======== Example of parsing C code: >>> from sympy.parsing.sym_expr import SymPyExpression >>> src = ''' ... int a,b; ... float c = 2, d =4; ... ''' >>> a = SymPyExpression(src, 'c') >>> a.return_expr() [Declaration(Variable(a, type=intc)), Declaration(Variable(b, type=intc)), Declaration(Variable(c, type=float32, value=2.0)), Declaration(Variable(d, type=float32, value=4.0))] An example of variable definition: >>> from sympy.parsing.sym_expr import SymPyExpression >>> src2 = ''' ... integer :: a, b, c, d ... real :: p, q, r, s ... ''' >>> p = SymPyExpression() >>> p.convert_to_expr(src2, 'f') >>> p.convert_to_c() ['int a = 0', 'int b = 0', 'int c = 0', 'int d = 0', 'double p = 0.0', 'double q = 0.0', 'double r = 0.0', 'double s = 0.0'] An example of Assignment: >>> from sympy.parsing.sym_expr import SymPyExpression >>> src3 = ''' ... integer :: a, b, c, d, e ... d = a + b - c ... e = b * d + c * e / a ... ''' >>> p = SymPyExpression(src3, 'f') >>> p.convert_to_python() ['a = 0', 'b = 0', 'c = 0', 'd = 0', 'e = 0', 'd = a + b - c', 'e = b*d + c*e/a'] An example of function definition: >>> from sympy.parsing.sym_expr import SymPyExpression >>> src = ''' ... integer function f(a,b) ... integer, intent(in) :: a, b ... integer :: r ... end function ... ''' >>> a = SymPyExpression(src, 'f') >>> a.convert_to_python() ['def f(a, b):\n f = 0\n r = 0\n return f'] cVt||s |sg|_y|r|ru|jdk(r"trt ||_yt d|jdk(r"trt||_yt dtdtdtd) z%Constructor for SymPyExpression classf4LFortran is not installed, cannot parse Fortran codec+Clang is not installed, cannot parse C codez0Parser for specified language is not implementedzSource code not presentz$Please specify a mode for conversionN) super__init___exprlowerrr ImportErrorcinr NotImplementedError ValueError)self source_codemode __class__s \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/parsing/sym_expr.pyrzSymPyExpression.__init__^s {DJ ::<3&%1+%> )*`aaZZ\S(%,[%9 )*WXX-J!!:;;CD Dc|jdk(r"trt||_yt d|jdk(r"t rt ||_yt dtd)aeConverts the given source code to SymPy Expressions Attributes ========== src_code : String the source code or filename of the source code that is to be converted mode: String the mode to determine which parser is to be used according to the language of the source code f or F for Fortran c or C for C/C++ Examples ======== >>> from sympy.parsing.sym_expr import SymPyExpression >>> src3 = ''' ... integer function f(a,b) result(r) ... integer, intent(in) :: a, b ... integer :: x ... r = a + b -x ... end function ... ''' >>> p = SymPyExpression() >>> p.convert_to_expr(src3, 'f') >>> p.return_expr() [FunctionDefinition(integer, name=f, parameters=(Variable(a), Variable(b)), body=CodeBlock( Declaration(Variable(r, type=integer, value=0)), Declaration(Variable(x, type=integer, value=0)), Assignment(Variable(r), a + b - x), Return(Variable(r)) ))] rrrrz6Parser for specified language has not been implementedN)rrr rrrr r)rsrc_coder s r"convert_to_exprzSymPyExpression.convert_to_exprxsgR ::<3 )(3 !"XYY ZZ\S $X. !"OPP%H r#cg|_|jD]&}|jjt|(|jS)a0Returns a list with Python code for the SymPy expressions Examples ======== >>> from sympy.parsing.sym_expr import SymPyExpression >>> src2 = ''' ... integer :: a, b, c, d ... real :: p, q, r, s ... c = a/b ... d = c/a ... s = p/q ... r = q/p ... ''' >>> p = SymPyExpression(src2, 'f') >>> p.convert_to_python() ['a = 0', 'b = 0', 'c = 0', 'd = 0', 'p = 0.0', 'q = 0.0', 'r = 0.0', 's = 0.0', 'c = a/b', 'd = c/a', 's = p/q', 'r = q/p'] )_pycoderappendrriters r"convert_to_pythonz!SymPyExpression.convert_to_pythons>( JJ .D LL  t - .||r#cg|_|jD]&}|jjt|(|jS)aReturns a list with the c source code for the SymPy expressions Examples ======== >>> from sympy.parsing.sym_expr import SymPyExpression >>> src2 = ''' ... integer :: a, b, c, d ... real :: p, q, r, s ... c = a/b ... d = c/a ... s = p/q ... r = q/p ... ''' >>> p = SymPyExpression() >>> p.convert_to_expr(src2, 'f') >>> p.convert_to_c() ['int a = 0', 'int b = 0', 'int c = 0', 'int d = 0', 'double p = 0.0', 'double q = 0.0', 'double r = 0.0', 'double s = 0.0', 'c = a/b;', 'd = c/a;', 's = p/q;', 'r = q/p;'] )_ccoderr)rr*s r" convert_to_czSymPyExpression.convert_to_cs>, JJ ,D KK  uT{ + ,{{r#cg|_|jD]&}|jjt|(|jS)aReturns a list with the fortran source code for the SymPy expressions Examples ======== >>> from sympy.parsing.sym_expr import SymPyExpression >>> src2 = ''' ... integer :: a, b, c, d ... real :: p, q, r, s ... c = a/b ... d = c/a ... s = p/q ... r = q/p ... ''' >>> p = SymPyExpression(src2, 'f') >>> p.convert_to_fortran() [' integer*4 a', ' integer*4 b', ' integer*4 c', ' integer*4 d', ' real*8 p', ' real*8 q', ' real*8 r', ' real*8 s', ' c = a/b', ' d = c/a', ' s = p/q', ' r = q/p'] )_fcoderr)rr*s r"convert_to_fortranz"SymPyExpression.convert_to_fortrans>( JJ ,D KK  uT{ + ,{{r#c|jS)aReturns the expression list Examples ======== >>> from sympy.parsing.sym_expr import SymPyExpression >>> src3 = ''' ... integer function f(a,b) ... integer, intent(in) :: a, b ... integer :: r ... r = a+b ... f = r ... end function ... ''' >>> p = SymPyExpression() >>> p.convert_to_expr(src3, 'f') >>> p.return_expr() [FunctionDefinition(integer, name=f, parameters=(Variable(a), Variable(b)), body=CodeBlock( Declaration(Variable(f, type=integer, value=0)), Declaration(Variable(r, type=integer, value=0)), Assignment(Variable(f), Variable(r)), Return(Variable(f)) ))] )r)rs r" return_exprzSymPyExpression.return_exprs4zzr#)NN) __name__ __module__ __qualname____doc__rr&r,r/r2r4 __classcell__)r!s@r"rr s*M^E46p262r#rN)sympy.printingrrrsympy.externalrsympy.utilities.decoratorrrr$sympy.parsing.fortran.fortran_parserr sympy.parsing.c.c_parserr rr#r"r@s^//(8  $NZ(4LM A0Z89II:Ir#