# 矩阵库Eigen的MatrixXd中预定义的类型

`template <typename Derived> void cov(const MatrixBase<Derived>& X）{}`

• 1、通用性强，X既可以是MatrixXd类型的，也可以是MatrixXf类型的，甚至是矩阵的一个子块m1.block(0,0,10,10)的 或者表达式 m1+m2  (假设m1,m2为MatrixXd类型的)
• 2、速度快（表达式类型不用先进行运算，存为Matrix了）

• Scalar： 矩阵中存储的类型
• Index:   矩阵下标的类型，貌似为unsigned int型的
• PlainObject: 表达式对应的矩阵类型，  比如m1+m2对应的PlainObject为 m1的类型，即decltype(m1)

`typename Derived::Scalar s;`

`typedef Matrix<typename internal::traits<Derived>::Scalar, internal::traits<Derived>::RowsAtCompileTime, internal::traits<Derived>::ColsAtCompileTime, AutoAlign | (internal::traits<Derived>::Flags&RowMajorBit ? RowMajor : ColMajor), internal::traits<Derived>::MaxRowsAtCompileTime, internal::traits<Derived>::MaxColsAtCompileTime > PlainObject`

```typedef typename internal::traits<Derived>::Index Index;
typedef typename internal::traits<Derived>::Scalar Scalar;
typedef typename NumTraits<Scalar>::Real RealScalar;```

Let us now explain the internal::traits here. The internal::scalar_sum_op class takes one template parameter: the type of the numbers to handle. Here of course we want to pass the scalar type (a.k.a. numeric type) of VectorXf, which is float. How do we determine which is the scalar type of Derived ? Throughout Eigen, all matrix and expression types define a typedef Scalar which gives its scalar type. For example, VectorXf::Scalar is a typedef for float. So here, if life was easy, we could find the numeric type of Derived as just

typename Derived::Scalar
Unfortunately, we can't do that here, as the compiler would complain that the type Derived hasn't yet been defined. So we use a workaround: in src/Core/util/ForwardDeclarations.h, we declared (not defined!) all our subclasses, like Matrix, and we also declared the following class template:

template<typename T> struct internal::traits;
In src/Core/Matrix.h, right before the definition of class Matrix, we define a partial specialization of internal::traits for T=Matrix<any template parameters>. In this specialization of internal::traits, we define the Scalar typedef. So when we actually define Matrix, it is legal to refer to "typename internal::traits\<Matrix\>::Scalar".

Matrix类官方文档：
http://eigen.tuxfamily.org/dox/classEigen_1_1Matrix.html

MatrixBase类官方文档：

http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html

http://eigen.tuxfamily.org/dox/TopicInsideEigenExample.html

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊

Map类 是 矩阵库Eigen中用来将内存数据 映射为 任意形状的矩阵的类。 其基本用法参见：http://cheris