简体   繁体   English

获得特征矩阵元素类型的好方法

[英]Good way to get Eigen matrix element type

I am writing a simple interface between std::vector and Eigen for my project. 我正在为我的项目在std::vectorEigen之间编写一个简单的接口。 For a simple matrix-matrix multiplication code: 对于简单的矩阵矩阵乘法代码:

template<typename MatrixElementType1, typename MatrixElementType2>
    inline auto _matmat( const vector<MatrixElementType1>& Mat1, 
                         const vector<MatrixElementType2>& Mat2,
                         const size_t M, const size_t N, const size_t K)
    {
        using MatrixType1 = Matrix<MatrixElementType1, Dynamic, Dynamic>;
        using MatrixType2 = Matrix<MatrixElementType2, Dynamic, Dynamic>;
        Map<const MatrixType1> m1(Mat1.data(), M, N); 
        Map<const MatrixType2> m2(Mat2.data(), N, K); 
        auto rst = m1 * m2; 
        return vector<???>(rst.data(), rst.data() + rst.size());

    }

The questions is what should I use for ??? 问题是我应该使用??? .. I know decltype(rst(0,0)) works, but is there a more elegent way? ..我知道decltype(rst(0,0))可以用,但是还有更优雅的方法吗? It seems decltype(rst)::value_type does not work for Eigen Matrix.. 似乎decltype(rst)::value_type不适用于本矩阵。

First of all, since m1*m2 returns an expression and not the result of the product, you need to evaluate it explicitly if using auto: 首先,由于m1*m2返回一个表达式而不是乘积的结果,因此如果使用auto,则需要显式评估它:

auto rst = (m1 * m2).eval();

Then, you can get the scalar type with decltype(rst)::Scalar . 然后,您可以使用decltype(rst)::Scalar获得标量类型。 A better strategy though might be to declare a std::vector of appropriate type and size, and map it: 不过,更好的策略可能是声明适当类型和大小的std :: vector并将其映射:

typedef decltype(m1*m2)::Scalar ResScalar;
vector<ResScalar> res(M,K);
Matrix<ResScalar,Dynamic,Dynamic>::Map(res.data(),M,K).noalias() = m1*m2;
return res;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM