Search code examples
c++templateseigeneigen3

How to obtain storage options from Eigen::MatrixBase<Derived>


I am writing template function which should take some Eigen::MatrixBase<Derived> as input, perform some computations, and then return new eigen value. I want to return value with same storage order as input.

But i have no idea how to obtain storage order from Eigen::MatrixBase<Derived>. What can i do in this situation, and is it possible at all? I know that i can pass storage order as another template parameter, or receive Eigen::Matrix<InpScalar, InpStatRows, InpStatCols, InpStorageOrder>, but i want avoid it, if it possible

PS Sorry for my poor English


Solution

  • To see the storage order of MatrixBase<Derived>, you can check the IsRowMajor enum:

    int const StorageOrder = Derived::IsRowMajor ? Eigen::RowMajor : Eigen::ColMajor;
    

    If you want to have a type with the same storage order and same size as Derived you can directly use typename Derived::PlainObject (or PlainMatrix):

    template<class Derived>
    typename Derived::PlainObject foo(const Eigen::MatrixBase<Derived> & input)
    {
       typename Derived::PlainObject return_value;
       // do some complicated calculations
       return return_value;
    }