test_printing.py 21 KB

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