没有合适的资源?快使用搜索试试~ 我知道了~
C++调用Eigen矩阵运算库技巧的直观理解
需积分: 43 4 下载量 201 浏览量
2022-08-20
03:26:34
上传
评论
收藏 864KB PDF 举报
温馨提示
试读
15页
Eigen是一个非常实用的C++矩阵运算库,其只需要包含头文件,而且功能强大、矩阵运算效率高,C++配上Eigen之后,使用起来的方便程度可以和matlab媲美。这个文档给出了很多使用Eigen的实用技巧。
资源详情
资源评论
资源推荐
1
Eigen 主页
http://eigen.tuxfamily.org/index.php?title=Main_Page
Eigen 在线说明文档
http://eigen.tuxfamily.org/dox/
IDE(集成开发环境)设置
将 Eigen 程序文件夹添加到包含文件路径中
调用 Intel 的 MKL 数学库 (Eigen 和 MKL 配合得很好)
安装好 MKL 后,在任何 Eigen 声明之前加上#define EIGEN_USE_MKL_ALL
需包含头文件
#include <Eigen/Dense>
包含命名空间
using namespace Eigen;
Matrix 类的完整参数模板
下标类型
与 C++相同,从 0 开始.
默认矩阵元素排列
列主元
Eigen 矩阵写入错误原因
用写入地址作为读取地址且写入地址因矩阵信息改变被清空,如
VectorXd x(6,1);
x<<2,8,-1,3,-2,7;
x=x.head(5);
块读取和写入的地址冲突,如
VectorXd x(5);
x << 1, 2, 3, 4, 5;
2
x.tail(4) = x.head(4);
动态矩阵和静态矩阵区别
动态矩阵是指其大小在运行时确定,静态矩阵是指其大小在编译时确定。
Eigen 不使用指针清空内存(Eigen 矩阵改变尺寸前会先调用析构函数)
u1.resize(0,0);
u1 = Matrix<MatrixXd, -1, 1>(1);
初始化
MatrixXd A(int m, int n);
MatrixXd A; A=MatrixXd(m,n);
MatrixXcd A(int m, int n);
MatrixXf A(int m, int n);
MatrixXi A(int m, int n)
VectorXd vec(int m);
VectorXcd vec(int m);
VectorXf vec(int m);
VectorXi vec(int m);
VectorXd、RowVectorXd 与 MatrixXd 的关系
VectorXd、RowVectorXd 和 MatrixXd 本质都是 Matrix 类的子类;
VectorXd 等价于 Matrix<double,Dynamic,1>或 Matrix<double,-1,1>;
RowVectorXd 等价于 Matrix<double,1,Dynamic>或 Matrix<double,1,-1>;
MatrixXd 等价于 Matrix<double,Dynamic Dynamic>或 Matrix<double,-1,-1>;
存在 VectorXd 到 MatrixXd 的隐式转换,但不存在 VectorXd 到 Matrix<T,-1,-1>
的隐式转换;
当 MatrixXd 的列数为 1 时,存在 MatrixXd 到 VectorXd 的隐式转换
Eigen 向量调用专为 Matrix<T, -1, -1>所写函数
将向量强制转换为矩阵后调用:VectorXd y = round((MatrixXd)x)
行向量和列向量之间隐式转换
RowVectorXd x(3);
x << 1, 2, 3;
VectorXd y = x;
实数矩阵快速转换为复数矩阵
MatrixXd Ar(2, 2);
Ar << 1, 2,
3, 4;
MatrixXcd A=MatrixXcd::Zero(2, 2);
A.real() = Ar;
程序中快速赋值(必须注意的是,使用这种方法需要先初始化矩阵)
3
MatrixXd A(3,3);
A << 1, 2, 3, // Initialize A. The elements can also be
4, 5, 6, // matrices, which are stacked along cols
7, 8, 9; // and then the rows are stacked.
VectorXd a(3);
a<<1,2,3;
MatrixXd A = MatrixXd::Zero(4, 4);
A.row(0) << 1, 2, 3, 4;
cout << A << endl;
快速生成常用矩阵和向量
MatrixXd A=MatrixXd::Zero(m, n); //matlab 中的 A=zeros(m, n)
MatrixXd A; A.setZero(m, n);
MatrixXd A(m,n);A.setZero();
MatrixXd A=MatrixXd::Ones(m, n); //matlab 中的 A=ones(m, n)
MatrixXd A; A.setOnes(m, n);
MatrixXd A(m,n);A.setOnes();
MatrixXd A=MatrixXd::Identity(m, n); //matlab 中的 A=eye(m, n)
MatrixXd A; A.setIdentity(m, n);
MatrixXd A; A=C*MatrixXd::Ones(m, n); //matlab 中的 A=C*ones(m,n)
MatrixXd A; A=MatrixXd::Constant(m, n, C);
MatrixXd A(m, n); A.fill(C); A.setConstant(C);
VectorXd a_vec=VectorXd::Constant(m, C); //matlab 中的 a_vec=C*ones(m,1)
VecotrXcd a_vec=VectorXcd::LinSpaced(n, x1, x2); //matlab 中的 a_vec=linspace(x1,
x2, n).’;
VectorXd a_vec; a_vec.setLinSpaced(n, x1, x2);
RowVectorXd r=RowVectorXd::Constant(10,5); //matlab 中的 r=5*ones(1,10)
生成字符向量
Matrix<char, Dynamic, Dynamic> str(1,3);
str << 'a', 'b', 'c';
cout << str << endl;
转置、共轭和共轭转置
MatrixXd B=A.transpose(); //matlab 中的 B=A.’
MatrixXcd B=A.conjugate(); //matlab 中的 B=conj(A)
MatrixXd B=A.adjoint(); //matlab 中的 B=A’
A.transposeInPlace(); //matlab 中的 A=A.’
A.adjointInPlace(); //matlab 中的 A=A’
翻转
B=A.rowwise().reverse(); //matlab 中的 B=fliplr(A)
B=A.colwise().reverse(); //matlab 中的 B=flipud(A)
B=A.reverse(); //matlab 中的 B=flipud(fliplr(A))
剩余14页未读,继续阅读
斗迷飞鸟
- 粉丝: 241
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 计科11班未参与实习认知名单.xlsx
- Java 使用回溯法解决01背包问题(含算法原理和代码)
- 毕业答辩模板2016超值实用黑板风毕业论文答辩模版
- 大学生创新创业的心得.doc
- MMDF3N06HDR2G-VB一款SOP8封装2个N-Channel场效应MOS管
- 毕业答辩模板2016超值实用黑板风毕业论文答辩模版(赠手绘图表)
- 毕业答辩模板(中文版式)黑白风毕业论文答辩通用PPT模板B-14
- MMDF3N04HDR2G-VB一款SOP8封装2个N-Channel场效应MOS管
- MMDF3301-VB一款SOP8封装2个P-Channel场效应MOS管
- 毕业答辩模板(医药专业)红色医药论文答辩模板
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0