Eigen: Best way to evaluate A*S*A_transpose and store the result in a symmetric matrix

StackOverflow https://stackoverflow.com/questions/13215467

  •  30-07-2021
  •  | 
  •  

Frage

Let S be a symmetric n x n matrix and A be a m x n matrix.

Given: B = A * S * A_transpose (where "*" represents a matrix product operation)

B will also be a symmetric matrix.

Using the tuxfamily Eigen library, version 3, what is a clean and efficient way to implement this computation? (By efficient, I mostly mean that duplicate computations of the elements of B are not performed where symmetry makes them unnecessary.)

I'm guessing it will make use of SelfAdjointView, but I have searched high and low and not found a clean example of this.

The application is a Kalman filter, which depends heavily on operations involving (symmetric) covariance matrices, so I want to be sure get the implementation/design correct.

Thank you!

War es hilfreich?

Lösung

This should be quite simple. As you say yourself, you can make Eigen aware of the fact that your matrix is a symmetric matrix through the SelfAdjointView. There is also another view, which is the TriangularView, which you can use to store your results. According to the reference if you assign to a TriangularView, only the relevant parts of the rhs are evaluated. So

B.triangularView<Upper>() = A * S.selfadjointView<Upper>() * A.transpose();

will store the result in the upper triangle of B. You can then use B.selfadjointView<Upper> in any further calculations. I am not sure if this is optimal in terms of required operations and you might do some benchmarking to verify.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top