In Eigen, can a sparse matrix contain vectors/objects instead of simple scalar values?

I need to have a sparse matrix whose elements are not simple numbers, but objects, e.g. a couple of floating point values and a bunch of integer indices.

I am wondering if Eigen has something similar, beyond its Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex > class template (the only sparse matrix I have found so far), which only accepts a scalar template parameter.

I know I can use a SoA (Structure of Arrays) approach, but in this specific case, for data locality reasons, I am trying with an AoS (Array of Structures) one.

1 Answer

Certainly. There are a few things you have to define for your type that are listed on this page in the documentation:

https://eigen.tuxfamily.org/dox/TopicCustomizing_CustomScalar.html

It basically boils down to defining arithmetic operators appropriately for your type, plus specializing a traits template NumTraits that describes your type. The link above has a few examples that you can follow.