当前位置:网站首页>PCL Eigen介绍及简单使用
PCL Eigen介绍及简单使用
2022-07-02 07:00:00 【AICVer】
Eigen是可以用来进行线性代数、矩阵、向量操作等运算的C++库。
相关代码操作如下:
//#include "stdafx.h"//原博客中用的是这个,但是我的电脑无法找到,于是用下面这三行替换。
#include <stdio.h>
#include "stdlib.h"
#include <tchar.h>
#include <iostream>
#include <Eigen/Dense>
//十、eigen的使用
using namespace std;
using namespace Eigen;
template <typename T>
static void matrix_mul_matrix(T* p1, int iRow1, int iCol1, T* p2, int iRow2, int iCol2, T* p3)
{
if (iRow1 != iRow2) return;
//列优先
//Eigen::Map< Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > map1(p1, iRow1, iCol1);
//Eigen::Map< Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > map2(p2, iRow2, iCol2);
//Eigen::Map< Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > map3(p3, iCol1, iCol2);
//行优先
Eigen::Map< Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > map1(p1, iRow1, iCol1);
Eigen::Map< Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > map2(p2, iRow2, iCol2);
Eigen::Map< Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > map3(p3, iCol1, iCol2);
map3 = map1 * map2;
}
int main(int argc, char* argv[])
{
//1. 矩阵的定义
cout << "1. 矩阵的定义" << endl;
Eigen::MatrixXd m(2, 2);
Eigen::Vector3d vec3d;
Eigen::Vector4d vec4d(1.0, 2.0, 3.0, 4.0);
//2. 动态矩阵、静态矩阵
cout << "2. 动态矩阵、静态矩阵" << endl;
Eigen::MatrixXd matrixXd;
Eigen::Matrix3d matrix3d;
//3. 矩阵元素的访问
cout << "3. 矩阵元素的访问" << endl;
m(0, 0) = 1;
m(0, 1) = 2;
m(1, 0) = m(0, 0) + 3;
m(1, 1) = m(0, 0) * m(0, 1);
std::cout << m << std::endl << std::endl;
//4. 设置矩阵的元素
cout << "4. 设置矩阵的元素" << endl;
m << -1.5, 2.4,
6.7, 2.0;
std::cout << m << std::endl << std::endl;
int row = 4;
int col = 5;
Eigen::MatrixXf matrixXf(row, col);
matrixXf << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20;
std::cout << matrixXf << std::endl << std::endl;
matrixXf << Eigen::MatrixXf::Identity(row, col);//Identity是单位矩阵的意思:Identity matrix
std::cout << matrixXf << std::endl << std::endl;
//5. 重置矩阵大小
cout << "5. 重置矩阵大小" << endl;
Eigen::MatrixXd matrixXd1(3, 3);
m = matrixXd1;
std::cout << m.rows() << " " << m.cols() << std::endl << std::endl;
//6. 矩阵运算
cout << "6. 矩阵运算" << endl;
m << 1, 2, 7,
3, 4, 8,
5, 6, 9;
std::cout << m << std::endl;
matrixXd1 = Eigen::Matrix3d::Random();
m += matrixXd1;
std::cout << m << std::endl << std::endl;
m *= 2;
std::cout << m << std::endl << std::endl;
std::cout << -m << std::endl << std::endl;
std::cout << m << std::endl << std::endl;
//7. 求矩阵的转置、共轭矩阵、伴随矩阵
cout << "7. 求矩阵的转置、共轭矩阵、伴随矩阵" << endl;
std::cout << m.transpose() << std::endl << std::endl;
std::cout << m.conjugate() << std::endl << std::endl;
std::cout << m.adjoint() << std::endl << std::endl;
std::cout << m << std::endl << std::endl;
m.transposeInPlace();
std::cout << m << std::endl << std::endl;
//8. 矩阵相乘、矩阵向量相乘
cout << "8. 矩阵相乘、矩阵向量相乘" << endl;
std::cout << m*m << std::endl << std::endl;
vec3d = Eigen::Vector3d(1, 2, 3);
std::cout << m * vec3d << std::endl << std::endl;
std::cout << vec3d.transpose()*m << std::endl << std::endl;
//9. 矩阵的块操作
cout << "9. 矩阵的块操作" << endl;
std::cout << m << std::endl << std::endl;
std::cout << m.block(1, 1, 2, 2) << std::endl << std::endl;
std::cout << m.block<1, 2>(0, 0) << std::endl << std::endl;
std::cout << m.col(1) << std::endl << std::endl;
std::cout << m.row(0) << std::endl << std::endl;
//10. 向量的块操作
cout << "10. 向量的块操作" << endl;
Eigen::ArrayXf arrayXf(10);
arrayXf << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;
std::cout << vec3d << std::endl << std::endl;
std::cout << arrayXf << std::endl << std::endl;
std::cout << arrayXf.head(5) << std::endl << std::endl;
std::cout << arrayXf.tail(4) * 2 << std::endl << std::endl;
//11. 求解矩阵的特征值和特征向量
cout << "11. 求解矩阵的特征值和特征向量" << endl;
Eigen::Matrix2f matrix2f;
matrix2f << 1, 2, 3, 4;
Eigen::SelfAdjointEigenSolver<Eigen::Matrix2f> eigenSolver(matrix2f);//形式就是这个形式,遇到高维的就直接用高维的替换掉matrix2f。
if (eigenSolver.info() == Eigen::Success) {
std::cout << eigenSolver.eigenvalues() << std::endl << std::endl;
std::cout << eigenSolver.eigenvectors() << std::endl << std::endl;
}
//12. 类Map及动态矩阵的使用
cout << "12. 类Map及动态矩阵的使用" << endl;
int array1[4] = {
1, 2, 3, 4 };
int array2[4] = {
5, 6, 7, 8 };
int array3[4] = {
0, 0, 0, 0 };
matrix_mul_matrix(array1, 2, 2, array2, 2, 2, array3);
for (int i = 0; i < 4; i++)
std::cout << array3[i] << std::endl;
return 0;
}
边栏推荐
- (五)APA场景搭建之挡位控制设置
- UVM factory mechanism
- 01-spooldir
- Stm32 et développement de moteurs (système supérieur)
- [unity3d] production progress bar - make image have the functions of filled and sliced at the same time
- [pit avoidance guide] pit encountered using ugui: the text component cannot indent the first line by two spaces
- 简洁、快速、节约内存的Excel处理工具EasyExcel
- 【Lua】常见知识点汇总(包含常见面试考点)
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- Feature (5): how to organize information
猜你喜欢
《MySQL 8 DBA基础教程》简介
[SUCTF2018]followme
Sum the two numbers to find the target value
两数之和,求目标值
Basic usage of mock server
axis设备的rtsp setup头中的url不能带参
Kustomize使用手册
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
MYSQL环境配置
[pit avoidance guide] pit encountered using ugui: the text component cannot indent the first line by two spaces
随机推荐
2021-10-04
转换YV12到RGB565图像转换,附YUV转RGB测试
简洁、快速、节约内存的Excel处理工具EasyExcel
pytest--之测试报告allure配置
大华设备播放过程中设置播放速度
Postman -- use
Zlib download and use
网络通信学习
Feature (5): how to organize information
(五)APA场景搭建之挡位控制设置
UVM——Callback
[Fantasy 4] introduction and use of UMG components (under update...)
Start class, data analysis, high salary training plan, elite class
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
Delivery mode design of Spartacus UI of SAP e-commerce cloud
stm32和电机开发(上位系统)
[MySQL] an exception occurs when connecting to MySQL: connection must be valid and open
MySQL -- time zone / connector / driver type
flume 190 INSTALL
【Lua】常见知识点汇总(包含常见面试考点)