how do I find the pivots of my matrix

I want to get non-singular matrix with the C++ linear algebra library eigen, but I do not know which function works, any help will be very appreciated.

assume that X is matrix with dimension of 100*50. I want to get (X.transpose()*X).inverse() in C++, but error is that singular matrix occurs. Multicollinearity occurs in some columns of matrix, i just want to get non-singular matrix.
Here is the matrix pivot in R software:

qr(X)$pivot

and the result is: qr(X)$pivot= 1 2 4 5 7 10 ..(11-41).. 42 45 49 50 3 6 8 9 43 44 46 47 48, that is non-singular matrix contains columns except for 3 6 8 9 43 44 46 47 48
Last edited on
That library looks really nice!
Looking at the docs, it doesn't look much like there is a function to do it, but you can create one yourself.

Given, for example, the following matrix:

1 0 0 0 
0 0 1 0
0 0 0 1

You can find pivots by scanning left and down, in that order:

1
2
3
4
5
6
7
8
9
10
11
int m = 0;
int n = 0;
while ((m < r.rows()) && (n < r.cols()))
{
  if (r( m, n ) != 0)
  {
    cout << "pivot at " << m << ", " << n << " = " << r( m, n ) << "\n";
    n++;
  }
  else m++;
}

This is faster than you think, O(N+M) time. Make sure to use an upper triangular matrix (I see you are using R, good).

Hope this helps.
@Duoas Sorry, may be my problem is vague, i will edit it.
Last edited on
I'd say that counts as a totally different question than "how do I find the pivots of my matrix."

As for finding a non-singular matrix, that is not always possible. (It is actually fairly rare.) [edit] that is to say, most matrices are not invertible.
Last edited on
[Continuing from a PM asking for explanation about matrices being invertible]
Sorry, I was half asleep, and made a dumb comment.

Most matrices are not invertible, because only square matrices are invertible. If I had been paying attention (or awake) I would have noticed that OP was obviously playing with square matrices...

If you want to find the inverse of your matrix it must have certain properties. Uh, here
http://en.wikipedia.org/wiki/Invertible_matrix#Properties
If your given matrix does not satisfy those properties, then you must permute it in some way such that it does. Here's an explanation for you
http://www.johndcook.com/blog/2012/06/13/matrix-condition-number/

Hope this helps.
@Duoas So good answer. thank you!
Topic archived. No new replies allowed.