矩阵分解与Ax=b

文章目录

    • 1. 矩阵分解
      • 1.1相关概念
      • 1.2 矩阵分解概述
      • 1.3 QR分解
      • 1.4 LU,LDU分解法
      • 1.5 乔里斯基(Cholesky)分解
      • 1.6 SVD
    • 2.使用Eigen解Ax=b线性方程组
    • 参考

1. 矩阵分解

1.1相关概念

正交矩阵:

若一个方阵其行与列皆为正交的单位向量,则该矩阵为正交矩阵,且该矩阵的转置和其逆相等。两个向量正交的意思是两个向量的内积为 0

正定矩阵:

如果对于所有的非零实系数向量x ,都有 x’Ax>0,则称矩阵A 是正定的。正定矩阵的行列式必然大于 0, 所有特征值也必然 > 0。相对应的,半正定矩阵的行列式必然 ≥ 0。

反射变换:

即欧氏平面上的轴反射变换和欧氏空间中的镜面反射变换统称反射变换

1.2 矩阵分解概述

  • SVD分解
  • QR分解
  • LU,LDU分解(等价于高斯消元)
  • Cholesky 分解(LDLT 和LL分解合起来称为乔里斯基分解Cholesky decomposition)

1.3 QR分解

根据《计算机视觉中的数学方法》定义 8.1.1 如果非奇异实矩阵 A 能够表示为正交矩阵 Q与上三角矩阵 R 的积,即 AQR (8.1.1) 则称式(8.1.1)是 A 的 QR 分解。

与 QR 分解类似,还有 QL,LQ,RQ 分解,其中 L 表示下三角矩阵。矩阵的 QR 分解,RQ 分 解,QL 分解,LQ 分解统称为矩阵的正交三角分解。

QR分解也有若干种算法,常见的包括Gram–Schmidt、Householder和Givens算法

schmidt正交化:
非奇异的矩阵各列向量线性无关,可以利用schmidt正交化公式(再加上单位化),将每个列向量化为相互正交的单位向量Q。则A=QR,R可以由正交化和单位化过程中生成的中间变量直接写出。
givens变换(初等旋转变换):

[外链图片转存失败(img-NA1IWszB-1564388122353)(1.PNG)]

主要是利用旋转矩阵变换不改变向量的模,所以是一种正交变换

A 是上三角矩阵 R 与Q=GxGyGz旋转矩阵的乘积。这样,我们就完成了 矩阵 A 的 RQ 分解。Givens 旋转方法,对于 n 阶矩阵需要作大量 Givens 旋转矩阵的积,计算量较大
Householder变换(初等反射变换):
[外链图片转存失败(img-b6w2V0zP-1564388122356)(2.png)]

称为 Householder 矩阵, Hv 是正交矩阵,更确切地说它是反射矩阵。

n 阶非奇异方阵A 作一系列的 Householder 变换,可化将它化为正交矩阵与上三角矩阵。

只需要作(n-1)个 Householder 矩阵的积,其计算量 大约是 Givens 旋转方法的一半

1.4 LU,LDU分解法

LU分解, 把矩阵分成下三角矩阵(Lower)和上三角矩阵(Upper)的一种分解。 所以LU分解只用到了三角矩阵;

在LU的基础上, 如果我们再进一步,引入对角矩阵(Diagonal)D, 那么LU分解就变成了LDU分解。

1.5 乔里斯基(Cholesky)分解

正定对称矩阵 A 可以唯一地分解为对角元素均大于零的上三角矩阵 L 与其转置 LT的乘积,其中 L为对角元素均大于零的上三角矩阵.
A = L L T = [ L 11 0 0 L 21 L 22 0 L 31 L 32 L 33 ] [ L 11 L 21 L 31 0 L 22 L 32 0 0 L 33 ] A=LL^T=\begin{bmatrix} L_{11} & 0 & 0 \\ L_{21} & L_{22} & 0 \\ L_{31} & L_{32} & L_{33} \end{bmatrix}\begin{bmatrix} L_{11} & L_{21} & L_{31} \\ 0 & L_{22} & L_{32} \\ 0 & 0& L_{33} \end{bmatrix} A=LLT=L11L21L310L22L3200L33L1100L21L220L31L32L33
对称矩阵的LDL分解就是LDU分解的一种特例。
A = L D L T = [ 1 0 0 L 21 1 0 L 31 L 32 0 ] [ D 1 0 0 0 D 2 0 0 0 D 3 ] [ 1 L 21 L 31 0 1 L 32 0 0 1 ] A=LDL^T=\begin{bmatrix} 1 & 0 & 0 \\ L_{21} & 1 & 0 \\ L_{31} & L_{32} & 0 \end{bmatrix}\begin{bmatrix} D_1 & 0 & 0 \\ 0 & D_2 & 0 \\ 0 & 0 & D_3 \end{bmatrix}\begin{bmatrix} 1 & L_{21} & L_{31} \\ 0 & 1 & L_{32} \\ 0 & 0& 1 \end{bmatrix} A=LDLT=1L21L3101L32000D1000D2000D3100L2110L31L321

1.6 SVD

奇异值分解定义:

有一个m×n的实数矩阵A,我们想要把它分解成如下的形式:
A = U Σ V T A=U\Sigma V^T A=UΣVT
其中UV均为单位正交阵

2.使用Eigen解Ax=b线性方程组

Decomposition Method Requirements on the matrix Speed (small-to-medium) Speed (large) Accuracy
PartialPivLU partialPivLu() Invertible ++ ++ +
FullPivLU fullPivLu() None - - - +++
HouseholderQR householderQr() None ++ ++ +
ColPivHouseholderQR colPivHouseholderQr() None + - +++
FullPivHouseholderQR fullPivHouseholderQr() None - - - +++
CompleteOrthogonalDecomposition completeOrthogonalDecomposition() None + - +++
LLT llt() Positive definite +++ +++ +
LDLT ldlt() Positive or negative semidefinite +++ + ++
BDCSVD bdcSvd() None - - +++
JacobiSVD jacobiSvd() None - - - - +++
  • PartialPivLU

LU decomposition of a matrix with partial pivoting。LU decomposition of a square invertible matrix, with partial pivoting(部分消元): the matrix A is decomposed as A = PLU where L is unit-lower-triangular(单位下三角), U is upper-triangular(上三角), and P is a permutation matrix(置换矩阵).

  • FullPivLU-比上面的要慢一些

LU decomposition of a matrix with complete pivoting。This class represents a LU decomposition of any matrix, with complete pivoting: the matrix A is decomposed as $ A = P^{-1} L U Q^{-1} $ where L is unit-lower-triangular, U is upper-triangular, and P and Q are permutation matrices

  • HouseholderQR

This class performs a QR decomposition of a matrix A into matrices Q and R such that
A = Q   R \mathbf{A} = \mathbf{Q} \, \mathbf{R} A=QR
by using Householder transformations. Here, Q a unitary matrix and R an upper triangular matrix

  • ColPivHouseholderQR

    Householder rank-revealing QR decomposition of a matrix with column-pivoting(列主消元法)

This class performs a rank-revealing(有置换矩阵变换的,特征值都会进行排序,由大到小,所以HouseholderQR不支持的) QR decomposition of a matrix A into matrices P, Q and R such that
A   P = Q   R \mathbf{A} \, \mathbf{P} = \mathbf{Q} \, \mathbf{R} AP=QR
by using Householder transformations. Here, P is a permutation matrix, Q a unitary matrix and R an upper triangular matrix.

  • FullPivHouseholderQR-比上面两个要慢

Householder rank-revealing QR decomposition of a matrix with full pivoting

This class performs a rank-revealing QR decomposition of a matrix A into matrices P, P’, Q and R such that
P   A   P ′ = Q   R \mathbf{P} \, \mathbf{A} \, \mathbf{P}' = \mathbf{Q} \, \mathbf{R} PAP=QR
by using Householder transformations. Here, P and P’ are permutation matrices, Q a unitary matrix and R an upper triangular matrix.

  • LLT

Standard Cholesky decomposition (LL^T) of a matrix and associated features

This class performs a LL^T Cholesky decomposition of a symmetric, positive definite matrix A such that A = LL^* = U^*U, where L is lower triangular

  • LDLT

Robust Cholesky decomposition of a matrix with pivoting。

Perform a robust Cholesky decomposition of a positive semidefinite or negative semidefinite matrix , where P is a permutation matrix, L is lower triangular with a unit diagonal and D is a diagonal matrix.

  • BDCSVD -最快的SVD算法

class Bidiagonal Divide and Conquer SVD

http://eigen.tuxfamily.org/dox/classEigen_1_1BDCSVD.html

  • JacobiSVD-SVD方法对于小型矩阵时,速度快,大型的慢

    Two-sided Jacobi SVD decomposition of a rectangular matrix

#include 
#include 
using namespace std;
using namespace Eigen;
int main()
{
   Matrix2f A, b;
   A << 2, -1, -1, 3;
   b << 1, 2, 3, 1;
   cout << "Here is the matrix A:\n" << A << endl;
   cout << "Here is the right hand side b:\n" << b << endl;
   Matrix2f x = A.ldlt().solve(b);
   cout << "The solution is:\n" << x << endl;
}

#include 
using namespace std;
#include 
// Eigen 部分
#include 
// 稠密矩阵的代数运算(逆,特征值等)
#include 
 
#define MATRIX_SIZE 100
 
/****************************
* 本程序演示了 Eigen 基本类型的使用
****************************/
 
int main( int argc, char** argv )
{
    // 解方程
    // 我们求解 A * x = b 这个方程
    // 直接求逆自然是最直接的,但是求逆运算量大
 
    Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > A1;
    A1 = Eigen::MatrixXd::Random( MATRIX_SIZE, MATRIX_SIZE );
 
    Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > b1;
    b1 = Eigen::MatrixXd::Random( MATRIX_SIZE, 1 );
 
    clock_t time_stt = clock(); // 计时
    // 直接求逆
    Eigen::Matrix<double,MATRIX_SIZE,1> x = A1.inverse()*b1;
    cout <<"time use in normal inverse is " << 1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC << "ms"<< endl;
    cout<<x<<endl;
    // QR分解colPivHouseholderQr()
    time_stt = clock();
    x = A1.colPivHouseholderQr().solve(b1);
    cout <<"time use in Qr decomposition is " <<1000*(clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl;
    cout <<x<<endl;
    //QR分解fullPivHouseholderQr()
    time_stt = clock();
    x = A1.fullPivHouseholderQr().solve(b1);
    cout <<"time use in Qr decomposition is " <<1000*(clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl;
    cout <<x<<endl;
    /* //llt分解 要求矩阵A正定
    time_stt = clock();
    x = A1.llt().solve(b1);
    cout <<"time use in llt decomposition is " <<1000*(clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl;
    cout <
    /*//ldlt分解  要求矩阵A正或负半定
    time_stt = clock();
    x = A1.ldlt().solve(b1);
    cout <<"time use in ldlt decomposition is " <<1000*(clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl;
    cout <
    //lu分解 partialPivLu()
    time_stt = clock();
    x = A1.partialPivLu().solve(b1);
    cout <<"time use in lu decomposition is " <<1000*(clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl;
    cout <<x<<endl;
    //lu分解(fullPivLu()
    time_stt = clock();
    x = A1.fullPivLu().solve(b1);
    cout <<"time use in lu decomposition is " <<1000*(clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl;
    cout <<x<<endl;
 
    return 0;
 
}

示例程序直接用的这个:https://blog.csdn.net/hanshihao1336295654/article/details/83512082

参考

矩阵分解 (加法篇)-讲解比较基础

矩阵分解 (乘法篇)

《计算机视觉中的数学方法》

《计算机视觉中的多视图几何》

矩阵分解之Givens变换与Householder变换-讲解深入浅出

你可能感兴趣的:(c++,计算机视觉,opencv,SLAM)