本文主要是介绍eigen在c++的基本使用,矩阵运算,几何运算,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
矩阵运算
CMakeLists.txt文件
cmake_minimum_required(VERSION 3.2)
project(useEigen)set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-O3")include_directories("/usr/include/eigen3")
add_executable(eigenMatrix eigenMatrix.cpp)
eigenMatrix.cpp文件
#include <iostream>using namespace std;#include <ctime>
#include <eigen3/Eigen/Core> // Eigen 核心部分
#include <eigen3/Eigen/Dense> // 稠密矩阵的代数运算(逆,特征值等)using namespace Eigen;#define MATRIX_SIZE 10int main(int argc, char **argv) {Matrix<float, 2, 3> matrixf_23;matrixf_23 << 1, 2, 3, 4, 5, 6;cout << "matrix 2x3 from 1 to 6: \n" << matrixf_23 << endl;Vector3d v_3d; // //本质是Matrix<double,3,1>v_3d << 3, 2, 1;Matrix3d matrix_33 = Matrix3d::Zero(); //本质是Matrix<double,3,3>,这里初始化为零Matrix<double, Dynamic, Dynamic> matrix_x; // 动态矩阵,不确定矩阵大小,等效于MatrixXd matrix_x;// Eigen里你不能混合两种不同类型的矩阵,像这样是错的// Matrix<double, 2, 1> result_wrong_type = matrixf_23 * v_3d;// 应该显式转换Matrix<double, 2, 1> result = matrixf_23.cast<double>() * v_3d;cout << "[1,2,3;4,5,6]*[3,2,1]=" << result.transpose() << endl;// 一些矩阵运算,四则运算直接用+-*/即可matrix_33 = Matrix3d::Random(); // 随机数矩阵cout << "random matrix: \n" << matrix_33 << endl;cout << "transpose: \n" << matrix_33.transpose() << endl; // 转置cout << "sum: " << matrix_33.sum() << endl; // 元素和cout << "trace: " << matrix_33.trace() << endl; // 迹cout << "times 10: \n" << 10 * matrix_33 << endl; // 数乘cout << "inverse: \n" << matrix_33.inverse() << endl; // 逆cout << "det: " << matrix_33.determinant() << endl; // 行列式// 特征值与特征向量, 这里使用AD分解,实对称矩阵可以保证对角化成功SelfAdjointEigenSolver<Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33);cout << "Eigen values = \n" << eigen_solver.eigenvalues() << endl;cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl;// 解方程,求解 matrix_NN * x = v_Nd 这个方程Matrix<double, MATRIX_SIZE, MATRIX_SIZE> matrix_NN = MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE);matrix_NN = matrix_NN * matrix_NN.transpose(); // 保证半正定Matrix<double, MATRIX_SIZE, 1> v_Nd = MatrixXd::Random(MATRIX_SIZE, 1);clock_t time_stt = clock(); // 计时// 直接求逆自然是最直接的,但是求逆运算量大Matrix<double, MATRIX_SIZE, 1> x = matrix_NN.inverse() * v_Nd;cout << "time of normal inverse is " << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;cout << "x = " << x.transpose() << endl;// 通常用矩阵分解来求,如QR分解,速度会快很多time_stt = clock();x = matrix_NN.colPivHouseholderQr().solve(v_Nd);cout << "time of Qr decomposition is " << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;cout << "x = " << x.transpose() << endl;// 对于正定矩阵,可以用cholesky分解来解方程time_stt = clock();x = matrix_NN.ldlt().solve(v_Nd);cout << "time of ldlt decomposition is " << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;cout << "x = " << x.transpose() << endl;return 0;
}
几何运算
CMakeLists.txt文件
cmake_minimum_required(VERSION 3.2)
project(useEigen)set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-O3")include_directories("/usr/include/eigen3")
add_executable(eigenGeometry eigenGeometry.cpp)
eigenGeometry.cpp文件
#include <iostream>
#include <cmath>using namespace std;#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/Geometry>using namespace Eigen;// Eigen几何模块的使用方法,Geometry模块提供了各种旋转和平移的表示int main(int argc, char **argv) {cout.precision(3); // 设置了cout流的输出精度,使其仅保留小数点后三位Vector3d v(1, 0, 0);// 3D旋转矩阵,直接使用 Matrix3d 或 Matrix3fMatrix3d rotation_matrix = Matrix3d::Identity();// 旋转向量,使用AngleAxis, 它底层不直接是Matrix,但运算可以当作矩阵(因为重载了运算符)AngleAxisd rotation_vector(M_PI/4,Vector3d(0,0,1)); // 沿Z轴旋转 45 度cout << "rotation matrix =\n" << rotation_vector.matrix() << endl; // 将旋转向量转换成旋转矩阵rotation_matrix = rotation_vector.toRotationMatrix(); // 也可以直接赋值Vector3d v_rotated = rotation_vector * v; // 旋转向量执行坐标变换cout << "(1,0,0) after rotation (by angle axis) = " << v_rotated.transpose() << endl;v_rotated = rotation_matrix * v; // 旋转矩阵执行坐标变换cout << "(1,0,0) after rotation (by matrix) = " << v_rotated.transpose() << endl;// 欧拉角: 可以将旋转矩阵直接转换成欧拉角Vector3d euler_angles = rotation_matrix.eulerAngles(2, 1, 0); // ZYX顺序,即yaw-pitch-roll顺序cout << "yaw pitch roll = " << euler_angles.transpose() << endl;// 欧氏变换矩阵使用IsometryIsometry3d T = Isometry3d::Identity(); // 虽然称为3d,实质上是4*4的矩阵T.rotate(rotation_vector); // 设置旋转T.pretranslate(Vector3d(1, 3, 4)); // 设置平移cout << "Transform matrix = \n" << T.matrix() << endl;Vector3d v_transformed = T * v; // 变换矩阵进行坐标变换,相当于R*v+tcout << "v tranformed = " << v_transformed.transpose() << endl;// 对于仿射和射影变换,使用Affine3d和Projective3d即可,略// 四元数QuaterniondQuaterniond q = Quaterniond(rotation_vector); // 可以直接把旋转向量赋值给四元数,反之亦然cout << "quaternion from rotation vector = " << q.coeffs().transpose() << endl; // coeffs的顺序是(v,s)q = Quaterniond(rotation_matrix); // 也可以把旋转矩阵赋给四元数cout << "quaternion from rotation matrix = " << q.coeffs().transpose() << endl;v_rotated = q*v; // 使用四元数旋转一个向量,使用重载的乘法即可cout << "(1,0,0) after rotation = " << v_rotated.transpose() << endl;cout << "should be equal to " << (q*Quaterniond(0,1,0,0)*q.inverse()).coeffs().transpose() << endl;return 0;
}
这篇关于eigen在c++的基本使用,矩阵运算,几何运算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!