Mahout源码分析之 -- QR矩阵分解
一、算法原理
请参考我在大学时写的《QR方法求矩阵全部特征值》,其包含原理、实例及C语言实现:http://www.docin.com/p-114587383.html
二、源码分析
这里有一篇文章《使用MapRedece进行QR分解的步骤》可以看看
/**
For an <tt>m x n</tt> matrix <tt>A</tt> with <tt>m >= n</tt>, the QR decomposition is an <tt>m x n</tt>
orthogonal matrix <tt>Q</tt> and an <tt>n x n</tt> upper triangular matrix <tt>R</tt> so that
<tt>A = Q*R</tt>.
<P>
The QR decomposition always exists, even if the matrix does not have
full rank, so the constructor will never fail. The primary use of the
QR decomposition is in the least squares solution of non-square systems
of simultaneous linear equations. This will fail if <tt>isFullRank()</tt>
returns <tt>false</tt>.
*/ public class QRDecomposition implements QR {
private final Matrix q;
private final Matrix r;
private final boolean fullRank;
private final int rows;
private final int columns; /**
* Constructs and returns a new QR decomposition object; computed by Householder reflections; The
* decomposed matrices can be retrieved via instance methods of the returned decomposition
* object.
*
* @param a A rectangular matrix.
* @throws IllegalArgumentException if <tt>A.rows() < A.columns()</tt>.
*/
public QRDecomposition(Matrix a) { rows = a.rowSize();//m
int min = Math.min(a.rowSize(), a.columnSize());
columns = a.columnSize();//n Matrix qTmp = a.clone(); boolean fullRank = true; r = new DenseMatrix(min, columns); for (int i = 0; i < min; i++) {
Vector qi = qTmp.viewColumn(i);
double alpha = qi.norm(2);
if (Math.abs(alpha) > Double.MIN_VALUE) {
qi.assign(Functions.div(alpha));
} else {
if (Double.isInfinite(alpha) || Double.isNaN(alpha)) {
throw new ArithmeticException("Invalid intermediate result");
}
fullRank = false;
}
r.set(i, i, alpha); for (int j = i + 1; j < columns; j++) {
Vector qj = qTmp.viewColumn(j);
double norm = qj.norm(2);
if (Math.abs(norm) > Double.MIN_VALUE) {
double beta = qi.dot(qj);
r.set(i, j, beta);
if (j < min) {
qj.assign(qi, Functions.plusMult(-beta));
}
} else {
if (Double.isInfinite(norm) || Double.isNaN(norm)) {
throw new ArithmeticException("Invalid intermediate result");
}
}
}
}
if (columns > min) {
q = qTmp.viewPart(0, rows, 0, min).clone();
} else {
q = qTmp;
}
this.fullRank = fullRank;
} /**
* Generates and returns the (economy-sized) orthogonal factor <tt>Q</tt>.
*
* @return <tt>Q</tt>
*/
@Override
public Matrix getQ() {
return q;
} /**
* Returns the upper triangular factor, <tt>R</tt>.
*
* @return <tt>R</tt>
*/
@Override
public Matrix getR() {
return r;
} /**
* Returns whether the matrix <tt>A</tt> has full rank.
*
* @return true if <tt>R</tt>, and hence <tt>A</tt>, has full rank.
*/
@Override
public boolean hasFullRank() {
return fullRank;
} /**
* Least squares solution of <tt>A*X = B</tt>; <tt>returns X</tt>.
*
* @param B A matrix with as many rows as <tt>A</tt> and any number of columns.
* @return <tt>X</tt> that minimizes the two norm of <tt>Q*R*X - B</tt>.
* @throws IllegalArgumentException if <tt>B.rows() != A.rows()</tt>.
*/
@Override
public Matrix solve(Matrix B) {
if (B.numRows() != rows) {
throw new IllegalArgumentException("Matrix row dimensions must agree.");
} int cols = B.numCols();
Matrix x = B.like(columns, cols); // this can all be done a bit more efficiently if we don't actually
// form explicit versions of Q^T and R but this code isn't so bad
// and it is much easier to understand
Matrix qt = getQ().transpose();
Matrix y = qt.times(B); Matrix r = getR();
for (int k = Math.min(columns, rows) - 1; k >= 0; k--) {
// X[k,] = Y[k,] / R[k,k], note that X[k,] starts with 0 so += is same as =
x.viewRow(k).assign(y.viewRow(k), Functions.plusMult(1 / r.get(k, k))); // Y[0:(k-1),] -= R[0:(k-1),k] * X[k,]
Vector rColumn = r.viewColumn(k).viewPart(0, k);
for (int c = 0; c < cols; c++) {
y.viewColumn(c).viewPart(0, k).assign(rColumn, Functions.plusMult(-x.get(k, c)));
}
}
return x;
} /**
* Returns a rough string rendition of a QR.
*/
@Override
public String toString() {
return String.format(Locale.ENGLISH, "QR(%d x %d,fullRank=%s)", rows, columns, hasFullRank());
}
}
Mahout源码分析之 -- QR矩阵分解的更多相关文章
- Mahout源码分析之 -- 文档向量化TF-IDF
fesh个人实践,欢迎经验交流!Blog地址:http://www.cnblogs.com/fesh/p/3775429.html Mahout之SparseVectorsFromSequenceFi ...
- Mahout源码分析:并行化FP-Growth算法
FP-Growth是一种常被用来进行关联分析,挖掘频繁项的算法.与Aprior算法相比,FP-Growth算法采用前缀树的形式来表征数据,减少了扫描事务数据库的次数,通过递归地生成条件FP-tree来 ...
- mahout源码分析之Decision Forest 三部曲之二BuildForest(1)
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. BuildForest是在mahout-examples-0.7-job.jar包的org\apache ...
- mahout源码分析之DistributedLanczosSolver(五)Job over
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 1. Job 篇 接上篇,分析到EigenVerificationJob的run方法: public i ...
- mahout源码分析之DistributedLanczosSolver(六)完结篇
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 接上篇,分析完3个Job后得到继续往下:其实就剩下两个函数了: List<Map.Entry< ...
- mahout算法源码分析之Collaborative Filtering with ALS-WR (四)评价和推荐
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 首先来总结一下 mahout算法源码分析之Collaborative Filtering with AL ...
- mahout算法源码分析之Collaborative Filtering with ALS-WR拓展篇
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 额,好吧,心头的一块石头总算是放下了.关于Collaborative Filtering with AL ...
- zxing源码分析——QR码部分
Android应用横竖屏切换 zxing源码分析——DataMatrix码部分 zxing源码分析——QR码部分 2013-07-10 17:16:03| 分类: 默认分类 | 标签: |字号大中 ...
- mahout算法源码分析之Collaborative Filtering with ALS-WR 并行思路
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. mahout算法源码分析之Collaborative Filtering with ALS-WR 这个算 ...
随机推荐
- Python 面向对象(初级篇) (转)
概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发"更快更好更强..." 面 ...
- [dijkstra+heap优化] 模板
var n,m,s,i,j,x,y,z,l,tot :longint; pre,last,other,len :..] of longint; heap,d,pl :Array[..] of long ...
- QPS/QPS/PV/UV/服务器数量/并发数/吐吞量/响应时间计算公式
QPS:每秒查询率(Query Per Second) ,每秒的响应请求数,也即是最大吞吐能力.QPS = req/sec = 请求数/秒QPS统计方式 [一般使用 http_load 进行统计]QP ...
- PyCharm/IntelliJ IDEA Golang开发环境搭建(最方便快捷的GO语言开发环境)
IntelliJ太牛了.为了配置Go语言开发环境,折腾了半天,下IDE(Sublime Text,IntelliJ Idea),然后装Go插件.装Go插件还要下载插件项目源码,编译等等,Sublime ...
- SQL Server Profiler工具
一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收集查询信息: 分析性能: 诊断像死锁之类的问 ...
- 通过前台选择输入用来计算圆,三角形以及长方形的面积(此题目主要是while以及if 的使用)
#!/bin/usr/env python#coding=utf-8'''完成一段简单的Python程序,用于实现计算圆面积,三角形面积,长方形面积'''flag=Truewhile flag: pi ...
- 如何有效的使用C#读取文件
如何有效的使用C#读取文件 你平时是怎么读取文件的?使用流读取.是的没错,C#给我们提供了非常强大的类库(又一次吹捧了.NET一番),里面封装了几乎所有我们可以想到的和我们没有想到的类,流是读取文件 ...
- Rabbitmq集群高可用测试
Rabbitmq集群高可用 RabbitMQ是用erlang开发的,集群非常方便,因为erlang天生就是一门分布式语言,但其本身并不支持负载均衡. Rabbit模式大概分为以下三种:单一模式.普通模 ...
- view坐标_ _ Android应用坐标系统全面详解
转:http://blog.csdn.net/yanbober/article/details/50419117 1 背景 去年有很多人私信告诉我让说说自定义控件,其实通观网络上的很多博客都在讲各种自 ...
- 每天一个Linux命令
每天一个Linux命令(1):ls命令 每天一个Linux命令(2):cd命令 每天一个Linux命令(3):pwd命令 每天一个 Linux 命令(4):mkdir 每天一个 Linux 命令(5) ...