#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. 使用EXECUTE IMMEDIATE来生成含有绑定变量的SQL

    一个SQL,通过SPM固定它的执行计划,可以通过DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE实现.也可以通地此功能在不修改原SQL的情况下对其加HINT来固定执行计划.D ...

  2. python学习道路(day1note)(变量,注释,用户输入,格式化输出,if,while,for循环并扩展练习)

    python是一门动态解释性的强类型定义语言,其应用范围非常之广 1:进入python语言 #!/usr/bin/env python #_*_coding:utf-8_*_ print(" ...

  3. Task示例,多线程

    class Program { static void Main(string[] args) { Run(); } public static async void Run() { var task ...

  4. wordpress搬家到本地URL修改问题

    把原来服务器上面的WordPress的数据库和目录文件全部备份下来,在本地用xampp搭了一个服务器,然后将数据库和目录文件全部导入,更改conf文件中的数据库账号密码.没想到本地网站的所有CSS样式 ...

  5. MVC项目实践,在三层架构下实现SportsStore,从类图看三层架构

    在"MVC项目实践,在三层架构下实现SportsStore-02,DbSession层.BLL层"一文的评论中,博友浪花一朵朵建议用类图来理解本项目的三层架构.于是就有了本篇: I ...

  6. cocos2dx 3.x(移动修改精灵坐标MoveTo与MoveBy)

    // // MainScene.cpp // helloworld // // Created by apple on 16/11/8. // // #include "MainScene. ...

  7. Mysql乱码

    MySql字符集 1.系统默认的.数据库默认的.表格默认的.列的 真正决定权在列定义上 2.latin1 系统默认字符编码 字符范围是0x00-0xff,可以存放任意编码的字符序列. 3.utf8编码 ...

  8. Linux内核中大小端判定宏

    #include <stdio.h> ];unsigned long mylong;} endian_test = { {'l','?','?','b'} }; #define ENDIA ...

  9. 快速掌握Flyway

    什么是Flyway? Flyway is an open-source database migration tool. It strongly favors simplicity and conve ...

  10. Python Decorator 和函数式编程

    看到一篇翻译不错的文章,原文链接: Python Decorator 和函数式编程