I want to generate a Fortran subroutine with SymPy codegen utility. I can generate a Fortran function without problem with codegen(("f", x*y*z), "f95", "filename")
. But I want to generate a Fortran subroutine so I can modify input arrays. How can I do this? The documentation is very poor.
相关问题
- Octave 4.2.2 and sympy 1.6
- Generate C code with Sympy. Replace Pow(x,2) by x*
- Assuming a symbol is between zero and one with Sym
- Sympy: Get functions from expression
- How to rewrite `sin(x)^2` to cos(2*x) form in Symp
相关文章
- Generate Fortran subroutine with SymPy codegen for
- Add multiplication signs (*) between coefficients
- Is there a vectorized way to calculate the gradien
- Is there a vectorized way to calculate the gradien
- Dealing with piecewise equations returned by sympy
- Lambdify works with Python, but throws an exceptio
- (Some function) is not defined with SymPy Lambdify
- How do I define a conditional function using sympy
The codegen utility creates a function if there is a single scalar return value, and a subroutine otherwise. There is some support for arrays, but the array functionality won't be triggered unless you feed codegen an array like expression. The documentation is scattered, so I'll give you some pointers:
Take a look at the matrix-vector example in the autowrap documentation: http://docs.sympy.org/latest/modules/utilities/autowrap.html. Autowrap uses codegen behind the scenes.
In the current developer version there is also functionality for generating code that correspond to matrices with symbolic elements. See the examples for fcode() at http://docs.sympy.org/dev/modules/printing.html#fortran-printing.
Here is sample code that should output a Fortran 95 subroutine for a matrix-vector product:
By saving these lines to my_file.py and running
python my_file.py
, I get the following output: