[英]How to get the pivot and rank from Matrix::qr() like that of base::qr()?
When applying Matrix::qr() on the sparse matrix in R, the output is quite different from that of base::qr.在 R 中的稀疏矩阵上应用 Matrix::qr() 时,output 与 base::qr 有很大不同。 There are V, beta, p, R, q but not rank and pivot.
有V、beta、p、R、q但没有rank和pivot。 Below is a small example code.
下面是一个小示例代码。 I want to detect linear dependent columns of the A sparse matrix, which requires the pivot and rank.
我想检测 A 稀疏矩阵的线性相关列,这需要 pivot 和秩。 How should I get these information?
我应该如何获得这些信息?
library(Matrix)
A <- matrix(c(0, -2, 1, 0,
0, -4, 2, 0,
1, -2, 1, 2,
1, -2, 1, 2,
1, -2, 1, 2), nrow = 5, byrow = T)
A.s <- as(A, "dgCMatrix")
qrA.M <- Matrix::qr(A.s)
qrA.R <- base::qr(A)
There is another related but not answered question, Get base::qr pivoting in Matrix::qr method还有另一个相关但未回答的问题, Get base::qr pivoting in Matrix::qr method
I would reconstruct your example matrix A
a little bit:我会重建你的示例矩阵
A
一点点:
A <- A[, c(1,4,3,2)]
# [,1] [,2] [,3] [,4]
#[1,] 0 0 1 -2
#[2,] 0 0 2 -4
#[3,] 1 2 1 -2
#[4,] 1 2 1 -2
#[5,] 1 2 1 -2
You did not mention in your question why rank
and pivot
returned by a dense QR factorization are useful.您在问题中没有提到为什么密集 QR 分解返回的
rank
和pivot
是有用的。 But I think this is what you are looking for:但我认为这就是你要找的东西:
dQR <- base::qr(A)
with(dQR, pivot[1:rank])
#[1] 1 3
So columns 1 and 3 of A
gives a basis for A
's column space.因此
A
A
列空间提供了基础。
I don't really understand the logic of a sparse QR factorization.我不太了解稀疏 QR 分解的逻辑。 The 2nd column of
A
is perfectly linearly dependent on the 1st column, so I expect column pivoting to take place during the factorization. A
的第二列完全线性地依赖于第一列,所以我希望在分解过程中发生列旋转。 But very much to my surprise, it doesn't!但令我惊讶的是,事实并非如此!
library(Matrix)
sA <- Matrix(A, sparse = TRUE)
sQR <- Matrix::qr(sA)
sQR@q + 1L
#[1] 1 2 3 4
No column pivoting is done, As a result, there isn't an obvious way to determine the rank of A
.没有进行列旋转,因此,没有明显的方法来确定
A
的等级。
At this moment, I could only think of performing a dense QR factorization on the R factor to get what you are looking for.此时,我只能考虑对R因子执行密集 QR 分解来得到你想要的。
R <- as.matrix(Matrix::qrR(sQR))
QRR <- base::qr(R)
with(QRR, pivot[1:rank])
#[1] 1 3
Why does this work?为什么这行得通? Well, the Q factor has orthogonal hence linearly independent columns, thus columns of R inherit linear dependence or independence of
A
.好吧, Q因子具有正交因此线性独立的列,因此R的列继承了
A
线性相关性或独立性。 For a matrix with much more rows than columns, the computational costs of this 2nd QR factorization is negligible.对于行数多于列数的矩阵,第二次 QR 分解的计算成本可以忽略不计。
I need to figure out the algorithm behind a sparse QR factorization before coming up with a better idea.在想出一个更好的主意之前,我需要弄清楚稀疏 QR 分解背后的算法。
I've been looking at a similar problem and I ended up not relying on Matrix::qr()
to calculate rank and to detect linear dependency.我一直在研究一个类似的问题,但我最终没有依赖
Matrix::qr()
来计算排名和检测线性依赖关系。 Instead I programmed the function GaussIndependent
in the package SSBtools
.相反,我在 package
GaussIndependent
中对 function SSBtools
进行了编程。
In the package examples I included an example that demonstrates wrong conclusion from rankMatrix(x, method = "qr")
.在 package 示例中,我包含了一个示例,该示例演示了
rankMatrix(x, method = "qr")
的错误结论。 Input x is a 44*20 dummy matrix.输入 x 是一个 44*20 的虚拟矩阵。
Starting with your example matrix, As
:从您的示例矩阵开始,
As
:
library(SSBtools)
GaussIndependent(A.s) # List of logical vectors specifying independent rows and columns
# $rows
# [1] TRUE FALSE TRUE FALSE FALSE
#
# $columns
# [1] TRUE TRUE FALSE FALSE
GaussRank(A.s) # the rank
# [1] 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.