SLAM十四讲—CH3
标签: SLAM十四讲笔记
设线性⽅程 A x = b Ax = b Ax=b,在A 为⽅阵的前提下,请回答以下问题:
熟悉Eigen矩阵运算
1. 在什么条件下, x ⃗ \vec{x} x 有解且唯⼀?
如果A为n维方阵
∣
A
∣
≠
0
或
R
(
A
)
=
R
(
A
,
b
)
=
n
|A| \neq 0 或R(A)=R(A,b)=n
∣A∣=0或R(A)=R(A,b)=n
2. 高斯消元法的原理是什么?
Gauss消去法是一种规则化的加减消元法。它的基本思想是:通过逐次消元计算把需求解的线性方程组转化成上三角形方程组,也就是把线性方程组的系数矩阵转化为上三角矩阵,从而使一般线性方程组的求解转化为等价(同解)的上三角方程组求解。
- 顺序Gauss消去法
- 列主元Gauss消去法
3. QR 分解的原理是什么?
4.Cholesky分解的原理是什么?
Cholesky 分解是把一个对称正定的矩阵表示成一个下三角矩阵L和其转置的乘积的分解。它要求矩阵的所有特征值必须大于零,故分解的下三角的对角元也是大于零的。Cholesky分解法又称平方根法,是当A为实对称正定矩阵时,LU三角分解法的变形。
5.编程实现A 为100 x 100 随机矩阵时,⽤QR 和Cholesky 分解求x 的程序。你可以参考本次课⽤到的useEigen 例程。
#include "iostream"
using namespace std;
#include "ctime"
#include "Eigen/Core"
#include "Eigen/Dense"
#include <Eigen/Cholesky>
#include <Eigen/QR>
int main(int argc,char **argv){
Eigen::Matrix<double,100,100> matrix100_100;
matrix100_100 = Eigen::MatrixXd::Random(100,100);
Eigen::Matrix<double,100,1> vector100;
Eigen::Matrix<double,100,1> x;
vector100 = Eigen::MatrixXd::Random(100,1);
clock_t time_stt = clock();
x = matrix100_100.colPivHouseholderQr().solve(vector100);
cout <<x<<endl;
cout <<"time used in Qr decomposition is"<<1000*(clock()-time_stt)/(double)CLOCKS_PER_SEC<<"ms"<<endl;
time_stt = clock();
x = matrix100_100.householderQr().solve(vector100);
cout <<x<<endl;
cout <<"time used in qr decomposition is"<<1000*(clock()-time_stt)/(double)CLOCKS_PER_SEC<<"ms"<<endl;
time_stt = clock();
x = matrix100_100.ldlt().solve(vector100);
cout <<x<<endl;
cout <<"time used in ldlt decomposition is"<<1000*(clock()-time_stt)/(double)CLOCKS_PER_SEC<<"ms"<<endl;
}
几何运算练习
四元数->旋转向量->旋转矩阵
#include <iostream>
#include <Eigen/Geometry>
#include <cmath>
using namespace std;
int main(int argc,char **argv){
Eigen::Matrix<double,4,1> V_q1,V_q2;
V_q1<<0.55 ,0.3,0.2,0.2;
V_q2<<-0.1,0.3,-0.7,0.2;
double temp;
temp = sqrt(pow(V_q1[0],2)+pow(V_q1[1],2)+pow(V_q1[2],2)+pow(V_q1[3],2));
int i;
for(i=0;i<=3;i++)
{
V_q1[i]=V_q1[i]/temp;
}
temp = sqrt(pow(V_q2[0],2)+pow(V_q2[1],2)+pow(V_q2[2],2)+pow(V_q2[3],2));
for(i=0;i<=3;i++) {
V_q2[i] = V_q2[i] / temp;
}
Eigen::Quaterniond q1(V_q1[0],V_q1[1],V_q1[2],V_q1[3]);
cout<<q1.coeffs()<<endl;
Eigen::Quaterniond q2(V_q2[0],V_q2[1],V_q2[2],V_q2[3]);
cout<<q2.coeffs()<<endl;
Eigen::Matrix<double,3,1> p1,t1,t2;
t1<<0.7,1.1,0.2;
t2<<-0.1,0.4,0.8;
p1<<0.5,-0.1,0.2;
Eigen::AngleAxisd rotation_vectorT1=Eigen::AngleAxisd(q1);
Eigen::Isometry3d T1=Eigen::Isometry3d::Identity();
T1.rotate(rotation_vectorT1);
T1.pretranslate(t1);
Eigen::AngleAxisd rotation_vectorT2=Eigen::AngleAxisd(q2);
Eigen::Isometry3d T2=Eigen::Isometry3d::Identity();
T2.rotate(rotation_vectorT2);
T2.pretranslate(t2);
Eigen::Vector3d V_rotation;
V_rotation =T2*T1.inverse()*p1;
cout<<V_rotation.transpose()<<endl;
return 0;
}
四元数
#include <iostream>
#include <Eigen/Geometry>
#include <cmath>
using namespace std;
int main(int argc,char **argv){
Eigen::Matrix<double,4,1> V_q1,V_q2;
V_q1<<0.55 ,-0.3,-0.2,-0.2;
V_q2<<-0.1,0.3,-0.7,0.2;
double temp;
temp = sqrt(pow(V_q1[0],2)+pow(V_q1[1],2)+pow(V_q1[2],2)+pow(V_q1[3],2));
int i;
for(i=0;i<=3;i++)
{
V_q1[i]=V_q1[i]/temp;
}
temp = sqrt(pow(V_q2[0],2)+pow(V_q2[1],2)+pow(V_q2[2],2)+pow(V_q2[3],2));
for(i=0;i<=3;i++) {
V_q2[i] = V_q2[i] / temp;
}
Eigen::Quaterniond q1(V_q1[0],V_q1[1],V_q1[2],V_q1[3]);
cout<<q1.coeffs()<<endl;
Eigen::Quaterniond q2(V_q2[0],V_q2[1],V_q2[2],V_q2[3]);
cout<<q2.coeffs()<<endl;
Eigen::Matrix<double,3,1> t1,t2;
Eigen::Matrix<double,3,1> p1;
t1<<0.7,1.1,0.2;
t2<<-0.1,0.4,0.8;
Eigen::Vector3d V_rotation;
p1<<0.5,-0.1,0.2;
V_rotation = q1*(p1-t1);
V_rotation = q2*V_rotation+t2;
cout<<V_rotation.transpose();
return 0;
}
#include <iostream>
#include <Eigen/Geometry>
#include <cmath>
using namespace std;
using namespace Eigen;
int main(int argc,char **argv) {
Quaterniond q1(0.55,0.3,0.2,0.2),q2(-0.1,0.3,-0.7,0.2);
q1.normalize();
q2.normalize();
Vector3d t1(0.7,1.1,0.2),t2(-0.1,0.4,0.8);
Vector3d p1(0.5,-0.1,0.2);
Isometry3d T1w(q1),T2w(q2);
T1w.pretranslate(t1);
T2w.pretranslate(t2);
Vector3d p2 = T2w*T1w.inverse()*p1;
cout<<endl<<p2.transpose()<<endl;
return 0;
}
在利用四元数计算,发现
Eigen::Vector3d V_rotation;
p1<<0.5,-0.1,0.2;
V_rotation = q1*(p1-t1);
V_rotation = q2*V_rotation+t2;
cout<<V_rotation.transpose();
其中代码为q1*(p1-t1)与q1*p1-t1
即先平移再旋转和先旋转后平移结果不一样,并且四元数的逆操作不是乘以逆矩阵而是乘以共轭向量。
矩阵变换
旋转的表达
1.设有旋转矩阵 R R R,证明: R T R = I R^TR = I RTR=I 且 d e t R = + 1 2 detR = +1^2 detR=+12。
证明:
在坐标系中
A
A
A中有:
x
^
A
=
(
1
,
0
,
0
)
,
\hat{x}_A=(1,0,0),
x^A=(1,0,0),
y
^
A
=
(
0
,
1
,
0
)
,
\hat{y}_A=(0,1,0),
y^A=(0,1,0),
z
^
A
=
(
0
,
0
,
1
)
\hat{z}_A=(0,0,1)
z^A=(0,0,1)。
经过旋转后变成坐标系
B
B
B,用矩阵描述为:
M
B
=
R
M
A
M_B=RM_A
MB=RMA
旋转后的X轴在
A
A
A的坐标系中可表示为
x
^
B
=
a
x
^
A
+
b
y
^
A
+
c
z
^
A
\hat{x}_B=a\hat{x}_A+b\hat{y}_A+c\hat{z}_A
x^B=ax^A+by^A+cz^A
也即
x
^
B
=
(
x
^
B
x
^
A
T
,
x
^
B
y
^
A
T
,
x
^
B
z
^
A
T
)
\hat{x}_B=(\hat{x}_B\hat{x}_A^T,\hat{x}_B\hat{y}_A^T,\hat{x}_B\hat{z}_A^T)
x^B=(x^Bx^AT,x^By^AT,x^Bz^AT)
同理可得
y
^
B
,
z
^
B
\hat{y}_B,\hat{z}_B
y^B,z^B,则
R
=
[
x
^
B
x
^
A
T
y
^
B
x
^
A
T
z
^
B
x
^
A
T
x
^
B
y
^
A
T
y
^
B
y
^
A
T
z
^
B
y
^
A
T
x
^
B
z
^
A
T
y
^
B
z
^
A
T
z
^
B
z
^
A
T
]
R=\begin{bmatrix} \hat{x}_B\hat{x}_A^T&\hat{y}_B\hat{x}_A^T&\hat{z}_B\hat{x}_A^T\\ \hat{x}_B\hat{y}_A^T&\hat{y}_B\hat{y}_A^T&\hat{z}_B\hat{y}_A^T\\ \hat{x}_B\hat{z}_A^T&\hat{y}_B\hat{z}_A^T&\hat{z}_B\hat{z}_A^T \end{bmatrix}
R=
x^Bx^ATx^By^ATx^Bz^ATy^Bx^ATy^By^ATy^Bz^ATz^Bx^ATz^By^ATz^Bz^AT
R
T
=
[
x
^
B
x
^
A
T
x
^
B
y
^
A
T
x
^
B
z
^
A
T
y
^
B
x
^
A
T
y
^
B
y
^
A
T
y
^
B
z
^
A
T
z
^
B
x
^
A
T
z
^
B
y
^
A
T
z
^
B
z
^
A
T
]
R^T=\begin{bmatrix} \hat{x}_B\hat{x}_A^T&\hat{x}_B\hat{y}_A^T&\hat{x}_B\hat{z}_A^T\\ \hat{y}_B\hat{x}_A^T&\hat{y}_B\hat{y}_A^T&\hat{y}_B\hat{z}_A^T\\ \hat{z}_B\hat{x}_A^T&\hat{z}_B\hat{y}_A^T&\hat{z}_B\hat{z}_A^T \end{bmatrix}
RT=
x^Bx^ATy^Bx^ATz^Bx^ATx^By^ATy^By^ATz^By^ATx^Bz^ATy^Bz^ATz^Bz^AT
将坐标系
B
,
B,
B,逆旋转变成坐标系
A
A
A后,也可得到一个旋转矩阵为
R
R
R的逆矩阵,易知:
R
−
1
=
[
x
^
A
x
^
B
T
y
^
A
x
^
B
T
z
^
B
x
^
B
T
x
^
A
y
^
B
T
y
^
A
y
^
B
T
z
^
B
y
^
B
T
x
^
A
z
^
B
T
y
^
A
z
^
B
T
z
^
B
z
^
B
T
]
R^{-1}=\begin{bmatrix} \hat{x}_A\hat{x}_B^T&\hat{y}_A\hat{x}_B^T&\hat{z}_B\hat{x}_B^T\\ \hat{x}_A\hat{y}_B^T&\hat{y}_A\hat{y}_B^T&\hat{z}_B\hat{y}_B^T\\ \hat{x}_A\hat{z}_B^T&\hat{y}_A\hat{z}_B^T&\hat{z}_B\hat{z}_B^T \end{bmatrix}
R−1=
x^Ax^BTx^Ay^BTx^Az^BTy^Ax^BTy^Ay^BTy^Az^BTz^Bx^BTz^By^BTz^Bz^BT
以
x
^
B
x
^
A
T
\hat{x}_B\hat{x}_A^T
x^Bx^AT和
x
^
A
x
^
B
T
\hat{x}_A\hat{x}_B^T
x^Ax^BT为例:
前者表示在坐标系A下两个向量的点乘,后者表示在坐标系B下两个向量的点乘,两个向量长度相同,在不同坐标系下,点乘结果相同,也即相互的投影长度相等,即
x
^
B
x
^
A
T
=
x
^
A
x
^
B
T
\hat{x}_B\hat{x}_A^T=\hat{x}_A\hat{x}_B^T
x^Bx^AT=x^Ax^BT,同理可知
R
T
R^T
RT和
R
−
1
R^{-1}
R−1对应位置处的数值相等
R
T
=
R
−
1
R^T=R^{-1}
RT=R−1
所以旋转矩阵的逆为它的转置矩阵,即
R
R
T
=
R
R
−
1
=
E
RR^T=RR^{-1}=E
RRT=RR−1=E因此R矩阵为正交矩阵。
2.设有四元数 q q q,我们把虚部记为 ϵ \epsilon ϵ,实部记为 η \eta η,那么 q = ( ϵ , η ) q=(\epsilon,\eta) q=(ϵ,η)。请说明 ϵ \epsilon ϵ和 η \eta η的维度。
答:四元数 q q q,虚部有三个,实部有一个。所以 ϵ \epsilon ϵ的维度是3, η \eta η的维度1。
3.定义运算 + ^{+} +和 ⨁ ^{\bigoplus} ⨁,为: q + = [ η 1 − ϵ × ϵ ϵ T η ] , q ⨁ = [ η 1 + ϵ × ϵ − ϵ T η ] (1) q^{+}=\begin{bmatrix} \eta 1-\epsilon^{\times}&\epsilon\\\epsilon^{T}&\eta\end{bmatrix},q^{\bigoplus}=\begin {bmatrix}\eta1+\epsilon^{\times}&\epsilon\\-\epsilon^{T}&\eta\end{bmatrix} \tag 1 q+=[η1−ϵ×ϵTϵη],q⨁=[η1+ϵ×−ϵTϵη](1)请证明对任意单位四元数 q 1 q_{1} q1, q 2 q_{2} q2,四元数乘法可写成矩阵乘法: q 1 ⋅ q 2 = q 1 + q 2 (2) q_{1} \cdot q_{2} =q_{1}^{+}q_{2} \tag 2 q1⋅q2=q1+q2(2)或者 q 1 ⋅ q 2 = q 2 ⨁ q 1 (3) q_{1} \cdot q_{2} =q_{2}^{\bigoplus}q_{1} \tag 3 q1⋅q2=q2⨁q1(3)
证明:
q
1
=
[
ϵ
1
η
1
]
,
q
2
=
[
ϵ
2
η
2
]
q_{1}=\begin {bmatrix}\epsilon_{1}\\ \eta_{1} \end {bmatrix},q_{2}=\begin{bmatrix} \epsilon_{2} \\\eta_{2}\end{bmatrix}
q1=[ϵ1η1],q2=[ϵ2η2]
q
1
q
2
=
[
η
1
ϵ
2
+
η
2
ϵ
1
+
ϵ
1
×
ϵ
2
η
1
η
2
−
ϵ
1
T
ϵ
2
]
q_{1}q_{2}=\begin{bmatrix} \eta_{1}\epsilon_{2}+\eta_{2}\epsilon_{1}+\epsilon_{1}\times\epsilon_{2} \\\eta_{1}\eta_{2}-\epsilon_{1}^{T}\epsilon_{2}\end{bmatrix}
q1q2=[η1ϵ2+η2ϵ1+ϵ1×ϵ2η1η2−ϵ1Tϵ2]
q
1
+
q
2
=
[
η
1
1
+
ϵ
1
×
ϵ
1
−
ϵ
1
T
η
1
]
[
ϵ
2
η
2
]
=
[
(
η
1
1
+
ϵ
1
×
)
∗
ϵ
2
+
ϵ
1
∗
η
2
η
1
∗
η
2
−
ϵ
1
T
∗
ϵ
2
]
=
[
η
1
∗
ϵ
2
+
ϵ
1
∗
η
2
+
ϵ
1
×
ϵ
2
η
1
∗
η
2
−
ϵ
1
T
∗
ϵ
2
]
=
q
1
q
2
q_{1}^{+}q_{2}=\begin{bmatrix} \eta_{1}1+\epsilon_{1}^{\times}&\epsilon_{1}\\-\epsilon_{1}^{T}&\eta_{1}\end{bmatrix}\begin{bmatrix} \epsilon_{2}\\\eta_{2}\end{bmatrix}=\begin{bmatrix}(\eta_{1}1+\epsilon_{1}^{\times})*\epsilon_{2}+\epsilon_{1}*\eta_{2}\\\eta_{1}*\eta_{2}-\epsilon_{1}^{T}*\epsilon_{2}\end{bmatrix}=\begin{bmatrix}\eta_{1}*\epsilon_{2}+\epsilon_{1}*\eta_{2}+\epsilon_{1}\times\epsilon_{2}\\\eta_{1}*\eta_{2}-\epsilon_{1}^{T}*\epsilon_{2}\end{bmatrix}=q_{1}q_{2}
q1+q2=[η11+ϵ1×−ϵ1Tϵ1η1][ϵ2η2]=[(η11+ϵ1×)∗ϵ2+ϵ1∗η2η1∗η2−ϵ1T∗ϵ2]=[η1∗ϵ2+ϵ1∗η2+ϵ1×ϵ2η1∗η2−ϵ1T∗ϵ2]=q1q2
q
2
⨁
q
1
=
[
η
2
1
+
ϵ
2
×
ϵ
2
−
ϵ
2
T
η
2
]
[
ϵ
1
η
1
]
=
[
(
η
2
1
+
ϵ
2
×
)
∗
ϵ
1
+
ϵ
2
∗
η
1
η
1
∗
η
2
−
ϵ
2
T
∗
ϵ
1
]
=
[
η
1
∗
ϵ
2
+
ϵ
1
∗
η
2
+
ϵ
1
×
ϵ
2
η
1
∗
η
2
−
ϵ
1
T
∗
ϵ
2
]
=
q
1
q
2
q_{2}^{\bigoplus}q_{1}=\begin{bmatrix} \eta_{2}1+\epsilon_{2}^{\times}&\epsilon_{2}\\-\epsilon_{2}^{T}&\eta_{2}\end{bmatrix}\begin{bmatrix} \epsilon_{1}\\\eta_{1}\end{bmatrix}=\begin{bmatrix}(\eta_{2}1+\epsilon_{2}^{\times})*\epsilon_{1}+\epsilon_{2}*\eta_{1}\\\eta_{1}*\eta_{2}-\epsilon_{2}^{T}*\epsilon_{1}\end{bmatrix}=\begin{bmatrix}\eta_{1}*\epsilon_{2}+\epsilon_{1}*\eta_{2}+\epsilon_{1}\times\epsilon_{2}\\\eta_{1}*\eta_{2}-\epsilon_{1}^{T}*\epsilon_{2}\end{bmatrix}=q_{1}q_{2}
q2⨁q1=[η21+ϵ2×−ϵ2Tϵ2η2][ϵ1η1]=[(η21+ϵ2×)∗ϵ1+ϵ2∗η1η1∗η2−ϵ2T∗ϵ1]=[η1∗ϵ2+ϵ1∗η2+ϵ1×ϵ2η1∗η2−ϵ1T∗ϵ2]=q1q2
4.罗德里格斯公式证明
5.四元数运算性质的验证
SLAM十四讲第二版P59-60.