3

I am working on a piece of code to find the QR factorization of a matrix in R.

X <- structure(c(0.8147, 0.9058, 0.127, 0.9134, 0.6324, 0.0975, 0.2785, 
0.5469, 0.9575, 0.9649, 0.1576, 0.9706, 0.9572, 0.4854, 0.8003
), .Dim = c(5L, 3L))


myqr <- function(A) {
  n <- nrow(A)
  p <- ncol(A)
  Q <- diag(n)
  Inp <- diag(nrow = n, ncol = p)

  for(k in c(1:ncol(A))) {
    # extract the kth column of the matrix
    col<-A[k:n,k]
    # calculation of the norm of the column in order to create the vector
    norm1<-sqrt(sum(col^2))
    # Define the sign positive if a1 > 0 (-) else a1 < 0(+)  
    sign <- ifelse(col[1] >= 0, -1, +1)  
    # Calculate of the vector a_r
    a_r <- col - sign * Inp[k:n,k] * norm1
    # beta = 2 / ||a-r||^2  
    beta <- 2 / sum(t(a_r) %*% a_r)
    # the next line of code calculates the matrix Q in every step
    Q <- Q - beta *Q %*% c(rep(0,k-1),a_r) %*% t(c(rep(0,k-1),a_r))    
    # calculates the matrix R in each step
    A[k:n,k:p] <- A[k:n,k:p] - beta * a_r %*% t(a_r) %*% A[k:n,k:p]
    }

  list(Q=Q,R=A)
  }

But, Here I have not calculated in every step the matrix H that represents the householder reflection, also I have not calculated the matrix A in every step.

As H = I - 2 v v', if I multiply by Q I obtain

QH = Q - 2 (Qv) v'    // multiplication on the left
HQ = Q - 2 v (Q'v)'    // multiplication on the right

Now, this operations should be work in every step. However if I consider the first matrix H and he the second matrix H1.... these matrices will be of smaller that the first one. In order to avoid that I have used the next line of code:

 Q <- Q - beta * Q %*% c(rep(0,k-1),a_r) %*% t(c(rep(0,k-1),a_r))

but, I am not sure why the code is working well, when I generate the new vector a_r with the first k entries of zeros at every step.

4

1 回答 1

4

我以为你想要与返回的输出完全相同的输出qr.default,它使用紧凑的 QR 存储。但后来我意识到你是分开存储QR因素的。

通常,QR 分解只形成R而不是Q. 在下文中,我将描述形成两者的 QR 分解。对 QR 因式分解缺乏基本了解的朋友,请先阅读以下内容:lm():LINPACK / LAPACK 中 QR 分解返回的 qraux 是什么,其中有排列整齐的 LaTeX 数学公式。在下文中,我将假设人们知道 Householder 反射是什么以及它是如何计算的。


QR分解过程

首先,Householder 反射向量是H = I - beta * v v'beta在您的代码中计算的位置),而不是H = I - 2 * v v'

然后,QR 因式分解A = Q R(Hp ... H2 H1) A = R,其中Q = H1 H2 ... Hp。为了计算Q,我们初始化Q = I(单位矩阵),然后Hk在循环中迭代地在右边相乘。为了计算 R,我们在循环中迭代地在左侧进行初始化R = A和乘法运算。Hk

现在,在第 k 次迭代中,我们在和上有一个秩为 1 的矩阵更新QA

Q := Q Hk = Q (I - beta v * v') = Q - (Q v) (beta v)'
A := Hk A = (I - beta v * v') A = A - (beta v) (A' v)'

v = c(rep(0, k-1), a_r),其中a_r是全反射向量的减少的非零部分。

您拥有的代码正在以残酷的力量进行此类更新:

Q <- Q - beta * Q %*% c(rep(0,k-1), a_r) %*% t(c(rep(0,k-1),a_r))

它首先填充a_r以获得完整的反射向量并在整个矩阵上执行 rank-1 更新。但实际上我们可以去掉那些零并写(如果不清楚,做一些矩阵代数):

Q[,k:n] <- Q[,k:n] - tcrossprod(Q[, k:n] %*% a_r, beta * a_r)
A[k:n,k:p] <- A[k:n,k:p] - tcrossprod(beta * a_r, crossprod(A[k:n,k:p], a_r))

所以只有一小部分QA被更新。


对您的代码的其他几条评论

  • 你已经用t()"%*%"很多!但几乎所有这些都可以替换为crossprod()or tcrossprod()。这消除了显式转置t()并且内存效率更高;
  • Inp您初始化另一个不必要的对角矩阵。要获得户主反射向量a_r,您可以替换

    sign <- ifelse(col[1] >= 0, -1, +1)
    a_r <- col - sign * Inp[k:n,k] * norm1
    

    经过

    a_r <- col; a_r[1] <- a_r[1] + sign(a_r[1]) * norm1
    

    其中sign是 R 基函数。


用于 QR 分解的 R 代码

## QR factorization: A = Q %*% R
## if `complete = FALSE` (default), return thin `Q`, `R` factor
## if `complete = TRUE`, return full `Q`, `R` factor

myqr <- function (A, complete = FALSE) {

  n <- nrow(A)
  p <- ncol(A)
  Q <- diag(n)

  for(k in 1:p) {
    # extract the kth column of the matrix
    col <- A[k:n,k]
    # calculation of the norm of the column in order to create the vector r
    norm1 <- sqrt(drop(crossprod(col)))
    # Calculate of the reflection vector a-r
    a_r <- col; a_r[1] <- a_r[1] + sign(a_r[1]) * norm1
    # beta = 2 / ||a-r||^2  
    beta <- 2 / drop(crossprod(a_r))
    # update matrix Q (trailing matrix only) by Householder reflection
    Q[,k:n] <- Q[,k:n] - tcrossprod(Q[, k:n] %*% a_r, beta * a_r)
    # update matrix A (trailing matrix only) by Householder reflection
    A[k:n, k:p] <- A[k:n, k:p] - tcrossprod(beta * a_r, crossprod(A[k:n,k:p], a_r))
    }

  if (complete) {
     A[lower.tri(A)] <- 0
     return(list(Q = Q, R = A))
     }
  else {
    R <- A[1:p, ]; R[lower.tri(R)] <- 0
    return(list(Q = Q[,1:p], R = R))
    }
  }

现在让我们做一个测试:

X <- structure(c(0.8147, 0.9058, 0.127, 0.9134, 0.6324, 0.0975, 0.2785, 
0.5469, 0.9575, 0.9649, 0.1576, 0.9706, 0.9572, 0.4854, 0.8003
), .Dim = c(5L, 3L))

#       [,1]   [,2]   [,3]
#[1,] 0.8147 0.0975 0.1576
#[2,] 0.9058 0.2785 0.9706
#[3,] 0.1270 0.5469 0.9572
#[4,] 0.9134 0.9575 0.4854
#[5,] 0.6324 0.9649 0.8003

首先是瘦 QR 版本:

## thin QR factorization
myqr(X)

#$Q
#            [,1]       [,2]        [,3]
#[1,] -0.49266686 -0.4806678  0.17795345
#[2,] -0.54775702 -0.3583492 -0.57774357
#[3,] -0.07679967  0.4754320 -0.63432053
#[4,] -0.55235290  0.3390549  0.48084552
#[5,] -0.38242607  0.5473120  0.03114461
#
#$R
#          [,1]       [,2]       [,3]
#[1,] -1.653653 -1.1404679 -1.2569776
#[2,]  0.000000  0.9660949  0.6341076
#[3,]  0.000000  0.0000000 -0.8815566

现在是完整的 QR 版本:

## full QR factorization
myqr(X, complete = TRUE)

#$Q
#            [,1]       [,2]        [,3]       [,4]       [,5]
#[1,] -0.49266686 -0.4806678  0.17795345 -0.6014653 -0.3644308
#[2,] -0.54775702 -0.3583492 -0.57774357  0.3760348  0.3104164
#[3,] -0.07679967  0.4754320 -0.63432053 -0.1497075 -0.5859107
#[4,] -0.55235290  0.3390549  0.48084552  0.5071050 -0.3026221
#[5,] -0.38242607  0.5473120  0.03114461 -0.4661217  0.5796209
#
#$R
#          [,1]       [,2]       [,3]
#[1,] -1.653653 -1.1404679 -1.2569776
#[2,]  0.000000  0.9660949  0.6341076
#[3,]  0.000000  0.0000000 -0.8815566
#[4,]  0.000000  0.0000000  0.0000000
#[5,]  0.000000  0.0000000  0.0000000

现在让我们检查返回的标准结果qr.default

QR <- qr.default(X)

## thin R factor
qr.R(QR)
#          [,1]       [,2]       [,3]
#[1,] -1.653653 -1.1404679 -1.2569776
#[2,]  0.000000  0.9660949  0.6341076
#[3,]  0.000000  0.0000000 -0.8815566

## thin Q factor
qr.Q(QR)
#            [,1]       [,2]        [,3]
#[1,] -0.49266686 -0.4806678  0.17795345
#[2,] -0.54775702 -0.3583492 -0.57774357
#[3,] -0.07679967  0.4754320 -0.63432053
#[4,] -0.55235290  0.3390549  0.48084552
#[5,] -0.38242607  0.5473120  0.03114461

## full Q factor
qr.Q(QR, complete = TRUE)
#            [,1]       [,2]        [,3]       [,4]       [,5]
#[1,] -0.49266686 -0.4806678  0.17795345 -0.6014653 -0.3644308
#[2,] -0.54775702 -0.3583492 -0.57774357  0.3760348  0.3104164
#[3,] -0.07679967  0.4754320 -0.63432053 -0.1497075 -0.5859107
#[4,] -0.55235290  0.3390549  0.48084552  0.5071050 -0.3026221
#[5,] -0.38242607  0.5473120  0.03114461 -0.4661217  0.5796209

所以我们的结果是正确的!

于 2016-10-04T11:39:56.903 回答