Eigen庫

MatrixXd表示任意size的矩陣,元素類型爲double; VectorXd表示任意size的向量,元素類型爲double.html

//建立3*1的向量v,並賦值爲1,2,3
VectorXd v(3); v << 1, 2, 3;

使用固定尺寸的Matrix,Vector相比於可變尺寸的Matrix,Vector,例如Matrix3d m代替MatrixXd m(3,3)有如下優勢:ide

運行速度更快,編譯期間可實現更嚴格的錯誤檢查。函數

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.lua

 transpose()計算矩陣的轉置, transpose()不支持就地轉置,使用transposeInPlace()來實現就地轉置。spa

 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. scala

matrix和array之間能夠相互進行轉換,經過調用matrix的.array()函數將matrix轉換爲array表達式;經過調用array的.matrix()函數將array轉換爲matrix表達式。3d

Eigen中禁止一個表達式中混合使用matrix和array,但容許賦值運算符這樣操做。指針

Eigen中爲matrix提供了cwiseProduct()函數以實現逐元素相乘。code

 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存儲元素值。

相關文章
相關標籤/搜索