user13590641
user13590641

Reputation: 3

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:

        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. What would be the best way to implement this?

Many thanks.

Upvotes: 0

Views: 342

Answers (1)

Louen
Louen

Reputation: 3677

With vectors you have to use array() to get component-wise multiplication. You can use the sum() function to compute out as the sum of elements of 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

Upvotes: 1

Related Questions