test_polyutils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """Tests for polyutils module.
  2. """
  3. import numpy as np
  4. import numpy.polynomial.polyutils as pu
  5. from numpy.testing import assert_, assert_almost_equal, assert_equal, assert_raises
  6. class TestMisc:
  7. def test_trimseq(self):
  8. tgt = [1]
  9. for num_trailing_zeros in range(5):
  10. res = pu.trimseq([1] + [0] * num_trailing_zeros)
  11. assert_equal(res, tgt)
  12. def test_trimseq_empty_input(self):
  13. for empty_seq in [[], np.array([], dtype=np.int32)]:
  14. assert_equal(pu.trimseq(empty_seq), empty_seq)
  15. def test_as_series(self):
  16. # check exceptions
  17. assert_raises(ValueError, pu.as_series, [[]])
  18. assert_raises(ValueError, pu.as_series, [[[1, 2]]])
  19. assert_raises(ValueError, pu.as_series, [[1], ['a']])
  20. # check common types
  21. types = ['i', 'd', 'O']
  22. for i in range(len(types)):
  23. for j in range(i):
  24. ci = np.ones(1, types[i])
  25. cj = np.ones(1, types[j])
  26. [resi, resj] = pu.as_series([ci, cj])
  27. assert_(resi.dtype.char == resj.dtype.char)
  28. assert_(resj.dtype.char == types[i])
  29. def test_trimcoef(self):
  30. coef = [2, -1, 1, 0]
  31. # Test exceptions
  32. assert_raises(ValueError, pu.trimcoef, coef, -1)
  33. # Test results
  34. assert_equal(pu.trimcoef(coef), coef[:-1])
  35. assert_equal(pu.trimcoef(coef, 1), coef[:-3])
  36. assert_equal(pu.trimcoef(coef, 2), [0])
  37. def test_vander_nd_exception(self):
  38. # n_dims != len(points)
  39. assert_raises(ValueError, pu._vander_nd, (), (1, 2, 3), [90])
  40. # n_dims != len(degrees)
  41. assert_raises(ValueError, pu._vander_nd, (), (), [90.65])
  42. # n_dims == 0
  43. assert_raises(ValueError, pu._vander_nd, (), (), [])
  44. def test_div_zerodiv(self):
  45. # c2[-1] == 0
  46. assert_raises(ZeroDivisionError, pu._div, pu._div, (1, 2, 3), [0])
  47. def test_pow_too_large(self):
  48. # power > maxpower
  49. assert_raises(ValueError, pu._pow, (), [1, 2, 3], 5, 4)
  50. class TestDomain:
  51. def test_getdomain(self):
  52. # test for real values
  53. x = [1, 10, 3, -1]
  54. tgt = [-1, 10]
  55. res = pu.getdomain(x)
  56. assert_almost_equal(res, tgt)
  57. # test for complex values
  58. x = [1 + 1j, 1 - 1j, 0, 2]
  59. tgt = [-1j, 2 + 1j]
  60. res = pu.getdomain(x)
  61. assert_almost_equal(res, tgt)
  62. def test_mapdomain(self):
  63. # test for real values
  64. dom1 = [0, 4]
  65. dom2 = [1, 3]
  66. tgt = dom2
  67. res = pu.mapdomain(dom1, dom1, dom2)
  68. assert_almost_equal(res, tgt)
  69. # test for complex values
  70. dom1 = [0 - 1j, 2 + 1j]
  71. dom2 = [-2, 2]
  72. tgt = dom2
  73. x = dom1
  74. res = pu.mapdomain(x, dom1, dom2)
  75. assert_almost_equal(res, tgt)
  76. # test for multidimensional arrays
  77. dom1 = [0, 4]
  78. dom2 = [1, 3]
  79. tgt = np.array([dom2, dom2])
  80. x = np.array([dom1, dom1])
  81. res = pu.mapdomain(x, dom1, dom2)
  82. assert_almost_equal(res, tgt)
  83. # test that subtypes are preserved.
  84. class MyNDArray(np.ndarray):
  85. pass
  86. dom1 = [0, 4]
  87. dom2 = [1, 3]
  88. x = np.array([dom1, dom1]).view(MyNDArray)
  89. res = pu.mapdomain(x, dom1, dom2)
  90. assert_(isinstance(res, MyNDArray))
  91. def test_mapparms(self):
  92. # test for real values
  93. dom1 = [0, 4]
  94. dom2 = [1, 3]
  95. tgt = [1, .5]
  96. res = pu. mapparms(dom1, dom2)
  97. assert_almost_equal(res, tgt)
  98. # test for complex values
  99. dom1 = [0 - 1j, 2 + 1j]
  100. dom2 = [-2, 2]
  101. tgt = [-1 + 1j, 1 - 1j]
  102. res = pu.mapparms(dom1, dom2)
  103. assert_almost_equal(res, tgt)