In [1]:
import numpy as np
import scipy.sparse as sparse
import scipy.linalg as sla
import scipy.sparse.linalg as spla
import matplotlib.pyplot as plt
%matplotlib inline

Let's make a random sparse matrix

First we'll set the density so that $$ density = \frac{nnz(A)}{n^2} $$

In [2]:
n = 100
density = 10.0 / n # 5 points per row
nnz = int(n*n*density)

Now make the entries:

In [3]:
def randsp(n, density):
    nnz = int(n*n*density)
    row = np.random.random_integers(low=0, high=n-1, size=nnz)
    col = np.random.random_integers(low=0, high=n-1, size=nnz)
    data = np.ones(nnz, dtype=float)

    A = sparse.coo_matrix((data, (row, col)), shape=(n, n))
    return A

But let's make it positive definite:

In [4]:
A = randsp(100, 5/100.)
plt.spy(A, marker='.')
Out[4]:
<matplotlib.lines.Line2D at 0x10a2fcd30>
In [5]:
A = randsp(100, 2/100.)
plt.spy(A, marker='.')
Out[5]:
<matplotlib.lines.Line2D at 0x10a394dd8>
In [6]:
A = randsp(100, 50/100.)
plt.spy(A, marker='.')
Out[6]:
<matplotlib.lines.Line2D at 0x10a3fc9e8>
In [7]:
A = randsp(100, 25/100.)
plt.spy(A, marker='.')
Out[7]:
<matplotlib.lines.Line2D at 0x10a4dcd30>
In [ ]: