Skip to content

Commit

Permalink
ENH,TST: Add symbol to _repr_latex_.
Browse files Browse the repository at this point in the history
 * Add tests for symbol in TestLatexRepr
  • Loading branch information
rossbar committed Apr 28, 2022
1 parent 9519056 commit 440ad13
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
10 changes: 5 additions & 5 deletions numpy/polynomial/_polybase.py
Expand Up @@ -438,18 +438,18 @@ def _repr_latex_(self):
# get the scaled argument string to the basis functions
off, scale = self.mapparms()
if off == 0 and scale == 1:
term = 'x'
term = self.symbol
needs_parens = False
elif scale == 1:
term = f"{self._repr_latex_scalar(off)} + x"
term = f"{self._repr_latex_scalar(off)} + {self.symbol}"
needs_parens = True
elif off == 0:
term = f"{self._repr_latex_scalar(scale)}x"
term = f"{self._repr_latex_scalar(scale)}{self.symbol}"
needs_parens = True
else:
term = (
f"{self._repr_latex_scalar(off)} + "
f"{self._repr_latex_scalar(scale)}x"
f"{self._repr_latex_scalar(scale)}{self.symbol}"
)
needs_parens = True

Expand Down Expand Up @@ -485,7 +485,7 @@ def _repr_latex_(self):
# in case somehow there are no coefficients at all
body = '0'

return rf"$x \mapsto {body}$"
return rf"${self.symbol} \mapsto {body}$"



Expand Down
21 changes: 21 additions & 0 deletions numpy/polynomial/tests/test_printing.py
Expand Up @@ -419,3 +419,24 @@ def test_multichar_basis_func(self):
p = poly.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)$')

def test_symbol_basic(self):
# default input
p = poly.Polynomial([1, 2, 3], symbol='z')
assert_equal(self.as_latex(p),
r'$z \mapsto 1.0 + 2.0\,z + 3.0\,z^{2}$')

# translated input
p = poly.Polynomial([1, 2, 3], domain=[-2, 0], symbol='z')
assert_equal(self.as_latex(p),
r'$z \mapsto 1.0 + 2.0\,\left(1.0 + z\right) + 3.0\,\left(1.0 + z\right)^{2}$')

# scaled input
p = poly.Polynomial([1, 2, 3], domain=[-0.5, 0.5], symbol='z')
assert_equal(self.as_latex(p),
r'$z \mapsto 1.0 + 2.0\,\left(2.0z\right) + 3.0\,\left(2.0z\right)^{2}$')

# affine input
p = poly.Polynomial([1, 2, 3], domain=[-1, 0], symbol='z')
assert_equal(self.as_latex(p),
r'$z \mapsto 1.0 + 2.0\,\left(1.0 + 2.0z\right) + 3.0\,\left(1.0 + 2.0z\right)^{2}$')

0 comments on commit 440ad13

Please sign in to comment.