test_printing.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. from math import nan, inf
  2. import pytest
  3. from numpy.core import array, arange, printoptions
  4. import numpy.polynomial as poly
  5. from numpy.testing import assert_equal, assert_
  6. # For testing polynomial printing with object arrays
  7. from fractions import Fraction
  8. from decimal import Decimal
  9. class TestStrUnicodeSuperSubscripts:
  10. @pytest.fixture(scope='class', autouse=True)
  11. def use_unicode(self):
  12. poly.set_default_printstyle('unicode')
  13. @pytest.mark.parametrize(('inp', 'tgt'), (
  14. ([1, 2, 3], "1.0 + 2.0·x + 3.0·x²"),
  15. ([-1, 0, 3, -1], "-1.0 + 0.0·x + 3.0·x² - 1.0·x³"),
  16. (arange(12), ("0.0 + 1.0·x + 2.0·x² + 3.0·x³ + 4.0·x⁴ + 5.0·x⁵ + "
  17. "6.0·x⁶ + 7.0·x⁷ +\n8.0·x⁸ + 9.0·x⁹ + 10.0·x¹⁰ + "
  18. "11.0·x¹¹")),
  19. ))
  20. def test_polynomial_str(self, inp, tgt):
  21. res = str(poly.Polynomial(inp))
  22. assert_equal(res, tgt)
  23. @pytest.mark.parametrize(('inp', 'tgt'), (
  24. ([1, 2, 3], "1.0 + 2.0·T₁(x) + 3.0·T₂(x)"),
  25. ([-1, 0, 3, -1], "-1.0 + 0.0·T₁(x) + 3.0·T₂(x) - 1.0·T₃(x)"),
  26. (arange(12), ("0.0 + 1.0·T₁(x) + 2.0·T₂(x) + 3.0·T₃(x) + 4.0·T₄(x) + "
  27. "5.0·T₅(x) +\n6.0·T₆(x) + 7.0·T₇(x) + 8.0·T₈(x) + "
  28. "9.0·T₉(x) + 10.0·T₁₀(x) + 11.0·T₁₁(x)")),
  29. ))
  30. def test_chebyshev_str(self, inp, tgt):
  31. res = str(poly.Chebyshev(inp))
  32. assert_equal(res, tgt)
  33. @pytest.mark.parametrize(('inp', 'tgt'), (
  34. ([1, 2, 3], "1.0 + 2.0·P₁(x) + 3.0·P₂(x)"),
  35. ([-1, 0, 3, -1], "-1.0 + 0.0·P₁(x) + 3.0·P₂(x) - 1.0·P₃(x)"),
  36. (arange(12), ("0.0 + 1.0·P₁(x) + 2.0·P₂(x) + 3.0·P₃(x) + 4.0·P₄(x) + "
  37. "5.0·P₅(x) +\n6.0·P₆(x) + 7.0·P₇(x) + 8.0·P₈(x) + "
  38. "9.0·P₉(x) + 10.0·P₁₀(x) + 11.0·P₁₁(x)")),
  39. ))
  40. def test_legendre_str(self, inp, tgt):
  41. res = str(poly.Legendre(inp))
  42. assert_equal(res, tgt)
  43. @pytest.mark.parametrize(('inp', 'tgt'), (
  44. ([1, 2, 3], "1.0 + 2.0·H₁(x) + 3.0·H₂(x)"),
  45. ([-1, 0, 3, -1], "-1.0 + 0.0·H₁(x) + 3.0·H₂(x) - 1.0·H₃(x)"),
  46. (arange(12), ("0.0 + 1.0·H₁(x) + 2.0·H₂(x) + 3.0·H₃(x) + 4.0·H₄(x) + "
  47. "5.0·H₅(x) +\n6.0·H₆(x) + 7.0·H₇(x) + 8.0·H₈(x) + "
  48. "9.0·H₉(x) + 10.0·H₁₀(x) + 11.0·H₁₁(x)")),
  49. ))
  50. def test_hermite_str(self, inp, tgt):
  51. res = str(poly.Hermite(inp))
  52. assert_equal(res, tgt)
  53. @pytest.mark.parametrize(('inp', 'tgt'), (
  54. ([1, 2, 3], "1.0 + 2.0·He₁(x) + 3.0·He₂(x)"),
  55. ([-1, 0, 3, -1], "-1.0 + 0.0·He₁(x) + 3.0·He₂(x) - 1.0·He₃(x)"),
  56. (arange(12), ("0.0 + 1.0·He₁(x) + 2.0·He₂(x) + 3.0·He₃(x) + "
  57. "4.0·He₄(x) + 5.0·He₅(x) +\n6.0·He₆(x) + 7.0·He₇(x) + "
  58. "8.0·He₈(x) + 9.0·He₉(x) + 10.0·He₁₀(x) +\n"
  59. "11.0·He₁₁(x)")),
  60. ))
  61. def test_hermiteE_str(self, inp, tgt):
  62. res = str(poly.HermiteE(inp))
  63. assert_equal(res, tgt)
  64. @pytest.mark.parametrize(('inp', 'tgt'), (
  65. ([1, 2, 3], "1.0 + 2.0·L₁(x) + 3.0·L₂(x)"),
  66. ([-1, 0, 3, -1], "-1.0 + 0.0·L₁(x) + 3.0·L₂(x) - 1.0·L₃(x)"),
  67. (arange(12), ("0.0 + 1.0·L₁(x) + 2.0·L₂(x) + 3.0·L₃(x) + 4.0·L₄(x) + "
  68. "5.0·L₅(x) +\n6.0·L₆(x) + 7.0·L₇(x) + 8.0·L₈(x) + "
  69. "9.0·L₉(x) + 10.0·L₁₀(x) + 11.0·L₁₁(x)")),
  70. ))
  71. def test_laguerre_str(self, inp, tgt):
  72. res = str(poly.Laguerre(inp))
  73. assert_equal(res, tgt)
  74. class TestStrAscii:
  75. @pytest.fixture(scope='class', autouse=True)
  76. def use_ascii(self):
  77. poly.set_default_printstyle('ascii')
  78. @pytest.mark.parametrize(('inp', 'tgt'), (
  79. ([1, 2, 3], "1.0 + 2.0 x + 3.0 x**2"),
  80. ([-1, 0, 3, -1], "-1.0 + 0.0 x + 3.0 x**2 - 1.0 x**3"),
  81. (arange(12), ("0.0 + 1.0 x + 2.0 x**2 + 3.0 x**3 + 4.0 x**4 + "
  82. "5.0 x**5 + 6.0 x**6 +\n7.0 x**7 + 8.0 x**8 + "
  83. "9.0 x**9 + 10.0 x**10 + 11.0 x**11")),
  84. ))
  85. def test_polynomial_str(self, inp, tgt):
  86. res = str(poly.Polynomial(inp))
  87. assert_equal(res, tgt)
  88. @pytest.mark.parametrize(('inp', 'tgt'), (
  89. ([1, 2, 3], "1.0 + 2.0 T_1(x) + 3.0 T_2(x)"),
  90. ([-1, 0, 3, -1], "-1.0 + 0.0 T_1(x) + 3.0 T_2(x) - 1.0 T_3(x)"),
  91. (arange(12), ("0.0 + 1.0 T_1(x) + 2.0 T_2(x) + 3.0 T_3(x) + "
  92. "4.0 T_4(x) + 5.0 T_5(x) +\n6.0 T_6(x) + 7.0 T_7(x) + "
  93. "8.0 T_8(x) + 9.0 T_9(x) + 10.0 T_10(x) +\n"
  94. "11.0 T_11(x)")),
  95. ))
  96. def test_chebyshev_str(self, inp, tgt):
  97. res = str(poly.Chebyshev(inp))
  98. assert_equal(res, tgt)
  99. @pytest.mark.parametrize(('inp', 'tgt'), (
  100. ([1, 2, 3], "1.0 + 2.0 P_1(x) + 3.0 P_2(x)"),
  101. ([-1, 0, 3, -1], "-1.0 + 0.0 P_1(x) + 3.0 P_2(x) - 1.0 P_3(x)"),
  102. (arange(12), ("0.0 + 1.0 P_1(x) + 2.0 P_2(x) + 3.0 P_3(x) + "
  103. "4.0 P_4(x) + 5.0 P_5(x) +\n6.0 P_6(x) + 7.0 P_7(x) + "
  104. "8.0 P_8(x) + 9.0 P_9(x) + 10.0 P_10(x) +\n"
  105. "11.0 P_11(x)")),
  106. ))
  107. def test_legendre_str(self, inp, tgt):
  108. res = str(poly.Legendre(inp))
  109. assert_equal(res, tgt)
  110. @pytest.mark.parametrize(('inp', 'tgt'), (
  111. ([1, 2, 3], "1.0 + 2.0 H_1(x) + 3.0 H_2(x)"),
  112. ([-1, 0, 3, -1], "-1.0 + 0.0 H_1(x) + 3.0 H_2(x) - 1.0 H_3(x)"),
  113. (arange(12), ("0.0 + 1.0 H_1(x) + 2.0 H_2(x) + 3.0 H_3(x) + "
  114. "4.0 H_4(x) + 5.0 H_5(x) +\n6.0 H_6(x) + 7.0 H_7(x) + "
  115. "8.0 H_8(x) + 9.0 H_9(x) + 10.0 H_10(x) +\n"
  116. "11.0 H_11(x)")),
  117. ))
  118. def test_hermite_str(self, inp, tgt):
  119. res = str(poly.Hermite(inp))
  120. assert_equal(res, tgt)
  121. @pytest.mark.parametrize(('inp', 'tgt'), (
  122. ([1, 2, 3], "1.0 + 2.0 He_1(x) + 3.0 He_2(x)"),
  123. ([-1, 0, 3, -1], "-1.0 + 0.0 He_1(x) + 3.0 He_2(x) - 1.0 He_3(x)"),
  124. (arange(12), ("0.0 + 1.0 He_1(x) + 2.0 He_2(x) + 3.0 He_3(x) + "
  125. "4.0 He_4(x) +\n5.0 He_5(x) + 6.0 He_6(x) + "
  126. "7.0 He_7(x) + 8.0 He_8(x) + 9.0 He_9(x) +\n"
  127. "10.0 He_10(x) + 11.0 He_11(x)")),
  128. ))
  129. def test_hermiteE_str(self, inp, tgt):
  130. res = str(poly.HermiteE(inp))
  131. assert_equal(res, tgt)
  132. @pytest.mark.parametrize(('inp', 'tgt'), (
  133. ([1, 2, 3], "1.0 + 2.0 L_1(x) + 3.0 L_2(x)"),
  134. ([-1, 0, 3, -1], "-1.0 + 0.0 L_1(x) + 3.0 L_2(x) - 1.0 L_3(x)"),
  135. (arange(12), ("0.0 + 1.0 L_1(x) + 2.0 L_2(x) + 3.0 L_3(x) + "
  136. "4.0 L_4(x) + 5.0 L_5(x) +\n6.0 L_6(x) + 7.0 L_7(x) + "
  137. "8.0 L_8(x) + 9.0 L_9(x) + 10.0 L_10(x) +\n"
  138. "11.0 L_11(x)")),
  139. ))
  140. def test_laguerre_str(self, inp, tgt):
  141. res = str(poly.Laguerre(inp))
  142. assert_equal(res, tgt)
  143. class TestLinebreaking:
  144. @pytest.fixture(scope='class', autouse=True)
  145. def use_ascii(self):
  146. poly.set_default_printstyle('ascii')
  147. def test_single_line_one_less(self):
  148. # With 'ascii' style, len(str(p)) is default linewidth - 1 (i.e. 74)
  149. p = poly.Polynomial([12345678, 12345678, 12345678, 12345678, 123])
  150. assert_equal(len(str(p)), 74)
  151. assert_equal(str(p), (
  152. '12345678.0 + 12345678.0 x + 12345678.0 x**2 + '
  153. '12345678.0 x**3 + 123.0 x**4'
  154. ))
  155. def test_num_chars_is_linewidth(self):
  156. # len(str(p)) == default linewidth == 75
  157. p = poly.Polynomial([12345678, 12345678, 12345678, 12345678, 1234])
  158. assert_equal(len(str(p)), 75)
  159. assert_equal(str(p), (
  160. '12345678.0 + 12345678.0 x + 12345678.0 x**2 + '
  161. '12345678.0 x**3 +\n1234.0 x**4'
  162. ))
  163. def test_first_linebreak_multiline_one_less_than_linewidth(self):
  164. # Multiline str where len(first_line) + len(next_term) == lw - 1 == 74
  165. p = poly.Polynomial(
  166. [12345678, 12345678, 12345678, 12345678, 1, 12345678]
  167. )
  168. assert_equal(len(str(p).split('\n')[0]), 74)
  169. assert_equal(str(p), (
  170. '12345678.0 + 12345678.0 x + 12345678.0 x**2 + '
  171. '12345678.0 x**3 + 1.0 x**4 +\n12345678.0 x**5'
  172. ))
  173. def test_first_linebreak_multiline_on_linewidth(self):
  174. # First line is one character longer than previous test
  175. p = poly.Polynomial(
  176. [12345678, 12345678, 12345678, 12345678.12, 1, 12345678]
  177. )
  178. assert_equal(str(p), (
  179. '12345678.0 + 12345678.0 x + 12345678.0 x**2 + '
  180. '12345678.12 x**3 +\n1.0 x**4 + 12345678.0 x**5'
  181. ))
  182. @pytest.mark.parametrize(('lw', 'tgt'), (
  183. (75, ('0.0 + 10.0 x + 200.0 x**2 + 3000.0 x**3 + 40000.0 x**4 + '
  184. '500000.0 x**5 +\n600000.0 x**6 + 70000.0 x**7 + 8000.0 x**8 + '
  185. '900.0 x**9')),
  186. (45, ('0.0 + 10.0 x + 200.0 x**2 + 3000.0 x**3 +\n40000.0 x**4 + '
  187. '500000.0 x**5 +\n600000.0 x**6 + 70000.0 x**7 + 8000.0 x**8 +\n'
  188. '900.0 x**9')),
  189. (132, ('0.0 + 10.0 x + 200.0 x**2 + 3000.0 x**3 + 40000.0 x**4 + '
  190. '500000.0 x**5 + 600000.0 x**6 + 70000.0 x**7 + 8000.0 x**8 + '
  191. '900.0 x**9')),
  192. ))
  193. def test_linewidth_printoption(self, lw, tgt):
  194. p = poly.Polynomial(
  195. [0, 10, 200, 3000, 40000, 500000, 600000, 70000, 8000, 900]
  196. )
  197. with printoptions(linewidth=lw):
  198. assert_equal(str(p), tgt)
  199. for line in str(p).split('\n'):
  200. assert_(len(line) < lw)
  201. def test_set_default_printoptions():
  202. p = poly.Polynomial([1, 2, 3])
  203. c = poly.Chebyshev([1, 2, 3])
  204. poly.set_default_printstyle('ascii')
  205. assert_equal(str(p), "1.0 + 2.0 x + 3.0 x**2")
  206. assert_equal(str(c), "1.0 + 2.0 T_1(x) + 3.0 T_2(x)")
  207. poly.set_default_printstyle('unicode')
  208. assert_equal(str(p), "1.0 + 2.0·x + 3.0·x²")
  209. assert_equal(str(c), "1.0 + 2.0·T₁(x) + 3.0·T₂(x)")
  210. with pytest.raises(ValueError):
  211. poly.set_default_printstyle('invalid_input')
  212. def test_complex_coefficients():
  213. """Test both numpy and built-in complex."""
  214. coefs = [0+1j, 1+1j, -2+2j, 3+0j]
  215. # numpy complex
  216. p1 = poly.Polynomial(coefs)
  217. # Python complex
  218. p2 = poly.Polynomial(array(coefs, dtype=object))
  219. poly.set_default_printstyle('unicode')
  220. assert_equal(str(p1), "1j + (1+1j)·x - (2-2j)·x² + (3+0j)·x³")
  221. assert_equal(str(p2), "1j + (1+1j)·x + (-2+2j)·x² + (3+0j)·x³")
  222. poly.set_default_printstyle('ascii')
  223. assert_equal(str(p1), "1j + (1+1j) x - (2-2j) x**2 + (3+0j) x**3")
  224. assert_equal(str(p2), "1j + (1+1j) x + (-2+2j) x**2 + (3+0j) x**3")
  225. @pytest.mark.parametrize(('coefs', 'tgt'), (
  226. (array([Fraction(1, 2), Fraction(3, 4)], dtype=object), (
  227. "1/2 + 3/4·x"
  228. )),
  229. (array([1, 2, Fraction(5, 7)], dtype=object), (
  230. "1 + 2·x + 5/7·x²"
  231. )),
  232. (array([Decimal('1.00'), Decimal('2.2'), 3], dtype=object), (
  233. "1.00 + 2.2·x + 3·x²"
  234. )),
  235. ))
  236. def test_numeric_object_coefficients(coefs, tgt):
  237. p = poly.Polynomial(coefs)
  238. poly.set_default_printstyle('unicode')
  239. assert_equal(str(p), tgt)
  240. @pytest.mark.parametrize(('coefs', 'tgt'), (
  241. (array([1, 2, 'f'], dtype=object), '1 + 2·x + f·x²'),
  242. (array([1, 2, [3, 4]], dtype=object), '1 + 2·x + [3, 4]·x²'),
  243. ))
  244. def test_nonnumeric_object_coefficients(coefs, tgt):
  245. """
  246. Test coef fallback for object arrays of non-numeric coefficients.
  247. """
  248. p = poly.Polynomial(coefs)
  249. poly.set_default_printstyle('unicode')
  250. assert_equal(str(p), tgt)
  251. class TestFormat:
  252. def test_format_unicode(self):
  253. poly.set_default_printstyle('ascii')
  254. p = poly.Polynomial([1, 2, 0, -1])
  255. assert_equal(format(p, 'unicode'), "1.0 + 2.0·x + 0.0·x² - 1.0·x³")
  256. def test_format_ascii(self):
  257. poly.set_default_printstyle('unicode')
  258. p = poly.Polynomial([1, 2, 0, -1])
  259. assert_equal(
  260. format(p, 'ascii'), "1.0 + 2.0 x + 0.0 x**2 - 1.0 x**3"
  261. )
  262. def test_empty_formatstr(self):
  263. poly.set_default_printstyle('ascii')
  264. p = poly.Polynomial([1, 2, 3])
  265. assert_equal(format(p), "1.0 + 2.0 x + 3.0 x**2")
  266. assert_equal(f"{p}", "1.0 + 2.0 x + 3.0 x**2")
  267. def test_bad_formatstr(self):
  268. p = poly.Polynomial([1, 2, 0, -1])
  269. with pytest.raises(ValueError):
  270. format(p, '.2f')
  271. @pytest.mark.parametrize(('poly', 'tgt'), (
  272. (poly.Polynomial, '1.0 + 2.0·z + 3.0·z²'),
  273. (poly.Chebyshev, '1.0 + 2.0·T₁(z) + 3.0·T₂(z)'),
  274. (poly.Hermite, '1.0 + 2.0·H₁(z) + 3.0·H₂(z)'),
  275. (poly.HermiteE, '1.0 + 2.0·He₁(z) + 3.0·He₂(z)'),
  276. (poly.Laguerre, '1.0 + 2.0·L₁(z) + 3.0·L₂(z)'),
  277. (poly.Legendre, '1.0 + 2.0·P₁(z) + 3.0·P₂(z)'),
  278. ))
  279. def test_symbol(poly, tgt):
  280. p = poly([1, 2, 3], symbol='z')
  281. assert_equal(f"{p:unicode}", tgt)
  282. class TestRepr:
  283. def test_polynomial_str(self):
  284. res = repr(poly.Polynomial([0, 1]))
  285. tgt = (
  286. "Polynomial([0., 1.], domain=[-1, 1], window=[-1, 1], "
  287. "symbol='x')"
  288. )
  289. assert_equal(res, tgt)
  290. def test_chebyshev_str(self):
  291. res = repr(poly.Chebyshev([0, 1]))
  292. tgt = (
  293. "Chebyshev([0., 1.], domain=[-1, 1], window=[-1, 1], "
  294. "symbol='x')"
  295. )
  296. assert_equal(res, tgt)
  297. def test_legendre_repr(self):
  298. res = repr(poly.Legendre([0, 1]))
  299. tgt = (
  300. "Legendre([0., 1.], domain=[-1, 1], window=[-1, 1], "
  301. "symbol='x')"
  302. )
  303. assert_equal(res, tgt)
  304. def test_hermite_repr(self):
  305. res = repr(poly.Hermite([0, 1]))
  306. tgt = (
  307. "Hermite([0., 1.], domain=[-1, 1], window=[-1, 1], "
  308. "symbol='x')"
  309. )
  310. assert_equal(res, tgt)
  311. def test_hermiteE_repr(self):
  312. res = repr(poly.HermiteE([0, 1]))
  313. tgt = (
  314. "HermiteE([0., 1.], domain=[-1, 1], window=[-1, 1], "
  315. "symbol='x')"
  316. )
  317. assert_equal(res, tgt)
  318. def test_laguerre_repr(self):
  319. res = repr(poly.Laguerre([0, 1]))
  320. tgt = (
  321. "Laguerre([0., 1.], domain=[0, 1], window=[0, 1], "
  322. "symbol='x')"
  323. )
  324. assert_equal(res, tgt)
  325. class TestLatexRepr:
  326. """Test the latex repr used by Jupyter"""
  327. def as_latex(self, obj):
  328. # right now we ignore the formatting of scalars in our tests, since
  329. # it makes them too verbose. Ideally, the formatting of scalars will
  330. # be fixed such that tests below continue to pass
  331. obj._repr_latex_scalar = lambda x, parens=False: str(x)
  332. try:
  333. return obj._repr_latex_()
  334. finally:
  335. del obj._repr_latex_scalar
  336. def test_simple_polynomial(self):
  337. # default input
  338. p = poly.Polynomial([1, 2, 3])
  339. assert_equal(self.as_latex(p),
  340. r'$x \mapsto 1.0 + 2.0\,x + 3.0\,x^{2}$')
  341. # translated input
  342. p = poly.Polynomial([1, 2, 3], domain=[-2, 0])
  343. assert_equal(self.as_latex(p),
  344. r'$x \mapsto 1.0 + 2.0\,\left(1.0 + x\right) + 3.0\,\left(1.0 + x\right)^{2}$')
  345. # scaled input
  346. p = poly.Polynomial([1, 2, 3], domain=[-0.5, 0.5])
  347. assert_equal(self.as_latex(p),
  348. r'$x \mapsto 1.0 + 2.0\,\left(2.0x\right) + 3.0\,\left(2.0x\right)^{2}$')
  349. # affine input
  350. p = poly.Polynomial([1, 2, 3], domain=[-1, 0])
  351. assert_equal(self.as_latex(p),
  352. r'$x \mapsto 1.0 + 2.0\,\left(1.0 + 2.0x\right) + 3.0\,\left(1.0 + 2.0x\right)^{2}$')
  353. def test_basis_func(self):
  354. p = poly.Chebyshev([1, 2, 3])
  355. assert_equal(self.as_latex(p),
  356. r'$x \mapsto 1.0\,{T}_{0}(x) + 2.0\,{T}_{1}(x) + 3.0\,{T}_{2}(x)$')
  357. # affine input - check no surplus parens are added
  358. p = poly.Chebyshev([1, 2, 3], domain=[-1, 0])
  359. assert_equal(self.as_latex(p),
  360. r'$x \mapsto 1.0\,{T}_{0}(1.0 + 2.0x) + 2.0\,{T}_{1}(1.0 + 2.0x) + 3.0\,{T}_{2}(1.0 + 2.0x)$')
  361. def test_multichar_basis_func(self):
  362. p = poly.HermiteE([1, 2, 3])
  363. assert_equal(self.as_latex(p),
  364. r'$x \mapsto 1.0\,{He}_{0}(x) + 2.0\,{He}_{1}(x) + 3.0\,{He}_{2}(x)$')
  365. def test_symbol_basic(self):
  366. # default input
  367. p = poly.Polynomial([1, 2, 3], symbol='z')
  368. assert_equal(self.as_latex(p),
  369. r'$z \mapsto 1.0 + 2.0\,z + 3.0\,z^{2}$')
  370. # translated input
  371. p = poly.Polynomial([1, 2, 3], domain=[-2, 0], symbol='z')
  372. assert_equal(
  373. self.as_latex(p),
  374. (
  375. r'$z \mapsto 1.0 + 2.0\,\left(1.0 + z\right) + 3.0\,'
  376. r'\left(1.0 + z\right)^{2}$'
  377. ),
  378. )
  379. # scaled input
  380. p = poly.Polynomial([1, 2, 3], domain=[-0.5, 0.5], symbol='z')
  381. assert_equal(
  382. self.as_latex(p),
  383. (
  384. r'$z \mapsto 1.0 + 2.0\,\left(2.0z\right) + 3.0\,'
  385. r'\left(2.0z\right)^{2}$'
  386. ),
  387. )
  388. # affine input
  389. p = poly.Polynomial([1, 2, 3], domain=[-1, 0], symbol='z')
  390. assert_equal(
  391. self.as_latex(p),
  392. (
  393. r'$z \mapsto 1.0 + 2.0\,\left(1.0 + 2.0z\right) + 3.0\,'
  394. r'\left(1.0 + 2.0z\right)^{2}$'
  395. ),
  396. )
  397. SWITCH_TO_EXP = (
  398. '1.0 + (1.0e-01) x + (1.0e-02) x**2',
  399. '1.2 + (1.2e-01) x + (1.2e-02) x**2',
  400. '1.23 + 0.12 x + (1.23e-02) x**2 + (1.23e-03) x**3',
  401. '1.235 + 0.123 x + (1.235e-02) x**2 + (1.235e-03) x**3',
  402. '1.2346 + 0.1235 x + 0.0123 x**2 + (1.2346e-03) x**3 + (1.2346e-04) x**4',
  403. '1.23457 + 0.12346 x + 0.01235 x**2 + (1.23457e-03) x**3 + '
  404. '(1.23457e-04) x**4',
  405. '1.234568 + 0.123457 x + 0.012346 x**2 + 0.001235 x**3 + '
  406. '(1.234568e-04) x**4 + (1.234568e-05) x**5',
  407. '1.2345679 + 0.1234568 x + 0.0123457 x**2 + 0.0012346 x**3 + '
  408. '(1.2345679e-04) x**4 + (1.2345679e-05) x**5')
  409. class TestPrintOptions:
  410. """
  411. Test the output is properly configured via printoptions.
  412. The exponential notation is enabled automatically when the values
  413. are too small or too large.
  414. """
  415. @pytest.fixture(scope='class', autouse=True)
  416. def use_ascii(self):
  417. poly.set_default_printstyle('ascii')
  418. def test_str(self):
  419. p = poly.Polynomial([1/2, 1/7, 1/7*10**8, 1/7*10**9])
  420. assert_equal(str(p), '0.5 + 0.14285714 x + 14285714.28571429 x**2 '
  421. '+ (1.42857143e+08) x**3')
  422. with printoptions(precision=3):
  423. assert_equal(str(p), '0.5 + 0.143 x + 14285714.286 x**2 '
  424. '+ (1.429e+08) x**3')
  425. def test_latex(self):
  426. p = poly.Polynomial([1/2, 1/7, 1/7*10**8, 1/7*10**9])
  427. assert_equal(p._repr_latex_(),
  428. r'$x \mapsto \text{0.5} + \text{0.14285714}\,x + '
  429. r'\text{14285714.28571429}\,x^{2} + '
  430. r'\text{(1.42857143e+08)}\,x^{3}$')
  431. with printoptions(precision=3):
  432. assert_equal(p._repr_latex_(),
  433. r'$x \mapsto \text{0.5} + \text{0.143}\,x + '
  434. r'\text{14285714.286}\,x^{2} + \text{(1.429e+08)}\,x^{3}$')
  435. def test_fixed(self):
  436. p = poly.Polynomial([1/2])
  437. assert_equal(str(p), '0.5')
  438. with printoptions(floatmode='fixed'):
  439. assert_equal(str(p), '0.50000000')
  440. with printoptions(floatmode='fixed', precision=4):
  441. assert_equal(str(p), '0.5000')
  442. def test_switch_to_exp(self):
  443. for i, s in enumerate(SWITCH_TO_EXP):
  444. with printoptions(precision=i):
  445. p = poly.Polynomial([1.23456789*10**-i
  446. for i in range(i//2+3)])
  447. assert str(p).replace('\n', ' ') == s
  448. def test_non_finite(self):
  449. p = poly.Polynomial([nan, inf])
  450. assert str(p) == 'nan + inf x'
  451. assert p._repr_latex_() == r'$x \mapsto \text{nan} + \text{inf}\,x$'
  452. with printoptions(nanstr='NAN', infstr='INF'):
  453. assert str(p) == 'NAN + INF x'
  454. assert p._repr_latex_() == \
  455. r'$x \mapsto \text{NAN} + \text{INF}\,x$'