Search code examples
c++eigen

How to modify some options for Matrix from existing type to create a new Matrix type in template programming?


For example, assuming we have a matrix type T, can we create a new type that has only 1 cols like Matrix<..., ..., 1 ,...> and keep other options exactly the same in type T?


Solution

  • After some research I give following solution:

    template<typename T1>
    auto fun(const Eigen::MatrixBase<T1>& _X)
    {
        using T = typename T1::PlainObject;
        using T2 = Eigen::Matrix<typename  T::Scalar,
            T::RowsAtCompileTime, T::ColsAtCompileTime,
            T::Options, T::MaxRowsAtCompileTime, T::MaxColsAtCompileTime>;
    
        if constexpr (std::is_same_v<T2, T>)
        {
            std::cout << "True" << std::endl;
        }
        return T();
    
    }