This forum has been archived. All content is frozen. Please use KDE Discuss instead.

How to invert a dynamic 9x9 MatrixXd?

Tags: None
(comma "," separated)
hsharma
Registered Member
Posts
11
Karma
0
Hi,
I have an Eigen::MatrixXd m(9,9);
And when I am doing this:

m.inverse();

I am getting the same output.

Is there any other way to invert such a large matrix?

Thanks in advance.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
As explained in the doc (http://eigen.tuxfamily.org/dox-devel/cl ... 030f79f9da), mat.inverse() returns the inverse of mat, keeping mat unchanged:
Code: Select all
mat_inv = mat.inverse();
hsharma
Registered Member
Posts
11
Karma
0
My problem is that when i calculate the inverse of my 9x9 matrix I get "nan" values.

I am having these values in my matrix:

1384.33 0 0 0 0 0 553.1 0 -1707.22
0 673.42 673.42 553.1 -601.406 229.515 0 241.537 0
0 673.42 673.42 553.1 -601.406 229.515 0 241.537 0
0 553.1 553.1 9288.69 771.387 4054.28 0 -672.069 0
0 -601.406 -601.406 771.387 2848.9 781.647 0 -777.88 0
0 229.515 229.515 4054.28 781.647 2986.75 0 -459.042 0
553.1 0 0 0 0 0 771.387 0 -672.069
0 241.537 241.537 -672.069 -777.88 -459.042 0 781.647 0
-1707.22 0 0 0 0 0 -672.069 0 4054.28

And when I printing the inverse I get:

-nan -nan -nan -nan -nan -nan -nan -nan -nan
-nan -nan -nan -nan -nan -nan -nan -nan -nan
-nan -nan -nan -nan -nan -nan -nan -nan -nan
-nan -nan -nan -nan -nan -nan -nan -nan -nan
-nan -nan -nan -nan -nan -nan -nan -nan -nan
-nan -nan -nan -nan -nan -nan -nan -nan -nan
-nan -nan -nan -nan -nan -nan -nan -nan -nan
-nan -inf inf -nan -nan -nan -nan -nan -nan
0.000636602 0 0 0 0 0 -9.36032e-06 0 0.000513168

I am not getting what's going wrong.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Your matrix is singular, so not invertible. Confirmed by matlab:
Code: Select all
inv(M)
warning: inverse: matrix singular to machine precision, rcond = 0
ans =

   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf
   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf
   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf
   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf
   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf
   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf
   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf
   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf
   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf


If what you what is to solve for A x = b, or equivalently apply the inverse of A to tome matrices (A^-1 * B), then use a rank-revealing decomposition, e.g.:
Code: Select all
ColPivHouseholderQr<MatrixXd> qr(A);
 ... qr.solve(B) ... ; // <=> A^-1 * B
hsharma
Registered Member
Posts
11
Karma
0
Yeah...
this is what i want to do.

x = bA^-1

where, x = 3x9 mat
b = 3x9 mat
A = 9x9 mat

Is it possible to compute this with LU decomposition as I tried but it is crashing?
Or can I get inverse of A using LU decomposition?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
You have many options:
Code: Select all
x = A.lu().solve(b);
x = A.fullPivLu().solve(b);
x = A.colPivHouseholderQr().solve(b);
x = A.jacobiSVD(ComputeThinU|ComputeThinV).solve(b);

If you want to solve for the same matrix multiple times, build the respective factorization object as shown in my previous post.
hsharma
Registered Member
Posts
11
Karma
0
I get this error when I am doing this:

x = A.lu().solve(b)

where, x = 3x9 mat
b = 3x9 mat
A = 9x9 mat

DeformationDynamics: ./Eigen/src/LU/PartialPivLU.h:449: void Eigen::internal::solve_retval<Eigen::PartialPivLU<_MatrixType>, Rhs>::evalTo(Dest&) const [with Dest = Eigen::Matrix<double, 3, 9>; _MatrixType = Eigen::Matrix<double, 9, 9>; Rhs = Eigen::Matrix<double, 3, 9>]: Assertion `rhs().rows() == dec().matrixLU().rows()' failed.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
x.transpose() = A.lu().solve(b.transpose());
hsharma
Registered Member
Posts
11
Karma
0
Yeah...
I did that but I am still getting some "nan" values in my output matrix.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
which solver did you tried? I shown the LU one for reference only, but is well known that a partial pivoting LU requires the matrix to be invertible, so not applicable in your case. This is also explain in our doc: http://eigen.tuxfamily.org/dox-devel/gr ... tions.html.

Have you tried the 3 others options I suggested? They are all working for me:

Code: Select all
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main() {
  MatrixXd A(9,9);
  A <<  1384.33, 0, 0, 0, 0, 0, 553.1, 0, -1707.22,
        0, 673.42, 673.42, 553.1, -601.406, 229.515, 0, 241.537, 0,
        0, 673.42, 673.42, 553.1, -601.406, 229.515, 0, 241.537, 0,
        0, 553.1, 553.1, 9288.69, 771.387, 4054.28, 0, -672.069, 0,
        0, -601.406, -601.406, 771.387, 2848.9, 781.647, 0, -777.88, 0,
        0, 229.515, 229.515, 4054.28, 781.647, 2986.75, 0, -459.042, 0,
        553.1, 0, 0, 0, 0, 0, 771.387, 0, -672.069,
        0, 241.537, 241.537, -672.069, -777.88, -459.042, 0, 781.647, 0,
        -1707.22, 0, 0, 0, 0, 0, -672.069, 0, 4054.28;
   RowVectorXd x(9), b(9);
   x.setRandom();
   b.transpose() = A * x.transpose(); // make sure a solution exists
   x.setZero();
   
   x.transpose() = A.jacobiSvd(ComputeThinU|ComputeThinV).solve(b.transpose());
   std::cout << "svd: " << (A * x.transpose() - b.transpose()).norm() << "\n";
   
   x.transpose() = A.fullPivLu().solve(b.transpose());
   std::cout << "LU:  " << (A * x.transpose() - b.transpose()).norm() << "\n";
   
   x.transpose() = A.colPivHouseholderQr().solve(b.transpose());
   std::cout << "QR:  " << (A * x.transpose() - b.transpose()).norm() << "\n";
}


Output:
Code: Select all
svd: 2.82792e-12
LU:  4.58286e-13
QR:  1.02002e-12
hsharma
Registered Member
Posts
11
Karma
0
Thank you for the solution but I don't understand why these really large values (2.82792e-12) are coming and how can I avoid them?

And one more thing rather a stupid question but if the inverse of the matrix doesn't exist then how does the solution is reliable through these decomposition?
gradstudentassistant
Registered Member
Posts
1
Karma
0


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft