/*求解 A * x = B 这个方程*/
#include
#include
#include
#include
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
#include
#include
#include
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 )
如上图, V r o t V_{rot} Vrot是由 V V V通过旋转得到,旋转轴是K,旋转的角度为 θ \theta θ,用旋转矩阵R表示该旋转,即为: V r o t = R ⋅ V V_{rot} = R \cdot V Vrot=R⋅V, 且 K K K为单位向量, ∣ ∣ K ⃗ ∣ ∣ = 1 ||\vec{K} || =1 ∣∣K∣∣=1,
V = V ⊥ + V ∥ V=V_\perp + V_\parallel V=V⊥+V∥,
V ⊥ = V − V ∥ = − K × ( K × V ) V_\perp = V-V_\parallel = -K \times (K \times V) V⊥=V−V∥=−K×(K×V),
V ∥ = ( K ⋅ V ) ⋅ K = K K T V V_\parallel = (K \cdot V) \cdot K = K K^T V V∥=(K⋅V)⋅K=KKTV,
则垂直分量和平行分量各自的旋转分量为:
V ∥ r o t = V ∥ V_{\parallel rot} = V_\parallel V∥rot=V∥
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⊥rot=cosθV⊥+sinθK×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} &= V_{\parallel rot} + V_{\perp rot} \\&= V_\parallel + cos\theta V_\perp +sin\theta K\times V \\&=V_\parallel + cos\theta(V-V_\parallel) + sin\theta K \times V \\&= cos\theta V + (1-cos\theta)V_\parallel +sin\theta K \times V \\&= cos\theta V+ (1-cos\theta) K K^T V + sin\theta K^\times V \\&=R \cdot V \end{aligned} ∴Vrot=V∥rot+V⊥rot=V∥+cosθV⊥+sinθK×V=V∥+cosθ(V−V∥)+sinθK×V=cosθV+(1−cosθ)V∥+sinθK×V=cosθV+(1−cosθ)KKTV+sinθK×V=R⋅V
∴ 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=cosθI+(1−cosθ)KKT+sinθK×
∴ 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 ∴R=cosθI+(1−cosθ)nnT+sinθn×
证明:
(1) p ′ p^{'} p′的实部为0
假定点p的坐标为 p ( x , y , z ) 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 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=[cos2θ,nsin2θ]=cos2θ+nxsin2θi+nysin2θj+nzsin2θ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∗=[cos2θ,−nsin2θ],q−1=q∗/∣∣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 q−1=[cos2θ,−nsin2θ]/∣∣q∣∣2=(cos2θ−nxsin2θi−nysin2θj−nzsin2θ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^{'} &= qpq^{-1} \\&=[cos\frac{\theta}{2} ,nsin\frac{\theta}{2}] [0,x,y,z] [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)(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} ∴p′=qpq−1=[cos2θ,nsin2θ][0,x,y,z][cos2θ,−nsin2θ]/∣∣q∣∣2=(cos2θ+nxsin2θi+nysin2θj+nzsin2θk)(xi+yj+zk)((cos2θ−nxsin2θi−nysin2θj−nzsin2θk)/∣∣q∣∣2)
将所有的实数取出,且忽略 ∣ ∣ q ∣ ∣ 2 ||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 xnxcos2θsin2θ+ynycos2θsin2θ+znzcos2θsin2θ+(−1)xnxsin2θxcos2θ+ynxnzsin2θsin2θ+(−1)znxnysin2θsin2θ+(−1)ynysin2θcos2θ+(−1)xnynzsin2θsin2θ+znxnysin2θsin2θ+xnynzsin2θsin2θ+(−1)ynxnzsin2θsin2θ+(−1)znzsin2θcos2θ=0
所以实部为0,即 p ′ p^{'} p′的实部为0。
(2) p ′ = Q p p^{'} = Qp p′=Qp,写出矩阵 Q Q Q
p ′ = q p q − 1 = q + p + q − 1 = q + q − 1 ⊕ p \begin{aligned} p^{'} &= qpq^{-1} \\ &= q^+ p^+q^{-1} \\&= q^+ q^{-1^\oplus}p \end{aligned} p′=qpq−1=q+p+q−1=q+q−1⊕p
∴ Q = q + q − 1 ⊕ \therefore Q= q^+ q^{-1^\oplus} ∴Q=q+q−1⊕
假定四元数 q = [ x , y , z , w ] q=[x,y,z,w] q=[x,y,z,w],其中 w w w是实部,那么 q − 1 = [ − x , − y , − z , w ] / ∣ ∣ q ∣ ∣ 2 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 & \varepsilon \\ -\varepsilon^T & \eta \end{bmatrix}=\begin{bmatrix} w & -z & y & x\\z & w & -x & y\\-y & x & w & z \\ -x & -y & -z & w\end{bmatrix} q+=[η1+ε×−εTεη]=⎣⎢⎢⎡wz−y−x−zwx−yy−xw−zxyzw⎦⎥⎥⎤,
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 & -\varepsilon \\ -(-\varepsilon)^T & \eta \end{bmatrix}/||q||^2 = \frac{1}{||q||^2 }\cdot \begin{bmatrix} \eta1 + \varepsilon ^\times & -\varepsilon \\ \varepsilon^T & \eta \end{bmatrix}=\frac{1}{||q||^2 }\cdot \begin{bmatrix} w & -z & y & -x\\z & w & -x & -y\\-y & x & w & -z \\ x & y & z & w\end{bmatrix} q−1⊕=[η1−(−ε)×−(−ε)T−εη]/∣∣q∣∣2=∣∣q∣∣21⋅[η1+ε×εT−εη]=∣∣q∣∣21⋅⎣⎢⎢⎡wz−yx−zwxyy−xwz−x−y−zw⎦⎥⎥⎤
∴ 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 &= q^+ q^{-1^\oplus} \\&= \frac{1}{||q||^2 }\cdot\begin{bmatrix} w & -z & y & x\\z & w & -x & y\\-y & x & w & z \\ -x & -y & -z & w\end{bmatrix}\begin{bmatrix} w & -z & y & -x\\z & w & -x & -y\\-y & x & w & -z \\ x & y & z & w\end{bmatrix} \\&= \frac{1}{||q||^2 }\cdot \begin{bmatrix}w^2+x^2-y^2+z^2& 2xy-2zw & 2wy+2xz & 0\\ 2xy+2zw& w^2-x^2+y^2-z^2 & 2yz-2xw & 0\\2xz-2yw & 2yz+2xw & w^2-x^2-y^2+z^2 & 0 \\ 0 & 0 & 0 & w^2+x^2+y^2+z^2\end{bmatrix} \end{aligned} ∴Q=q+q−1⊕=∣∣q∣∣21⋅⎣⎢⎢⎡wz−y−x−zwx−yy−xw−zxyzw⎦⎥⎥⎤⎣⎢⎢⎡wz−yx−zwxyy−xwz−x−y−zw⎦⎥⎥⎤=∣∣q∣∣21⋅⎣⎢⎢⎡w2+x2−y2+z22xy+2zw2xz−2yw02xy−2zww2−x2+y2−z22yz+2xw02wy+2xz2yz−2xww2−x2−y2+z20000w2+x2+y2+z2⎦⎥⎥⎤