gpt4 book ai didi

numpy - 从 Cython 调用 clapack

转载 作者:行者123 更新时间:2023-12-04 05:11:25 26 4
gpt4 key购买 nike

我编写了以下代码来调用 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 而没有改变)。我怀疑有一些基本的东西我不明白。谁能告诉我我可能做错了什么?

最佳答案

回答我自己的问题并不是我的意图,但我现在已经想通了。问题是 lapack 需要 Fortran 样式列优先顺序的矩阵,但默认情况下 numpy 使用 C 样式行优先顺序。如果在我的测试代码中我更改了这一行:

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

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

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

关于numpy - 从 Cython 调用 clapack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14881762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com