test_solve_toeplitz.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """Test functions for linalg._solve_toeplitz module
  2. """
  3. import numpy as np
  4. from scipy.linalg._solve_toeplitz import levinson
  5. from scipy.linalg import solve, toeplitz, solve_toeplitz
  6. from numpy.testing import assert_equal, assert_allclose
  7. import pytest
  8. from pytest import raises as assert_raises
  9. def test_solve_equivalence():
  10. # For toeplitz matrices, solve_toeplitz() should be equivalent to solve().
  11. random = np.random.RandomState(1234)
  12. for n in (1, 2, 3, 10):
  13. c = random.randn(n)
  14. if random.rand() < 0.5:
  15. c = c + 1j * random.randn(n)
  16. r = random.randn(n)
  17. if random.rand() < 0.5:
  18. r = r + 1j * random.randn(n)
  19. y = random.randn(n)
  20. if random.rand() < 0.5:
  21. y = y + 1j * random.randn(n)
  22. # Check equivalence when both the column and row are provided.
  23. actual = solve_toeplitz((c,r), y)
  24. desired = solve(toeplitz(c, r=r), y)
  25. assert_allclose(actual, desired)
  26. # Check equivalence when the column is provided but not the row.
  27. actual = solve_toeplitz(c, b=y)
  28. desired = solve(toeplitz(c), y)
  29. assert_allclose(actual, desired)
  30. def test_multiple_rhs():
  31. random = np.random.RandomState(1234)
  32. c = random.randn(4)
  33. r = random.randn(4)
  34. for offset in [0, 1j]:
  35. for yshape in ((4,), (4, 3), (4, 3, 2)):
  36. y = random.randn(*yshape) + offset
  37. actual = solve_toeplitz((c,r), b=y)
  38. desired = solve(toeplitz(c, r=r), y)
  39. assert_equal(actual.shape, yshape)
  40. assert_equal(desired.shape, yshape)
  41. assert_allclose(actual, desired)
  42. def test_native_list_arguments():
  43. c = [1,2,4,7]
  44. r = [1,3,9,12]
  45. y = [5,1,4,2]
  46. actual = solve_toeplitz((c,r), y)
  47. desired = solve(toeplitz(c, r=r), y)
  48. assert_allclose(actual, desired)
  49. def test_zero_diag_error():
  50. # The Levinson-Durbin implementation fails when the diagonal is zero.
  51. random = np.random.RandomState(1234)
  52. n = 4
  53. c = random.randn(n)
  54. r = random.randn(n)
  55. y = random.randn(n)
  56. c[0] = 0
  57. assert_raises(np.linalg.LinAlgError,
  58. solve_toeplitz, (c, r), b=y)
  59. def test_wikipedia_counterexample():
  60. # The Levinson-Durbin implementation also fails in other cases.
  61. # This example is from the talk page of the wikipedia article.
  62. random = np.random.RandomState(1234)
  63. c = [2, 2, 1]
  64. y = random.randn(3)
  65. assert_raises(np.linalg.LinAlgError, solve_toeplitz, c, b=y)
  66. def test_reflection_coeffs():
  67. # check that the partial solutions are given by the reflection
  68. # coefficients
  69. random = np.random.RandomState(1234)
  70. y_d = random.randn(10)
  71. y_z = random.randn(10) + 1j
  72. reflection_coeffs_d = [1]
  73. reflection_coeffs_z = [1]
  74. for i in range(2, 10):
  75. reflection_coeffs_d.append(solve_toeplitz(y_d[:(i-1)], b=y_d[1:i])[-1])
  76. reflection_coeffs_z.append(solve_toeplitz(y_z[:(i-1)], b=y_z[1:i])[-1])
  77. y_d_concat = np.concatenate((y_d[-2:0:-1], y_d[:-1]))
  78. y_z_concat = np.concatenate((y_z[-2:0:-1].conj(), y_z[:-1]))
  79. _, ref_d = levinson(y_d_concat, b=y_d[1:])
  80. _, ref_z = levinson(y_z_concat, b=y_z[1:])
  81. assert_allclose(reflection_coeffs_d, ref_d[:-1])
  82. assert_allclose(reflection_coeffs_z, ref_z[:-1])
  83. @pytest.mark.xfail(reason='Instability of Levinson iteration')
  84. def test_unstable():
  85. # this is a "Gaussian Toeplitz matrix", as mentioned in Example 2 of
  86. # I. Gohbert, T. Kailath and V. Olshevsky "Fast Gaussian Elimination with
  87. # Partial Pivoting for Matrices with Displacement Structure"
  88. # Mathematics of Computation, 64, 212 (1995), pp 1557-1576
  89. # which can be unstable for levinson recursion.
  90. # other fast toeplitz solvers such as GKO or Burg should be better.
  91. random = np.random.RandomState(1234)
  92. n = 100
  93. c = 0.9 ** (np.arange(n)**2)
  94. y = random.randn(n)
  95. solution1 = solve_toeplitz(c, b=y)
  96. solution2 = solve(toeplitz(c), y)
  97. assert_allclose(solution1, solution2)