方特网站是谁做的,wordpress lovevideo,找客户app,百度官网app下载文章目录 eigen矩阵初始化多维矩阵矩阵和向量size固定大小or 动态大小Matrix类六个模板参数初始化向量元素类型 参考文献 eigen
矩阵初始化
多维矩阵
数组
MatrixXi a { // construct a 2x2 matrix{1, 2}, // first row{3, 4} // second row
};
Matrixdo… 文章目录 eigen矩阵初始化多维矩阵矩阵和向量size固定大小or 动态大小Matrix类六个模板参数初始化向量元素类型 参考文献 eigen
矩阵初始化
多维矩阵
数组
MatrixXi a { // construct a 2x2 matrix{1, 2}, // first row{3, 4} // second row
};
Matrixdouble, 2, 3 b {{2, 3, 4},{5, 6, 7},
};#include iostream
#include f:/learn/eigen-3.4.0/Eigen/Denseusing Eigen::Matrix;
int main()
{Matrixint,3,2 a {{1, 2}, {3, 4}, {5, 6}};std::cout a std::endl;
}
1 2
3 4
5 6Process returned 0 (0x0) execution time : 0.117 s
Press any key to continue.
重定向操作符
Matrix3f m;
m 1, 2, 3,4, 5, 6,7, 8, 9;
std::cout m;
1 2 3
4 5 6
7 8 9矩阵和向量size
rows矩阵行cols矩阵列resize重新规划size
#include iostream
#include e:/eigen/Eigen/Dense
using namespace std;
using namespace Eigen;
int main()
{MatrixXf m(2,5);cout The matrix m is of size m.rows() x m.cols() std::endl;m.resize(4,3);cout The matrix m resized is of size m.rows() x m.cols() std::endl;cout It has m.size() coefficients std::endl;VectorXf v(2);cout The vector v is of size v.size() std::endl;v.resize(5);cout The vector v resized is of size v.size() std::endl;cout As a matrix, v is of size v.rows() x v.cols() std::endl;
}
The matrix m is of size 2x5
The matrix m resized is of size 4x3
It has 12 coefficients
The vector v is of size 2
The vector v resized is of size 5
As a matrix, v is of size 5x1Process returned 0 (0x0) execution time : 0.317 s
Press any key to continue.固定大小or 动态大小
什么时候应该使用固定大小(例如Matrix4f)什么时候应该使用动态大小(例如MatrixXf)?
简单的答案是:对于非常小的尺寸尽可能使用固定大小对于较大的尺寸或必须使用动态大小。对于较小的大小特别是小于(大约)16的大小使用固定大小对性能非常有益因为它允许Eigen避免动态内存分配并展开循环。当然使用固定大小的限制是只有在编译时知道大小时才有可能。此外对于足够大的大小例如大于(大约)32的大小使用固定大小的性能优势变得可以忽略不计。更糟糕的是尝试在函数内部使用固定大小创建一个非常大的矩阵可能会导致堆栈溢出因为Eigen会尝试将数组自动分配为局部变量而这通常是在堆栈上完成的。
Matrix类六个模板参数
模板参数
Matrixtypename Scalar,int RowsAtCompileTime,int ColsAtCompileTime,int Options 0,int MaxRowsAtCompileTime RowsAtCompileTime,int MaxColsAtCompileTime ColsAtCompileTime
Options是位字段。 RowMajor。它指定这种类型的矩阵使用行为主存储顺序;默认情况下存储顺序是以列为主的。请参阅存储订单页面。例如此类型表示行为主的3x3矩阵: Matrixfloat, 3, 3, RowMajorMaxRowsAtCompileTime和MaxColsAtCompileTime 在需要指定时非常有用即使在编译时不知道矩阵的确切大小但在编译时知道一个固定的上限。这样做的最大原因可能是为了避免动态内存分配。例如下面的矩阵类型使用12个浮点数的普通数组没有动态内存分配
Matrixfloat, Dynamic, Dynamic, 0, 3, 4存储顺序详解 矩阵和二维数组有两种不同的存储顺序:列为主和行为主 当矩阵存储在内存中时条目必须以某种方式线性排列。有两种主要的方法可以做到这一点按行和按列。
如果一个矩阵是逐行存储的我们就说它是按行主序存储 row-major的。首先存储整个第一行然后是整个第二行依此类推。以矩阵为例
8 2 2 9 9 1 4 4 3 5 4 5 如果一个矩阵是按列存储 column-major 的那么它是以列为主顺序存储的从整个第一列开始然后是整个第二列依此类推。若将上述矩阵按列主序存储则其布局如下:
8 9 3 2 1 5 2 4 4 9 4 5 3、例 定义一个4*3的矩阵A然后分别以按行或按列存储。 使用PlainObjectBase::data()函数该函数返回指向矩阵第一个条目的内存位置的指针。
#include iostream
#include e:/eigen/Eigen/Dense
using namespace std;
using namespace Eigen;
int main()
{Matrixint, 4, 3, ColMajor Acolmajor;Acolmajor 1,2,3,4,5,6,7,8,9,10,11,12;cout The matrix A: endl;cout Acolmajor endl endl;cout In memory (column-major): endl;for (int i 0; i Acolmajor.size(); i)cout *(Acolmajor.data() i) ;cout endl endl;Matrixint, 4, 3, RowMajor Arowmajor Acolmajor;cout In memory (row-major): endl;for (int i 0; i Arowmajor.size(); i)cout *(Arowmajor.data() i) ;cout endl;
}
The matrix A:1 2 34 5 67 8 9
10 11 12In memory (column-major):
1 4 7 10 2 5 8 11 3 6 9 12In memory (row-major):
1 2 3 4 5 6 7 8 9 10 11 12Process returned 0 (0x0) execution time : 0.404 s
Press any key to continue.Matrix类模板有六个模板参数其中三个是强制性的(Scalar, RowsAtCompileTime和ColsAtCompileTime)另外三个是可选的(Options, MaxRowsAtCompileTime和MaxColsAtCompileTime)。如果Options参数设置为RowMajor则矩阵或数组按行主要顺序存储;如果设置为ColMajor则按列主顺序存储。该机制在上述特征程序中用于指定存储顺序。 如果未指定存储顺序则Eigen默认以列为主的方式存储条目。如果使用方便类型(Matrix3f、ArrayXXd等)之一也会出现这种情况。 使用一种存储顺序的矩阵和数组可以分配给使用另一种存储顺序的矩阵和数组就像上面的程序中使用Acolmajor初始化Arowmajor时发生的那样。Eigen将自动重新排序条目。更一般地说行为主矩阵和列为主矩阵可以在表达式中混合使用。 您应该在程序中使用哪种存储顺序呢?这个问题没有简单的答案;这取决于您的应用程序。以下是一些需要牢记的要点:
1.您的用户可能希望您使用特定的存储顺序。或者您可以使用Eigen以外的其他库这些库可能需要特定的存储顺序。在这些情况下在整个程序中使用这种存储顺序可能是最简单和最快的。 2.当矩阵以行为主的顺序存储时由于更好的数据局部性逐行遍历矩阵的算法将运行得更快。类似地对于列主矩阵逐列遍历更快。为了找出适合您的特定应用程序的更快的方法进行一些实验可能是值得的。 3.缺省情况下Eigen是列为主的。自然地大多数特征库的开发和测试都是用列主矩阵完成的。这意味着尽管我们的目标是透明地支持列为主和行为主的存储顺序但Eigen库可能最适合列为主的矩阵。
初始化向量
#include iostream
#include f:/learn/eigen-3.4.0/Eigen/Denseusing Eigen::VectorXd;
using Eigen::RowVectorXd;
int main()
{VectorXd a {{1.5, 2.5, 3.5}};RowVectorXd b {{1.0, 2.0, 3.0, 4.0}};std::cout a std::endl;std::cout b std::endl;
}
1.5
2.5
3.5
1 2 3 4Process returned 0 (0x0) execution time : 0.110 s
Press any key to continue.
元素类型
MatrixNt for Matrixtype, N, N. For example, MatrixXi for Matrixint, Dynamic, Dynamic.
MatrixXNt for Matrixtype, Dynamic, N. For example, MatrixX3i for Matrixint, Dynamic, 3.
MatrixNXt for Matrixtype, N, Dynamic. For example, Matrix4Xd for Matrixd, 4, Dynamic.
VectorNt for Matrixtype, N, 1. For example, Vector2f for Matrixfloat, 2, 1.
RowVectorNt for Matrixtype, 1, N. For example, RowVector3d for Matrixdouble, 1, 3.N可以是2、3、4或X中的任意一个(表示动态)。T可以是I (int)、f (float)、d (double)、cf (complex)或CD (complex)中的任意一个。虽然只为这五种类型定义了类型定义但这并不意味着它们是唯一受支持的标量类型
参考文献
Eigen 文章转载自: http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.chzbq.cn.gov.cn.chzbq.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.ydxg.cn.gov.cn.ydxg.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.nmfml.cn.gov.cn.nmfml.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.bkslb.cn.gov.cn.bkslb.cn http://www.morning.gjlst.cn.gov.cn.gjlst.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.syynx.cn.gov.cn.syynx.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.zfwjh.cn.gov.cn.zfwjh.cn http://www.morning.nrll.cn.gov.cn.nrll.cn http://www.morning.chbcj.cn.gov.cn.chbcj.cn http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.mtgkq.cn.gov.cn.mtgkq.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.lsnnc.cn.gov.cn.lsnnc.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.qcdhg.cn.gov.cn.qcdhg.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.qzpw.cn.gov.cn.qzpw.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.wspjn.cn.gov.cn.wspjn.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.npmpn.cn.gov.cn.npmpn.cn http://www.morning.lmyq.cn.gov.cn.lmyq.cn http://www.morning.mrxgm.cn.gov.cn.mrxgm.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.gfhng.cn.gov.cn.gfhng.cn http://www.morning.rsnn.cn.gov.cn.rsnn.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.hhpkb.cn.gov.cn.hhpkb.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.rscrj.cn.gov.cn.rscrj.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.mymz.cn.gov.cn.mymz.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.lcwhn.cn.gov.cn.lcwhn.cn http://www.morning.pzrpz.cn.gov.cn.pzrpz.cn http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.mdmc.cn.gov.cn.mdmc.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.btjyp.cn.gov.cn.btjyp.cn