Skip to content

Commit d1a561a

Browse files
committed
MAIN: Make output of Polynomial representations consistent
1 parent b507488 commit d1a561a

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Modified representation for ``Polynomial``
2+
------------------------------------------
3+
The representation method for `numpy.polynomial.Polynomial` was updated to include the domain in the representation.
4+
The plain text and latex representations are now consistent.
5+
For example the output of ``str(np.polynomial.Polynomial([1, 1], domain=[.1, .2]))`` used to be ``1.0 + 1.0 x``, but
6+
now is ``1.0 + 1.0 (-3.0000000000000004 + 20.0 x)``.

numpy/polynomial/_polybase.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import abc
1111
import numbers
12+
from typing import Callable
1213

1314
import numpy as np
1415
from . import polyutils as pu
@@ -367,6 +368,14 @@ def _generate_string(self, term_method):
367368
if linewidth < 1:
368369
linewidth = 1
369370
out = pu.format_float(self.coef[0])
371+
372+
off, scale = self.mapparms()
373+
374+
scaled_symbol, needs_parens = self._format_term(pu.format_float,
375+
off, scale)
376+
if needs_parens:
377+
scaled_symbol = '(' + scaled_symbol + ')'
378+
370379
for i, coef in enumerate(self.coef[1:]):
371380
out += " "
372381
power = str(i + 1)
@@ -376,13 +385,13 @@ def _generate_string(self, term_method):
376385
# complex). In this case, represent the coefficient as-is.
377386
try:
378387
if coef >= 0:
379-
next_term = f"+ " + pu.format_float(coef, parens=True)
388+
next_term = "+ " + pu.format_float(coef, parens=True)
380389
else:
381-
next_term = f"- " + pu.format_float(-coef, parens=True)
390+
next_term = "- " + pu.format_float(-coef, parens=True)
382391
except TypeError:
383392
next_term = f"+ {coef}"
384393
# Polynomial term
385-
next_term += term_method(power, self.symbol)
394+
next_term += term_method(power, scaled_symbol)
386395
# Length of the current line with next term added
387396
line_len = len(out.split('\n')[-1]) + len(next_term)
388397
# If not the last term in the polynomial, it will be two
@@ -437,24 +446,30 @@ def _repr_latex_scalar(x, parens=False):
437446
# exponents in this function
438447
return r'\text{{{}}}'.format(pu.format_float(x, parens=parens))
439448

440-
def _repr_latex_(self):
441-
# get the scaled argument string to the basis functions
442-
off, scale = self.mapparms()
449+
def _format_term(self, scalar_format: Callable, off: float, scale: float):
450+
""" Format a single term in the expansion """
443451
if off == 0 and scale == 1:
444452
term = self.symbol
445453
needs_parens = False
446454
elif scale == 1:
447-
term = f"{self._repr_latex_scalar(off)} + {self.symbol}"
455+
term = f"{scalar_format(off)} + {self.symbol}"
448456
needs_parens = True
449457
elif off == 0:
450-
term = f"{self._repr_latex_scalar(scale)}{self.symbol}"
458+
term = f"{scalar_format(scale)}{self.symbol}"
451459
needs_parens = True
452460
else:
453461
term = (
454-
f"{self._repr_latex_scalar(off)} + "
455-
f"{self._repr_latex_scalar(scale)}{self.symbol}"
462+
f"{scalar_format(off)} + "
463+
f"{scalar_format(scale)}{self.symbol}"
456464
)
457465
needs_parens = True
466+
return term, needs_parens
467+
468+
def _repr_latex_(self):
469+
# get the scaled argument string to the basis functions
470+
off, scale = self.mapparms()
471+
term, needs_parens = self._format_term(self._repr_latex_scalar,
472+
off, scale)
458473

459474
mute = r"\color{{LightGray}}{{{}}}".format
460475

numpy/polynomial/tests/test_printing.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,27 @@ def test_symbol(poly, tgt):
323323
assert_equal(f"{p:unicode}", tgt)
324324

325325

326-
class TestRepr:
326+
class TestStr:
327327
def test_polynomial_str(self):
328+
res = str(poly.Polynomial([0, 1]))
329+
tgt = '0.0 + 1.0 x'
330+
assert_equal(res, tgt)
331+
332+
res = str(poly.Polynomial([0, 1], domain=[0, 2]))
333+
tgt = '0.0 + 1.0 (-1.0 + x)'
334+
assert_equal(res, tgt)
335+
336+
337+
class TestRepr:
338+
def test_polynomial_repr(self):
328339
res = repr(poly.Polynomial([0, 1]))
329340
tgt = (
330341
"Polynomial([0., 1.], domain=[-1, 1], window=[-1, 1], "
331342
"symbol='x')"
332343
)
333344
assert_equal(res, tgt)
334345

335-
def test_chebyshev_str(self):
346+
def test_chebyshev_repr(self):
336347
res = repr(poly.Chebyshev([0, 1]))
337348
tgt = (
338349
"Chebyshev([0., 1.], domain=[-1, 1], window=[-1, 1], "

0 commit comments

Comments
 (0)