从Cython调用clapack

时间:2013-02-14 18:38:04

标签: numpy cython lapack

我已经编写了以下代码来调用Cython中的clapack例程dgelsy_,但它没有给出最小二乘问题的正确解。

cimport numpy as np
import numpy as np
ctypedef np.float64_t NP_FLOAT_t
ctypedef np.int_t NP_INT_t
ctypedef np.uint8_t NP_BOOL_t    
ctypedef int integer

cdef extern from "clapack.h":
    integer dgelsy_(integer *m, integer *n, integer *nrhs, 
    double *a, integer *lda, double *b, integer *ldb, integer *
    jpvt, double *rcond, integer *rank, double *work, integer *
    lwork, integer *info)

cpdef dgelsy(np.ndarray[NP_FLOAT_t,ndim=2] A, np.ndarray[NP_FLOAT_t,ndim=1] b, np.ndarray[NP_INT_t,ndim=1] jpvt):
    cdef integer m = A.shape[0]
    cdef integer n = A.shape[1]
    cdef integer nrhs = 1
    cdef integer lda = m
    cdef integer ldb = m
    cdef integer rank
    cdef NP_FLOAT_t rcond = 1e-16
    cdef integer lwork = -1
    cdef integer info

    #First call as a workspace query
    cdef np.ndarray[NP_FLOAT_t, ndim=1] work1 = np.empty(shape=1,dtype=np.float)
    dgelsy_(&m, &n, &nrhs, <double*>A.data, &lda, <double*>b.data, &ldb, 
            <integer*>jpvt.data, &rcond, &rank, <double*>work1.data, &lwork, &info)

    #Now the actual call to solve the problem
    lwork = <integer>work1[0]
    cdef np.ndarray[NP_FLOAT_t, ndim=1] work2 = np.empty(shape=lwork,dtype=np.float)
    dgelsy_(&m, &n, &nrhs, <double*>A.data, &lda, <double*>b.data, &ldb, 
            <integer*>jpvt.data, &rcond, &rank, <double*>work2.data, &lwork, &info)
    return rank, info

我相信我的setup.py文件是正确的。我的代码编译,链接和运行,但我收到编译时警告,我得到的解决方案不正确。这是我的Python测试代码:

import numpy
import cylapack #cylapack is my cython module with the code above
numpy.random.seed(1)
A = numpy.random.normal(size=(100,10))
A_ = A.copy()
x = numpy.random.normal(size=10)
b = numpy.dot(A,x) + numpy.random.normal(size=100)
b_ = b.copy()
pivots = numpy.zeros(shape=10,dtype=numpy.int)

print cylapack.dgelsy(A,b,pivots)
print pivots
x_ = numpy.linalg.lstsq(A_,b_,1e-16)[0]
print numpy.sum((numpy.dot(A_,x_) - b_)**2)
print numpy.sum((numpy.dot(A_,b[0:10]) - b_)**2)

输出以下内容:

(10, 0)
[25769803780 12884901896 30064771077 38654705666  4294967306           0
           0           0           0           0]
99.8269537854
1087.62032064

最后两个数字分别是numpy和lapack解决方案的剩余平方和。它们应该都是相同的,但很明显,lapack解决方案实际上并不正确。以下是我的编译器警告:

cylapack.c:1424: warning: passing argument 1 of 'dgelsy_' from incompatible pointer type
cylapack.c:1424: warning: passing argument 2 of 'dgelsy_' from incompatible pointer type
cylapack.c:1424: warning: passing argument 3 of 'dgelsy_' from incompatible pointer type
cylapack.c:1424: warning: passing argument 5 of 'dgelsy_' from incompatible pointer type
cylapack.c:1424: warning: passing argument 7 of 'dgelsy_' from incompatible pointer type
cylapack.c:1424: warning: passing argument 8 of 'dgelsy_' from incompatible pointer type
cylapack.c:1424: warning: passing argument 10 of 'dgelsy_' from incompatible pointer type
cylapack.c:1424: warning: passing argument 12 of 'dgelsy_' from incompatible pointer type
cylapack.c:1424: warning: passing argument 13 of 'dgelsy_' from incompatible pointer type
cylapack.c:1495: warning: passing argument 1 of 'dgelsy_' from incompatible pointer type
cylapack.c:1495: warning: passing argument 2 of 'dgelsy_' from incompatible pointer type
cylapack.c:1495: warning: passing argument 3 of 'dgelsy_' from incompatible pointer type
cylapack.c:1495: warning: passing argument 5 of 'dgelsy_' from incompatible pointer type
cylapack.c:1495: warning: passing argument 7 of 'dgelsy_' from incompatible pointer type
cylapack.c:1495: warning: passing argument 8 of 'dgelsy_' from incompatible pointer type
cylapack.c:1495: warning: passing argument 10 of 'dgelsy_' from incompatible pointer type
cylapack.c:1495: warning: passing argument 12 of 'dgelsy_' from incompatible pointer type
cylapack.c:1495: warning: passing argument 13 of 'dgelsy_' from incompatible pointer type

显然,编译器抱怨我的所有整数指针(我尝试使用long而不做任何更改)。我怀疑有些东西我不理解。谁能告诉我我可能做错了什么?

1 个答案:

答案 0 :(得分:3)

我不打算回答我自己的问题,但我现在已经弄明白了。问题是lapack期望Fortran样式列主要顺序的矩阵,但numpy默认使用C样式行主要顺序。如果在我的测试代码中我改变了这一行:

A = numpy.random.normal(size=(100,10))

到此:

A = numpy.random.normal(size=(10,100)).transpose()

然后它工作正常。不过,我仍然不了解编译器警告或者数据透视中的值,但它们似乎与问题的正确解决方案无关。