eigen 笔记2
4. Block operations
1) Using block operations
Block of size(p, q), starting at (i, j)
dynamic-size block expression: matrix.block(i, j, p, q);
fixed-size block expression: matrix.block<p, q>(i, j);
- int main()
- {
- Eigen::MatrixXf m(,);
- m << , , , ,
- , , , ,
- ,,,,
- ,,,;
- cout << "Block in the middle" << endl;
- cout << m.block<,>(,) << endl << endl;
- for (int i = ; i <= ; ++i)
- {
- cout << "Block of size " << i << "x" << i << endl;
- cout << m.block(,,i,i) << endl << endl;
- }
- }
Output:
- Block in the middle
- Block of size 1x1
- Block of size 2x2
- Block of size 3x3
.block() 的返回值可以是左值, 也可以是右值.
2) Columns and rows
matrix.row(i), matrix.col(j)
- int main()
- {
- Eigen::MatrixXf m(,);
- m << ,,,
- ,,,
- ,,;
- cout << "Here is the matrix m:" << endl << m << endl;
- cout << "2nd Row: " << m.row() << endl;
- m.col() += * m.col();
- cout << "After adding 3 times the first column into the third column, the matrix m is:\n";
- cout << m << endl;
- }
3) Corner-related operations
太复杂, 没兴趣.
4) Block operations for vectors
同上, 用时再查.
5. Advanced initialization
1) The comma initializer
- RowVectorXd vec1();
- vec1 << , , ;
- RowVectorXd vec2();
- vec2 << , , , ;
- RowVectorXd joined();
- joined << vec1, vec2;
- MatrixXf matA(, );
- matA << , , , ;
- MatrixXf matB(, );
- matB << matA, matA/, matA/, matA;
- Matrix3f m;
- m.row() << , , ;
- m.block(,,,) << , , , ;
- m.col().tail() << , ;
2) Special matrices and arrays
Zero(), Constant(value), Constant(rows, cols, value), Random(), Identity(), LinSpaced(size, low, high),
Identity() only for matrix;
LinSpaced only for vectors and one-dimensional arrays; high也包含在数组中. 默认都是列向量.
- std::cout << "A fixed-size array:\n";
- Array33f a1 = Array33f::Zero();
- std::cout << "A one-dimensional dynamic-size array:\n";
- ArrayXf a2 = ArrayXf::Zero();
- std::cout << "A two-dimensional dynamic-size array:\n";ArrayXXf a3 = ArrayXXf::Zero(, );
- ArrayXXf table(, );
- table.col() = ArrayXf::LinSpaced(, , );
- table.col() = M_PI / * table.col();
- table.col() = table.col().sin();
- table.col() = table.col().cos();
- Output:
- Degrees Radians Sine Cosine
- 0.175 0.174 0.985
- 0.349 0.342 0.94
- 0.524 0.5 0.866
- 0.698 0.643 0.766
- 0.873 0.766 0.643
- 1.05 0.866 0.5
- 1.22 0.94 0.342
- 1.4 0.985 0.174
- 1.57 -4.37e-08
类似的, 有 setZero(), MatrixBase::setIdentity(), DenseBase::setLinSpaced()
3) Usage as temporary objects
- int main()
- {
- MatrixXd m = MatrixXd::Random(,);
- m = (m + MatrixXd::Constant(,,1.2)) * ;
- cout << "m =" << endl << m << endl;
- VectorXd v();
- v << , , ;
- cout << "m * v =" << endl << m * v << endl;
- }
comma-initializer
- MatrixXf mat = MatrixXf::Random(, );
- std::cout << mat << std::endl << std::endl;
- mat = (MatrixXf(,) << , , , ).finished() * mat;
注意到, .finished() 是必须的.
6. Reductions, visitors and broadcasting
1) Reducations
sum(), prod(), mean(), minCoeff(), maxCoeff(), trace()
2) Norm computations
应该用不到.
3) Boolean reductions
all(), any(), count()
- int main()
- {
- ArrayXXf a(,);
- a << ,,
- ,;
- cout << "(a > 0).all() = " << (a > ).all() << endl;
- cout << "(a > 0).any() = " << (a > ).any() << endl;
- cout << "(a > 0).count() = " << (a > ).count() << endl;
- cout << endl;
- cout << "(a > 2).all() = " << (a > ).all() << endl;
- cout << "(a > 2).any() = " << (a > ).any() << endl;
- cout << "(a > 2).count() = " << (a > ).count() << endl;
- }
- Output:
- (a > ).all() =
- (a > ).any() =
- (a > ).count() =
- (a > ).all() =
- (a > ).any() =
- (a > ).count() =
4) Visitors
maxCoeff(&x, &y), minCoeff(&x, &y)
x, y 即为行索引和列索引, MatrixXf::Index, 注意, 不是 int
- //get location of maximum
- MatrixXf::Index maxRow, maxCol;
- float max = m.maxCoeff(&maxRow, &maxCol);
- //get location of minimum
- MatrixXf::Index minRow, minCol;
- float min = m.minCoeff(&minRow, &minCol);
5) Partial reductions
colwise(), rowwise()
- int main() {
- Eigen::MatrixXf mat(, );
- mat << , , , ,
- , , , ;
- cout << "Column's maximum: " << endl
- << mat.colwise().maxCoeff() << endl;
- cout << "Row's maximum: " << endl
- << mat.rowwise().maxCoeff() << endl;
- }
- Output:
- Column's maximum:
- Row's maximum:
combining partial reductions with other operations
- int main() {
- MatrixXf mat(, );
- mat << , , , ,
- , , , ;
- MatrixXf::Index maxIndex;
- float maxNorm = mat.colwise().sum().maxCoeff(&maxIndex);
- cout << "Maximum sum at position: " << maxIndex << endl;
- cout << "The corresponding vector is: " << endl;
- cout << mat.col(maxIndex) << endl;
- cout << "And its sum is: " << maxNorm << endl;
- }
- Output:
- Maximum sum at position
- The corresponding vector is:
- And its sum is is:
6) Broadcasting
- int main() {
- MatrixXf mat(, );
- VectorXf v();
- mat << , , , ,
- , , , ;
- v << , ;
- w << , , , ;
- mat.colwise() += v;
- cout << "Broadcasting result: " << endl;
- cout << mat << endl;
- mat.rowwise() += w.transpose();
- cout << "Broadcasting result: " << endl;
- cout << mat << endl;
- }
- Output:
- Broadcasting result:
- 1 2 6 9
- 4 2 8 3
- Broadcasting result:
7) Combining broadcasting with other operations
略.
7. Interfacing with raw buffers: the Map class
1) Map types and declaring Map variables
RowsAtCompileTime 和 ColsAtCompileTime 是生成矩阵的行数和列数
Map<MatrixXf> mf(pf, rows, columns);
pf 指向一个定义 coefficient array 的 region 的起始, 这里注意 pf 是一个指针, 指向的是一个 coefficient, rows 和 columns 是该 mf 的行和列, 默认全部.
这里有一个问题, Map 模板参数指定的 RowsAtCompileTime 和 ColsAtCompileTime 与括号中的 rows 和 columns 有什么关系?
a. Matrix
Dynamic
Map<Matrix<int, Dynamic, Dynamic> > 即 Map<MatrixXi>, 此时生成 matrix 的行和列由 rows 和 columns 指定
这里还要注意一下, pf 的属性, 如果 pf 是指向一个内存区域, 内存区域的数字怎么获取, 要看这个区域存储的是什么.
存储 array, 假设 array 长度为 15, 要转换成3行4列矩阵, 那么取前12个数
存储 matrix, 注意 matrix 默认是按列存储的, 所以会取按列排布的前12个数, 也就是前4列
fixed-size
Map<Matrix<int, 3, 4> > , 此时生成 Matrix 的行和列已定, rows 和 columns 要与 3, 4 保持一致, 或者省略否则报错.
b. Vector
Dynamic
Map<Matrix<int, 1, Dynamic> > or Map<Matrix<int, Dynamic, 1> >
Vector 有一个特别地方, rows 和 columns 可以只写一个, 比如 Map<VectorXi> m2map(p, m2.size()), 也可以全写, 但是维度要与 Map 中的一致.
fixed-size
注意维度一致.
Map<const Vector4i> mi(pi);
声明一个 fixed-size read-only vector.
Map 还有几个可选的模板参数
Map<typename MatrixType, int MapOptions, typename StrideType>
MapOptions: Aligned or Unaligned, 默认 Unaligned
StrideType: 决定 array 的排布, 使用 Stride class
- int array[];
- for(int i = ; i < ; ++i) array[i] = i;
- cout << "Column-major:\n" << Map<Matrix<int,,> >(array) << endl;
- cout << "Row-major:\n" << Map<Matrix<int,,,RowMajor> >(array) << endl;
- cout << "Row-major using stride:\n" <<
- Map<Matrix<int,,>, Unaligned, Stride<,> >(array) << endl;
- Output:
- Column-major:
- Row-major:
- Row-major using stride:
2) Using Map variables
注意: a Map type is not identical to its Dense equivalent.
- typedef Matrix<float,,Dynamic> MatrixType;
- typedef Map<MatrixType> MapType;
- typedef Map<const MatrixType> MapTypeConst; // a read-only map
- const int n_dims = ;
- MatrixType m1(n_dims), m2(n_dims);
- m1.setRandom();
- m2.setRandom();
- float *p = &m2(); // get the address storing the data for m2
- MapType m2map(p,m2.size()); // m2map shares data with m2
- MapTypeConst m2mapconst(p,m2.size()); // a read-only accessor for m2
- cout << "m1: " << m1 << endl;
- cout << "m2: " << m2 << endl;
- cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl;
- cout << "Squared euclidean distance, using map: " <<
- (m1-m2map).squaredNorm() << endl;
- m2map() = ; // this will change m2, since they share the same array
- cout << "Updated m2: " << m2 << endl;
- cout << "m2 coefficient 2, constant accessor: " << m2mapconst() << endl;
- /* m2mapconst(2) = 5; */ // this yields a compile-time error
- Output:
- m1: 0.68 -0.211 0.566 0.597 0.823
- m2: -0.605 -0.33 0.536 -0.444 0.108
- Squared euclidean distance: 3.26
- Squared euclidean distance, using map: 3.26
- Updated m2: -0.605 -0.33 0.536 0.108
- m2 coefficient , constant accessor: 0.536
这里注意一下, m2map, m2mapconst 与 m2 共享内存空间, 所以对 m2map 的修改, 会同步到 m2 和 m2mapconst.
但是, 如果如果 *p 指向的是 array 中的地址, 那么 map 转化出的 matrix 不会与 array 共享内存空间.
3) Changing the mapped array
用 new 可以对声明后的 Map object 进行修改
- int data[] = {,,,,,,,,};
- Map<RowVectorXi> v(data,);
- cout << "The mapped vector v is: " << v << "\n";
- new (&v) Map<RowVectorXi>(data+,);
- cout << "Now v is: " << v << "\n";
- Output:
- The mapped vector v is:
- Now v is:
所以也可以声明未知 array 的 map object
- Map<Matrix3f> A(NULL); // don't try to use this matrix yet!
- VectorXf b(n_matrices);
- for (int i = ; i < n_matrices; i++)
- {
- new (&A) Map<Matrix3f>(get_matrix_pointer(i));
- b(i) = A.trace();
- }
8. Reshape and Slicing
1) Reshape
- MatrixXf M1(,); // Column-major storage
- M1 << , , ,
- , , ,
- , , ;
- Map<RowVectorXf> v1(M1.data(), M1.size());
- cout << "v1:" << endl << v1 << endl;
- Matrix<float,Dynamic,Dynamic,RowMajor> M2(M1);
- Map<RowVectorXf> v2(M2.data(), M2.size());
- cout << "v2:" << endl << v2 << endl;
- Output:
- v1:
- v2:
- MatrixXf M1(,); // Column-major storage
- M1 << , , , , , ,
- , , , , , ;
- Map<MatrixXf> M2(M1.data(), ,);
- cout << "M2:" << endl << M2 << endl;
- Output:
- M2:
注意这里, 存储方式的不同, 会导致输出不同.
2) Slicing
- RowVectorXf v = RowVectorXf::LinSpaced(,,);
- cout << "Input:" << endl << v << endl;
- Map<RowVectorXf,,InnerStride<> > v2(v.data(), v.size()/);
- cout << "Even:" << v2 << endl;
- Output:
- Input:
- Even:
- MatrixXf M1 = MatrixXf::Random(,);
- cout << "Column major input:" << endl << M1 << "\n";
- Map<MatrixXf,,OuterStride<> > M2(M1.data(), M1.rows(), (M1.cols()+)/, OuterStride<>(M1.outerStride()*));
- cout << "1 column over 3:" << endl << M2 << "\n";
- typedef Matrix<float,Dynamic,Dynamic,RowMajor> RowMajorMatrixXf;
- RowMajorMatrixXf M3(M1);
- cout << "Row major input:" << endl << M3 << "\n";
- Map<RowMajorMatrixXf,,Stride<Dynamic,> > M4(M3.data(), M3.rows(), (M3.cols()+)/,
- Stride<Dynamic,>(M3.outerStride(),));
- cout << "1 column over 3:" << endl << M4 << "\n";
- Output:
- Column major input:
- 0.68 0.597 -0.33 0.108 -0.27 0.832 -0.717 -0.514
- -0.211 0.823 0.536 -0.0452 0.0268 0.271 0.214 -0.726
- 0.566 -0.605 -0.444 0.258 0.904 0.435 -0.967 0.608
- column over :
- 0.68 0.108 -0.717
- -0.211 -0.0452 0.214
- 0.566 0.258 -0.967
- Row major input:
- 0.68 0.597 -0.33 0.108 -0.27 0.832 -0.717 -0.514
- -0.211 0.823 0.536 -0.0452 0.0268 0.271 0.214 -0.726
- 0.566 -0.605 -0.444 0.258 0.904 0.435 -0.967 0.608
- column over :
- 0.68 0.108 -0.717
- -0.211 -0.0452 0.214
- 0.566 0.258 -0.967
注意 M1.data(), 返回一个指针.
9. Aliasing
m = m.transpose() 会造成 aliasing
a) m = m.transpose().eval();
b) m = m.transposeInPlace();
component-wise operations is safe.
mat = 2 * mat;
mat = mat - MatrixXf::Identity(2, 2);
matB = matA * matA; // Simple bu not quite as efficient
matB.noalias() = matA * matA; // Use this
10. Storage orders
默认 column-major, 也可以选择 row-major
11. Quick reference guide
Eigen/LU Inverse, determinant, LU decompositions with solver (FullPivLU, PartialPivLU)
Eigen/SVD SVD decompositions with least-squares solver (JacobiSVD, BDCSVD)
Eigen/QR decomposition with solver (HouseholderQR, ColPivHouseholderQR, FullPivHouseholderQR)
...
Finished.
eigen 笔记2的更多相关文章
- eigen 笔记1
c++ 的 eigen 类似于 python 的 numpy, 还有一个类似的库是 Armadillo, 当然还有 opencv. Armadillo 与 matlab 在函数名称上更接近, 但是 T ...
- Eigen学习笔记2-Matrix类
在Eigen中,所有的矩阵Matrix和向量Vector都是由Matrix类构造的.向量只不过是矩阵的特殊形式,只有一列(列向量)或者一行. Matrix模板类有6个参数,其中前三个参数是必须的.前三 ...
- Eigen学习笔记2:C++矩阵运算库Eigen介绍
Eigen常规矩阵定义 1.使用 Eigen的使用在官网上有详细的介绍,这里对我学习过程中用到的基本操作进行介绍.首先是矩阵的定义.在矩阵类的模板参数共有6个.一般情况下我们只需要关注前三个参数即可. ...
- Eigen学习笔记1:在VS2015下Eigen(矩阵变换)的配置
一.Eigen简介 Eigen是一个高层次的C ++库,有效支持线性代数,矩阵和矢量运算,数值分析及其相关的算法. Eigen适用范围广,支持包括固定大小.任意大小的所有矩阵操作,甚至是稀疏矩阵:支持 ...
- 从零开始编写深度学习库(五)Eigen Tensor学习笔记2.0
1.extract_image_patches函数的使用: 假设Eigen::Tensor形状为(3,8,8,9),现在要对第二维.第三维根据size大小为(2,2),stride=(2,2),那么如 ...
- Eigen库笔记整理(二)
Eigen/Geometry 模块提供了各种旋转和平移的表示 旋转矩阵直接使用 Matrix3d 或 Matrix3f Eigen::Matrix3d rotation_matrix = Eigen: ...
- Eigen库笔记整理(一)
首先熟悉Eigen库的用途,自行百度. 引入头文件: // Eigen 部分 #include <Eigen/Core> // 稠密矩阵的代数运算(逆,特征值等) #include < ...
- Eigen 矩阵库学习笔记
最近为了在C++中使用矩阵运算,简单学习了一下Eigen矩阵库.Eigen比Armadillo相对底层一点,但是只需要添加头文库即可使用,不使用额外的编译和安装过程. 基本定义 Matrix3f是3* ...
- Eigen 学习笔记
1. 初始化 //外部指针初始化 ]={...}; ] = ...; kernels[].mu = Vector3d(_mu0); kernels[].sigma_inv = Matrix3d(_s ...
随机推荐
- 斐讯K2刷不死breed与第三方固件教程
本文主要就是简单的斐讯 K2 刷机教程,方便大家了解一下 K2 怎样刷固件.斐讯 K2 是一款 1200M AC 双频无线路由器,支持 5G 和 2.4G WiFi 信号,虽然缺少 USB 且只有百兆 ...
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
- 从Java代码到字节码(1)
理解Java代码是如何被编译为字节码并在Java虚拟机(JVM)上执行是非常重要的,这将帮助理解你的程序是如何执行的.这样的理解不仅仅能够让你在逻辑上更好的掌握语言特性,而且能够有机会理解在做出重要决 ...
- [工具] Snipaste
https://zh.snipaste.com/ Snipaste 是一个简单但强大的截图工具,也可以让你将截图贴回到屏幕上! 下载并打开 Snipaste,按下 F1 来开始截图, 选择“复制到剪贴 ...
- [HTML5]移动平台的HTML5开发框架
jQuery Mobile http://jquerymobile.com/ jQTouch http://jqtouch.com/ DHTMLX Touch http://dhtmlx.com/to ...
- Office2007 每次打开斗需要检查 【配置进度】
打开C:\Program Files\Common Files\microsoft shared\OFFICE12\Office Setup Controller目录下将SETUP.EXE 重命名为 ...
- Linux 常用命令标记
1.linux 服务器之间拷贝文件 scp 本地用户名@IP地址:文件名1 远程用户名@IP地址:文件名2 该命令可以变型为目的服务器204上输入如下命令:红色部分是需要接受目的地,-r是递归复制该文 ...
- 为什么用VUE,而不用Jquery了?
在没有任何前端框架之前,我们写代码,只能用原生的JS,进行数据的处理,DOM的操作,譬如对一个id 为txtName 的文本框进行赋值,我们是这样的 document.getElementById(' ...
- iOS钱包卡券开发(往钱包里面加自己的卡券)
参考文章 https://blog.csdn.net/sz_vcp2007/article/details/60762349 https://blog.csdn.net/eqera/article/d ...
- python3学习笔记(3)_dict-set
# !/usr/bin/env python3 # -*- coding:utf8 -*- #dict 和 set #dict dictionary 用于存放 键值对的, 无序,key 不可变 #姓名 ...