R 高斯消除和 qr 分解 (R Gaussian Elimination and qr factorization)


問題描述

R 高斯消除和 qr 分解 (R Gaussian Elimination and qr factorization)

I found the following R code using qr factorization cannot recover the original matrix. I cannot figure out why.

a <‑ matrix(runif(180),ncol=6)
a[,c(2,4)] <‑ 0
b <‑ qr(a)
d <‑ qr.Q(b) %*% qr.R(b)

then d is different from a in the way that all the zero columns are moved the the right side. It seems that qr factorization does not keep the row space.

‑‑‑‑‑

參考解法

方法 1:

When you read the help for qr you see that R uses a pivoted QR‑decomposition. So

str(b) 

gives

List of 4
 $ qr   : num [1:30, 1:6] ‑3.2292 0.218 0.0623 0.0371 0.302 ...
 $ rank : int 4
 $ qraux: num [1:6] 1.05 1.11 1.04 1.22 0 ...
 $ pivot: int [1:6] 1 3 5 6 2 4
 ‑ attr(*, "class")= chr "qr"

Thus you need to apply pivot to a or the inverse of pivot to d to line the matrices up correctly. So

pivots <‑ b$pivot
d.ok <‑ d[,order(pivots)]
all.equal(a,d.ok)

gives

[1] TRUE

You can also do

a.p <‑ a[,pivots]
all.equal(a.p,d)

which also results in TRUE.

(by user1539634Bhas)

參考文件

  1. R Gaussian Elimination and qr factorization (CC BY‑SA 3.0/4.0)

#matrix #R #linear-algebra






相關問題

BLAS 子程序 dgemm、dgemv 和 ddot 不適用於標量? (BLAS subroutines dgemm, dgemv and ddot doesn't work with scalars?)

為什麼我們需要維護自己的矩陣來轉換遊戲對象? (Why we need to maintain our own matrices to transform Game objects?)

R 高斯消除和 qr 分解 (R Gaussian Elimination and qr factorization)

生成尺寸為 8x8 的正定矩陣 (Generating Positive definite matrix of dimensions 8x8)

替代在此 Ruby 代碼中使用基於時間間隔分配標籤的巨型 if/else (Alternative to using a giant if/else in this Ruby code that assigns labels based on the time interval)

如何創建一個行矩陣,其元素是我的 while 循環的迭代 (How to create a row matrix whose elements are the iterations of my while loop)

在Matlab中找到矩陣中相同元素的開始索引和結束索引 (Find the Start Index and End Index of the same Element in a Matrix in Matlab)

用 Matlab 寫一個方程(矩陣大小) (writing an equation with Matlab (Matrix size))

使用 numpy 或 pandas 從元組列表中為二元組創建頻率矩陣 (Create a frequency matrix for bigrams from a list of tuples, using numpy or pandas)

如何在循環和 if 語句中使用遞歸公式 (How to use recursive formula in loop and if statement)

如何從 p 值矩陣中獲得緊湊的字母顯示? (How to get a compact letter display from a matrix of p-values?)

刺激基質上的液體流動 (Stimulating Liquid Flow on Matrix)







留言討論