Eigen库
MatrixXd表示任意size的矩陣,元素類型為double; VectorXd表示任意size的向量,元素類型為double.
//創建3*1的向量v,并賦值為1,2,3 VectorXd v(3); v << 1, 2, 3;使用固定尺寸的Matrix,Vector相比于可變尺寸的Matrix,Vector,例如Matrix3d?m代替MatrixXd?m(3,3)有以下優點:
運行速度更快,編譯期間可實現更嚴格的錯誤檢查。
Eigen中,所有的矩陣,向量都是Matrix模板類的對象,向量只是矩陣的特例,行數或者列數為1.
//便捷定義 typedef Matrix<float, 4, 4> Matrix4f; typedef Matrix<float, 3, 1> Vector3f; typedef Matrix<int, 1, 2> RowVector2i; ? typedef Matrix<double, Dynamic, Dynamic> MatrixXd;? //編譯期未知尺寸?Eigen中通過()獲取其元素,起始索引為0。The operator[] is also overloaded for index-based access in vectors, but keep in mind that C++ doesn't allow operator[] to take more than one argument.
?transpose()計算矩陣的轉置,?transpose()不支持就地轉置,使用transposeInPlace()來實現就地轉置。
?Array
We adopt the convention that typedefs of the form ArrayNt stand for 1-dimensional arrays, where N and t are the size and the scalar type.For 2-dimensional arrays, we use typedefs of the form ArrayNNt.?
matrix和array之間可以相互進行轉換,通過調用matrix的.array()函數將matrix轉換為array表達式;通過調用array的.matrix()函數將array轉換為matrix表達式。
Eigen中禁止一個表達式中混合使用matrix和array,但允許賦值運算符這樣操作。
Eigen中為matrix提供了cwiseProduct()函數以實現逐元素相乘。
?Block Operation
matrix.row(i) 獲取矩陣matrix的第i行,matrix.col(j)獲取矩陣matrix的第j列。相比于使用block()性能更好。
Eigen?also provides special methods for blocks that are flushed against one of the corners or sides of a matrix or array. For instance,?.topLeftCorner()?can be used to refer to a block in the top-left corner of a matrix.
?
squaredNorm() 計算2范數的平方,norm()計算2范數,lpNorm<p>()計算p范數,p設置為Infinity可計算無窮范數。
The following reductions operate on boolean values:
- all()?returns?true?if all of the coefficients in a given?Matrix?or?Array?evaluate to?true?.
- any()?returns?true?if at least one of the coefficients in a given?Matrix?or?Array?evaluates to?true?.
- count()?returns the number of coefficients in a given?Matrix?or?Array?that evaluate to?true.
?Visitors
Visitors are useful when one wants to obtain the location of a coefficient inside a?Matrix?or?Array. The simplest examples are?maxCoeff(&x,&y)?and?minCoeff(&x,&y), which can be used to find the location of the greatest or smallest coefficient in a?Matrix?or?Array.
The arguments passed to a visitor are pointers to the variables where the row and column position are to be stored. These variables should be of type?Index?
?matrix.data()函數返回一個指向矩陣內存地址的指針。Eigen中矩陣默認以column-major存儲元素值。
?
轉載于:https://www.cnblogs.com/larry-xia/p/10666834.html
總結
- 上一篇: win10看视频背景声音大、人声小、或其
- 下一篇: Python 循环删除指定文件夹下所有的