How to use f2py (passing arguments)?

758 views Asked by At

I have the following fortran code:

      SUBROUTINE MMUL(A,B,D,n)
C
      INTEGER :: n
      REAL, DIMENSION (n, n) :: A, B, D
C
CF2PY INTENT(IN) :: A
CF2PY INTENT(IN) :: B
CF2PY INTENT(IN) :: n
CF2PY INTENT(OUT) :: D   

      D = MATMUL(A, B)

      END

It says this code takes two 1D arrays (i.e. vectors) and returns dot product between these vectors. I need to wrap it into my python script. I use smth like this:

f2py.compile(fsqrtmatmul.encode(), modulename='fsqrtmatmul', verbose=0)
import fsqrtmatmul
res = fsqrtmatmul.mmul([[1., 2.0]], [[2., 1.]])

But it doesn't work:

fsqrtmatmul.error: failed in converting 2nd argument `b' of fsqrtmatmul.mmul to C/Fortran array

How to fix it?

1

There are 1 answers

0
Vladimir F Героям слава On BEST ANSWER

Your description is wrong.

A and B are arrays n x n and it clearly computes their matrix product.

> f2py -c mmul.f -m mmul


> ipython


In [1]: import mmul

In [2]: D = mmul.mmul([[2.,0.],[0.,2.]],[[3.,0.],[0.,3.]],2)

In [3]: D
Out[3]: 
array([[ 6.,  0.],
       [ 0.,  6.]], dtype=float32)