ylikx / forpy

Forpy - use Python from Fortran
GNU Lesser General Public License v3.0
212 stars 51 forks source link

Create a 2d Fortran matrix from a 2d numpy array #28

Closed elnazazmi closed 3 years ago

elnazazmi commented 4 years ago

I could create a 1d Fortran matrix from a 1d numpy array using:

ierror = numpy_array%get_data(fortran_matrix)

but, when I try to do the same for a 2d matrix I get an ierror = -1 with no result returned. Is there another function for doing this?

ylikx commented 4 years ago

Getting a Fortran pointer to the data of a numpy array can fail for various reasons. For multidimensional arrays storage order is an additional complication. Here is a summary of these problems and possible solutions: https://github.com/ylikx/forpy/wiki/Working-with-arrays#accessing-array-elements-with-ndarrayget_data You can insert a call err_print statement to check what the problem is with your matrix.

elnazazmi commented 3 years ago

Thank you for your reply, it was very helpful. As you also mentioned, my error was the numpy array storage order which was in 'C' order. With

ierror = arr%get_data(matrix, order='C')

everything is fine. I should only know that the result is the transpose of the array.