summaryrefslogtreecommitdiff
path: root/numpy/polynomial/polyutils.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/polyutils.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/polyutils.py')
-rw-r--r--numpy/polynomial/polyutils.py41
1 files changed, 40 insertions, 1 deletions
diff --git a/numpy/polynomial/polyutils.py b/numpy/polynomial/polyutils.py
index a2bc75a4d..482913892 100644
--- a/numpy/polynomial/polyutils.py
+++ b/numpy/polynomial/polyutils.py
@@ -32,9 +32,13 @@ import warnings
import numpy as np
+from numpy.core.multiarray import dragon4_positional, dragon4_scientific
+from numpy.core.umath import absolute
+
__all__ = [
'RankWarning', 'as_series', 'trimseq',
- 'trimcoef', 'getdomain', 'mapdomain', 'mapparms']
+ 'trimcoef', 'getdomain', 'mapdomain', 'mapparms',
+ 'format_float']
#
# Warnings and Exceptions
@@ -748,3 +752,38 @@ def _deprecate_as_int(x, desc):
return ix
raise TypeError(f"{desc} must be an integer") from e
+
+
+def format_float(x, parens=False):
+ if not np.issubdtype(type(x), np.floating):
+ return str(x)
+
+ opts = np.get_printoptions()
+
+ if np.isnan(x):
+ return opts['nanstr']
+ elif np.isinf(x):
+ return opts['infstr']
+
+ exp_format = False
+ if x != 0:
+ a = absolute(x)
+ if a >= 1.e8 or a < 10**min(0, -(opts['precision']-1)//2):
+ exp_format = True
+
+ trim, unique = '0', True
+ if opts['floatmode'] == 'fixed':
+ trim, unique = 'k', False
+
+ if exp_format:
+ s = dragon4_scientific(x, precision=opts['precision'],
+ unique=unique, trim=trim,
+ sign=opts['sign'] == '+')
+ if parens:
+ s = '(' + s + ')'
+ else:
+ s = dragon4_positional(x, precision=opts['precision'],
+ fractional=True,
+ unique=unique, trim=trim,
+ sign=opts['sign'] == '+')
+ return s