SLAM十四讲第二次作业-深蓝学院

在这里插入图片描述

  1. r(A)=n:矩阵A的秩等于未知数的个数。
  2. ⾼斯消元法:通过用初等行变换将增广矩阵化为行阶梯阵,然后通过回代求解线性方程组的解。原理是将方程组中每个方程含有的未知数的个数降到最低,并且最下面的方程含有的未知数的个数最少。
  3. QR分解:把矩阵分解成一个列向量正交矩阵与一个上三角矩阵的积。原理是将矩阵每个列作为一个基本单元,将其化为正交的基向量与在这个基向量上的投影长度的积。
  4. Cholesky 分解:将一个对称正定矩阵分解成一个下三角矩阵与其共轭转置之乘积。
  5. 代码见:eigenMatrix.cpp与CMakeLists.txt
    eigenMatrix.cpp文件
/*求解 A * x = B 这个方程*/

#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Cholesky>

using namespace std;
using namespace Eigen;

#define MATRIX_SIZE 100

int main( int argc,char** argv )
{
    MatrixXd A_pre = MatrixXd::Random( MATRIX_SIZE, MATRIX_SIZE );
    MatrixXd A = A_pre.transpose()*A_pre ;                  //使得A为正定对称矩阵,才能使得cholesky分解成功
    VectorXd B = VectorXd::Random( MATRIX_SIZE );
    VectorXd x = A.colPivHouseholderQr().solve(B);          //调用QR分解求解
    VectorXd y = A.llt().solve(B);                          //调用cholesky分解求解

    cout <<"A*x=B方程的解为\n"<< x << endl;
    cout <<"A*y=B方程的解为\n"<< y << endl;
}

CMaleLists.txt文件

cmake_minimum_required( VERSION 2.8 )
project( useEigen )

set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-O3" )

# 添加Eigen头文件
include_directories( "/usr/include/eigen3" )

add_executable( eigen eigenMatrix.cpp )

在这里插入图片描述
代码见:useGeometry.cpp与CMakeLists.txt

useGeometry.cpp文件

#include <iostream>
#include <cmath>
#include <Eigen/Core>
#include <Eigen/Geometry>

using namespace std;
using namespace Eigen;

int main(int arcg,char** argv)
{
    Quaterniond q1 = Quaterniond(0.55,0.3,0.2,0.2).normalized();   //定义并归一化四元数
    Quaterniond q2 = Quaterniond(-0.1,0.3,-0.7,0.2).normalized();
    Vector3d t1 ,t2,p1,p,p2;
    t1 << 0.7,1.1,0.2;
    t2 << -0.1,0.4,0.8;
    p1 << 0.5,-0.1,0.2;
  

    Isometry3d T_cw1 = Isometry3d::Identity();
    T_cw1.rotate ( q1 );
    T_cw1.pretranslate ( t1 );

    Isometry3d T_cw2 = Isometry3d::Identity();
    T_cw2.rotate ( q2 );
    T_cw2.pretranslate ( t2 );



    p = T_cw1.inverse() *p1;   //不能用转置,因为这里不是纯旋转,见书42页
    p2 = T_cw2 *p ;

    cout<<"p2 = "<<p2.transpose()<<endl;
}

CMakeLists.txt文件

cmake_minimum_required( VERSION 2.8 )
project( geometry )

# 添加Eigen头文件
include_directories( "/usr/include/eigen3" )

add_executable( eigenGeometry eigenGeometry.cpp )

在这里插入图片描述

  1. 证明
    设某个单位正交基 ( e 1 , e 2 , e 3 ) (e_1,e_2,e_3) 经过一次旋转变成了 ( f 1 , f 2 , f 3 ) (f_1,f_2,f_3) ,那么旋转矩阵
    R = [ e 1 T e 2 T e 3 T ] [ f 1 f 2 f 3 ] [ e 1 T e 2 T e 3 T ] [ e 1 e 2 e 3 ] = I R T R = [ f 1 T f 2 T f 3 T ] [ e 1 e 2 e 3 ] [ e 1 T e 2 T e 3 T ] [ f 1 f 2 f 3 ] = [ f 1 T f 2 T f 3 T ] I [ f 1 f 2 f 3 ] = I R T R = I R = \begin{bmatrix} e_1^T \\ e_2^T \\ e_3^T \end{bmatrix} \cdot \begin{bmatrix} f_1 &amp; f_2 &amp;f_3 \end{bmatrix} \\ \because \begin{bmatrix} e_1^T \\ e_2^T \\ e_3^T \end{bmatrix} \cdot \begin{bmatrix} e_1&amp; e_2 &amp;e_3 \end{bmatrix} = I \\ \begin{aligned}\therefore R^T \cdot R &amp;= \begin{bmatrix} f_1^T \\ f_2^T \\ f_3^T \end{bmatrix} \cdot \begin{bmatrix} e_1 &amp; e_2 &amp; e_3 \end{bmatrix} \cdot \begin{bmatrix} e_1^T \\ e_2^T \\ e_3^T \end{bmatrix} \cdot \begin{bmatrix} f_1 &amp; f_2 &amp;f_3 \end{bmatrix} \\ &amp;= \begin{bmatrix} f_1^T \\ f_2^T \\ f_3^T \end{bmatrix} \cdot I \cdot \begin{bmatrix} f_1 &amp; f_2 &amp;f_3 \end{bmatrix} \\ &amp;= I \end{aligned} \\ \therefore R^T \cdot R=I
    R = e 1 T e 2 T e 3 T f 1 f 2 f 3 = 1 d e t R = ± 1 \begin{aligned} |R| &amp;= \begin{vmatrix} e_1^T \\ e_2^T \\ e_3^T \end{vmatrix} \cdot \begin{vmatrix} f_1 &amp; f_2 &amp;f_3 \end{vmatrix} \\ &amp;=1 \\ \therefore detR = \pm 1 \end{aligned}
    又因为单位正交基的模为+1,
    d e t R = + 1 \therefore detR = +1
    所以 R T R = I R^TR=I ,且 d e t R = + 1 detR=+1
  2. 证明
    四元数由四个数组成,有一个实部和三个虚部,形如: q = q 0 + q 1 i + q 2 j + q 3 k q=q_0+q_1i+q_2j+q_3k 。那么上述的 ε = [ q 1 , q 2 , q 3 ] T R 3 \varepsilon = [ q_1,q_2,q_3]^T \in \mathbb{R}^3 η = q 0 R \eta = q_0 \in \mathbb{R}
    所以 ε \varepsilon η \eta 的维度分别为 3和1。
  3. 证明(一定要注意四元数实部与虚部的顺序,这里实部在最后)
    q 1 = [ x 1 , y 1 , z 1 , w 1 ] , q 2 = [ x 2 , y 2 , z 2 , w 2 ] q_1=[x_1,y_1,z_1,w_1],q_2=[x_2,y_2,z_2,w_2] ,其中 w 1 , w 2 w_1,w_2 为实部
    q 1 q 2 = ( w 1 x 2 + x 1 w 2 + y 1 z 2 z 1 y 2 ) i + ( w 1 y 2 x 1 z 2 + y 1 w 2 + z 1 x 2 ) j + ( w 1 z 2 + x 1 y 2 y 1 x 2 + z 1 w 2 ) k + w 1 w 2 x 1 x 2 y 1 y 2 z 1 z 2 \begin{aligned} q_1\cdot q_2 &amp;=(w_1x_2+x_1w_2+y_1z_2-z_1y_2)i \\&amp;+(w_1y_2-x_1z_2+y_1w_2+z_1x_2) j \\&amp;+(w_1z_2+x_1y_2-y_1x_2+z_1w_2) k \\&amp;+w_1w_2-x_1x_2-y_1y_2-z_1z_2 \end{aligned}
    q 1 + q 2 = [ w 1 z 1 y 1 x 1 z 1 w 1 x 1 y 1 y 1 x 1 w 1 z 1 x 1 y 1 z 1 w 1 ] [ x 2 y 2 z 2 w 2 ] = [ w 1 x 2 + x 1 w 2 + y 1 z 2 z 1 y 2 w 1 y 2 x 1 z 2 + y 1 w 2 + z 1 x 2 w 1 z 2 + x 1 y 2 y 1 x 2 + z 1 w 2 w 1 w 2 x 1 x 2 y 1 y 2 z 1 z 2 ] = ( w 1 x 2 + x 1 w 2 + y 1 z 2 z 1 y 2 ) i + ( w 1 y 2 x 1 z 2 + y 1 w 2 + z 1 x 2 ) j + ( w 1 z 2 + x 1 y 2 y 1 x 2 + z 1 w 2 ) k + w 1 w 2 x 1 x 2 y 1 y 2 z 1 z 2 = q 1 q 2 \begin{aligned} q_1^+q_2 &amp;= \begin{bmatrix} w_1 &amp; -z_1 &amp; y_1 &amp; x_1 \\z_1 &amp; w_1 &amp; -x_1 &amp; y_1\\-y_1 &amp; x_1 &amp; w_1 &amp; z_1 \\ -x_1 &amp; -y_1 &amp; -z_1 &amp; w_1 \end{bmatrix} \begin{bmatrix} x_2 \\ y_2 \\z_2 \\w_2 \end{bmatrix} \\ &amp;= \begin{bmatrix} w_1x_2+x_1w_2+y_1z_2-z_1y_2 \\ w_1y_2-x_1z_2+y_1w_2+z_1x_2 \\ w_1z_2+x_1y_2-y_1x_2+z_1w_2 \\ w_1w_2-x_1x_2-y_1y_2-z_1z_2 \end{bmatrix} \\&amp;=(w_1x_2+x_1w_2+y_1z_2-z_1y_2)i \\&amp;+(w_1y_2-x_1z_2+y_1w_2+z_1x_2) j \\&amp;+(w_1z_2+x_1y_2-y_1x_2+z_1w_2) k \\&amp;+w_1w_2-x_1x_2-y_1y_2-z_1z_2 \\ &amp;= q_1\cdot q_2 \end{aligned}
    所以: q 1 q 2 = q 1 + q 2 q_1 \cdot q_2 = q_1^+q_2
    同理可证: q 1 q 2 = q 2 q 1 q_1 \cdot q_2 = q_2^ \oplus q_1

证明方法二:
在这里插入图片描述
在这里插入图片描述
证明

如上图, V r o t V_{rot} 是由 V V 通过旋转得到,旋转轴是K,旋转的角度为 θ \theta ,用旋转矩阵R表示该旋转,即为: V r o t = R V V_{rot} = R \cdot V , 且 K K 为单位向量, K = 1 ||\vec{K} || =1 ,
V = V + V V=V_\perp + V_\parallel ,
V = V V = K × ( K × V ) V_\perp = V-V_\parallel = -K \times (K \times V)
V = ( K V ) K = K K T V V_\parallel = (K \cdot V) \cdot K = K K^T V ,
则垂直分量和平行分量各自的旋转分量为:
V r o t = V V_{\parallel rot} = V_\parallel
V r o t = c o s θ V + s i n θ K × V V_{\perp rot} = cos\theta V_\perp+sin\theta K \times V
V r o t = V r o t + V r o t = V + c o s θ V + s i n θ K × V = V + c o s θ ( V V ) + s i n θ K × V = c o s θ V + ( 1 c o s θ ) V + s i n θ K × V = c o s θ V + ( 1 c o s θ ) K K T V + s i n θ K × V = R V \begin{aligned}\therefore V_{rot} &amp;= V_{\parallel rot} + V_{\perp rot} \\&amp;= V_\parallel + cos\theta V_\perp +sin\theta K\times V \\&amp;=V_\parallel + cos\theta(V-V_\parallel) + sin\theta K \times V \\&amp;= cos\theta V + (1-cos\theta)V_\parallel +sin\theta K \times V \\&amp;= cos\theta V+ (1-cos\theta) K K^T V + sin\theta K^\times V \\&amp;=R \cdot V \end{aligned}
R = c o s θ I + ( 1 c o s θ ) K K T + s i n θ K × \therefore R = cos\theta I + (1-cos\theta) K K^T + sin\theta K^\times
R = c o s θ I + ( 1 c o s θ ) n n T + s i n θ n × \therefore R = cos\theta I + (1-cos\theta)nn^T +sin\theta n^\times

在这里插入图片描述

证明
(1) p p^{&#x27;} 的实部为0
假定点p的坐标为 p ( x , y , z ) p(x,y,z) ,则用四元数表示为: p = [ 0 , x , y , z ] = x i + y j + z k p=[0,x,y,z]=xi+yj+zk ,
q = [ c o s θ 2 , n s i n θ 2 ] = c o s θ 2 + n x s i n θ 2 i + n y s i n θ 2 j + n z s i n θ 2 k q=[cos\frac{\theta}{2} ,nsin\frac{\theta}{2}]=cos\frac{\theta}{2}+n_x sin\frac{\theta}{2}i+n_y sin\frac{\theta}{2}j+n_z sin\frac{\theta}{2}k
q = [ c o s θ 2 , n s i n θ 2 ] , q 1 = q / q 2 \because q^\ast=[cos\frac{\theta}{2} ,-\mathbf{n}sin\frac{\theta}{2}],q^{-1}=q^\ast/||q||^2
q 1 = [ c o s θ 2 , n s i n θ 2 ] / q 2 = ( c o s θ 2 n x s i n θ 2 i n y s i n θ 2 j n z s i n θ 2 k ) / q 2 q^{-1} = [cos\frac{\theta}{2} ,-nsin\frac{\theta}{2}]/||q||^2=(cos\frac{\theta}{2} - n_x sin\frac{\theta}{2}i - n_y sin\frac{\theta}{2}j - n_z sin\frac{\theta}{2}k)/||q||^2
p = q p q 1 = [ c o s θ 2 , n s i n θ 2 ] [ 0 , x , y , z ] [ c o s θ 2 , n s i n θ 2 ] / q 2 = ( c o s θ 2 + n x s i n θ 2 i + n y s i n θ 2 j + n z s i n θ 2 k ) ( x i + y j + z k ) ( ( c o s θ 2 n x s i n θ 2 i n y s i n θ 2 j n z s i n θ 2 k ) / q 2 ) \begin{aligned} \therefore p^{&#x27;} &amp;= qpq^{-1} \\&amp;=[cos\frac{\theta}{2} ,nsin\frac{\theta}{2}] [0,x,y,z] [cos\frac{\theta}{2} ,-nsin\frac{\theta}{2}]/||q||^2\\&amp;=(cos\frac{\theta}{2}+n_x sin\frac{\theta}{2}i+n_y sin\frac{\theta}{2}j+n_z sin\frac{\theta}{2}k)(xi+yj+zk)((cos\frac{\theta}{2} - n_x sin\frac{\theta}{2}i - n_y sin\frac{\theta}{2}j - n_z sin\frac{\theta}{2}k)/||q||^2) \end{aligned}
将所有的实数取出,且忽略 q 2 ||q||^2 ,则:
x n x c o s θ 2 s i n θ 2 + y n y c o s θ 2 s i n θ 2 + z n z c o s θ 2 s i n θ 2 + ( 1 ) x n x s i n θ 2 x c o s θ 2 + y n x n z s i n θ 2 s i n θ 2 + ( 1 ) z n x n y s i n θ 2 s i n θ 2 + ( 1 ) y n y s i n θ 2 c o s θ 2 + ( 1 ) x n y n z s i n θ 2 s i n θ 2 + z n x n y s i n θ 2 s i n θ 2 + x n y n z s i n θ 2 s i n θ 2 + ( 1 ) y n x n z s i n θ 2 s i n θ 2 + ( 1 ) z n z s i n θ 2 c o s θ 2 = 0 x n_x cos\frac{\theta}{2} sin\frac{\theta}{2} + y n_y cos\frac{\theta}{2} sin\frac{\theta}{2} +z n_z cos\frac{\theta}{2} sin\frac{\theta}{2} \\ + (-1) x n_x sin\frac{\theta}{2} x cos\frac{\theta}{2} + y n_x n_z sin\frac{\theta}{2} sin\frac{\theta}{2} + (-1)z n_x n_ysin\frac{\theta}{2} sin\frac{\theta}{2} \\+(-1)y n_y sin\frac{\theta}{2} cos\frac{\theta}{2} + (-1) x n_y n_z sin\frac{\theta}{2} sin\frac{\theta}{2} + z n_x n_y sin\frac{\theta}{2} sin\frac{\theta}{2} \\+ x n_y n_z sin\frac{\theta}{2} sin\frac{\theta}{2} +(-1) y n_x n_z sin\frac{\theta}{2}sin\frac{\theta}{2} + (-1) z n_z sin\frac{\theta}{2}cos\frac{\theta}{2} \\ \\=0
所以实部为0,即 p p^{&#x27;} 的实部为0。

(2) p = Q p p^{&#x27;} = Qp ,写出矩阵 Q Q
p = q p q 1 = q + p + q 1 = q + q 1 p \begin{aligned} p^{&#x27;} &amp;= qpq^{-1} \\ &amp;= q^+ p^+q^{-1} \\&amp;= q^+ q^{-1^\oplus}p \end{aligned}
Q = q + q 1 \therefore Q= q^+ q^{-1^\oplus}
假定四元数 q = [ x , y , z , w ] q=[x,y,z,w] ,其中 w w 是实部,那么 q 1 = [ x , y , z , w ] / q 2 q^{-1}=[-x,-y,-z,w]/||q||^2
q + = [ η 1 + ε × ε ε T η ] = [ w z y x z w x y y x w z x y z w ] q^+ = \begin{bmatrix} \eta1 + \varepsilon ^\times &amp; \varepsilon \\ -\varepsilon^T &amp; \eta \end{bmatrix}=\begin{bmatrix} w &amp; -z &amp; y &amp; x\\z &amp; w &amp; -x &amp; y\\-y &amp; x &amp; w &amp; z \\ -x &amp; -y &amp; -z &amp; w\end{bmatrix} ,
q 1 = [ η 1 ( ε ) × ε ( ε ) T η ] / q 2 = 1 q 2 [ η 1 + ε × ε ε T η ] = 1 q 2 [ w z y x z w x y y x w z x y z w ] q^{-1^\oplus}= \begin{bmatrix} \eta1 - (-\varepsilon) ^\times &amp; -\varepsilon \\ -(-\varepsilon)^T &amp; \eta \end{bmatrix}/||q||^2 = \frac{1}{||q||^2 }\cdot \begin{bmatrix} \eta1 + \varepsilon ^\times &amp; -\varepsilon \\ \varepsilon^T &amp; \eta \end{bmatrix}=\frac{1}{||q||^2 }\cdot \begin{bmatrix} w &amp; -z &amp; y &amp; -x\\z &amp; w &amp; -x &amp; -y\\-y &amp; x &amp; w &amp; -z \\ x &amp; y &amp; z &amp; w\end{bmatrix}
Q = q + q 1 = 1 q 2 [ w z y x z w x y y x w z x y z w ] [ w z y x z w x y y x w z x y z w ] = 1 q 2 [ w 2 + x 2 y 2 + z 2 2 x y 2 z w 2 w y + 2 x z 0 2 x y + 2 z w w 2 x 2 + y 2 z 2 2 y z 2 x w 0 2 x z 2 y w 2 y z + 2 x w w 2 x 2 y 2 + z 2 0 0 0 0 w 2 + x 2 + y 2 + z 2 ] \begin{aligned} \therefore Q &amp;= q^+ q^{-1^\oplus} \\&amp;= \frac{1}{||q||^2 }\cdot\begin{bmatrix} w &amp; -z &amp; y &amp; x\\z &amp; w &amp; -x &amp; y\\-y &amp; x &amp; w &amp; z \\ -x &amp; -y &amp; -z &amp; w\end{bmatrix}\begin{bmatrix} w &amp; -z &amp; y &amp; -x\\z &amp; w &amp; -x &amp; -y\\-y &amp; x &amp; w &amp; -z \\ x &amp; y &amp; z &amp; w\end{bmatrix} \\&amp;= \frac{1}{||q||^2 }\cdot \begin{bmatrix}w^2+x^2-y^2+z^2&amp; 2xy-2zw &amp; 2wy+2xz &amp; 0\\ 2xy+2zw&amp; w^2-x^2+y^2-z^2 &amp; 2yz-2xw &amp; 0\\2xz-2yw &amp; 2yz+2xw &amp; w^2-x^2-y^2+z^2 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; w^2+x^2+y^2+z^2\end{bmatrix} \end{aligned}

证明方法二:
在这里插入图片描述
在这里插入图片描述

  1. for(atuo& a: avec)
    范围for循环,用a遍历avec中的每个量;
  2. for(atuo& a: avec)
    自动类型推导,根据a获得的值,用auto自动推断出a的类型;
  3. [](const A&a1,const A&a2){return a1.index<a2.index;})
    运用了lambda表达式。
  4. begin()

猜你喜欢

转载自blog.csdn.net/weixin_41074793/article/details/84241776
今日推荐