如何使用 SymPy codegen 生成 Fortran 子程序 [英] How to generate Fortran subroutine with SymPy codegen

查看:33
本文介绍了如何使用 SymPy codegen 生成 Fortran 子程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 SymPy 代码生成工具生成一个 Fortran 子程序.我可以使用 codegen(("f", x*y*z), "f95", "filename") 生成 Fortran 函数而不会出现问题.但我想生成一个 Fortran 子例程,以便我可以修改输入数组.我怎样才能做到这一点?文档很差.

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.

推荐答案

如果有单个标量返回值,codegen 实用程序会创建一个函数,否则创建一个子例程.有一些对数组的支持,但除非您向 codegen 提供类似表达式的数组,否则不会触发数组功能.文档比较分散,给大家指点一下:

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:

看看 autowrap 文档中的矩阵向​​量示例:http://docs.sympy.org/latest/modules/utilities/autowrap.html.Autowrap 在幕后使用代码生成器.

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.

在当前的开发者版本中,还有用于生成与具有符号元素的矩阵相对应的代码的功能.请参阅 http://docs.sympy.org 上的 fcode() 示例/dev/modules/printing.html#fortran-printing.

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.

以下示例代码应该为矩阵向量乘积输出 Fortran 95 子例程:

Here is sample code that should output a Fortran 95 subroutine for a matrix-vector product:

from sympy import *
from sympy.utilities.codegen import codegen
A, B, C = symbols('A B C', cls=IndexedBase)
m, n = symbols('m n', integer=True)
i = Idx('i', m)
j = Idx('j', n)
expr = Eq(C[i], A[i, j]*B[j])
result = codegen(('my_function', expr), 'f95', 'my_project')
print result[0][1]

通过将这些行保存到 my_file.py 并运行 python my_file.py,我得到以下输出:

By saving these lines to my_file.py and running python my_file.py, I get the following output:

!******************************************************************************
!*                    Code generated with sympy 0.7.5-git                     *
!*                                                                            *
!*              See http://www.sympy.org/ for more information.               *
!*                                                                            *
!*                       This file is part of 'project'                       *
!******************************************************************************

subroutine my_function(A, B, m, n, C)
implicit none
INTEGER*4, intent(in) :: m
INTEGER*4, intent(in) :: n
REAL*8, intent(in), dimension(1:m, 1:n) :: A
REAL*8, intent(in), dimension(1:n) :: B
REAL*8, intent(out), dimension(1:m) :: C
INTEGER*4 :: i
INTEGER*4 :: j

do i = 1, m
   C(i) = 0
end do
do i = 1, m
   do j = 1, n
      C(i) = B(j)*A(i, j) + C(i)
   end do
end do

end subroutine

这篇关于如何使用 SymPy codegen 生成 Fortran 子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆