You may be interested in using scipy.linalg.cython_lapack.
It provides access to LAPACK's function dgetri
among others. And the good news is:
This makes it possible to use SciPy's BLAS and LAPACK from any 3rd party
Cython module without explicitely linking with the libraries. This means
that projects like scikit-learn and statsmodels do not need to maintain a
separate build dependency on BLAS and LAPACK.
An exemple using dger
is available at Calling BLAS / LAPACK directly using the SciPy interface and Cython . See also Improving Cython Lapack performance with internal array definitions?
I detailed how to use cython_blas in my answer to MPI python-Open-MPI , so here is how it can be adapted to dgetri:
The critical part of the code are written in Cython
, in a dedicated file myinverse.pyx
.
This file is turned into a myinverse.c
file by Cython
This c file is compiled by your favorite c compiler gcc
to build a shared library myinverse.so
The optimized function can be used in your program after import myinverse
.
Here is a cython module, to be placed in the .pyx file:
import numpy
cimport numpy
cimport scipy.linalg.cython_lapack
ctypedef numpy.float64_t DTYPE_t
cimport cython
from libc.stdlib cimport malloc, free
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def invert(numpy.ndarray[DTYPE_t, ndim=2] array):
cdef int rows = array.shape[0]
cdef int cols = array.shape[1]
cdef int info = 0
if cols !=rows:
return array,1,"not a square matrix"
cdef int* ipiv = <int *> malloc(rows * sizeof(int))
if not ipiv:
raise MemoryError()
scipy.linalg.cython_lapack.dgetrf(&cols,&rows,&array[0,0],&rows,ipiv,&info)
if info !=0:
free(ipiv)
return array,info,"dgetrf failed, INFO="+str(info)
#workspace query
cdef double workl
cdef int lwork=-1
scipy.linalg.cython_lapack.dgetri(&cols,&array[0,0],&rows,ipiv,&workl,&lwork,&info)
if info !=0:
free(ipiv)
return array,info,"dgetri failed, workspace query, INFO="+str(info)
#allocation workspace
lwork= int(workl)
cdef double* work = <double *> malloc(lwork * sizeof(double))
if not work:
raise MemoryError()
scipy.linalg.cython_lapack.dgetri(&cols,&array[0,0],&rows,ipiv,work,&lwork,&info)
if info !=0:
free(ipiv)
free(work)
return array,info,"dgetri failed, INFO="+str(info)
free(ipiv)
free(work)
return array,info,""
To cythonize and compile the .pyx file, the following makefile can be used (I hope you are using Linux...)
all: myinverse myinverseb
myinverse: myinverse.pyx
cython -a myinverse.pyx
myinverseb: myinverse.c
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -o myinverse.so myinverse.c
The new python myinverse function, chainng LAPACK's dgetrf()
and dgetri()
, is called in the main python file:
import numpy as np
import myinverse
n=42
#A=np.zeros((n,n))
#for i in range(n):
# A[i,i]=10
A=np.random.rand(n,n)
#A=np.zeros((n,n))
Am,info,string=myinverse.invert(A.copy())
if info==0:
print np.linalg.norm(A.dot(Am)-np.identity(n), np.inf)
else :
print "inversion failed, info=",info, string