Doolitter分解 三对角矩阵分解 拟三对角分解
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <vector>
#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)
{
;
;
;
}
/**
矩阵的 Doolittle分解:
[1] Mat是方阵
[2] Mat的前n-1阶主子式行列式不为0
[3] 分解的L为单位下三角阵
[4] 分解的U为上三角阵
[5] 返回值为<L,R>
**/
std::pair<MclMatrix,MclMatrix> DoolittleSplit(const MclMatrix &Mat)
{
int n=Mat.row;
MclMatrix L=MclMatrix(n,n);
MclMatrix U=MclMatrix(n,n);
;k<=n;k++)
{
for(int j=k;j<=n;j++)
{
U[k][j]=Mat[k][j];
;t<=k-;t++) U[k][j]-=L[k][t]*U[t][j];
}
if(k==n) continue;
;i<=n;i++)
{
L[i][k]=Mat[i][k];
;t<=k-;t++) L[i][k]-=L[i][t]*U[t][k];
L[i][k]/=U[k][k];
}
}
;i<=n;i++) L[i][i]=;
return std::make_pair(L,U);
}
/**
三角矩阵分解:
[1] Mat是方阵
[2] j<i且i-j>r时 Mat[i][j]=0
[2] j>i且j-i>s时 Mat[i][j]=0
**/
std::pair<MclMatrix,MclMatrix> TriangleSplit(const MclMatrix &Mat,int r,int s)
{
int n=Mat.row;
MclMatrix L=MclMatrix(n,n);
MclMatrix U=MclMatrix(n,n);
;k<=n;k++)
{
for(int j=k;j<=n;j++)
{
U[k][j]=Mat[k][j];
,std::max(k-r,j-s));t<=k-;t++) U[k][j]-=L[k][t]*U[t][j];
}
if(k==n) continue;
;i<=n;i++)
{
L[i][k]=Mat[i][k];
,std::max(i-r,k-s));t<=k-;t++) L[i][k]-=L[i][t]*U[t][k];
L[i][k]/=U[k][k];
}
}
;i<=n;i++) L[i][i]=;
return std::make_pair(L,U);
}
/**
拟三对角矩阵分解
对n=5 矩阵A样子如下:
a1 c1 0 0 d1
d2 a2 c2 0 0
0 d3 a3 c3 0
0 0 d4 a4 c4
c5 0 0 d5 a5
即输入为三个长度为n的向量
A=LU
L样子如下:
p1 0 0 0 0
d2 p2 0 0 0
0 d3 p3 0 0
0 0 d3 p4 0
r1 r2 r3 r4 r5
U样子如下:
1 q1 0 0 s1
0 1 q2 0 s2
0 0 1 q3 s3
0 0 0 1 s4
0 0 0 0 1
即将返回p,q,s,r四个向量(所有的向量长度都是n)
vector[0]=p
vector[1]=q
vector[2]=s
vector[3]=r
**/
std::vector<MclVector> QuasiDiagonalSplit(const MclVector &a,const MclVector &c,const MclVector &d)
{
int n=a.n;
assert(c.n==n);
assert(d.n==n);
assert(n>);
MclVector p=MclVector(n);
MclVector q=MclVector(n);
MclVector s=MclVector(n);
MclVector r=MclVector(n);
p[]=a[];
;i<=n-;i++)
{
q[i]=c[i]/p[i];
p[i+]=a[i+]-d[i+]*q[i];
}
s[]=d[]/p[];
;i<=n-;i++) s[i]=-d[i]*s[i-]/p[i];
s[n-]=(c[n-]-d[n-]*s[n-])/p[n-];
r[]=c[n];
;j<=n-;j++) r[j]=-r[j-]*q[j-];
r[n-]=d[n]-r[n-]*q[n-];
r[n]=a[n];
;j<=n-;j++) r[n]=r[n]-r[j]*s[j];
std::vector<MclVector> ans;
ans.push_back(p);
ans.push_back(q);
ans.push_back(s);
ans.push_back(r);
return ans;
}
Doolitter分解 三对角矩阵分解 拟三对角分解的更多相关文章
- 【矩阵】RQ/QR 分解
Multiple View Geometry in Computer Vision A.4.1.1 (page 579) 将一个 3x3 矩阵 $ A $ 进行 RQ 分解是将其分解成为一个上三角阵 ...
- 矩阵的五种分解的matlab实现
由于这学期修了矩阵分析这门课,课程要求用matlab实现矩阵的5种分解,仅仅是实现了分解,上传到博客存档,万一哪天某位同学就需要了呢.. 1.矩阵的满秩分解 代码实现 %矩阵的满秩分解 clear % ...
- 三对角矩阵(Tridiagonal Matrices)的求法:Thomas Algorithm(TDMA)
转载http://www.cnblogs.com/xpvincent/archive/2013/01/25/2877411.html 做三次样条曲线时,需要解三对角矩阵(Tridiagonal Mat ...
- gemm() 与 gesvd() 到矩阵求逆(inverse)(根据 SVD 分解和矩阵乘法求矩阵的逆)
可逆方阵 A 的逆记为,A−1,需满足 AA−1=I. 在 BLAS 的各种实现中,一般都不会直接给出 matrix inverse 的直接实现,其实矩阵(方阵)的逆是可以通过 gemm()和gesv ...
- 【matlab】 QR分解 求矩阵的特征值
"QR_H.m" function [Q,R] = QR_tao(A) %输入矩阵A %输出正交矩阵Q和上三角矩阵R [n,n]=size(A); E = eye(n); X = ...
- 整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...
- FZU 1075 分解素因子【数论/唯一分解定理/分解素因子裸模板】
[唯一分解定理]:https://www.cnblogs.com/mjtcn/p/6743624.html 假设x是一个正整数,它的值不超过65535(即1<x<=65535),请编写一个 ...
- CodeForces - 24D :Broken robot (DP+三对角矩阵高斯消元 随机)
pro:给定N*M的矩阵,以及初始玩家位置. 规定玩家每次会等概率的向左走,向右走,向下走,原地不动,问走到最后一行的期望.保留4位小数. sol:可以列出方程,高斯消元即可,发现是三角矩阵,O(N* ...
- Broken robot CodeForces - 24D (三对角矩阵简化高斯消元+概率dp)
题意: 有一个N行M列的矩阵,机器人最初位于第i行和第j列.然后,机器人可以在每一步都转到另一个单元.目的是转到最底部(第N个)行.机器人可以停留在当前单元格处,向左移动,向右移动或移动到当前位置下方 ...
随机推荐
- Java MD5加密算法学习
MD5,即"Message-Digest Algorithm 5(信息-摘要算法)",它由MD2.MD3.MD4发展而来的一种单向函数算法(也就是HASH算法),它是国际著名的公钥 ...
- request获取请求头和请求数据
package cn.itcast.request; import java.io.IOException; import java.io.InputStream; import java.io.Pr ...
- Linux内核设计第八周 ——进程的切换和系统的一般执行过程
Linux内核设计第八周 ——进程的切换和系统的一般执行过程 第一部分 知识点总结 第二部分 实验部分 1.配置实验环境,确保menu内核可以正常启动 2.进入gdb调试,在shedule和conte ...
- 经典C#编程理解,概要,经典
一.NET框架 ADO.NET微软提供的一组类库,可以帮助程序员和数据库交互. CLS(公共语言规范) CTS(通用语言类型) 类库: 可以看成一个承载了N个类的容器. 类库和命名空间: 一个类库对应 ...
- iOS,XMPP本地环境搭建和框架使用
1.XMPP的MySQL和openfire环境配置 2.XmppFramework框架导入和介绍 XMPP的MySQL和openfire环境配置 1.下载mysql安装 mysql下载 打开MySQL ...
- adop - ERRORMSG: Since earlier patching session failed and you are invoking apply again
$ adop phase=apply patches= hotpatch=yes *******FATAL ERROR******* PROGRAM : (/app/oracle/apps/VIS/f ...
- Maven聚合与继承的实例讲解(一)
概述 在javaweb高速发展的今天,我们软件设计人员往往会用很多种方式对软件划分模块,目的就是为了能有清晰的设计和低耦合性的,高重用性的软件.Maven有很好的依赖管理系统(Dependency M ...
- js 给样式添加随机颜色
下面提供了三种获取随机颜色值的方法 方法一: 创建一个颜色 HEX 值数组,然后随机抽取这个数组里6个值,组合生成颜色. function color1(){ var color = "&q ...
- Design and Analysis of Algorithms_Introduction
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- struts2-通配符映射(基本没啥卵用)和动态调用
通配符 使用*代表任意字符 一般在action的name中使用*,并可以使用多个 可以使用{通配符的序号}引用对应的通配符所代表的值,序号从1开始 {0}代表整个URI 匹配规则 首先完全匹配,没有完 ...