2

I have a 14x14 matrix of which I'm trying to take the rank. The problem is that it has a high condition number so using double precision my matrix is not full rank. I know that it should be, so I'm trying to take the rank in higher precision.

So far I have installed the bigfloat package in python, but have been unsuccessful in trying to get the rank in higher precision. I have also scaled my matrix, I tried python's jacobi preconditioner and some other scaling methods but it was not sufficient.

I'm not trying to solve a system of linear equations, I just need to verify that all my columns are linearly independent. In other words, I want to verify that a (simplified) matrix such as the one shown is of rank 2, not 1.

[1, 0;
 0, 1e-20]

Any Suggestions?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
0_o
  • 333
  • 3
  • 9
  • 1
    just pointing out that "rank" means something entirely different in python than what it means *mathematically*. It would help to illustrate with an example just so there's no confusion. – Tasos Papastylianou Jul 22 '16 at 18:05
  • Possible duplicate of [Calculate Matrix Rank using scipy](http://stackoverflow.com/questions/2473983/calculate-matrix-rank-using-scipy) – dot.Py Jul 22 '16 at 18:07
  • take a look at [numpy.linalg.matrix_rank()](http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.linalg.matrix_rank.html) – dot.Py Jul 22 '16 at 18:08
  • I have used both matlab's rank function as well as python's np.linalg.matrix_rank() but that only performs the calculation in double precision. – 0_o Jul 22 '16 at 18:18
  • I found the Multiprecision Computing Toolbox for MATLAB, which does exactly what I want. Is there something similar for Python? – 0_o Jul 22 '16 at 20:44

2 Answers2

0

Has matlab's rank function not worked for you?

>> A = [1,0; 0, 1e-20];
>> rank(A, 1e-19)
ans =  1
>> rank(A, 1e-21)
ans =  2
Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • Oh, apologies. I misread the `matrix` tag from the post as `matlab` hence all the matlab answers as I thought you wanted an "either python or matlab" solution ... *facepalm*. On the plus side, if you're happy to use the `oct2py` module, you can use octave's `rank` function (though, I'm sure python probably has a native function for it) :) – Tasos Papastylianou Jul 22 '16 at 18:15
  • Indeed I can change the tolerance and get a good result. However, I thought the default tolerance was the best, since it determines when numerical round-off error is introduced. The calculation is still performed in double precision and as a result if you're going down to higher precision your results are invalid since it's within the error. I could be wrong, but that's what I thought. – 0_o Jul 22 '16 at 18:17
0

If you're working with ill-conditioned matrices, it's just hard to tell the rank of your matrix. The 1.0e-20 in your example might perhaps just be a round-off error in actual computation.

In numpy, the rank is checked by looking at the SVD and counting the number of "zero" eigenvalues, where "zero" is with some tolerance. Depending on what you set here, you get different results:

import numpy

a = numpy.array([[1, 0], [0, 1e-20]])

rank = numpy.linalg.matrix_rank(a, tol=1.0e-10)
print(rank)

rank = numpy.linalg.matrix_rank(a, tol=1.0e-30)
print(rank)
1
2
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249