In [1]:
Copied!
import sparse
import numpy as np
import scipy.sparse as sps
import sparse
import numpy as np
import scipy.sparse as sps
Create Arrays¶
In [2]:
Copied!
rng = np.random.default_rng(42)
M = 1_000
DENSITY = 0.01
a = sparse.random((M, M), density=DENSITY, format="csc")
identity = sparse.eye(M, format="csc")
rng = np.random.default_rng(42)
M = 1_000
DENSITY = 0.01
a = sparse.random((M, M), density=DENSITY, format="csc")
identity = sparse.eye(M, format="csc")
Invert and verify matrix¶
This showcases the scipy.sparse.linalg
integration.
In [3]:
Copied!
a_inv = sps.linalg.spsolve(a, identity)
np.testing.assert_array_almost_equal((a_inv @ a).todense(), identity.todense())
a_inv = sps.linalg.spsolve(a, identity)
np.testing.assert_array_almost_equal((a_inv @ a).todense(), identity.todense())
Calculate the graph distances¶
This showcases the scipy.sparse.csgraph
integration.
In [4]:
Copied!
sps.csgraph.bellman_ford(sparse.eye(5, k=1) + sparse.eye(5, k=-1), return_predecessors=False)
sps.csgraph.bellman_ford(sparse.eye(5, k=1) + sparse.eye(5, k=-1), return_predecessors=False)
Out[4]:
array([[0., 1., 2., 3., 4.], [1., 0., 1., 2., 3.], [2., 1., 0., 1., 2.], [3., 2., 1., 0., 1.], [4., 3., 2., 1., 0.]])