博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用qr()对矩阵QR分解
阅读量:6881 次
发布时间:2019-06-27

本文共 7891 字,大约阅读时间需要 26 分钟。

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 4attr(,"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. 
2. > help(qr)
qr                    package:base                     R DocumentationThe QR Decomposition of a MatrixDescription:     ‘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 methodsDetails:     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 
and
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
. 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

转载地址:http://gubbl.baihongyu.com/

你可能感兴趣的文章
Sql语句查询某列A相同值的另一列B最大值的数据
查看>>
技术串讲 CAS 有用
查看>>
怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
查看>>
Tensorflow学习笔记(1):tf.slice()函数使用
查看>>
ORA-01102的解决办法
查看>>
奇怪的iphone6 plus,H5调用拍照浏览器崩溃
查看>>
MVC接受JSON的一些注意事项
查看>>
response对象设置输出缓冲大小
查看>>
MVC+Ninject+三层架构+代码生成 -- 总结(七、顯示層 一)
查看>>
[CF1105D]Kilani and the Game
查看>>
[bzoj4195][Noi2015]程序自动分析
查看>>
简单的bfs(最短路径)c++
查看>>
Matlab2013a许可证过期问题,反复提示激活
查看>>
向上下左右不间断无缝滚动图片的效果(兼容火狐和IE)
查看>>
MongoDB 基础
查看>>
redis分布式集群3种架构方案
查看>>
C++ 编程思想——继承和组合
查看>>
Charles抓包显示乱码解决方法
查看>>
Web前端开发中的MCRV模式(转)
查看>>
VC中的字符串转换宏
查看>>