Creating Fortran/C/Matlab function/(sub)routine for numerical symbolic matrix/array evaluation

134 views Asked by At

I would like to evaluate numerically symbolic matrices created in python (sympy) using external function generated automatically.

My goal is to generate such a subroutine

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

I try to use codegen in the following way

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)

but the result is quite far

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

In few words I'm not able to input more than one expression and to generate the array freely. I get some idea from How to generate Fortran subroutine with SymPy codegen

Bests

0

There are 0 answers