矩阵的QR分解
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <ctime>
class MclVector
{
public:
int n;
double *Mat;
/**
type=0: 列向量
type=1: 行向量
**/
int type;
MclVector() { Mat=NULL; n=; }
MclVector(int len,double initVal=0.0)
{
n=len;
Mat=];
;i<=n;i++) Mat[i]=initVal;
type=;
}
double operator[](int id) const
{
return Mat[id];
}
double& operator[](int id)
{
return Mat[id];
}
double length() const
{
;
;i<=n;i++) sum+=Mat[i]*Mat[i];
return sqrt(sum);
}
MclVector operator*(double val) const
{
MclVector ans=MclVector(n);
;i<=n;i++) ans[i]=Mat[i]*val;
return ans;
}
MclVector operator/(double val) const
{
MclVector ans=MclVector(n);
;i<=n;i++) ans[i]=Mat[i]/val;
return ans;
}
MclVector operator+(const MclVector &newVector) const
{
MclVector ans=MclVector(n);
;i<=n;i++) ans[i]=Mat[i]+newVector[i];
return ans;
}
MclVector operator-(const MclVector &newVector) const
{
MclVector ans=MclVector(n);
;i<=n;i++) ans[i]=Mat[i]-newVector[i];
return ans;
}
MclVector operator*=(double val)
{
;i<=n;i++) Mat[i]=Mat[i]*val;
return *this;
}
MclVector operator/=(double val)
{
;i<=n;i++) Mat[i]=Mat[i]/val;
return *this;
}
MclVector operator+=(const MclVector &newVector)
{
;i<=n;i++) Mat[i]+=newVector[i];
return *this;
}
MclVector operator-=(const MclVector &newVector)
{
;i<=n;i++) Mat[i]-=newVector[i];
return *this;
}
MclVector GetTranspose() const
{
MclVector ans=*this;
ans.type=;
return ans;
}
void print() const
{
;i<=n;i++) printf("%8.3lf ",Mat[i]);
puts("");
}
};
class MclMatrix
{
public:
int row,col;
MclVector *Mat;
MclMatrix() {Mat=NULL;}
MclMatrix(int _row,int _col,double initVal=0.0)
{
row=_row;
col=_col;
Mat=];
;i<=row;i++) Mat[i]=MclVector(col,initVal);
}
void setIdentityMatrix()
{
;i<=row;i++)
{
;j<=col;j++)
{
;
;
}
}
}
MclMatrix GetTranspose() const
{
MclMatrix ans=MclMatrix(col,row);
;i<=ans.row;i++)
{
;j<=ans.col;j++)
{
ans[i][j]=Mat[j][i];
}
}
return ans;
}
void print() const
{
;i<=row;i++) Mat[i].print();
puts("");
}
MclVector& operator[](int id) const
{
return Mat[id];
}
MclVector& operator[](int id)
{
return Mat[id];
}
MclMatrix operator*(const MclMatrix &Matrix) const
{
MclMatrix ans=MclMatrix(row,Matrix.col);
;i<=row;i++)
{
;j<=Matrix.col;j++)
{
;k<=col;k++)
{
ans[i][j]+=Mat[i][k]*Matrix[k][j];
}
}
}
return ans;
}
MclMatrix operator+(const MclMatrix &Matrix) const
{
MclMatrix ans=MclMatrix(row,Matrix.col);
;i<=row;i++)
{
;j<=Matrix.col;j++)
{
ans[i][j]=Mat[i][j]+Matrix[i][j];
}
}
return ans;
}
MclMatrix operator-(const MclMatrix &Matrix) const
{
MclMatrix ans=MclMatrix(row,Matrix.col);
;i<=row;i++)
{
;j<=Matrix.col;j++)
{
ans[i][j]=Mat[i][j]-Matrix[i][j];
}
}
return ans;
}
MclVector GetCol(int colId) const
{
MclVector ans=MclVector(row);
;i<=row;i++) ans[i]=Mat[i][colId];
return ans;
}
MclVector GetRow(int rowId) const
{
MclVector ans=MclVector(row);
;i<=col;i++) ans[i]=Mat[rowId][i];
return ans;
}
MclMatrix operator*=(const MclMatrix &Matrix)
{
return *this=*this*Matrix;
}
MclMatrix operator+=(const MclMatrix &Matrix)
{
return *this=*this+Matrix;
}
MclMatrix operator-=(const MclMatrix &Matrix)
{
return *this=*this-Matrix;
}
MclMatrix operator*(double x) const
{
MclMatrix ans=*this;
;i<=row;i++)
{
;j<=col;j++)
{
ans[i][j]*=x;
}
}
return ans;
}
};
MclMatrix vectorMulVector(const MclVector &A,const MclVector& B)
{
)
{
MclMatrix ans=MclMatrix(A.n,B.n);
;i<=A.n;i++)
{
;j<=B.n;j++)
{
ans[i][j]+=A[i]*B[j];
}
}
return ans;
}
else
{
assert(A.n==B.n);
MclMatrix ans=MclMatrix(,);
;i<=A.n;i++)
{
ans[][]+=A[i]*B[i];
}
return ans;
}
}
int sgn(double x)
{
;
;
;
}
/**
将矩阵A分解为一个正交矩阵Q和一个上三角矩阵R
A为任意实数矩阵
**/
std::pair<MclMatrix,MclMatrix> QRSplit(const MclMatrix &A)
{
assert(A.col==A.row);
int n=A.row;
MclMatrix Q=MclMatrix(n,n); Q.setIdentityMatrix();
MclMatrix R=A;
;i<n;i++)
{
MclVector s=R.GetCol(i);
;j<i;j++) s[j]=;
) continue;
double c=s.length();
) c*=-sgn(R[i][i]);
MclVector u=s; u[i]-=c;
MclVector uT=s.GetTranspose();
MclMatrix H=MclMatrix(n,n);
H.setIdentityMatrix();
H=H-vectorMulVector(u,uT)*(2.0/(u.length()*u.length()));
R=H*R;
Q=Q*H;
}
return std::make_pair(Q,R);
}
矩阵的QR分解的更多相关文章
- 矩阵的QR分解(三种方法)Python实现
1.Gram-Schmidt正交化 假设原来的矩阵为[a,b],a,b为线性无关的二维向量,下面我们通过Gram-Schmidt正交化使得矩阵A为标准正交矩阵: 假设正交化后的矩阵为Q=[A,B],我 ...
- QR 分解
将学习到什么 介绍了平面旋转矩阵,Householder 矩阵和 QR 分解以入相关性质. 预备知识 平面旋转与 Householder 矩阵是特殊的酉矩阵,它们在建立某些基本的矩阵分解过程中起着 ...
- 机器学习中的矩阵方法03:QR 分解
1. QR 分解的形式 QR 分解是把矩阵分解成一个正交矩阵与一个上三角矩阵的积.QR 分解经常用来解线性最小二乘法问题.QR 分解也是特定特征值算法即QR算法的基础.用图可以将分解形象地表示成: 其 ...
- 矩阵QR分解
1 orthonormal 向量与 Orthogonal 矩阵 orthonormal 向量定义为 ,任意向量 相互垂直,且模长为1: 如果将 orthonormal 向量按列组织成矩阵,矩阵为 ...
- QR分解
从矩阵分解的角度来看,LU和Cholesky分解目标在于将矩阵转化为三角矩阵的乘积,所以在LAPACK种对应的名称是trf(Triangular Factorization).QR分解的目的在 ...
- QR分解与最小二乘
主要内容: 1.QR分解定义 2.QR分解求法 3.QR分解与最小二乘 4.Matlab实现 一.QR分解 R分解法是三种将矩阵分解的方式之一.这种方式,把矩阵分解成一个正交矩阵与一个上三角矩阵的 ...
- QR分解与最小二乘(转载自AndyJee)
转载网址:http://www.cnblogs.com/AndyJee/p/3846455.html 主要内容: 1.QR分解定义 2.QR分解求法 3.QR分解与最小二乘 4.Matlab实现 一. ...
- QR分解迭代求特征值——原生python实现(不使用numpy)
QR分解: 有很多方法可以进行QR迭代,本文使用的是Schmidt正交化方法 具体证明请参考链接 https://wenku.baidu.com/view/c2e34678168884868762d6 ...
- MATLAB矩阵的LU分解及在解线性方程组中的应用
作者:凯鲁嘎吉 - 博客园http://www.cnblogs.com/kailugaji/ 三.实验程序 五.解答(按如下顺序提交电子版) 1.(程序) (1)LU分解源程序: function [ ...
随机推荐
- 使用ImageCreate()创建一个代表空白图像的变量
在建立图像创建环境之前,还需要做一些准备工作.首先,安装t1lib接着安装jpeg-6b,然后再安装GD库文件.在安装时一定要按这里给定的顺序进行安装,因为在编译GD入库时会用到jpeg-6b,如果没 ...
- Java的析构函数System的finalize()
一个对象是由产生 到使用 到销毁的过程 即C++中 构造函数-> body->析构函数 在Java之中为了回收不需要的空间可以使用System类的finalize() class A{ p ...
- iOS开发-Masonry简易教程
关于iOS布局自动iPhone6之后就是AutoLayOut,AutoLayOut固然非常好用,不过有时候我们需要在页面手动进行页面布局,VFL算是一种选择,如果对VFL不是很熟悉可以参考iOS开发- ...
- 一个关于Delphi XML处理单元的BUG
使用delphi的XML处理单元 XMLDoc XMLIntf 在获取XML文本内容的时候, 高版本的Delphi会丢失编码描述....在D7上却是正常的, 下面是测试源码: procedure TF ...
- Xcode 杂七杂八
一.Exception 的捕捉 1.message send to dealloc instance a, 输出控制台(lldb)后面输入:c + enter, 找到对应的行 b, po ...
- CSS 伪元素&伪类
单冒号(:)用于CSS3伪类,双冒号(::)用于CSS3伪元素 伪元素 属性 描述 CSS :first-letter 向文本的第一个字母添加特殊样式 1 :first-line 向文本的首行添加特殊 ...
- mui待解决问题
$.plusReady(function () { }); 里面的方法不执行: plusReady仅在5+ App或流应用中会触发 plusReady 参考网址: http://ask.dcloud ...
- 【转】OpenStack奥斯汀峰会Keynotes国内抢先看
http://www.openstack.cn/?p=5341 OpenStack奥斯汀峰会Keynotes国内抢先看入口:http://www.tudou.com/home/_903780397/i ...
- Android (二维码)关于java.lang.UnsatisfiedLinkError的小案例
在许多项目中我们都会用到第三方动态库.so文件,但是往往会引来很多烦恼,比如:Java.lang.UnsatisfiedLinkError - ::-/com.ishow.scan E/Android ...
- [SLAM] GMapping SLAM源码阅读(草稿)
目前可以从很多地方得到RBPF的代码,主要看的是Cyrill Stachniss的代码,据此进行理解. Author:Giorgio Grisetti; Cyrill Stachniss http: ...