简体   繁体   English

C++:如何将 std::vector 操作转换为 Eigen::VectorXf?

[英]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.

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