test_gcrotmk.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/env python
  2. """Tests for the linalg.isolve.gcrotmk module
  3. """
  4. from __future__ import division, print_function, absolute_import
  5. from numpy.testing import assert_, assert_allclose, assert_equal
  6. from scipy._lib._numpy_compat import suppress_warnings
  7. import numpy as np
  8. from numpy import zeros, array, allclose
  9. from scipy.linalg import norm
  10. from scipy.sparse import csr_matrix, eye, rand
  11. from scipy.sparse.linalg.interface import LinearOperator
  12. from scipy.sparse.linalg import splu
  13. from scipy.sparse.linalg.isolve import gcrotmk, gmres
  14. Am = csr_matrix(array([[-2,1,0,0,0,9],
  15. [1,-2,1,0,5,0],
  16. [0,1,-2,1,0,0],
  17. [0,0,1,-2,1,0],
  18. [0,3,0,1,-2,1],
  19. [1,0,0,0,1,-2]]))
  20. b = array([1,2,3,4,5,6])
  21. count = [0]
  22. def matvec(v):
  23. count[0] += 1
  24. return Am*v
  25. A = LinearOperator(matvec=matvec, shape=Am.shape, dtype=Am.dtype)
  26. def do_solve(**kw):
  27. count[0] = 0
  28. with suppress_warnings() as sup:
  29. sup.filter(DeprecationWarning, ".*called without specifying.*")
  30. x0, flag = gcrotmk(A, b, x0=zeros(A.shape[0]), tol=1e-14, **kw)
  31. count_0 = count[0]
  32. assert_(allclose(A*x0, b, rtol=1e-12, atol=1e-12), norm(A*x0-b))
  33. return x0, count_0
  34. class TestGCROTMK(object):
  35. def test_preconditioner(self):
  36. # Check that preconditioning works
  37. pc = splu(Am.tocsc())
  38. M = LinearOperator(matvec=pc.solve, shape=A.shape, dtype=A.dtype)
  39. x0, count_0 = do_solve()
  40. x1, count_1 = do_solve(M=M)
  41. assert_equal(count_1, 3)
  42. assert_(count_1 < count_0/2)
  43. assert_(allclose(x1, x0, rtol=1e-14))
  44. def test_arnoldi(self):
  45. np.random.rand(1234)
  46. A = eye(2000) + rand(2000, 2000, density=5e-4)
  47. b = np.random.rand(2000)
  48. # The inner arnoldi should be equivalent to gmres
  49. with suppress_warnings() as sup:
  50. sup.filter(DeprecationWarning, ".*called without specifying.*")
  51. x0, flag0 = gcrotmk(A, b, x0=zeros(A.shape[0]), m=15, k=0, maxiter=1)
  52. x1, flag1 = gmres(A, b, x0=zeros(A.shape[0]), restart=15, maxiter=1)
  53. assert_equal(flag0, 1)
  54. assert_equal(flag1, 1)
  55. assert_(np.linalg.norm(A.dot(x0) - b) > 1e-3)
  56. assert_allclose(x0, x1)
  57. def test_cornercase(self):
  58. np.random.seed(1234)
  59. # Rounding error may prevent convergence with tol=0 --- ensure
  60. # that the return values in this case are correct, and no
  61. # exceptions are raised
  62. for n in [3, 5, 10, 100]:
  63. A = 2*eye(n)
  64. with suppress_warnings() as sup:
  65. sup.filter(DeprecationWarning, ".*called without specifying.*")
  66. b = np.ones(n)
  67. x, info = gcrotmk(A, b, maxiter=10)
  68. assert_equal(info, 0)
  69. assert_allclose(A.dot(x) - b, 0, atol=1e-14)
  70. x, info = gcrotmk(A, b, tol=0, maxiter=10)
  71. if info == 0:
  72. assert_allclose(A.dot(x) - b, 0, atol=1e-14)
  73. b = np.random.rand(n)
  74. x, info = gcrotmk(A, b, maxiter=10)
  75. assert_equal(info, 0)
  76. assert_allclose(A.dot(x) - b, 0, atol=1e-14)
  77. x, info = gcrotmk(A, b, tol=0, maxiter=10)
  78. if info == 0:
  79. assert_allclose(A.dot(x) - b, 0, atol=1e-14)
  80. def test_nans(self):
  81. A = eye(3, format='lil')
  82. A[1,1] = np.nan
  83. b = np.ones(3)
  84. with suppress_warnings() as sup:
  85. sup.filter(DeprecationWarning, ".*called without specifying.*")
  86. x, info = gcrotmk(A, b, tol=0, maxiter=10)
  87. assert_equal(info, 1)
  88. def test_truncate(self):
  89. np.random.seed(1234)
  90. A = np.random.rand(30, 30) + np.eye(30)
  91. b = np.random.rand(30)
  92. for truncate in ['oldest', 'smallest']:
  93. with suppress_warnings() as sup:
  94. sup.filter(DeprecationWarning, ".*called without specifying.*")
  95. x, info = gcrotmk(A, b, m=10, k=10, truncate=truncate, tol=1e-4,
  96. maxiter=200)
  97. assert_equal(info, 0)
  98. assert_allclose(A.dot(x) - b, 0, atol=1e-3)
  99. def test_CU(self):
  100. for discard_C in (True, False):
  101. # Check that C,U behave as expected
  102. CU = []
  103. x0, count_0 = do_solve(CU=CU, discard_C=discard_C)
  104. assert_(len(CU) > 0)
  105. assert_(len(CU) <= 6)
  106. if discard_C:
  107. for c, u in CU:
  108. assert_(c is None)
  109. # should converge immediately
  110. x1, count_1 = do_solve(CU=CU, discard_C=discard_C)
  111. if discard_C:
  112. assert_equal(count_1, 2 + len(CU))
  113. else:
  114. assert_equal(count_1, 3)
  115. assert_(count_1 <= count_0/2)
  116. assert_allclose(x1, x0, atol=1e-14)
  117. def test_denormals(self):
  118. # Check that no warnings are emitted if the matrix contains
  119. # numbers for which 1/x has no float representation, and that
  120. # the solver behaves properly.
  121. A = np.array([[1, 2], [3, 4]], dtype=float)
  122. A *= 100 * np.nextafter(0, 1)
  123. b = np.array([1, 1])
  124. with suppress_warnings() as sup:
  125. sup.filter(DeprecationWarning, ".*called without specifying.*")
  126. xp, info = gcrotmk(A, b)
  127. if info == 0:
  128. assert_allclose(A.dot(xp), b)