我尝试为 Python 编写 Fortran 包装器并获得了
failed in converting hidden c of test.wrap to C/Fortran array
。我可以知道如何解决吗?这是python
部分
import test
import numpy as np
A = np.random.random((5,5))
B = np.random.random((5,5))
C = np.zeros((5, 5))
A = np.asfortranarray(A)
B = np.asfortranarray(B)
C = np.asfortranarray(C)
test.wrap(A, B)
print(C[0,0])
这里是
Fortran
部分,叫做test.f90
。我用了python -m numpy.f2py -c --f90flags='-O3' -m test test.f90
.
subroutine wrap(A, B, C)
implicit none
real, intent(in) :: A(:, :), B(:, :)
real, intent(out) :: C(:, :)
integer :: m, n, p
m = size(A, 1)
n = size(A, 2)
p = size(B, 2)
if (n /= size(B, 1)) then
print *, "Error: Incompatible dimensions for matrix multiplication"
return
end if
C = matmul(A, B)
end