test_pydata_sparse.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import pytest
  2. import numpy as np
  3. import scipy.sparse as sp
  4. import scipy.sparse.linalg as splin
  5. from numpy.testing import assert_allclose, assert_equal
  6. try:
  7. import sparse
  8. except Exception:
  9. sparse = None
  10. pytestmark = pytest.mark.skipif(sparse is None,
  11. reason="pydata/sparse not installed")
  12. msg = "pydata/sparse (0.8) does not implement necessary operations"
  13. sparse_params = (pytest.param("COO"),
  14. pytest.param("DOK", marks=[pytest.mark.xfail(reason=msg)]))
  15. scipy_sparse_classes = [
  16. sp.bsr_matrix,
  17. sp.csr_matrix,
  18. sp.coo_matrix,
  19. sp.csc_matrix,
  20. sp.dia_matrix,
  21. sp.dok_matrix
  22. ]
  23. @pytest.fixture(params=sparse_params)
  24. def sparse_cls(request):
  25. return getattr(sparse, request.param)
  26. @pytest.fixture(params=scipy_sparse_classes)
  27. def sp_sparse_cls(request):
  28. return request.param
  29. @pytest.fixture
  30. def same_matrix(sparse_cls, sp_sparse_cls):
  31. np.random.seed(1234)
  32. A_dense = np.random.rand(9, 9)
  33. return sp_sparse_cls(A_dense), sparse_cls(A_dense)
  34. @pytest.fixture
  35. def matrices(sparse_cls):
  36. np.random.seed(1234)
  37. A_dense = np.random.rand(9, 9)
  38. A_dense = A_dense @ A_dense.T
  39. A_sparse = sparse_cls(A_dense)
  40. b = np.random.rand(9)
  41. return A_dense, A_sparse, b
  42. def test_isolve_gmres(matrices):
  43. # Several of the iterative solvers use the same
  44. # isolve.utils.make_system wrapper code, so test just one of them.
  45. A_dense, A_sparse, b = matrices
  46. x, info = splin.gmres(A_sparse, b, atol=1e-15)
  47. assert info == 0
  48. assert isinstance(x, np.ndarray)
  49. assert_allclose(A_sparse @ x, b)
  50. def test_lsmr(matrices):
  51. A_dense, A_sparse, b = matrices
  52. res0 = splin.lsmr(A_dense, b)
  53. res = splin.lsmr(A_sparse, b)
  54. assert_allclose(res[0], res0[0], atol=1.8e-5)
  55. # test issue 17012
  56. def test_lsmr_output_shape():
  57. x = splin.lsmr(A=np.ones((10, 1)), b=np.zeros(10), x0=np.ones(1))[0]
  58. assert_equal(x.shape, (1,))
  59. def test_lsqr(matrices):
  60. A_dense, A_sparse, b = matrices
  61. res0 = splin.lsqr(A_dense, b)
  62. res = splin.lsqr(A_sparse, b)
  63. assert_allclose(res[0], res0[0], atol=1e-5)
  64. def test_eigs(matrices):
  65. A_dense, A_sparse, v0 = matrices
  66. M_dense = np.diag(v0**2)
  67. M_sparse = A_sparse.__class__(M_dense)
  68. w_dense, v_dense = splin.eigs(A_dense, k=3, v0=v0)
  69. w, v = splin.eigs(A_sparse, k=3, v0=v0)
  70. assert_allclose(w, w_dense)
  71. assert_allclose(v, v_dense)
  72. for M in [M_sparse, M_dense]:
  73. w_dense, v_dense = splin.eigs(A_dense, M=M_dense, k=3, v0=v0)
  74. w, v = splin.eigs(A_sparse, M=M, k=3, v0=v0)
  75. assert_allclose(w, w_dense)
  76. assert_allclose(v, v_dense)
  77. w_dense, v_dense = splin.eigsh(A_dense, M=M_dense, k=3, v0=v0)
  78. w, v = splin.eigsh(A_sparse, M=M, k=3, v0=v0)
  79. assert_allclose(w, w_dense)
  80. assert_allclose(v, v_dense)
  81. def test_svds(matrices):
  82. A_dense, A_sparse, v0 = matrices
  83. u0, s0, vt0 = splin.svds(A_dense, k=2, v0=v0)
  84. u, s, vt = splin.svds(A_sparse, k=2, v0=v0)
  85. assert_allclose(s, s0)
  86. assert_allclose(u, u0)
  87. assert_allclose(vt, vt0)
  88. def test_lobpcg(matrices):
  89. A_dense, A_sparse, x = matrices
  90. X = x[:,None]
  91. w_dense, v_dense = splin.lobpcg(A_dense, X)
  92. w, v = splin.lobpcg(A_sparse, X)
  93. assert_allclose(w, w_dense)
  94. assert_allclose(v, v_dense)
  95. def test_spsolve(matrices):
  96. A_dense, A_sparse, b = matrices
  97. b2 = np.random.rand(len(b), 3)
  98. x0 = splin.spsolve(sp.csc_matrix(A_dense), b)
  99. x = splin.spsolve(A_sparse, b)
  100. assert isinstance(x, np.ndarray)
  101. assert_allclose(x, x0)
  102. x0 = splin.spsolve(sp.csc_matrix(A_dense), b)
  103. x = splin.spsolve(A_sparse, b, use_umfpack=True)
  104. assert isinstance(x, np.ndarray)
  105. assert_allclose(x, x0)
  106. x0 = splin.spsolve(sp.csc_matrix(A_dense), b2)
  107. x = splin.spsolve(A_sparse, b2)
  108. assert isinstance(x, np.ndarray)
  109. assert_allclose(x, x0)
  110. x0 = splin.spsolve(sp.csc_matrix(A_dense),
  111. sp.csc_matrix(A_dense))
  112. x = splin.spsolve(A_sparse, A_sparse)
  113. assert isinstance(x, type(A_sparse))
  114. assert_allclose(x.toarray(), x0.toarray())
  115. def test_splu(matrices):
  116. A_dense, A_sparse, b = matrices
  117. n = len(b)
  118. sparse_cls = type(A_sparse)
  119. lu = splin.splu(A_sparse)
  120. assert isinstance(lu.L, sparse_cls)
  121. assert isinstance(lu.U, sparse_cls)
  122. Pr = sparse_cls(sp.csc_matrix((np.ones(n), (lu.perm_r, np.arange(n)))))
  123. Pc = sparse_cls(sp.csc_matrix((np.ones(n), (np.arange(n), lu.perm_c))))
  124. A2 = Pr.T @ lu.L @ lu.U @ Pc.T
  125. assert_allclose(A2.toarray(), A_sparse.toarray())
  126. z = lu.solve(A_sparse.toarray())
  127. assert_allclose(z, np.eye(n), atol=1e-10)
  128. def test_spilu(matrices):
  129. A_dense, A_sparse, b = matrices
  130. sparse_cls = type(A_sparse)
  131. lu = splin.spilu(A_sparse)
  132. assert isinstance(lu.L, sparse_cls)
  133. assert isinstance(lu.U, sparse_cls)
  134. z = lu.solve(A_sparse.toarray())
  135. assert_allclose(z, np.eye(len(b)), atol=1e-3)
  136. def test_spsolve_triangular(matrices):
  137. A_dense, A_sparse, b = matrices
  138. A_sparse = sparse.tril(A_sparse)
  139. x = splin.spsolve_triangular(A_sparse, b)
  140. assert_allclose(A_sparse @ x, b)
  141. def test_onenormest(matrices):
  142. A_dense, A_sparse, b = matrices
  143. est0 = splin.onenormest(A_dense)
  144. est = splin.onenormest(A_sparse)
  145. assert_allclose(est, est0)
  146. def test_inv(matrices):
  147. A_dense, A_sparse, b = matrices
  148. x0 = splin.inv(sp.csc_matrix(A_dense))
  149. x = splin.inv(A_sparse)
  150. assert_allclose(x.toarray(), x0.toarray())
  151. def test_expm(matrices):
  152. A_dense, A_sparse, b = matrices
  153. x0 = splin.expm(sp.csc_matrix(A_dense))
  154. x = splin.expm(A_sparse)
  155. assert_allclose(x.toarray(), x0.toarray())
  156. def test_expm_multiply(matrices):
  157. A_dense, A_sparse, b = matrices
  158. x0 = splin.expm_multiply(A_dense, b)
  159. x = splin.expm_multiply(A_sparse, b)
  160. assert_allclose(x, x0)
  161. def test_eq(same_matrix):
  162. sp_sparse, pd_sparse = same_matrix
  163. assert (sp_sparse == pd_sparse).all()
  164. def test_ne(same_matrix):
  165. sp_sparse, pd_sparse = same_matrix
  166. assert not (sp_sparse != pd_sparse).any()