Call LAPACK function in Fortran code using f2py

Viewed 378

I want to inverse matrices using Fortran90 code and LAPACK libraries and make it usable with Python using f2py. Here is my subroutine that I want to implement in Python with f2py:

subroutine inv(A,Ainv)
    implicit none

  real(kind=8), dimension(:,:), intent(in) :: A
  real(kind=8), dimension(size(A,1) ,size(A,2)),intent(out) :: Ainv

  real(kind=8), dimension(size(A,1)) :: work  ! work array for LAPACK
  integer, dimension(size(A,1)) :: ipiv   ! pivot indices
  integer :: n, info

  ! External procedures defined in LAPACK
  external DGETRF
  external DGETRI

  ! Store A in Ainv to prevent it from being overwritten by LAPACK
  Ainv = A
  n = size(A,1)

  ! DGETRF computes an LU factorization of a general M-by-N matrix A
  ! using partial pivoting with row interchanges.
  call DGETRF(n, n, Ainv, n, ipiv, info)

  if (info /= 0) then
     stop 'Matrix is numerically singular!'
  end if

  ! DGETRI computes the inverse of a matrix using the LU factorization
  ! computed by DGETRF.
  call DGETRI(n, Ainv, n, ipiv, work, n, info)

  if (info /= 0) then
     stop 'Matrix inversion failed!'
  end if
end subroutine inv

This code works when i compile and run it in a fortran program.

Then, to compile it with f2py I use the command:

f2py -c inverse.f90 -m inverse --fcompiler=gnu95 --compiler=mingw32 -L. -lliblapack

It compiles and the module inverse.cp38-win_amd64.pyd is created and normally I should be able to import it in Python shell but I have the following error:

>>> import inverse
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing inverse: the specified module is not found.

I think that the DLL file is the LAPACK library used, so I would like to know if there is a way to link LAPACK library with f2py different from -L/path/to/library and -l<library.dll>

The gfortran compiler that I use is with MinGW 64bit, my python version is 3.8.5 and 64bit, my LAPACK libraries are pre-build libraries in 64bit also and I am on Windows 10.

Thank you for your help !

Dorian

0 Answers
Related