R 有一个 qr() 函数,它使用 LINPACK 或 LAPACK 执行 QR 分解(根据我的经验,后者快 5%)。返回的主要对象是一个矩阵“qr”,其中包含上三角矩阵 R(即 R=qr[upper.tri(qr)])。到目前为止,一切都很好。 qr 的下三角部分包含“紧凑形式”的 Q。可以使用 qr.Q() 从 qr 分解中提取 Q。我想找到 qr.Q()
的逆。换句话说,我确实有 Q 和 R,并且想将它们放入“qr”对象中。 R 是微不足道的,但 Q 却不是。目标是应用 qr.solve()
,这比大型系统上的 solve()
快得多。
R has a qr()
function, which performs QR decomposition using either LINPACK or LAPACK (in my experience, the latter is 5% faster). The main object returned is a matrix "qr" that contains in the upper triangular matrix R (i.e. R=qr[upper.tri(qr)]
). So far so good. The lower triangular part of qr contains Q "in compact form". One can extract Q from the qr decomposition by using qr.Q()
. I would like to find the inverse of qr.Q()
. In other word, I do have Q and R, and would like to put them in a "qr" object. R is trivial but Q is not. The goal is to apply to it qr.solve()
, which is much faster than solve()
on large systems.
发布评论
评论(2)
简介
R 使用 LINPACK
dqrdc
例程,默认情况下,或 LAPACKDGEQP3
例程,当指定时,用于计算 QR 分解。两个例程都使用 Householder 反射来计算分解。 mxn 矩阵 A 分解为 mxn 经济尺寸正交矩阵 (Q) 和 nxn 上三角矩阵 (R),如 A = QR,其中 Q 可以通过 t 个 Householder 反射矩阵的乘积计算,其中 t 为较小者m-1 和 n:Q = H1H2...Ht。每个反射矩阵Hi可以由长度(m-i+1)向量表示。例如,H1 需要长度为 m 的向量以进行紧凑存储。该向量中除一个条目外的所有条目都放置在输入矩阵下三角形的第一列中(对角线由 R 因子使用)。因此,每次反射都需要多一个标量存储,这是由辅助向量(在 R 的
qr
结果中称为$qraux
)提供的。LINPACK 和 LAPACK 例程使用的紧凑表示不同。
LINPACK 方式
Householder 反射计算为 Hi = I - vi viT/pi,其中I是单位矩阵,pi是<中的对应条目code>$qraux,vi如下:
qr
后 A 的下三角)LINPACK 示例
让我们看一下 示例维基百科上 R 中的 QR 分解文章。
被分解的矩阵是
我们进行分解,结果最相关的部分如下所示:
该分解是通过计算两个 Householder 反射(在幕后)完成的将它们乘以 A 即可得到 R。我们现在将根据
$qr
中的信息重新创建反射。现在让我们验证上面计算的 Q 是否正确:
看起来不错!我们还可以验证 QR 等于 A。LAPACK
方式
A Householder 反射计算为 Hi = I - piviv iT,其中 I 是单位矩阵,pi 是
$qraux
中的相应条目,v i 如下:qr
后A的下三角形的一列)还有另一个转折当在 R 中使用 LAPACK 例程时:使用列旋转,因此分解正在解决一个不同的相关问题:AP = QR,其中 P 是 排列矩阵。
LAPACK 示例
本节执行与之前相同的示例。
注意
$pivot
字段;我们稍后会再讨论这一点。现在我们根据Aqr
的信息生成Q。再次,上面计算的 Q 与 R 提供的 Q 一致。
最后,让我们计算 QR。
注意到区别了吗? QR 是 A,其列按照上面
Bqr$pivot
中的顺序排列。Introduction
R uses the LINPACK
dqrdc
routine, by default, or the LAPACKDGEQP3
routine, when specified, for computing the QR decomposition. Both routines compute the decomposition using Householder reflections. An m x n matrix A is decomposed into an m x n economy-size orthogonal matrix (Q) and an n x n upper triangular matrix (R) as A = QR, where Q can be computed by the product of t Householder reflection matrices, with t being the lesser of m-1 and n: Q = H1H2...Ht.Each reflection matrix Hi can be represented by a length-(m-i+1) vector. For example, H1 requires a length-m vector for compact storage. All but one entry of this vector is placed in the first column of the lower triangle of the input matrix (the diagonal is used by the R factor). Therefore, each reflection needs one more scalar of storage, and this is provided by an auxiliary vector (called
$qraux
in the result from R'sqr
).The compact representation used is different between the LINPACK and LAPACK routines.
The LINPACK Way
A Householder reflection is computed as Hi = I - viviT/pi, where I is the identity matrix, pi is the corresponding entry in
$qraux
, and vi is as follows:qr
)LINPACK Example
Let's work through the example from the QR decomposition article at Wikipedia in R.
The matrix being decomposed is
We do the decomposition, and the most relevant portions of the result is shown below:
This decomposition was done (under the covers) by computing two Householder reflections and multiplying them by A to get R. We will now recreate the reflections from the information in
$qr
.Now let's verify the Q computed above is correct:
Looks good! We can also verify QR is equal to A.
The LAPACK Way
A Householder reflection is computed as Hi = I - piviviT, where I is the identity matrix, pi is the corresponding entry in
$qraux
, and vi is as follows:qr
)There is another twist when using the LAPACK routine in R: column pivoting is used, so the decomposition is solving a different, related problem: AP = QR, where P is a permutation matrix.
LAPACK Example
This section does the same example as before.
Notice the
$pivot
field; we will come back to that. Now we generate Q from the information theAqr
.Once again, the Q computed above agrees with the R-provided Q.
Finally, let's compute QR.
Notice the difference? QR is A with its columns permuted given the order in
Bqr$pivot
above.我已经按照OP的要求研究了同样的问题,但我认为这是不可能的。基本上,OP 问题是是否具有显式计算的 Q,可以恢复 H1 H2 ... Ht。我认为如果不从头开始计算 QR,这是不可能的,但我也很有兴趣知道是否有这样的解决方案。
我有一个与OP类似的问题,但在不同的上下文中,我的迭代算法需要通过添加列和/或行来改变矩阵A。第一次,使用 DGEQRF 计算 QR,从而使用紧凑的 LAPACK 格式。在矩阵 A 发生突变(例如使用新行)后,我可以快速构建一组新的反射器或旋转器,它们将消除现有 R 的最低对角线的非零元素并构建新的 R,但现在我有一组 H1_old H2_old ... Hn_old 和 H1_new H2_new ... Hn_new (以及类似的 tau )不能混合成单个 QR 紧凑表示。我有两种可能性,也许OP也有同样的两种可能性:
David 的长篇回答基本上解释了紧凑 QR 格式是什么,但没有解释如何获得这种具有显式计算的 Q 和 R 作为输入的紧凑 QR 格式。
I have researched for this same problem as the OP asks and I don't think it is possible. Basically the OP question is whether having the explicitly computed Q, one can recover the H1 H2 ... Ht. I do not think this is possible without computing the QR from scratch but I would also be very interested to know whether there is such solution.
I have a similar issue as the OP but in a different context, my iterative algorithm needs to mutate the matrix A by adding columns and/or rows. The first time, the QR is computed using DGEQRF and thus, the compact LAPACK format. After the matrix A is mutated e.g. with new rows I can quickly build a new set of reflectors or rotators that will annihilate the non-zero elements of the lowest diagonal of my existing R and build a new R but now I have a set of H1_old H2_old ... Hn_old and H1_new H2_new ... Hn_new (and similarly tau's) which can't be mixed up into a single QR compact representation. The two possibilities I have are, and maybe the OP has the same two possibilities:
The long answer from David basically explains what the compact QR format is but not how to get to this compact QR format having the explicit computed Q and R as input.