[Fortran] 纯文本查看 复制代码
module py2f90
use,intrinsic::iso_c_binding
implicit none
contains
subroutine transferMat2For(matrix,n1,n2)bind(c,name='array2py')
implicit none
integer(c_int),intent(in),value::n1,n2
real(c_float),intent(out)::matrix(n1,n2)
integer::i,j
! initialize matrix
matrix = 0.0E0
! loop
do i=1,n1
do j=1,n2
matrix(i,j) = real(i,4)*1.E1+real(j,4)*2.E0
write(*,"('Row:',i4,1x,'Col:',i4,1x,'Value:',1x,F5.2)")i,j,matrix(i,j)
enddo
enddo
return
end subroutine
end module
[Fortran] 纯文本查看 复制代码
import numpy as np
from numpy.ctypeslib import load_library,ndpointer
from ctypes import c_int
# shape of 2d array
n1,n2 = 2,4+1
# create an empty 2d array
data = np.empty(shape=(n1,n2),dtype='f4',order='f')
flib = load_library("mydll.dll","./")
flib.argtypes = [ndpointer(dtype='f4',ndim=2),c_int,c_int]
flib.array2py(data.ctypes.data,n1,n2)