#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分解的更多相关文章

  1. 矩阵的QR分解(三种方法)Python实现

    1.Gram-Schmidt正交化 假设原来的矩阵为[a,b],a,b为线性无关的二维向量,下面我们通过Gram-Schmidt正交化使得矩阵A为标准正交矩阵: 假设正交化后的矩阵为Q=[A,B],我 ...

  2. QR 分解

    将学习到什么 介绍了平面旋转矩阵,Householder 矩阵和 QR 分解以入相关性质.   预备知识 平面旋转与 Householder 矩阵是特殊的酉矩阵,它们在建立某些基本的矩阵分解过程中起着 ...

  3. 机器学习中的矩阵方法03:QR 分解

    1. QR 分解的形式 QR 分解是把矩阵分解成一个正交矩阵与一个上三角矩阵的积.QR 分解经常用来解线性最小二乘法问题.QR 分解也是特定特征值算法即QR算法的基础.用图可以将分解形象地表示成: 其 ...

  4. 矩阵QR分解

    1 orthonormal 向量与 Orthogonal 矩阵 orthonormal 向量定义为 ,任意向量  相互垂直,且模长为1: 如果将  orthonormal 向量按列组织成矩阵,矩阵为  ...

  5. QR分解

        从矩阵分解的角度来看,LU和Cholesky分解目标在于将矩阵转化为三角矩阵的乘积,所以在LAPACK种对应的名称是trf(Triangular Factorization).QR分解的目的在 ...

  6. QR分解与最小二乘

    主要内容: 1.QR分解定义 2.QR分解求法 3.QR分解与最小二乘 4.Matlab实现   一.QR分解 R分解法是三种将矩阵分解的方式之一.这种方式,把矩阵分解成一个正交矩阵与一个上三角矩阵的 ...

  7. QR分解与最小二乘(转载自AndyJee)

    转载网址:http://www.cnblogs.com/AndyJee/p/3846455.html 主要内容: 1.QR分解定义 2.QR分解求法 3.QR分解与最小二乘 4.Matlab实现 一. ...

  8. QR分解迭代求特征值——原生python实现(不使用numpy)

    QR分解: 有很多方法可以进行QR迭代,本文使用的是Schmidt正交化方法 具体证明请参考链接 https://wenku.baidu.com/view/c2e34678168884868762d6 ...

  9. MATLAB矩阵的LU分解及在解线性方程组中的应用

    作者:凯鲁嘎吉 - 博客园http://www.cnblogs.com/kailugaji/ 三.实验程序 五.解答(按如下顺序提交电子版) 1.(程序) (1)LU分解源程序: function [ ...

随机推荐

  1. HTTP常见错误代码总结

    1.HTTP 401 用户验证失败.不允许继续访问 2.HTTP 403 禁止访问,访问web应用,没有指定要访问页面的名称 3.HTTP 404 请求的文件找不到,一般情况是在浏览器输入地址时,输入 ...

  2. 用Scala实现集合中相邻元素间的差值

    欢迎转载,转载请注明出处,徽沪一郎. 概要 代码这东西,不写肯定不行,新学Scala不久,将实际遇到的一些问题记录下来,日后也好查找. 今天讲的是如何计算同一集合中元素两两之间的差值,即求开始集合(a ...

  3. 解决Sublime Text 3 Package Control 问题

    我使用的环境是 Mac OS X 10.11.5. 安装Packet Control之后,尝试安装插件,出现如下问题: There are no packages available for inst ...

  4. 12.162s 1805.867s

    [SQL]DROP PROCEDURE IF EXISTS truncate_insert_sales_rank_toparow_week; 受影响的行: 时间: .001s [SQL] CREATE ...

  5. HTML问题集锦及笔记

    1.<html>和<body>之间的输出? 加入<p>or<script>均可正常运行 2.<br />换行,用<br>< ...

  6. loadrunner generators (controller in windows)

    http://my.oschina.net/u/2391658/blog/735690 http://blog.csdn.net/xu1314/article/details/7455114 http ...

  7. python时间操作总结

    Unix时间戳 Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月 ...

  8. IntelliJ IDEA Community Edition 14.1.4下使用 Apache-Subversion搭建代码管理环境

    当前我的idea 版本是14.1.4. 1,)SVN Server下载与安装(https://www.visualsvn.com/server/): 因为我开发机是x64的,所以我优先下载 x64的 ...

  9. PHP---------去除数组里面值为空或者为空字符串的元素

    array_filter(array('a'=>'','',null,'b'=>3),function($val){         if($val===''||$val===null){ ...

  10. 。。。再战JQuery。。。

    今天从学习JQurery的第一个函数开始!!! JQuery里面的show这个函数很不错,我很喜欢,他的使用方法如下:JQuery对象.show(speed,callback); speed你可以指定 ...