矩阵的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 [ ...
随机推荐
- Objective-C 中类属性(修饰)
Objective-C 中类属性(修饰) (2013-07-13 14:38:35) 转载▼ 标签: it 分类: IOS笔记 nonatomic: 非原子性访问,对属性赋值的时候不加锁,多线程并发访 ...
- Thinking in Java——笔记(4)
Controlling Execution true and false Java doesn't allow you to use a number as a boolean. If you wan ...
- 如何真正抓住微信小程序的红利? 阿禅知乎live总结
微信App定义 为满足用户某种开发需求.完全基于微信的消息或网页应用,入口是公众号,用户无需离开微信即可完成所有操作,所有需求都在公众号里被满足 微信App的优势 1. 顾客在哪里,就让顾客在哪里看到 ...
- 采用CSS3设计的登录界面,动态效果(动画)
与上一篇的“采用CSS3设计的登陆界面”的相同,只是样式style添加了CSS3的动画元素. style内容如下: <style> html,body,div{ margin:0; pad ...
- 多线程 - CountDownLatch
一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown() 方法,所以在当前计数到达 ...
- 以Debug模式启动JBoss
JBoss服务器的启动方法: 假设JBoss的安装目录为$JBOSS_HOME,Windows以及Linux环境下的Debug模式的启动方法分别为:Windows环境:找到Windows下的JBoss ...
- RDIFramework.NET ━ 9.2 员工管理 ━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.2 员工管理 -Web部分 员工(职员)管理主要是对集团.企事业内部员工进行管理.在后面的章节可以看到有一个用户管理,这两者 ...
- 帝国CMS列表模板页面内容截取
$listtemp = '<div class="c_n_item">';$listtemp .= '<div class="c_n_title&quo ...
- zjuoj 3606 Lazy Salesgirl
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3606 Lazy Salesgirl Time Limit: 5 Secon ...
- 9.请写出PHP5权限控制修饰符
1.public:public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private:private表示私有,私有的意思就是除了class自己之外,任何人都不可以直 ...