当前位置:网站首页>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;
}
边栏推荐
- Sus system availability scale
- AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
- 高考的意义是什么
- (五)APA场景搭建之挡位控制设置
- 618再次霸榜的秘密何在?耐克最新财报给出答案
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- Excuse me, is it cost-effective to insure love life patron saint 2.0 increased lifelong life insurance? What are the advantages of this product?
- UVM learning - object attribute of UVM phase
- Shutter - canvas custom graph
- Considerations for Apache deploying static web page projects
猜你喜欢

KS009基于SSH实现宠物管理系统

How to get the password of cpolar?
![[pit avoidance guide] pit encountered using ugui: the text component cannot indent the first line by two spaces](/img/6f/e5a30dae824ccb22d1157818be58be.png)
[pit avoidance guide] pit encountered using ugui: the text component cannot indent the first line by two spaces

shell编程01_Shell基础

VLAN experiment

【JetBrain Rider】构建项目出现异常:未找到导入的项目“D:\VisualStudio2017\IDE\MSBuild\15.0\Bin\Roslyn\Microsoft.CSh

Kustomize使用手册

【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View

Flutter——Canvas自定义曲线图

【TS】1368- 秒懂 TypeScript 泛型工具类型!
随机推荐
Transport Optimization abstraction
Kustomize使用手册
sqoop创建job出现的一系列问题解决方法
pytest--之测试报告allure配置
"Talking about podcasts" vol.352 the age of children: breaking the inner scroll, what can we do before high school?
《实习报告》Skywalking分布式链路追踪?
高考的意义是什么
使用sqlcipher打开加密的sqlite方法
Basic usage of mock server
UVM——Callback
01 install virtual machine
Use WinDbg to statically analyze dump files (summary of practical experience)
Shapiro Wilk normal analysis by SPSS
Message mechanism -- getting to know messages and message queues for the first time
UVM - configuration mechanism
pytest学习--base
SPSS做Shapiro-Wilk正态分析
【TS】1368- 秒懂 TypeScript 泛型工具类型!
lunix重新分配root 和 home 空间内存
js promise.all