我以两种不同的方式执行 QR 分解:使用标准 numpy 方法和使用在 CULA 库中实现的 GEQRF LAPACK 函数。这是python中的简单示例(用于访问CULA的PyCULA):
from PyCULA.cula import culaInitialize,culaShutdown
from PyCULA.cula import gpu_geqrf, gpu_orgqr
import numpy as np
import sys
def test_numpy(A):
Q, R = np.linalg.qr(A)
print "Q"
print Q
print "R"
print R
print "transpose(Q)*Q"
print np.dot(np.transpose(Q), Q)
print "Q*R"
print np.dot(Q,R)
def test_cula(A):
culaInitialize()
QR, TAU = gpu_geqrf(A)
R = np.triu(QR)
Q = gpu_orgqr(QR, A.shape[0], TAU)
culaShutdown()
print "Q"
print Q
print "R"
print R
print "transpose(Q)*Q"
print np.dot(np.transpose(Q), Q)
print "Q*R"
print np.dot(Q,R)
def main():
rows = int(sys.argv[1])
cols = int(sys.argv[2])
A = np.array(np.ones((rows,cols)).astype(np.float64))
print "A"
print A
print "NUMPY"
test_numpy(A.copy())
print "CULA"
test_cula(A.copy())
if __name__ == '__main__':
main()
它产生以下输出:
A
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
NUMPY
Q
[[-0.57735027 -0.57735027 -0.57735027]
[-0.57735027 0.78867513 -0.21132487]
[-0.57735027 -0.21132487 0.78867513]]
R
[[-1.73205081 -1.73205081 -1.73205081]
[ 0. 0. 0. ]
[ 0. 0. 0. ]]
transpose(Q)*Q
[[ 1.00000000e+00 2.77555756e-17 0.00000000e+00]
[ 2.77555756e-17 1.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]
Q*R
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
CULA
Q
[[-0.57735027 -0.57735027 -0.57735027]
[-0.57735027 0.78867513 -0.21132487]
[-0.57735027 -0.21132487 0.78867513]]
R
[[-1.73205081 0.3660254 0.3660254 ]
[-0. 0. 0. ]
[-0. 0. 0. ]]
transpose(Q)*Q
[[ 1.00000000e+00 2.77555756e-17 0.00000000e+00]
[ 2.77555756e-17 1.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]
Q*R
[[ 1. -0.21132487 -0.21132487]
[ 1. -0.21132487 -0.21132487]
[ 1. -0.21132487 -0.21132487]]
我的代码有什么问题?