[英]Good way to get Eigen matrix element type
I am writing a simple interface between std::vector
and Eigen for my project. 我正在为我的项目在
std::vector
和Eigen之间编写一个简单的接口。 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.