输入 [1]
已复制!
import sparse
import numpy as np
import scipy.sparse as sps
import sparse import numpy as np import scipy.sparse as sps
创建数组¶
输入 [2]
已复制!
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")
矩阵求逆并验证¶
这展示了 scipy.sparse.linalg
的集成。
输入 [3]
已复制!
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())
计算图距离¶
这展示了 scipy.sparse.csgraph
的集成。
输入 [4]
已复制!
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.]])