summaryrefslogtreecommitdiff
path: root/numpy/polynomial/tests/test_classes.py
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2018-07-06 22:12:02 -0700
committerEric Wieser <wieser.eric@gmail.com>2018-08-12 14:27:39 -0700
commite6e60c02e2c8833e45eab575732011e75b5b7c73 (patch)
tree899626310a37207f6993cffbfd33cc8760e3760e /numpy/polynomial/tests/test_classes.py
parent739443679b50b43c34808b8fb767bac643fcd91d (diff)
downloadnumpy-e6e60c02e2c8833e45eab575732011e75b5b7c73.tar.gz
ENH: Add support for ipython latex printing to polynomial
Choices made, and the alternatives rejected (for no particularly strong reason): 1. Show terms in ascending order, to match their internal representation * alternative: descending, to match convention 2. Shows 0 terms in gray * alternative: omit entirely * alternative: show normally to aid comparison 3. Write each term as `basis(ax + b) * alternative: write as `basis(u) ... where u = ax + b` * alternative: show the normalized polynomial In future it would perhaps make sense to expose these options to the end user
Diffstat (limited to 'numpy/polynomial/tests/test_classes.py')
-rw-r--r--numpy/polynomial/tests/test_classes.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/numpy/polynomial/tests/test_classes.py b/numpy/polynomial/tests/test_classes.py
index 738741668..15e24f92b 100644
--- a/numpy/polynomial/tests/test_classes.py
+++ b/numpy/polynomial/tests/test_classes.py
@@ -562,6 +562,56 @@ def test_ufunc_override(Poly):
assert_raises(TypeError, np.add, x, p)
+
+class TestLatexRepr(object):
+ """Test the latex repr used by ipython """
+
+ def as_latex(self, obj):
+ # right now we ignore the formatting of scalars in our tests, since
+ # it makes them too verbose. Ideally, the formatting of scalars will
+ # be fixed such that tests below continue to pass
+ obj._repr_latex_scalar = lambda x: str(x)
+ try:
+ return obj._repr_latex_()
+ finally:
+ del obj._repr_latex_scalar
+
+ def test_simple_polynomial(self):
+ # default input
+ p = Polynomial([1, 2, 3])
+ assert_equal(self.as_latex(p),
+ r'$x \mapsto 1.0 + 2.0\,x + 3.0\,x^{2}$')
+
+ # translated input
+ p = Polynomial([1, 2, 3], domain=[-2, 0])
+ assert_equal(self.as_latex(p),
+ r'$x \mapsto 1.0 + 2.0\,\left(1.0 + x\right) + 3.0\,\left(1.0 + x\right)^{2}$')
+
+ # scaled input
+ p = Polynomial([1, 2, 3], domain=[-0.5, 0.5])
+ assert_equal(self.as_latex(p),
+ r'$x \mapsto 1.0 + 2.0\,\left(2.0x\right) + 3.0\,\left(2.0x\right)^{2}$')
+
+ # affine input
+ p = Polynomial([1, 2, 3], domain=[-1, 0])
+ assert_equal(self.as_latex(p),
+ r'$x \mapsto 1.0 + 2.0\,\left(1.0 + 2.0x\right) + 3.0\,\left(1.0 + 2.0x\right)^{2}$')
+
+ def test_basis_func(self):
+ p = Chebyshev([1, 2, 3])
+ assert_equal(self.as_latex(p),
+ r'$x \mapsto 1.0\,{T}_{0}(x) + 2.0\,{T}_{1}(x) + 3.0\,{T}_{2}(x)$')
+ # affine input - check no surplus parens are added
+ p = Chebyshev([1, 2, 3], domain=[-1, 0])
+ assert_equal(self.as_latex(p),
+ 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)$')
+
+ def test_multichar_basis_func(self):
+ p = HermiteE([1, 2, 3])
+ assert_equal(self.as_latex(p),
+ r'$x \mapsto 1.0\,{He}_{0}(x) + 2.0\,{He}_{1}(x) + 3.0\,{He}_{2}(x)$')
+
+
#
# Test class method that only exists for some classes
#