mercredi 20 juillet 2016

std::accumulate() only the real part of a complex std::vector

I used to take the sum of a symmetric (Hermitian) matrix (the matrix is in an std::vector), which is a huge waste because the imaginary part will always add to zero (I'm talking about huge matrices with side-length of n>1000, so I think it makes a difference.

So now I only add the upper-triangular part of the matrix. But I want to optimize this further and avoid adding the complex part because I don't need it.

What I currently use is:

std::real(std::accumulate(myMatrix.begin(), myMatrix.end(), std::complex<Real>(0,0)));

This adds all the element of myMatrix to std::complex<Real>(0,0), resulting in the sum I need.

But this will add real and imaginary components of my vector, which is a waste! How can I write the most optimized version of this that adds only the real part of this matrix?

Aucun commentaire:

Enregistrer un commentaire