ubuntu20.04 使用vs code编写
现放cmake文件(记得链接库文件和配置C++版本)
cmake_minimum_required( VERSION 2.8 )
project(learingMatrix)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++17")
include_directories("/usr/include/eigen3")
add_executable(learningMatrix eigenMatrix.cpp)
add_executable(learnningGeometry useGeometry.cpp)
add_executable(coordinatetrans coordinateTransform.cpp)
add_executable(plot plotTrajectory.cpp)
add_executable(cube visualizeGeometry.cpp)
include_directories(${Pangolin_INCLUDE_DIRS})
find_package( Pangolin )
target_link_libraries(plot ${Pangolin_LIBRARIES})
target_link_libraries(cube ${Pangolin_LIBRARIES})
以下为eigen的简单运算部分
#include <iostream>
using namespace std;
#include <ctime>
#include <Eigen/Core> //eigen 核心
#include <Eigen/Dense> //稠密矩阵运算规则
using namespace Eigen;
#define MATRIX_SIZE 50
int main(int argc, char** argv)
{
Matrix<float,2,3> matrix_23; //Matrix模板类用三个参数生成一个矩阵 数据类型,行,列
Vector3d v_3d; //Vector3d生成一个三维行向量,d是double的意思,本质上还是Matrix一个特殊形式
Matrix<double,3,1> v1_3d; //同一个意思
Matrix3d matrix_33 = Matrix3d::Zero(); //Matrix3d生成一个3*3的方阵, Zero是0阵
Matrix<double , Dynamic,Dynamic> matrix_dynamic; //定义动态矩阵,行和列随数据变化
MatrixXd matrix_x; //同上
// ------------------------------------------------------------------------------------
matrix_23<<1,2,3,4,5,6; //输入数据(运算符重载)
cout<<"matrix 2*3 from 1 to 6 : "<<matrix_23<<endl; //输出(运算符重载)
cout<<"print matrix 2*3: "<<endl;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<matrix_23(i,j)<<"\t";
}
cout<<endl;
}
//-----------------------------------------------------------------------------------------
v_3d << 3,2,1;
v1_3d <<4,5,6;
//Matrix<double ,2,1> result_wrong_type =matrix_23*v1_3d;(不能混用不同数据类型的矩阵,eigen不会自动升级数据类型)
Matrix<double , 2, 1> result = matrix_23.cast<double>()*v_3d; //cast转换数据类型
cout<<"[1,2,3;4,5,6]*[3,2,1]="<<result.transpose()<<endl;
Matrix<double , 2,1> result2 = matrix_23.cast<double>()*v1_3d;
cout<<"[1,2,3;4,5,6]*[4,5,6]="<<result2.transpose()<<endl; //transpose转置
//Eigen::Matrix<double , 2,3 > result_wrong_dimension = matrix_23.cast<double>()*v_3d; (不同维度的矩阵不能直接赋值,eigen不会自动填充空白)
//----------------------------------------------------------------------------------------
matrix_33 = Matrix3d::Random(); //随机填充数
cout<<"random matrix:\n"<<matrix_33<<endl;
cout<<"transpose :\n"<<matrix_33.transpose()<<endl;
cout<<"sum: \n"<<matrix_33.sum()<<endl;
cout<<"trace: \n"<<matrix_33.trace()<<endl;
cout<<"10 times: \n"<<10*matrix_33<<endl;
cout<<"inverse ; \n"<<matrix_33.inverse()<<endl; //不推荐直接求逆,维度一大起来,计算量太大
cout<<"det: \n"<<matrix_33.determinant()<<