![]() |
Eigen
5.0.1
|
The Scalar template parameter of Matrix, Array, SparseMatrix, and the other Eigen containers determines the type of the coefficients. This page lists the scalar types that work out of the box and points to the mechanism for adding new ones.
The following types are supported without any additional work:
float, double, and long double;std::complex of any of those, most commonly std::complex<float> and std::complex<double>;int, unsigned int, short, std::int64_t, ...);bool, with the arithmetic operators interpreted in the Boolean semiring (see Matrices with boolean coefficients);Eigen::half (IEEE binary16) and Eigen::bfloat16, which Eigen provides itself in Eigen/Core.A few practical notes:
MatrixXd or Vector4i exist for float, double, int, and the two standard complex types; for every other scalar type simply spell out Matrix<Scalar, Rows, Cols> or define your own typedef.float matrix to a double matrix is a compile-time error. Convert explicitly with .cast<NewScalar>(). The exceptions are the documented real-times-complex combinations, e.g. multiplying a real matrix by a complex scalar.long double follows the platform's definition (80-bit extended precision on x86 Linux, 128-bit on some platforms, plain double on MSVC). It is never vectorized.Eigen::half and Eigen::bfloat16 are primarily storage and interchange formats: on hardware without native arithmetic, operations are emulated by converting to float and back. Both are vectorized on instruction sets with hardware support (see Vectorization).The scalar types with vectorized kernels for a given instruction set are listed on the vectorization page; every supported scalar type also works through the scalar code paths.
The properties of a scalar type are centralized in the NumTraits class template: whether the type is integer, signed, or complex, the corresponding real type (e.g. float for std::complex<float>), machine epsilon, and the tolerances used by the fuzzy comparison functions such as isApprox(). Generic code should query NumTraits and use the math functions from the Eigen::numext namespace (numext::sqrt, numext::abs2, ...) rather than hard-coding properties of float or double, so that it keeps working for every supported scalar type.
Any user-defined type with the usual arithmetic operators can be used as a scalar type after specializing NumTraits for it and providing the math functions that make sense for the type. The full recipe, with complete examples (an automatic-differentiation type and a GMP rational type), is given in Using custom scalar types. Ready-made support for the MPFR arbitrary-precision type is available in the contrib module MPRealSupport.