3

I was trying to use sympy - codegen to get a fortran code. It works fine except for one annoying thing for which I can`t find a solution. I simplified my example to this:

bar = Matrix([x*x,y*z,z*y])
result = codegen(('foo', bar), 'f95', 'project')

the results is:

subroutine foo(x, y, z, out_6551735094710235777)
implicit none
REAL*8, intent(in) :: x
REAL*8, intent(in) :: y
REAL*8, intent(in) :: z
REAL*8, intent(out), dimension(1:3, 1:1) :: out_6551735094710235777

out_6551735094710235777(1, 1) = x**2
out_6551735094710235777(2, 1) = y*z
out_6551735094710235777(3, 1) = y*z

end subroutine

The output of the routine is a two dimensional array. Is there a way to make it single dimensional - for this case the second dimension is one anyway. If I generate the C code for this example I am getting a one dimensional vector by default. Why for fortran it is different?

Also, how to define a name for an output for case like this instead of the auto generated one?

MatG
  • 31
  • 1
  • Do you want to change the existing fortran code to 1D output, or understand how to get `sympy` to generate it as a 1D output yourself? – Ross Oct 26 '15 at 22:23
  • I guess, it's because it is a Matrix. It appears to me that sympy treats vectors as 1 column matrices, and it probably always uses 2d arrays for its matrices. C is different from Fortran in the sense, that it does not provide real 2d arrays but instead emulates it with arrays of arrays, and it might be that sympy sticks to the 1d array for all its matrices. – haraldkl Oct 27 '15 at 02:38
  • Hey Ross, I want to understand how to get sympy to generate 1D output. Is it possible? – MatG Oct 27 '15 at 22:33
  • I opened an issue for this https://github.com/sympy/sympy/issues/10060. I don't know if there's a way to do it with the existing version. – asmeurer Oct 28 '15 at 20:47
  • Yes, one reason is that we only have Matrix objects in SymPy. There is not an nD array object, so we print matrices as matrices, even if they are 1D. But yes, the C printer (and JS printer?) prints matrices as flattened 1D arrays. I think this is simply because it is an easier model to implement in those languages and we had trouble getting things working as 2D arrays. I forget. – moorepants Oct 28 '15 at 23:59
  • Hey guys, thanks a lot for the comments. So do you think that the 1D array functionality will be possible in the near/far future? – MatG Oct 29 '15 at 11:34
  • Yes, we will happily accept PRs for this. You should open and issue about this on SymPy so that we at least make this functionality optional. You may be able to subclass the Fortran matrix printer too, to get this to work, but it may be hassle getting everything going with the codegen package. – moorepants Oct 31 '15 at 17:16

0 Answers0