矩阵的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 [ ...
随机推荐
- java中类的继承
我们都知道java的四大特性:抽象.继承.封装.多态: 那么关于继承有哪些特性呢?下面看具体实例: (1) public class Person { public int a=2; public ...
- 【iCore3 双核心板】例程二十六:MODBUS TCP实验——电源监控
实验指导书及代码包下载: http://pan.baidu.com/s/1pKhxKd9 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- php Output Control 函数 ob_系列函数详解
<?php /* * 输出缓冲控制 * * flush — 刷新输出缓冲 ob_clean — 清空(擦掉)输出缓冲区 ob_end_clean — 清空(擦除)缓冲区并关闭输出缓冲 ob_en ...
- kuohao
#include <stdio.h> int b[50]; int a[50]; int w[50]; int main() { freopen("in.txt",&q ...
- Android Http请求方法汇总
[转] 原文 这篇文章主要实现了在Android中使用JDK的HttpURLConnection和Apache的HttpClient访问网络资源,服务端采用python+flask编写,使用Serv ...
- .NET 框架基本原理透析⑴
.NET框架的核心便是通用语言运行时(CLR),顾名思义它是一个可被各种不同的编程语言所使用的运行时.CLR的很多特性可用于所有面向它的编程语言.比如,如果CLR用异常来报告错误,那么所有面向它的语言 ...
- 戴尔商务机OptiPlex5040问题
windows安装程序无法将Windows配置为在此计算机的硬件 你讲的那个提示准确讲应该是在系统装完重启后进入硬件检测和对应驱动开始阶段,应该是突然提示出来:windows安装程序无法将window ...
- SQL Nexus
在前面的SQLdiag系列中有提到SQLNexus,当时我们用SQLNexus查看了Perfmon Summary(性能计数器).ReadTrace Reports(跟踪文件)两项报表.SQLNexu ...
- Saving changes is not permitted in SQL Server
From Save (Not Permitted) Dialog Box on MSDN : The Save (Not Permitted) dialog box warns you that sa ...
- Netty之Java堆外内存扫盲贴
Java的堆外内存本来是高贵而神秘的东西,只在一些缓存方案的收费企业版里出现.但自从用了Netty,就变成了天天打交道的事情,毕竟堆外内存能减少IO时的内存复制,不需要堆内存Buffer拷贝一份到直接 ...