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
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).
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.
[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...