使用qr()对矩阵QR分解

简介:
QR分解法 是三种将 矩阵分解 的方式之一。这种方式,把 矩阵 分解成一个 半正交矩阵 与一个 上三角矩阵 的积。QR分解经常用来解 线性最小二乘法 问题。QR分解也是特定 特征值算法 QR算法 的基础。

A为m×n矩阵可以进行QR分解,A=QR,其中:Q'Q=I,在R中可以用函数qr()进行QR分解,例如:
> A=matrix(1:16,4,4)
> qr(A)
$qr
      [,1]     [,2]       [,3]       [,4]
[1,] -5.4772256 -12.7801930 -2.008316e+01 -2.738613e+01
[2,] 0.3651484 -3.2659863 -6.531973e+00 -9.797959e+00
[3,] 0.5477226 -0.3781696 2.641083e-15 2.056562e-15
[4,] 0.7302967 -0.9124744 8.583032e-01 -2.111449e-16

$rank
[1] 2

$qraux
[1] 1.182574e+00 1.156135e+00 1.513143e+00 2.111449e-16

$pivot
[1] 1 2 3 4

attr(,"class")
[1] "qr"

rank项返回矩阵的秩,qr项包含了矩阵Q和R的信息,要得到矩阵Q和R,可以用函数qr.Q()和qr.R()作用qr()的返回结果,
例如:
> qr.R(qr(A))
      [,1]     [,2]       [,3]       [,4]
[1,] -5.477226 -12.780193 -2.008316e+01 -2.738613e+01
[2,] 0.000000 -3.265986 -6.531973e+00 -9.797959e+00
[3,] 0.000000   0.000000 2.641083e-15 2.056562e-15
[4,] 0.000000   0.000000 0.000000e+00 -2.111449e-16

> qr.Q(qr(A))
      [,1]       [,2]     [,3]     [,4]
[1,] -0.1825742 -8.164966e-01 -0.4000874 -0.37407225
[2,] -0.3651484 -4.082483e-01 0.2546329 0.79697056
[3,] -0.5477226 -8.131516e-19 0.6909965 -0.47172438
[4,] -0.7302967 4.082483e-01 -0.5455419 0.04882607


验证 : 
A=QR
> qr.Q(qr(A))%*%qr.R(qr(A))
  [,1] [,2] [,3] [,4]
[1,]   1   5   9   13
[2,]   2   6   10   14
[3,]   3   7   11   15
[4,]   4   8   12   16


验证 Q'Q=I
 
    
> t(qr.Q(qr(A)))%*%qr.Q(qr(A))
        [,1]       [,2]       [,3]       [,4]
[1,] 1.000000e+00 -1.457168e-16 -6.760001e-17 -7.659550e-17
[2,] -1.457168e-16 1.000000e+00 -4.269046e-17 7.011739e-17
[3,] -6.760001e-17 -4.269046e-17 1.000000e+00 -1.596437e-16
[4,] -7.659550e-17 7.011739e-17 -1.596437e-16 1.000000e+00
> round(t(qr.Q(qr(A)))%*%qr.Q(qr(A)))
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1

> t(qr.Q(qr(A)))%*%qr.Q(qr(A))
        [,1]       [,2]       [,3]       [,4]
[1,] 1.000000e+00 -1.457168e-16 -6.760001e-17 -7.659550e-17
[2,] -1.457168e-16 1.000000e+00 -4.269046e-17 7.011739e-17
[3,] -6.760001e-17 -4.269046e-17 1.000000e+00 -1.596437e-16
[4,] -7.659550e-17 7.011739e-17 -1.596437e-16 1.000000e+00
> round(t(qr.Q(qr(A)))%*%qr.Q(qr(A)))
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1


qr.X(qr(A))也可以得到A
> qr.X(qr(A))
  [,1] [,2] [,3] [,4]
[1,]   1   5   9   13
[2,]   2   6   10   14
[3,]   3   7   11   15
[4,]   4   8   12   16



[参考]
2. > help(qr)

qr                    package:base                     R Documentation

The QR Decomposition of a Matrix

Description:

     ‘qr’ computes the QR decomposition of a matrix.

Usage:

     qr(x, ...)
     ## Default S3 method:
     qr(x, tol = 1e-07 , LAPACK = FALSE, ...)
     
     qr.coef(qr, y)
     qr.qy(qr, y)
     qr.qty(qr, y)
     qr.resid(qr, y)
     qr.fitted(qr, y, k = qr$rank)
     qr.solve(a, b, tol = 1e-7)
     ## S3 method for class 'qr'
     solve(a, b, ...)
     
     is.qr(x)
     as.qr(x)
     
Arguments:

       x: a numeric or complex matrix whose QR decomposition is to be
          computed.  Logical matrices are coerced to numeric.

     tol: the tolerance for detecting linear dependencies in the
          columns of ‘x’. Only used if ‘LAPACK’ is false and ‘x’ is
          real.

      qr: a QR decomposition of the type computed by ‘qr’.

    y, b: a vector or matrix of right-hand sides of equations.

       a: a QR decomposition or (‘qr.solve’ only) a rectangular matrix.

       k: effective rank.

  LAPACK: logical.  For real ‘x’, if true use LAPACK otherwise use
          LINPACK (the default).

     ...: further arguments passed to or from other methods

Details:

     The QR decomposition plays an important role in many statistical
     techniques.  In particular it can be used to solve the equation Ax
     = b for given matrix A, and vector b.  It is useful for computing
     regression coefficients and in applying the Newton-Raphson
     algorithm.

     The functions ‘qr.coef’, ‘qr.resid’, and ‘qr.fitted’ return the
     coefficients, residuals and fitted values obtained when fitting
     ‘y’ to the matrix with QR decomposition ‘qr’.  (If pivoting is
     used, some of the coefficients will be ‘NA’.)  ‘qr.qy’ and
     ‘qr.qty’ return ‘Q %*% y’ and ‘t(Q) %*% y’, where ‘Q’ is the
     (complete) Q matrix.

     All the above functions keep ‘dimnames’ (and ‘names’) of ‘x’ and
     ‘y’ if there are any.

     ‘solve.qr’ is the method for ‘solve’ for ‘qr’ objects.  ‘qr.solve’
     solves systems of equations via the QR decomposition: if ‘a’ is a
     QR decomposition it is the same as ‘solve.qr’, but if ‘a’ is a
     rectangular matrix the QR decomposition is computed first.  Either
     will handle over- and under-determined systems, providing a
     least-squares fit if appropriate.

     ‘is.qr’ returns ‘TRUE’ if ‘x’ is a ‘list’ with components named
     ‘qr’, ‘rank’ and ‘qraux’ and ‘FALSE’ otherwise.

     It is not possible to coerce objects to mode ‘"qr"’.  Objects
     either are QR decompositions or they are not.

     The LINPACK interface is restricted to matrices ‘x’ with less than
     2^31 elements.

     ‘qr.fitted’ and ‘qr.resid’ only support the LINPACK interface.

Value:

     The QR decomposition of the matrix as computed by LINPACK or
     LAPACK.  The components in the returned value correspond directly
     to the values returned by DQRDC/DGEQP3/ZGEQP3.

      qr: a matrix with the same dimensions as ‘x’.  The upper triangle
          contains the R of the decomposition and the lower triangle
          contains information on the Q of the decomposition (stored in
          compact form).  Note that the storage used by DQRDC and
          DGEQP3 differs.

   qraux: a vector of length ‘ncol(x)’ which contains additional
          information on Q.

    rank: the rank of ‘x’ as computed by the decomposition: always full
          rank in the LAPACK case.

   pivot: information on the pivoting strategy used during the
          decomposition.
     Non-complex QR objects computed by LAPACK have the attribute
     ‘"useLAPACK"’ with value ‘TRUE’.

Note:

     To compute the determinant of a matrix (do you _really_ need it?),
     the QR decomposition is much more efficient than using Eigen
     values (‘eigen’).  See ‘det’.

     Using LAPACK (including in the complex case) uses column pivoting
     and does not attempt to detect rank-deficient matrices.

Source:

     For ‘qr’, the LINPACK routine ‘DQRDC’ and the LAPACK routines
     ‘DGEQP3’ and ‘ZGEQP3’.  Further LINPACK and LAPACK routines are
     used for ‘qr.coef’, ‘qr.qy’ and ‘qr.aty’.

     LAPACK and LINPACK are from <URL: http://www.netlib.org/lapack>
     and <URL: http://www.netlib.org/linpack> and their guides are
     listed in the references.

References:

     Anderson. E. and ten others (1999) _LAPACK Users' Guide_. Third
     Edition. SIAM.
     Available on-line at <URL:
     http://www.netlib.org/lapack/lug/lapack_lug.html>.

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_.  Wadsworth & Brooks/Cole.

     Dongarra, J. J., Bunch, J. R., Moler, C. B. and Stewart, G. W.
     (1978) _LINPACK Users Guide._ Philadelphia: SIAM Publications.

See Also:

     ‘qr.Q’, ‘qr.R’, ‘qr.X’ for reconstruction of the matrices.
     ‘lm.fit’, ‘lsfit’, ‘eigen’, ‘svd’.

     ‘det’ (using ‘qr’) to compute the determinant of a matrix.

Examples:

     hilbert <- function(n) { i <- 1:n; 1 / outer(i - 1, i, "+") }
     h9 <- hilbert(9); h9
     qr(h9)$rank           #--> only 7
     qrh9 <- qr(h9, tol = 1e-10)
     qrh9$rank             #--> 9
     ##-- Solve linear equation system  H %*% x = y :
     y <- 1:9/10
     x <- qr.solve(h9, y, tol = 1e-10) # or equivalently :
     x <- qr.coef(qrh9, y) #-- is == but much better than
                           #-- solve(h9) %*% y
     h9 %*% x              # = y
     
     
     ## overdetermined system
     A <- matrix(runif(12), 4)
     b <- 1:4
     qr.solve(A, b) # or solve(qr(A), b)
     solve(qr(A, LAPACK = TRUE), b)
     # this is a least-squares solution, cf. lm(b ~ 0 + A)
     
     ## underdetermined system
     A <- matrix(runif(12), 3)
     b <- 1:3
     qr.solve(A, b)
     solve(qr(A, LAPACK = TRUE), b)
     # solutions will have one zero, not necessarily the same one

目录
相关文章
|
Rust 安全 Linux
如何使用Rust进行系统编程?
在 Rust 中,要调用系统调用并与底层 C 函数进行交互,通常会使用 `libc` crate。`libc` 提供了 Rust 到 C 的 FFI(Foreign Function Interface)绑定,允许 Rust 代码调用和使用底层的 C 函数和系统调用。
338 0
|
Python
Python:计算两个蛋白或小分子之间的RMSD
Python:计算两个蛋白或小分子之间的RMSD
1045 0
Python:计算两个蛋白或小分子之间的RMSD
|
7月前
|
搜索推荐 开发者 UED
【开发者必看—运动篇】数据赋能运动App留存率再创新高
如何在拉新后促活并成功留存?如何减少新用户流失?
【开发者必看—运动篇】数据赋能运动App留存率再创新高
|
人工智能 数据可视化 安全
瀑布模型是什么?在软件开发中有哪些主要阶段和步骤?
瀑布模型是一种经典的软件开发方法,将开发过程划分为需求分析、设计、编码、测试和维护等顺序阶段,强调阶段性和文档化。适用于需求明确、稳定且对安全性和可靠性要求高的项目。尽管存在局限性,但在特定场景下仍具重要价值。未来,瀑布模型可能与其他开发模型结合,更加灵活高效。
2401 3
瀑布模型是什么?在软件开发中有哪些主要阶段和步骤?
ffmpeg之QT开发环境搭建
ffmpeg之QT开发环境搭建
217 0
|
存储 NoSQL 关系型数据库
向量数据库有什么用?
向量数据库是一种特殊类型的数据库,它可以将非结构化数据映射为高维向量,并计算数据之间的相似性。它可以用于查找相似的数据、推荐系统、异常检测和临时存储等应用。目前市场上有一些专门的向量数据库产品,同时也可以使用已有的数据库产品来构建向量数据库。向量数据库的发展前景还不确定,但它已经成为热门技术,并吸引了大量的投资。
|
存储 安全 NoSQL
SpringSecurity 核心过滤器——CsrfFilter
Spring Security除了认证授权外功能外,还提供了安全防护功能。本文我们来介绍下SpringSecurity中是如何阻止CSRF攻击的。
478 0
|
计算机视觉 索引
NR PRACH(一)Preamble的确定
因为具有良好的自相关性和互相关性,恒幅低峰均比等特性,使用Zaddof-Chu序列作为PRACH 信道的上行同步序列
|
网络协议 BI 调度
NR PRACH(五) type1 RA(4-step)基本过程
无线通信,最重要的前提是建立接收端和发射端之间的时间同步。
|
项目管理
IPD(集成产品开发)—核心思想
IPD(集成产品开发)—核心思想
1276 0