summaryrefslogtreecommitdiff
path: root/numpy/polynomial/_polybase.py
diff options
context:
space:
mode:
authorLev Maximov <axil.github@gmail.com>2022-06-14 23:58:32 +0700
committerGitHub <noreply@github.com>2022-06-14 10:58:32 -0600
commita5535dc6242b0decae1e65a3d4feb220fefedc49 (patch)
tree8e6f06fa4430567e9c4b81ff30dc9366ab3f267c /numpy/polynomial/_polybase.py
parent4d07c069422974c425390772c8e19f585a4f0bdf (diff)
downloadnumpy-a5535dc6242b0decae1e65a3d4feb220fefedc49.tar.gz
MAINT: limit the number of decimals in Polynomial representation (#21654)
* limit the number of decimals in Polynomial representation * tests pass * parenthesize exponential notation in polynomials * fixed a long line warning * added polynomial printoptions tests * polynomial printoptions typo fixed * made switch to exp notation in polynomial display more natural * added a test on switching polynomials to exp notation * fixed linter errors/warnings * support for nanstr and infstr printoptions in polynomials * 10^8 threshold for switching to exp notation when displaying polynomials * merged in PR #21696 fixing issue #21695 * made linter happy * made some docstring tests pass * fixed the docs Co-authored-by: Lev Maximov <lev.maximov@gmail.com>
Diffstat (limited to 'numpy/polynomial/_polybase.py')
-rw-r--r--numpy/polynomial/_polybase.py14
1 files changed, 7 insertions, 7 deletions
diff --git a/numpy/polynomial/_polybase.py b/numpy/polynomial/_polybase.py
index 6382732dc..9674dee0b 100644
--- a/numpy/polynomial/_polybase.py
+++ b/numpy/polynomial/_polybase.py
@@ -366,7 +366,7 @@ class ABCPolyBase(abc.ABC):
linewidth = np.get_printoptions().get('linewidth', 75)
if linewidth < 1:
linewidth = 1
- out = f"{self.coef[0]}"
+ out = pu.format_float(self.coef[0])
for i, coef in enumerate(self.coef[1:]):
out += " "
power = str(i + 1)
@@ -376,9 +376,9 @@ class ABCPolyBase(abc.ABC):
# complex). In this case, represent the coefficient as-is.
try:
if coef >= 0:
- next_term = f"+ {coef}"
+ next_term = f"+ " + pu.format_float(coef, parens=True)
else:
- next_term = f"- {-coef}"
+ next_term = f"- " + pu.format_float(-coef, parens=True)
except TypeError:
next_term = f"+ {coef}"
# Polynomial term
@@ -432,10 +432,10 @@ class ABCPolyBase(abc.ABC):
return f"{{{cls.basis_name}}}_{{{i}}}({arg_str})"
@staticmethod
- def _repr_latex_scalar(x):
+ def _repr_latex_scalar(x, parens=False):
# TODO: we're stuck with disabling math formatting until we handle
# exponents in this function
- return r'\text{{{}}}'.format(x)
+ return r'\text{{{}}}'.format(pu.format_float(x, parens=parens))
def _repr_latex_(self):
# get the scaled argument string to the basis functions
@@ -466,9 +466,9 @@ class ABCPolyBase(abc.ABC):
elif not isinstance(c, numbers.Real):
coef_str = f" + ({self._repr_latex_scalar(c)})"
elif not np.signbit(c):
- coef_str = f" + {self._repr_latex_scalar(c)}"
+ coef_str = f" + {self._repr_latex_scalar(c, parens=True)}"
else:
- coef_str = f" - {self._repr_latex_scalar(-c)}"
+ coef_str = f" - {self._repr_latex_scalar(-c, parens=True)}"
# produce the string for the term
term_str = self._repr_latex_term(i, term, needs_parens)