Hi
I wrote the next function just to learn how ctypes work with numpy arrays. I am not trying to write yet another wrapper to lapack, it's just an experiment. (you can cut and paste the code)
from ctypes import c_int from numpy import array,float64 from numpy.ctypeslib import load_library,ndpointer
def dgesv(N,A,B): # A=array([[1,2],[3,4]],dtype=float64,order='FORTRAN') # B=array([1,2],dtype=float64,order='FORTRAN') cN=c_int(N) NRHS=c_int(1) LDA=c_int(N) IPIV=(c_int * N)() LDB=c_int(N) INFO=c_int(1)
lapack=load_library('liblapack.so','/usr/lib/')
lapack.argtypes=[c_int,c_int, ndpointer(dtype=float64, ndim=2, flags='FORTRAN'), c_int,c_int, ndpointer(dtype=float64, ndim=1, flags='FORTRAN'), c_int,c_int]
lapack.dgesv_(cN,NRHS,A,LDA,IPIV,B,LDB,INFO)
return B
And...
In [2]: from lapackw import dgesv In [3]: from numpy import array In [4]: dgesv(2,array([[1,2],[3,4]]),array([1,2])) ...
ArgumentError: argument 3: exceptions.TypeError: Don't know how to convert parameter 3
Maybe I am sooo dumb but I just don't know what I am doing wrong. numpy-1.0.1, ctypes 1.0.1, python 2.4.3.
thanks
guillem
Hi Guillem
On Wed, May 02, 2007 at 12:38:12AM +0200, Guillem Borrell i Nogueras wrote:
I wrote the next function just to learn how ctypes work with numpy arrays. I am not trying to write yet another wrapper to lapack, it's just an experiment. (you can cut and paste the code)
from ctypes import c_int from numpy import array,float64 from numpy.ctypeslib import load_library,ndpointer
def dgesv(N,A,B): # A=array([[1,2],[3,4]],dtype=float64,order='FORTRAN') # B=array([1,2],dtype=float64,order='FORTRAN')
Here, I'd use something like
A = asfortranarray(A.astype(float64)) B = asfortranarray(B.astype(float64))
cN=c_int(N) NRHS=c_int(1) LDA=c_int(N) IPIV=(c_int * N)() LDB=c_int(N) INFO=c_int(1) lapack=load_library('liblapack.so','/usr/lib/') lapack.argtypes=[c_int,c_int, ndpointer(dtype=float64, ndim=2, flags='FORTRAN'), c_int,c_int, ndpointer(dtype=float64, ndim=1, flags='FORTRAN'), c_int,c_int]
This should be lapack.dgesv_.argtypes.
Cheers Stéfan
On Wed, May 02, 2007 at 12:38:12AM +0200, Guillem Borrell i Nogueras wrote:
lapack.argtypes=[c_int,c_int, ndpointer(dtype=float64, ndim=2, flags='FORTRAN'), c_int,c_int, ndpointer(dtype=float64, ndim=1, flags='FORTRAN'), c_int,c_int]
This also isn't correct, according to the dgesv documentation. It should be
lapack.dgesv_.argtypes=[POINTER(c_int),POINTER(c_int), ndpointer(dtype=np.float64, ndim=2, flags='FORTRAN'), POINTER(c_int), POINTER(c_int),
ndpointer(dtype=np.float64, ndim=2, flags='FORTRAN'), POINTER(c_int),POINTER(c_int)]
I attach a working version of your script.
Cheers Stéfan