使用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

[参考]
1. http://zh.wikipedia.org/zh-cn/QR%E5%88%86%E8%A7%A3

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
时间: 2024-08-02 09:18:02

使用qr()对矩阵QR分解的相关文章

矩阵论 第二章 矩阵的分解

1. QR分解(UR分解): 这是最基础的分解. 定理1: 设满秩方程A∈R(n x n), 则存在正交矩阵Q及正线(主对角线上元为正)上三角阵R,满足 A = QR, 且分解唯一. 构造性证明: 将A的n个列向量正交化(斯密特正交化)为y1,y2-yn, 然后标准化为z1,z2-zn, 则Q即为{z1,z2-zn}, R为: rii = ||yi|| , rij = (xj, zi) (i≥j). 有效性代入计算即可证明. 唯一性证明思路: 设A=Q1R1 = Q2R2, 右乘R1的逆, 然后

跟我一起数据挖掘(13)——矩阵分解

矩阵分解 (decomposition, factorization)是将矩阵拆解为数个矩阵的乘积,可分为三角分解.满秩分解.QR分解.Jordan分解和SVD(奇异值)分解等,常见的有三种:1)三角分解法 (Triangular Factorization),2)QR 分解法 (QR Factorization),3)奇异值分解法 (Singular Value Decompostion). 三角分解法 三角分解法是将原正方 (square) 矩阵分解成一个上三角形矩阵 或是排列(permut

再谈矩阵分解在推荐系统中的应用

本文将简单介绍下最近学习到的矩阵分解方法. (1)PureSvd 矩阵分解的核心是将一个非常稀疏的评分矩阵分解为两个矩阵,一个表示user的特性,一个表示item的特性,将两个矩阵中各取一行和一列向量做内积就可以得到对应评分. 那么如何将一个矩阵分解为两个矩阵就是唯一的问题了.说到这里大家就可能想起了在线代和数值分析中学到的各种矩阵分解方法,QR,Jordan,三角分解,SVD... 这里说说svd分解. svd是将一个任意实矩阵分解为三个矩阵U,S,V,其中,U,V是两个正交矩阵,称为左右奇异

Java 二维码,QR码,J4L-QRCode 的资料整理_java

  开源码 Java 解码器 (编码解码)下载:http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip Java QR Code Open Source Decoder (只有编码)下载:http://www.java4less.com/qrcoded.zip J4L-QRCode 1.0 - Java component to create QR Code barcodes http://www.mayacode.com

如何在事件营销中使用QR码?

什么是QR码? QR 码(Quick Response Code的缩写)是1994年由日本Denso-Wave公司发明一种特定的矩阵条码(或二维空间条码 ).QR 码呈正方形,只有黑白两色.它的原理就是将黑色模块排列在背景为白色的正方形图案上.码中存储的信息可能是文字.网址或其他数据.它的信息容量大.可靠性高 ,且无论以任何角度扫描,资料仍可被正确读取.QR码最常见于日本,并为目前日本最流行的二维空间条码.目前该技术已在日本,荷兰,韩国等地频繁使用.QR码原本是为了汽车制造厂便于追踪零件而设计的

卡巴斯基:提防“有毒”QR码

近日,知名信息安全厂商卡巴斯基发布了2011年第三季度IT威胁演化报告,显示不仅企业.组织遭受了多次未知黑客和黑客组织的攻击,个人用户也面临着巨大的网络威胁,主要表现为针对移动设备的恶意程序数量正以惊人的速率增长. 尤其在上一季度,针对安卓系统的手机恶意软件占到2011年所有手机恶意软件的40%,确凿的证实安卓平台已经成为恶意程序攻击的首要目标. 卡巴斯基实验室的分析师们早已预料到网络罪犯会寻找能够在安卓系统中使用的恶意软件,获取非法利益.而这一切的发生并没有花费很长的时间,7月一种Zitmo家

浅谈矩阵分解在推荐系统中的应用

推荐系统是当下越来越热的一个研究问题,无论在学术界还是在工业界都有很多优秀的人才参与其中.近几年举办的推荐系统比赛更是一次又一次地把推荐系统的研究推向了高潮,比如几年前的Neflix百万大奖赛,KDD CUP 2011的音乐推荐比赛,去年的百度电影推荐竞赛,还有最近的阿里巴巴大数据竞赛.这些比赛对推荐系统的发展都起到了很大的推动作用,使我们有机会接触到真实的工业界数据.我们利用这些数据可以更好地学习掌握推荐系统,这些数据网上很多,大家可以到网上下载. 推荐系统在工业领域中取得了巨大的成功,尤其是

使用PHP QR Code生成二维码

HP QR Code是一个PHP二维码生成类库,利用它可以轻松生成二维码,官网提供了下载和多个演示demo,查看地址: http://phpqrcode.sourceforge.net/ 下载官网提供的类库后,只需要使用phpqrcode.php就可以生成二维码了,当然您的PHP环境必须开启支持GD2. phpqrcode.php提供了一个关键的png()方法,其中参数$text表示生成二位的的信息文本:参数$outfile表示是否输出二维码图片 文件,默认否:参数$level表示容错率,也就是

用Spark学习矩阵分解推荐算法

在矩阵分解在协同过滤推荐算法中的应用中,我们对矩阵分解在推荐算法中的应用原理做了总结,这里我们就从实践的角度来用Spark学习矩阵分解推荐算法. 1. Spark推荐算法概述 在Spark MLlib中,推荐算法这块只实现了基于矩阵分解的协同过滤推荐算法.而基于的算法是FunkSVD算法,即将m个用户和n个物品对应的评分矩阵M分解为两个低维的矩阵: 其中k为分解成低维的维数,一般远比m和n小.如果大家对FunkSVD算法不熟悉,可以复习对应的原理篇. 2. Spark推荐算法类库介绍 在Spar