test_symbol.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. """
  2. Tests related to the ``symbol`` attribute of the ABCPolyBase class.
  3. """
  4. import pytest
  5. import numpy.polynomial as poly
  6. from numpy.core import array
  7. from numpy.testing import assert_equal, assert_raises, assert_
  8. class TestInit:
  9. """
  10. Test polynomial creation with symbol kwarg.
  11. """
  12. c = [1, 2, 3]
  13. def test_default_symbol(self):
  14. p = poly.Polynomial(self.c)
  15. assert_equal(p.symbol, 'x')
  16. @pytest.mark.parametrize(('bad_input', 'exception'), (
  17. ('', ValueError),
  18. ('3', ValueError),
  19. (None, TypeError),
  20. (1, TypeError),
  21. ))
  22. def test_symbol_bad_input(self, bad_input, exception):
  23. with pytest.raises(exception):
  24. p = poly.Polynomial(self.c, symbol=bad_input)
  25. @pytest.mark.parametrize('symbol', (
  26. 'x',
  27. 'x_1',
  28. 'A',
  29. 'xyz',
  30. 'β',
  31. ))
  32. def test_valid_symbols(self, symbol):
  33. """
  34. Values for symbol that should pass input validation.
  35. """
  36. p = poly.Polynomial(self.c, symbol=symbol)
  37. assert_equal(p.symbol, symbol)
  38. def test_property(self):
  39. """
  40. 'symbol' attribute is read only.
  41. """
  42. p = poly.Polynomial(self.c, symbol='x')
  43. with pytest.raises(AttributeError):
  44. p.symbol = 'z'
  45. def test_change_symbol(self):
  46. p = poly.Polynomial(self.c, symbol='y')
  47. # Create new polynomial from p with different symbol
  48. pt = poly.Polynomial(p.coef, symbol='t')
  49. assert_equal(pt.symbol, 't')
  50. class TestUnaryOperators:
  51. p = poly.Polynomial([1, 2, 3], symbol='z')
  52. def test_neg(self):
  53. n = -self.p
  54. assert_equal(n.symbol, 'z')
  55. def test_scalarmul(self):
  56. out = self.p * 10
  57. assert_equal(out.symbol, 'z')
  58. def test_rscalarmul(self):
  59. out = 10 * self.p
  60. assert_equal(out.symbol, 'z')
  61. def test_pow(self):
  62. out = self.p ** 3
  63. assert_equal(out.symbol, 'z')
  64. @pytest.mark.parametrize(
  65. 'rhs',
  66. (
  67. poly.Polynomial([4, 5, 6], symbol='z'),
  68. array([4, 5, 6]),
  69. ),
  70. )
  71. class TestBinaryOperatorsSameSymbol:
  72. """
  73. Ensure symbol is preserved for numeric operations on polynomials with
  74. the same symbol
  75. """
  76. p = poly.Polynomial([1, 2, 3], symbol='z')
  77. def test_add(self, rhs):
  78. out = self.p + rhs
  79. assert_equal(out.symbol, 'z')
  80. def test_sub(self, rhs):
  81. out = self.p - rhs
  82. assert_equal(out.symbol, 'z')
  83. def test_polymul(self, rhs):
  84. out = self.p * rhs
  85. assert_equal(out.symbol, 'z')
  86. def test_divmod(self, rhs):
  87. for out in divmod(self.p, rhs):
  88. assert_equal(out.symbol, 'z')
  89. def test_radd(self, rhs):
  90. out = rhs + self.p
  91. assert_equal(out.symbol, 'z')
  92. def test_rsub(self, rhs):
  93. out = rhs - self.p
  94. assert_equal(out.symbol, 'z')
  95. def test_rmul(self, rhs):
  96. out = rhs * self.p
  97. assert_equal(out.symbol, 'z')
  98. def test_rdivmod(self, rhs):
  99. for out in divmod(rhs, self.p):
  100. assert_equal(out.symbol, 'z')
  101. class TestBinaryOperatorsDifferentSymbol:
  102. p = poly.Polynomial([1, 2, 3], symbol='x')
  103. other = poly.Polynomial([4, 5, 6], symbol='y')
  104. ops = (p.__add__, p.__sub__, p.__mul__, p.__floordiv__, p.__mod__)
  105. @pytest.mark.parametrize('f', ops)
  106. def test_binops_fails(self, f):
  107. assert_raises(ValueError, f, self.other)
  108. class TestEquality:
  109. p = poly.Polynomial([1, 2, 3], symbol='x')
  110. def test_eq(self):
  111. other = poly.Polynomial([1, 2, 3], symbol='x')
  112. assert_(self.p == other)
  113. def test_neq(self):
  114. other = poly.Polynomial([1, 2, 3], symbol='y')
  115. assert_(not self.p == other)
  116. class TestExtraMethods:
  117. """
  118. Test other methods for manipulating/creating polynomial objects.
  119. """
  120. p = poly.Polynomial([1, 2, 3, 0], symbol='z')
  121. def test_copy(self):
  122. other = self.p.copy()
  123. assert_equal(other.symbol, 'z')
  124. def test_trim(self):
  125. other = self.p.trim()
  126. assert_equal(other.symbol, 'z')
  127. def test_truncate(self):
  128. other = self.p.truncate(2)
  129. assert_equal(other.symbol, 'z')
  130. @pytest.mark.parametrize('kwarg', (
  131. {'domain': [-10, 10]},
  132. {'window': [-10, 10]},
  133. {'kind': poly.Chebyshev},
  134. ))
  135. def test_convert(self, kwarg):
  136. other = self.p.convert(**kwarg)
  137. assert_equal(other.symbol, 'z')
  138. def test_integ(self):
  139. other = self.p.integ()
  140. assert_equal(other.symbol, 'z')
  141. def test_deriv(self):
  142. other = self.p.deriv()
  143. assert_equal(other.symbol, 'z')
  144. def test_composition():
  145. p = poly.Polynomial([3, 2, 1], symbol="t")
  146. q = poly.Polynomial([5, 1, 0, -1], symbol="λ_1")
  147. r = p(q)
  148. assert r.symbol == "λ_1"
  149. #
  150. # Class methods that result in new polynomial class instances
  151. #
  152. def test_fit():
  153. x, y = (range(10),)*2
  154. p = poly.Polynomial.fit(x, y, deg=1, symbol='z')
  155. assert_equal(p.symbol, 'z')
  156. def test_froomroots():
  157. roots = [-2, 2]
  158. p = poly.Polynomial.fromroots(roots, symbol='z')
  159. assert_equal(p.symbol, 'z')
  160. def test_identity():
  161. p = poly.Polynomial.identity(domain=[-1, 1], window=[5, 20], symbol='z')
  162. assert_equal(p.symbol, 'z')
  163. def test_basis():
  164. p = poly.Polynomial.basis(3, symbol='z')
  165. assert_equal(p.symbol, 'z')