[英]C++: how to convert std::vector operations to Eigen::VectorXf?
I have this vector operation and I'm trying to implement Eigen:VectorXf to optimise its speed:我有这个向量操作,我正在尝试实现 Eigen:VectorXf 以优化其速度:
for (int m = 0; m < vectorSize; ++m)
{
A[m] = ((B[m] * C[m]) + ((D[m] * E[m]) + (F[m] * G[m])));
H[m] = A[m] * I[m];
out = out + H[m];
E[m] = C[m];
C[m] = A[m];
}
'out' is a float variable. 'out' 是一个浮点变量。 What would be the best way to implement this?
实现这一点的最佳方法是什么?
Many thanks.非常感谢。
With vectors you have to use array()
to get component-wise multiplication.使用向量,您必须使用
array()
来获得组件乘法。 You can use the sum()
function to compute out
as the sum of elements of H.您可以使用
sum()
out
来计算 H 的元素之和。
VectorXf B, C, D, E, F, G, I;
//...
// Assuming all vectors have the same size
const VectorXf A = ((B.array() * C.array()) + (D.array()) * E.array()) + (F.array() * G.array());
const VectorXf H = A.array() * I.array();
const float out = H.sum();
E = C;
C = A;
Note that if your vector size is known at compile time it might be better to use Matrix<float,vectorSize,1>
instead of VectorXf
请注意,如果您的向量大小在编译时已知,最好使用
Matrix<float,vectorSize,1>
而不是VectorXf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.