2

我编写了以下代码来从 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 而不是没有变化)。我怀疑有一些我不理解的基本内容。谁能告诉我我可能做错了什么?

4

1 回答 1

3

我本来不想回答我自己的问题,但我现在想通了。问题是 lapack 需要 Fortran 风格的列优先顺序的矩阵,但 numpy 默认使用 C 风格的行优先顺序。如果在我的测试代码中更改此行:

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

对此:

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

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

于 2013-02-14T19:09:00.290 回答