Skip to content

Warn about Ill-Conditioned Matrices  #470

@gtamba

Description

@gtamba

I was able to reproduce #436 for every nxn square matrix whose elements are the natural numbers starting from 1 to n^n using this script. The output shows that each of these matrices has an almost zero determinant and returns an inverse when inverted.

These matrices cover a subset of the family of ill-conditioned matrices that are numerically unstable and sensitive to errors when precision is a requirement. (Like how singularity is defined EXACTLY at det = 0 and not the infinitesimally small neighborhood around it)

It is interesting to note that LAPACK does not supply a determinant routine. I googled rather hard to find out why but all I could find was this in the FAQ :-

Are there routines in LAPACK to compute determinants?

No. There are no routines in LAPACK to compute determinants. This is discussed in the "Accuracy and Stability" chapter in the LAPACK Users' Guide.

I decided to test out the same issues on Matlab and as expected an inverse was returned, however, Matlab was aware of it :-

>> a = [1.0, 2.0, 3.0; 4.0, 5.0, 6.0 ; 7.0 , 8.0 , 9.0]

a =

     1     2     3
     4     5     6
     7     8     9

>> inv(a)
Warning: Matrix is close to singular or badly scaled. 
Results may be inaccurate. RCOND =  1.541976e-18. 

ans =

   1.0e+16 *

   -0.4504    0.9007   -0.4504
    0.9007   -1.8014    0.9007
   -0.4504    0.9007   -0.4504

Determinant also fails to be zero.

>> det(a)

ans =

   6.6613e-16

It seems that Matlab uses a cond() and rcond() function to compute how numerically unstable the matrix is ; a higher condition number implies a more unstable matrix.

>> cond(a)

ans =

   3.8131e+16

I think the issue here is not with the decomposition algorithms, simply the hard limitations of machine floating point accuracies. The condition number in this case is incredibly high.

[TL ; DR;-]

I propose that we implement cond() and perhaps call it while calculating inverse to produce a warning. It might be useful as a public function for someone who is aware of this to check their workspace along the way.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions