3
votes

in Matlab if I write

A = B*inv(C)

(with A, B and C being square matrices), I get a warning that matrix inversion should be replaced with a matrix "right-division" (due to being numerically more stable and accurate) like

A = B/C

In my Eigen C++ project I have the following code:

Eigen::Matrix<double> A = B*(C.inverse());

and I was woundering if there is an equivalent replacement for taking the matrix inverse in Eigen analogous to the one in Matlab mentioned above?

I know that matrix "left-division" can be expressed by solving a system of equations for expressions like

A = inv(C)*B

but what about

A = C*inv(B)

in Eigen?

1
You could transpose everything to end up with the same form. Not sure if it will be more performant than calculating the inverse, though. - Nico Schertler
taking inverse of large matrix is inefficient way of solving AxC=B or CxA=B matrix equations. - karakfa

1 Answers

3
votes

At the moment the most efficient way to do this is to rewrite your equation to

A^T = inv(C^T) * B^T
A   = (inv(C^T) * B^T)^T

which can be implemented in Eigen as

SomeDecomposition decompC(C);  // decompose C with a suiting decomposition
Eigen::MatrixXd A = decompC.transpose().solve(B.transpose()).transpose();

There were/are plans, that eventually, one can write

A = B * decompC.inverse();

and Eigen will evaluate this in the most efficient way.