线性方程
高斯消元法相当于对方程组进行线性组合,组合时某一行消去其他所有的未知数,只剩下一个未知数,便能直接求解,然后反向代入其他方程式,求解其他未知数。
$[a_1,a_2 ,\cdots ,a_n]=[q_1, q_2 ,\cdots,q_n] \begin{bmatrix} r_{11} && r_{12} && \cdots && r_{1n} \ 0 && r_{22} && \cdots && r_{2n} \ \vdots && \vdots && \ddots && \vdots \ 0 && 0 && \cdots && r_{nn} \ \end{bmatrix} $
实际含义是已知矩阵有n个线性独立的行向量,利用QR分解求得此矩阵如何由n个单位正交基变换而来。
正交化:先求得与x
令
如此迭代下去,得到正交的
反解:
单位化:$q_i =\frac{y_i}{||y_i||}$
$A=[x_1,x_2 ,\cdots ,x_n]=[q_1, q_2 ,\cdots,q_n] \begin{bmatrix} r_{11} && r_{12} && \cdots && r_{1n} \ 0 && r_{22} && \cdots && r_{2n} \ \vdots && \vdots && \ddots && \vdots \ 0 && 0 && \cdots && r_{nn} \ \end{bmatrix} $
$=[q_1, q_2 ,\cdots,q_n] \begin{bmatrix} q_1^T x_1 && q_1^T x_2 && \cdots && q_1^T x_n \ 0 && q_2^T x_2 && \cdots && q_2^T x_n \ \vdots && \vdots && \ddots && \vdots \ 0 && 0 && \cdots && q_n^T x_n \ \end{bmatrix} $
令
$Q= \begin{bmatrix} cos(\theta) && -sin(\theta)\ sin(\theta) && cos(\theta) \end{bmatrix} = \frac{1}{\sqrt{x_1^2 +x_2^2}} \begin{bmatrix} x_1 && x_2 \ -x_2&& x_1 \end{bmatrix}$
在向量空间中任取两个坐标轴,假设是第i个和第j个轴,$i \neq j$ ,Given 旋转就是在这两个坐标轴所张开的平面上旋转。
$Q_{ji} = \begin{bmatrix} 1 && \cdots&& 0&&\cdots &&0&& \cdots&&0\ \vdots &&\ddots && \vdots && &&\vdots &&&&\vdots \ 1 && \cdots&& c&&\cdots &&-s&& \cdots&&0\ \vdots && && \vdots &&\ddots &&\vdots &&&&\vdots \ 1 && \cdots&& -s&&\cdots &&c&& \cdots&&0\ \vdots && && \vdots && &&\vdots && \ddots &&\vdots \ 1 && \cdots&& 0&&\cdots &&0&& \cdots&&1 \end{bmatrix} $
$ \begin{bmatrix} 1 && \cdots&& 0&&\cdots &&0&& \cdots&&0\ \vdots &&\ddots && \vdots && &&\vdots &&&&\vdots \ 1 && \cdots&& c&&\cdots &&-s&& \cdots&&0\ \vdots && && \vdots &&\ddots &&\vdots &&&&\vdots \ 1 && \cdots&& -s&&\cdots &&c&& \cdots&&0\ \vdots && && \vdots && &&\vdots && \ddots &&\vdots \ 1 && \cdots&& 0&&\cdots &&0&& \cdots&&1 \end{bmatrix} \begin{bmatrix}x_1 \ \vdots \ x_i \ \vdots \x_j \ \vdots \x_n\ \end{bmatrix} = \begin{bmatrix}x_1 \ \vdots \ \sqrt{x_i^2 +x_j ^2} \ \vdots \0 \ \vdots \x_n\ \end{bmatrix} $
设计
由于是依次消元,所以R是一个上三角矩阵
Householder 变换矩阵
几何意义为基本反射变换,单位向量 v 是反射超平面的法向量,并且是对称矩阵,正交矩阵
设 x 为一n维非零实向量,$\sigma = ||x||$
法向量
设计的特殊的Householder矩阵将向量x反射到向量空间的第1个轴上,其他维度都是0。
$Hx=x-(x\pm \sigma e_1) = \mp\sigma e_1 =\mp ||x||e_1 = \begin{bmatrix} \mp ||x|| \0\ \vdots \0 \end{bmatrix} $
$H_1 A=H_1[a_1,a_2,\cdots ,a_n]=[H_1 a_1,H_1 a_2,\cdots ,H_1 a_n]= \begin{bmatrix} r_{11} && r_{12 }&& \cdots &&r_{1n} \ 0&& * && \cdots &&* \\vdots &&\vdots &&\ddots &&\vdots \0&&* &&\cdots &&* \end{bmatrix}$
右下角为A11的余子式
$H_2 H_1 A= \begin{bmatrix} r_{11} &&r_1^T\ 0 &&\hat{H}_2 A_2 \end{bmatrix}$
如果
$H_n \cdots H_2 H_1 A = \begin{bmatrix} R\0 \end{bmatrix}$
依次消去,所以R是上三角矩阵
$A=Q\begin{bmatrix} R\0 \end{bmatrix}$
如果
n 阶实对称矩阵 A
如果 A 可分解为
如果D的主对角元素不含0,则LDU分解时唯一的,$U=L^T$
如果A是正定矩阵,则对于所有的非零实向量x
L是可逆矩阵,任何一个非零向量y都满足上述不等式,设y为标准单位向量
令
实对称正定矩阵
#include <iostream>
#include <ctime>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Cholesky>
#define MATRIX_SIZE 100
using namespace std;
int main( int argc, char** argv )
{
Eigen::MatrixXd matrix;
matrix = Eigen::MatrixXd::Random(MATRIX_SIZE,MATRIX_SIZE);
matrix = matrix.transponse()*matrix;
Eigen::VectorXd b;
b = Eigen::MatrixXd::Random( MATRIX_SIZE,1 );
clock_t time_stt = clock();
Eigen::Matrix<double,MATRIX_SIZE,1> x = matrix.inverse()*b;
cout<<b<<endl;
cout <<"time use in normal inverse is " << 1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC << "ms"<< endl;
// QR分解
time_stt = clock();
x = matrix.colPivHouseholderQr().solve(b);
cout <<"time use in Qr decomposition is " <<1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl;
// Cholesky 分解
time_stt = clock();
x = matrix.ldlt().solve(b);
cout <<"time use in Qr decomposition is " <<1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl;
return 0;
}
小萝卜一号的位姿为
小萝卜二号的位姿为
现在小萝卜一号看到某个点在自身坐标系下,坐标为
求该点在小萝卜二号下的坐标的代码
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Geometry>
using namespace std;
int main( int argc, char** argv )
{
Eigen::Quaterniond q_1(0.35,0.2,0.3,0.1);
Eigen::Quaterniond q_2(-0.5,0.4,-0.1,0.2);
q_1.normalize();
q_2.normalize();
Eigen::Vector3d p_1(0.5,0,0.2);
Eigen::Vector3d p_2;
Eigen::Vector3d t_1(0.3,0.1,0.1);
Eigen::Vector3d t_2(-0.1,0.5,0.3);
p_2 = q_2*(t_1+q_1.conjugate()*p_1 -t_2 );
cout<<p_2<<endl;
return 0;
}
$ R=cos(\theta) I -(1-cos(\theta))nn^T +sin(\theta) n^\land $
适当的正交矩阵的行列式为1,实正交矩阵的特征值的绝对值等于1,且实矩阵若存在复特征值必然为共轭对。因此,由欧拉旋转定理可推论$3\times 3$阶旋转矩阵R的特征值为$1,e^{i\theta},e^{-i \theta}$。旋转矩阵R满足$RR^\ast=R^\ast R$,此为正规矩阵,等价条件是R可么正对角化为$R=UDU^\ast$,其中U是酉矩阵,$U^\ast=U^{-1}$,且$D=\text{diag}(1,e^{i\theta},e^{ -i\theta})$。令$U=\begin{bmatrix} \mathbf{u}&\mathbf{v}&\mathbf{w} \end{bmatrix}$,其中$\mathbf{u},\mathbf{v},\mathbf{w}$组成一个单位正交集。酉对角化蕴含下列性质:
乘开$RU=UD$,$R\mathbf{u}=\mathbf{u}$表明$\mathbf{u}$是一个单位实向量,$R\mathbf{v}=e^{i\theta}\mathbf{v}$和$R\mathbf{w}=e^{-i\theta}\mathbf{w}$表明$\mathbf{v}$和$\mathbf{w}$是单位复向量。
使用这些性质以及欧拉公式$e^{\pm i\theta}=\cos\theta\pm i\sin\theta$,
$ R=\begin{bmatrix} \mathbf{u}&\mathbf{v}&\mathbf{w} \end{bmatrix}\begin{bmatrix} 1&0&0\ 0&e^{i\theta}&0\ 0&0&e^{-i\theta} \end{bmatrix} \begin{bmatrix} \mathbf{u}^\ast\ \mathbf{v}^\ast\ \mathbf{w}^\ast \end{bmatrix}$
$ =\cos\theta I+(1-\cos\theta)\mathbf{u}\mathbf{u}^T+i\sin\theta(\mathbf{v}\mathbf{v}^\ast-\mathbf{w}\mathbf{w}^\ast)=\cos\theta I+(1-\cos\theta)\mathbf{u}\mathbf{u}^T+\sin\theta K$
其中$K=i(\mathbf{v}\mathbf{v}^\ast-\mathbf{w}\mathbf{w}^\ast)$。因为R和$\mathbf{u}\mathbf{u}^T$是实矩阵,上式表明K也是一个实矩阵,则
推知K是反对称矩阵。令$K=\left[\begin{array}{rrr} 0&a&b\ -a&0&c\ -b&-c&0 \end{array}\right]$,其中$a,b,c\in\mathbb{R}$。对于任一$\theta$,$\mathbf{u}=R\mathbf{u}=\cos\theta\mathbf{u}+(1-\cos\theta)\mathbf{u}+\sin\theta K\mathbf{u}=\mathbf{u}+\sin\theta K\mathbf{u}$,可得
$\displaystyle K\mathbf{u}=\left[ \begin{array}{rrr} 0&a&b\ -a&0&c\ -b&-c&0 \end{array} \right]\begin{bmatrix} u_1\ u_2\ u_3 \end{bmatrix}=\begin{bmatrix} 0\ 0\ 0 \end{bmatrix}$
或改写为
$\displaystyle \left[ \begin{array}{rrr} u_2&u_3&0\ -u_1&0&u_3\ 0&-u_1&-u_2 \end{array}\right]\begin{bmatrix} a\ b\ c \end{bmatrix}=\begin{bmatrix} 0\ 0\ 0 \end{bmatrix}$。
解开上式,
$\displaystyle \begin{bmatrix} a\ b\ c \end{bmatrix}=\alpha\left[ \begin{array}{r} -u_3\ u_2\ -u_1 \end{array} \right]$,
其中$\alpha$是一个待确定的实数。定义「外积矩阵」
$ \mathbf{u}^\land=\left[\begin{array}{rrr} 0&-u_3&u_2\ u_3&0&-u_1\ -u_2&u_1&0 \end{array}\right]$,
即有$K=\alpha\mathbf{u}^\land
$\displaystyle\begin{aligned} K^2&=i^2(\mathbf{v}\mathbf{v}^\ast-\mathbf{w}\mathbf{w}^\ast)(\mathbf{v}\mathbf{v}^\ast-\mathbf{w}\mathbf{w}^\ast)\ &=-(\mathbf{v}\mathbf{v}^\ast\mathbf{v}\mathbf{v}^\ast-\mathbf{v}\mathbf{v}^\ast\mathbf{w}\mathbf {w}^\ast-\mathbf{w}\mathbf{w}^\ast\mathbf{v}\mathbf{v}^\ast+\mathbf{w}\mathbf{w}^\ast\mathbf{w}\mathbf{w}^\ast)\ &=-(\mathbf{v}\mathbf{v}^\ast+\mathbf{w}\mathbf{w}^\ast)\ &=\mathbf{u}\mathbf{u}^T-I\end{aligned}$
再将$K=\alpha\mathbf{u} ^\land$代入上式,立得$\alpha=\pm 1$。我们选择$\alpha=1$,原因是配合惯例使$\theta$代表逆时针转角。令$R_\mathbf{u}(\theta)$代表一个旋转矩阵,其特征值为$1,\cos\theta\pm i\sin\theta$且$\mathbf{u}$为对应特征值1的单位特征向量。综合以上结果,旋转矩阵$R_\mathbf{u}(\theta)$的公式如下:
$\displaystyle\begin{aligned} R_\mathbf{u}(\theta)&=\cos\theta I+(1-\cos\theta)\mathbf{u}\mathbf{u}^T+\sin\theta \mathbf{u} ^\land\ &=\begin{bmatrix} u_1u_1v\theta+c\theta&u_1u_2v\theta-u_3s\theta&u_1u_3v\theta +u_2s\theta\ u_1u_2v\theta+u_3s\theta&u_2u_2v\theta+c\theta&u_2u_3v\theta-u_1s\theta\ u_1u_3v\theta-u_2s\theta&u_2u_3v\theta+u_1s\theta&u_3u_3v\theta+c\theta \end{bmatrix},\end{aligned}$
若 $q=a+\mathbf{v} $ 为单位四元数,即
其中$\Vert\mathbf{u}\Vert=1$。我们可以证明:以单位向量$\mathbf{u}$为转轴,线性变换$L_q(\mathbf{x})=q\mathbf{x}q^\ast$ 代表$ \mathbf{x}$逆时针旋转$\theta$角度。将给定向量$\mathbf{x}$分解成$\mathbf{x}=\mathbf{z}+\ mathbf{n}$,其中$\mathbf{z}$是$\mathbf{x}$在转轴$\mathbf{u}$的正交投影,$\mathbf{n}$代表垂直于$\mathbf{u}$的成分。因为$L_q(\mathbf{x})=L_q(\mathbf{z}+\mathbf{n})=L_q(\mathbf{z})+L_q(\mathbf{n})$,我们要证明$L_q$不改变$\mathbf{z}$且$L_q$将$\mathbf{n}$逆时针旋转$\theta$角度,见下图。
因为存在唯一$\alpha\in\mathbb{R}$使$\mathbf{z}=\alpha\mathbf{u}$,并且由性质
$\displaystyle\begin{aligned} L_q(\mathbf{n})&=(a^2-\Vert\mathbf{v}\Vert^2)\mathbf{n}+2(\mathbf{v}\cdot\mathbf{n})\mathbf{v}+2a(\mathbf{v}\times\mathbf{n})\ &=(a^2-\Vert\mathbf{v}\Vert^2)\mathbf{n}+2a(\mathbf{v}\times\mathbf{n})=(a^2-\Vert\mathbf{v}\Vert^2)\mathbf{n}+2a\Vert\mathbf{v}\Vert(\mathbf{u}\times\mathbf{n})\ &=(a^2-\Vert\mathbf{v}\Vert^2)\mathbf{n}+2a\Vert\mathbf{v}\Vert\mathbf{n}_{\perp},\end{aligned}$
上面令$\mathbf{n}{\perp}=\mathbf{u}\times\mathbf{n}$。注意$\mathbf{n}\perp$垂直于$\mathbf{n}$且
最后,将$a=\cos\frac{\theta}{2}$和$\Vert\mathbf{v}\Vert=\sin\frac{\theta}{2}$代入$L_q(\mathbf{n})$的表达式,使用倍角公式,
$ L_q(\mathbf{n})= (\cos^2\frac{\theta}{2}-\sin^2\frac{\theta}{2})\mathbf{n}+ (2\cos\frac{\theta}{2}\sin\frac{\theta}{2} )\mathbf{n}\perp=\cos\theta\mathbf{n}+\sin\theta\mathbf{n}\perp $
证得$L_q(\mathbf{n})$即是垂直于转轴$\mathbf{u}$的平面上$\mathbf{n}$逆时针旋转$\theta$而得的向量。
将单位四元数$q=\cos\frac{\theta}{2}+\sin\frac{\theta}{2}\mathbf{u}$代入旋转变换$L_q(\mathbf{x})$的展开式,套用倍角公式和半角公式,推得
$ L_q(\mathbf{x})= (\cos^2\frac{\theta}{2}-\sin^2\frac{\theta}{2} )\mathbf{x}+2 (\sin\frac{\theta}{2}\mathbf{u}\cdot\mathbf{x} )\sin\frac{\theta}{2}\mathbf{u}+2\cos\frac{\theta}{2} (\sin\ frac{\theta}{2}\mathbf{u}\times\mathbf{x} ) $
用四元数
此时
证明:
$p' =qpq^{-1} =(c,sv)(0,x)(c,-sv)=(-svx,sv\times x+cx)*(c,-sv)$
实部为
上式亦可以写成矩阵运算:
$Q=\begin{bmatrix} 1-2y^2 -2z^2 &&2 xy+2wz && 2xz -2wy \ 2xy-2wz && 1-2x^2 -2z^2 && 2 yz +2wx \ 2 x z+ 2wy&& 2yz-2wx && 1-2x^2 -2y^2 \end{bmatrix}$
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class A {
public:
A(const int& i ) : index(i) {}
int index = 0;
};
int main() {
A a1(3), a2(5), a3(9);
vector<A> avec{a1, a2, a3};
std::sort(avec.begin(), avec.end(), [](const A&a1, const A&a2) {return a1.index<a2.index;});
for( auto& a: avec ) cout<<a.index<<" ";
cout<<endl;
return 0;
}
for( auto& a: avec )
基于范围的for循环
auto& a
类型推导
[](const A&a1, const A&a2)
lambda 表达式