The class hierarchy
This page explains the design of the core classes in Eigen's class hierarchy and how they fit together. Casual users probably need not concern themselves with these details, but it may be useful for both advanced users and Eigen developers.
Principles
Eigen's class hierarchy is designed so that virtual functions are avoided where their overhead would significantly impair performance. Instead, Eigen achieves polymorphism with the Curiously Recurring Template Pattern (CRTP). In this pattern, the base class (for instance, MatrixBase) is in fact a template class, and the derived class (for instance, Matrix) inherits the base class with the derived class itself as a template argument (in this case, Matrix inherits from MatrixBase<Matrix>). This allows Eigen to resolve the polymorphic function calls at compile time.
In addition, the design avoids multiple inheritance. One reason for this is that in our experience, some compilers (like MSVC) fail to perform empty base class optimization, which is crucial for our fixed-size types.
The core classes
These are the classes that you need to know about if you want to write functions that accept or return Eigen objects.
- Matrix means plain dense matrix. If
mis aMatrix, then, for instance,m+mis no longer aMatrix, it is a "matrix expression". - MatrixBase means dense matrix expression. This means that a
MatrixBaseis something that can be added, matrix-multiplied, LU-decomposed, QR-decomposed... All matrix expression classes, includingMatrixitself, inheritMatrixBase. - Array means plain dense array. If
xis anArray, then, for instance,x+xis no longer anArray, it is an "array expression". - ArrayBase means dense array expression. This means that an
ArrayBaseis something that can be added, array-multiplied, and on which you can perform all sorts of array operations... All array expression classes, includingArrayitself, inheritArrayBase. - DenseBase means dense (matrix or array) expression. Both
ArrayBaseandMatrixBaseinheritDenseBase.DenseBaseis where all the methods go that apply to dense expressions regardless of whether they are matrix or array expressions. For example, the block(...) methods are inDenseBase.
Base classes
These classes serve as base classes for the five core classes mentioned above. They are more internal and so less interesting for users of the Eigen library.
- PlainObjectBase means dense (matrix or array) plain object, i.e. something that stores its own dense array of coefficients. This is where, for instance, the resize() methods go.
PlainObjectBaseis inherited byMatrixand byArray. But above, we said thatMatrixinheritsMatrixBaseandArrayinheritsArrayBase. So does that mean multiple inheritance? No, becausePlainObjectBaseitself inheritsMatrixBaseorArrayBasedepending on whether we are in the matrix or array case. When we said above thatMatrixinheritedMatrixBase, we omitted to say it does so indirectly viaPlainObjectBase. Same forArray. - DenseCoeffsBase means something that has dense coefficient accessors. It is a base class for
DenseBase. The reason forDenseCoeffsBaseto exist is that the set of available coefficient accessors is very different depending on whether a dense expression has direct memory access or not (theDirectAccessBitflag). For example, ifxis a plain matrix, thenxhas direct access, andx.transpose()andx.block(...)also have direct access, because their coefficients can be read right off memory, but for example,x+xdoes not have direct memory access, because obtaining any of its coefficients requires a computation (an addition), it can't be just read off memory. - EigenBase means anything that can be evaluated into a plain dense matrix or array (even if that would be a bad idea).
EigenBaseis really the absolute base class for anything that remotely looks like a matrix or array. It is a base class forDenseCoeffsBase, so it sits below all our dense class hierarchy, but it is not limited to dense expressions. For example,EigenBaseis also inherited by diagonal matrices, sparse matrices, etc...
Inheritance diagrams
The inheritance diagram for Matrix looks as follows:
The inheritance diagram for Array looks as follows:
The inheritance diagram for some other matrix expression class, here denoted by SomeMatrixXpr, looks as follows:
The inheritance diagram for some other array expression class, here denoted by SomeArrayXpr, looks as follows:
Finally, consider an example of something that is not a dense expression, for instance a diagonal matrix. The corresponding inheritance diagram is: