3

When a sparse matrix is generated by scipy.sparse.rand, it can be singular. In fact,the below code raises an error "RuntimeError: superlu failure (singular matrix?) at line 100 in file scipy/sparse/linalg/dsolve/SuperLU/SRC/dsnode_bmod.c".

dim = 20000
ratio = 0.000133

A = scipy.sparse.rand(dim,dim,ratio)
inv_sparse = scipy.sparse.linalg.inv(A)

Is there a way to generate non-singular sparse matrix?

What I really want to do is comparing performance (process time) of scipy.sparse.linalg.inv with np.linalg.inv. That's why I need generate random sparse matrix which is not singular.

3
  • 1
    Comments here might help math.stackexchange.com/questions/449396/… you might also just check the determinant and then do the inversion... Or generate them and handle exceptions... Maybe generate cases beforehand that are known to be nonsingular Commented Sep 24, 2016 at 4:57
  • 1
    Is it singular because 2 or more rows or columns are all zero? That's possible if the density is low enough. Commented Sep 24, 2016 at 5:46
  • 4
    Try adding sparse.eye(A.shape[0]) to it. Commented Sep 24, 2016 at 5:54

2 Answers 2

2

The density ratio = 0.000133 of your matrix is very low. It means that about one item out of 7518 is non-null. Hence, the probability of each term to be null is about 7517/7518.

Each row is made of 20000 independent terms. So the probability for a row to be null is (7517/7518)^20000=6.99%. Hence, the probability for a row to be non-null is 1-(7517/7518)^20000=93.0%.

Then, the matrix is made of 20000 rows. The rows can be considered as being independent. Hence, the probability that the matrix does not contain null rows is (1-(7517/7518)^20000)^20000=(93.0%)^20000. This probability is very low.

As the matrix is likely to contain a null row, it is often singular.

Moreover, due to the the limited precision of floating-point numbers, programs often consider ill-conditionned matrices as singular. Indeed, in such cases, the computed inverse would be highly unprecise and meaningless.

Finally, to compare the inverse function, it may be better to use matrices known to be invertible... At least, you could try to increase the density so that the probability of a null row becomes very low.

Sign up to request clarification or add additional context in comments.

Comments

1

As was already suggested in the comments by @hpaulj, you can add an identity matrix to your random matrix to make it invertible.

However, in applications where you need to find approximate solutions to systems of linear equations, just multiply identity matrix by a small factor:

dim = 20000
ratio = 0.000133
A = csc_matrix(scipy.sparse.random(dim, dim, density=ratio), dtype=float)
A = A + scipy.sparse.csr_matrix(np.eye(A.shape[0]) * 1e-8)
A = scipy.sparse.csc_matrix(A)
inv_sparse = scipy.sparse.linalg.inv(A)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.