QR分解,LinearAlgebra包中计算结果疑问,望指教!

根据QR分解的定义:https://en.wikipedia.org/wiki/QR_decomposition
无论是采用 Gram–Schmidt法还是Householder reflectors法,计算出来的Q、R都与LinearAlgebra里面计算出的结果不一致,不能说LinearAlgebra计算的结果是错的,只是不太清楚包里是采用的什么算法?
image
image
image

using LinearAlgebra
A = [ 12.0 -51.0   4.0;
      6.0  167.0 -68.0;
     -4.0   24.0 -41.0]

F = qr(A)
Q = Matrix(F.Q)
R = F.R

同样的,对于矩阵B,结果也不一致,R对角元素里面竟然有负值。
image
image

B = [-3.0 -6.0;
     4.0 8.0;
     0.0 1.0]
FB = qr(B)
FQ = Matrix(FB.Q)
FR = FB.R

底层调用的是LAPACK。
wiki上的算法适合手算,不一定是最有效率的算法。

正交向量符号取反不影响正交性

谢谢,自己写的程序确实没有包里面的算法效率高

符号是不影响正交性,但我需要算出来的Q的元素和R中的对角元素进行下一步的计算,这样里面元素符号就会影响我下一步的计算

Let {\displaystyle \mathbf {x} }athbf {x} be an arbitrary real m -dimensional column vector of {\displaystyle A}A such that {\displaystyle |\mathbf {x} |=|\alpha |}athbf{x} = |lpha| for a scalar α . If the algorithm is implemented using floating-point arithmetic, then α should get the opposite sign as the k -th coordinate of {\displaystyle \mathbf {x} }athbf {x}, where {\displaystyle x_{k}}x_{k} is to be the pivot coordinate after which all entries are 0 in matrix A 's final upper triangular form, to avoid loss of significance.

取和x1相反的符号数值更稳定。

QR函数API中有如下解释:
qr(A, pivot=Val(false); blocksize) → F

The returned object F stores the factorization in a packed format:

  • if pivot == Val(true) then F is a QRPivoted object,
  • otherwise if the element type of A is a BLAS type (Float32, Float64, ComplexF32 or ComplexF64), then F is a QRCompactWY object,
  • otherwise F is a QR object.

从这里分析的返回的F应该有3种情况:QRPivaoted, QRCompactWY 和 QR 。
这三种对象的区别目前还不是很清楚,但是原因应该就在这里了。

还有一个问题,如何设置prvot的值?qr(A, true)会报错。

qr(A, Val(true))

1 个赞

另外还有一个问题:

julia> A
3×2 Array{Int64,2}:
 1  -4
 2   3
 2   2

julia> F=qr(A)
LinearAlgebra.QRCompactWY{Float64,Array{Float64,2}}
Q factor:
3×3 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:
 -0.333333   0.933333  -0.133333
 -0.666667  -0.333333  -0.666667
 -0.666667  -0.133333   0.733333
R factor:
2×2 Array{Float64,2}:
 -3.0  -2.0
  0.0  -5.0

julia> F.Q
3×3 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:
 -0.333333   0.933333  -0.133333
 -0.666667  -0.333333  -0.666667
 -0.666667  -0.133333   0.733333

julia> Matrix(F.Q)
3×2 Array{Float64,2}:
 -0.333333   0.933333
 -0.666667  -0.333333
 -0.666667  -0.133333

F.Q无法进行求逆运算,如果强制转换类型为Matrix,则矩阵尺寸少了一列,这是什么原因造成的?这就相当于只能看到QR分解的结果,但是却不能进一步使用

可以这样

julia> A = rand(3, 2);

julia> F = qr(A);

julia> F.Q * Matrix(I, 3, 3)
3×3 Array{Float64,2}:
 -0.543092   0.776157   0.320361
 -0.741833  -0.264778  -0.616097
 -0.393364  -0.572252   0.719578
1 个赞