为数值符号矩阵/数组评估创建Fortran / C / Matlab函数/(子)例程

时间:2014-11-19 12:10:44

标签: python c arrays fortran codegen

我想使用自动生成的外部函数来评估在python(sympy)中创建的数字符号矩阵。

我的目标是生成这样一个子程序

subroutine my_fun(y, x, z, m, C)
implicit none
REAL*8, intent(in) :: x
REAL*8, intent(in) :: y
REAL*8, intent(in) :: z
INTEGER*4, intent(in) :: m
REAL*8, intent(out), dimension(1:m) :: C
INTEGER*4 :: line

line=1
C(line) = x + y*z
line=line+1
C(line) = y*z-x


end subroutine

我尝试以下列方式使用codegen

C = symbols('C', cls=IndexedBase)
m= symbols('m', integer=True)
i = Idx('line', m)
[(c_name, c_code), (h_name, c_header)] = codegen(('my_fun', Eq(C[line],x+y*z)),\
 'F95', 'test10', header=True, empty=True)

但结果相当不错

subroutine my_fun(x, y, z, m, C)
implicit none
REAL*8, intent(in) :: x
REAL*8, intent(in) :: y
REAL*8, intent(in) :: z
INTEGER*4, intent(in) :: m
REAL*8, intent(out), dimension(1:m) :: C
INTEGER*4 :: line

do line = 1, m
   C(line) = x + y*z
end do

end subroutine

简而言之,我无法输入多个表达式并自由生成数组。我从How to generate Fortran subroutine with SymPy codegen

得到了一些建议

贝斯茨

0 个答案:

没有答案