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

<pre><code>dim = 20000
ratio = 0.000133

A = scipy.sparse.rand(dim,dim,ratio)
inv_sparse = scipy.sparse.linalg.inv(A)
</code></pre>

<p>Is there a way to generate non-singular sparse matrix?</p>

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

<p>The density <code>ratio = 0.000133</code> 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.</p>

<p>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%.</p>

<p>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. </p>

<p>As the matrix is likely to contain a null row, it is often singular.</p>

<p>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.</p>

<p>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.</p>

python numpy matrix scipy