summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Ritter-Gogerly <anton@antonviktor.com>2020-01-26 14:12:31 +1100
committerAnton Ritter-Gogerly <anton@antonviktor.com>2020-01-27 09:20:55 +1100
commit764c25bb8a9e5be60455ac7d6947318efbbb93aa (patch)
tree16f4f9b133ac94139f1abf398ef0dae55841f767
parent9b7e890b014f6ad3b4288246f0565f7afd6eca84 (diff)
downloadnumpy-764c25bb8a9e5be60455ac7d6947318efbbb93aa.tar.gz
Updated files in polynomial/ to use fstrings
-rw-r--r--numpy/polynomial/_polybase.py42
-rw-r--r--numpy/polynomial/polynomial.py4
-rw-r--r--numpy/polynomial/polyutils.py11
-rw-r--r--numpy/polynomial/tests/test_chebyshev.py12
-rw-r--r--numpy/polynomial/tests/test_classes.py2
-rw-r--r--numpy/polynomial/tests/test_hermite.py12
-rw-r--r--numpy/polynomial/tests/test_hermite_e.py12
-rw-r--r--numpy/polynomial/tests/test_laguerre.py12
-rw-r--r--numpy/polynomial/tests/test_legendre.py12
-rw-r--r--numpy/polynomial/tests/test_polynomial.py10
10 files changed, 61 insertions, 68 deletions
diff --git a/numpy/polynomial/_polybase.py b/numpy/polynomial/_polybase.py
index 28bd50ec6..3e16e8d8c 100644
--- a/numpy/polynomial/_polybase.py
+++ b/numpy/polynomial/_polybase.py
@@ -277,18 +277,16 @@ class ABCPolyBase(abc.ABC):
self.window = window
def __repr__(self):
- format = "%s(%s, domain=%s, window=%s)"
coef = repr(self.coef)[6:-1]
domain = repr(self.domain)[6:-1]
window = repr(self.window)[6:-1]
name = self.__class__.__name__
- return format % (name, coef, domain, window)
+ return f"{name}({coef}, domain={domain}, window={window})"
def __str__(self):
- format = "%s(%s)"
coef = str(self.coef)
name = self.nickname
- return format % (name, coef)
+ return f"{name}({coef})"
@classmethod
def _repr_latex_term(cls, i, arg_str, needs_parens):
@@ -297,9 +295,7 @@ class ABCPolyBase(abc.ABC):
"Subclasses must define either a basis name, or override "
"_repr_latex_term(i, arg_str, needs_parens)")
# since we always add parens, we don't care if the expression needs them
- return "{{{basis}}}_{{{i}}}({arg_str})".format(
- basis=cls.basis_name, i=i, arg_str=arg_str
- )
+ return f"{{{cls.basis_name}}}_{{{i}}}({arg_str})"
@staticmethod
def _repr_latex_scalar(x):
@@ -314,19 +310,15 @@ class ABCPolyBase(abc.ABC):
term = 'x'
needs_parens = False
elif scale == 1:
- term = '{} + x'.format(
- self._repr_latex_scalar(off)
- )
+ term = f"{self._repr_latex_scalar(off)} + x"
needs_parens = True
elif off == 0:
- term = '{}x'.format(
- self._repr_latex_scalar(scale)
- )
+ term = f"{self._repr_latex_scalar(scale)}x"
needs_parens = True
else:
- term = '{} + {}x'.format(
- self._repr_latex_scalar(off),
- self._repr_latex_scalar(scale)
+ term = (
+ f"{self._repr_latex_scalar(off)} + "
+ f"{self._repr_latex_scalar(scale)}x"
)
needs_parens = True
@@ -336,20 +328,20 @@ class ABCPolyBase(abc.ABC):
for i, c in enumerate(self.coef):
# prevent duplication of + and - signs
if i == 0:
- coef_str = '{}'.format(self._repr_latex_scalar(c))
+ coef_str = f"{self._repr_latex_scalar(c)}"
elif not isinstance(c, numbers.Real):
- coef_str = ' + ({})'.format(self._repr_latex_scalar(c))
+ coef_str = f" + ({self._repr_latex_scalar(c)})"
elif not np.signbit(c):
- coef_str = ' + {}'.format(self._repr_latex_scalar(c))
+ coef_str = f" + {self._repr_latex_scalar(c)}"
else:
- coef_str = ' - {}'.format(self._repr_latex_scalar(-c))
+ coef_str = f" - {self._repr_latex_scalar(-c)}"
# produce the string for the term
term_str = self._repr_latex_term(i, term, needs_parens)
if term_str == '1':
part = coef_str
else:
- part = r'{}\,{}'.format(coef_str, term_str)
+ part = rf"{coef_str}\,{term_str}"
if c == 0:
part = mute(part)
@@ -362,7 +354,7 @@ class ABCPolyBase(abc.ABC):
# in case somehow there are no coefficients at all
body = '0'
- return r'$x \mapsto {}$'.format(body)
+ return rf"$x \mapsto {body}$"
@@ -432,8 +424,10 @@ class ABCPolyBase(abc.ABC):
# could return the first n elements of an infinite series.
# It is hard to see where n would come from, though.
if not isinstance(other, numbers.Number) or isinstance(other, bool):
- form = "unsupported types for true division: '%s', '%s'"
- raise TypeError(form % (type(self), type(other)))
+ raise TypeError(
+ f"unsupported types for true division: "
+ f"'{type(self)}', '{type(other)}'"
+ )
return self.__floordiv__(other)
def __floordiv__(self, other):
diff --git a/numpy/polynomial/polynomial.py b/numpy/polynomial/polynomial.py
index 94c722628..97f66a152 100644
--- a/numpy/polynomial/polynomial.py
+++ b/numpy/polynomial/polynomial.py
@@ -1481,10 +1481,10 @@ class Polynomial(ABCPolyBase):
@staticmethod
def _repr_latex_term(i, arg_str, needs_parens):
if needs_parens:
- arg_str = r'\left({}\right)'.format(arg_str)
+ arg_str = rf"\left({arg_str}\right)"
if i == 0:
return '1'
elif i == 1:
return arg_str
else:
- return '{}^{{{}}}'.format(arg_str, i)
+ return f"{arg_str}^{{{i}}}"
diff --git a/numpy/polynomial/polyutils.py b/numpy/polynomial/polyutils.py
index 9b8e9fc42..ec7ba6f1d 100644
--- a/numpy/polynomial/polyutils.py
+++ b/numpy/polynomial/polyutils.py
@@ -468,10 +468,10 @@ def _vander_nd(vander_fs, points, degrees):
n_dims = len(vander_fs)
if n_dims != len(points):
raise ValueError(
- "Expected {} dimensions of sample points, got {}".format(n_dims, len(points)))
+ f"Expected {n_dims} dimensions of sample points, got {len(points)}")
if n_dims != len(degrees):
raise ValueError(
- "Expected {} dimensions of degrees, got {}".format(n_dims, len(degrees)))
+ f"Expected {n_dims} dimensions of degrees, got {len(degrees)}")
if n_dims == 0:
raise ValueError("Unable to guess a dtype or shape when no points are given")
@@ -786,12 +786,11 @@ def _deprecate_as_int(x, desc):
else:
if ix == x:
warnings.warn(
- "In future, this will raise TypeError, as {} will need to "
- "be an integer not just an integral float."
- .format(desc),
+ f"In future, this will raise TypeError, as {desc} will "
+ "need to be an integer not just an integral float.",
DeprecationWarning,
stacklevel=3
)
return ix
- raise TypeError("{} must be an integer".format(desc))
+ raise TypeError(f"{desc} must be an integer")
diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py
index ec0a7839a..2f54bebfd 100644
--- a/numpy/polynomial/tests/test_chebyshev.py
+++ b/numpy/polynomial/tests/test_chebyshev.py
@@ -65,7 +65,7 @@ class TestArithmetic:
def test_chebadd(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] += 1
@@ -75,7 +75,7 @@ class TestArithmetic:
def test_chebsub(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] -= 1
@@ -93,7 +93,7 @@ class TestArithmetic:
def test_chebmul(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(i + j + 1)
tgt[i + j] += .5
tgt[abs(i - j)] += .5
@@ -103,7 +103,7 @@ class TestArithmetic:
def test_chebdiv(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
ci = [0]*i + [1]
cj = [0]*j + [1]
tgt = cheb.chebadd(ci, cj)
@@ -114,7 +114,7 @@ class TestArithmetic:
def test_chebpow(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
c = np.arange(i + 1)
tgt = reduce(cheb.chebmul, [c]*j, np.array([1]))
res = cheb.chebpow(c, j)
@@ -139,7 +139,7 @@ class TestEvaluation:
x = np.linspace(-1, 1)
y = [polyval(x, c) for c in Tlist]
for i in range(10):
- msg = "At i=%d" % i
+ msg = f"At i={i}"
tgt = y[i]
res = cheb.chebval(x, [0]*i + [1])
assert_almost_equal(res, tgt, err_msg=msg)
diff --git a/numpy/polynomial/tests/test_classes.py b/numpy/polynomial/tests/test_classes.py
index 68656bc98..a2682bd36 100644
--- a/numpy/polynomial/tests/test_classes.py
+++ b/numpy/polynomial/tests/test_classes.py
@@ -42,7 +42,7 @@ def assert_poly_almost_equal(p1, p2, msg=""):
assert_(np.all(p1.window == p2.window))
assert_almost_equal(p1.coef, p2.coef)
except AssertionError:
- msg = "Result: %s\nTarget: %s", (p1, p2)
+ msg = f"Result: {p1}\nTarget: {p2}"
raise AssertionError(msg)
diff --git a/numpy/polynomial/tests/test_hermite.py b/numpy/polynomial/tests/test_hermite.py
index 4b67c1b18..53ee0844e 100644
--- a/numpy/polynomial/tests/test_hermite.py
+++ b/numpy/polynomial/tests/test_hermite.py
@@ -49,7 +49,7 @@ class TestArithmetic:
def test_hermadd(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] += 1
@@ -59,7 +59,7 @@ class TestArithmetic:
def test_hermsub(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] -= 1
@@ -80,7 +80,7 @@ class TestArithmetic:
pol1 = [0]*i + [1]
val1 = herm.hermval(self.x, pol1)
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
pol2 = [0]*j + [1]
val2 = herm.hermval(self.x, pol2)
pol3 = herm.hermmul(pol1, pol2)
@@ -91,7 +91,7 @@ class TestArithmetic:
def test_hermdiv(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
ci = [0]*i + [1]
cj = [0]*j + [1]
tgt = herm.hermadd(ci, cj)
@@ -102,7 +102,7 @@ class TestArithmetic:
def test_hermpow(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
c = np.arange(i + 1)
tgt = reduce(herm.hermmul, [c]*j, np.array([1]))
res = herm.hermpow(c, j)
@@ -127,7 +127,7 @@ class TestEvaluation:
x = np.linspace(-1, 1)
y = [polyval(x, c) for c in Hlist]
for i in range(10):
- msg = "At i=%d" % i
+ msg = f"At i={i}"
tgt = y[i]
res = herm.hermval(x, [0]*i + [1])
assert_almost_equal(res, tgt, err_msg=msg)
diff --git a/numpy/polynomial/tests/test_hermite_e.py b/numpy/polynomial/tests/test_hermite_e.py
index 3052500cc..2d262a330 100644
--- a/numpy/polynomial/tests/test_hermite_e.py
+++ b/numpy/polynomial/tests/test_hermite_e.py
@@ -49,7 +49,7 @@ class TestArithmetic:
def test_hermeadd(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] += 1
@@ -59,7 +59,7 @@ class TestArithmetic:
def test_hermesub(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] -= 1
@@ -80,7 +80,7 @@ class TestArithmetic:
pol1 = [0]*i + [1]
val1 = herme.hermeval(self.x, pol1)
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
pol2 = [0]*j + [1]
val2 = herme.hermeval(self.x, pol2)
pol3 = herme.hermemul(pol1, pol2)
@@ -91,7 +91,7 @@ class TestArithmetic:
def test_hermediv(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
ci = [0]*i + [1]
cj = [0]*j + [1]
tgt = herme.hermeadd(ci, cj)
@@ -102,7 +102,7 @@ class TestArithmetic:
def test_hermepow(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
c = np.arange(i + 1)
tgt = reduce(herme.hermemul, [c]*j, np.array([1]))
res = herme.hermepow(c, j)
@@ -127,7 +127,7 @@ class TestEvaluation:
x = np.linspace(-1, 1)
y = [polyval(x, c) for c in Helist]
for i in range(10):
- msg = "At i=%d" % i
+ msg = f"At i={i}"
tgt = y[i]
res = herme.hermeval(x, [0]*i + [1])
assert_almost_equal(res, tgt, err_msg=msg)
diff --git a/numpy/polynomial/tests/test_laguerre.py b/numpy/polynomial/tests/test_laguerre.py
index ec103c258..227ef3c55 100644
--- a/numpy/polynomial/tests/test_laguerre.py
+++ b/numpy/polynomial/tests/test_laguerre.py
@@ -46,7 +46,7 @@ class TestArithmetic:
def test_lagadd(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] += 1
@@ -56,7 +56,7 @@ class TestArithmetic:
def test_lagsub(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] -= 1
@@ -77,7 +77,7 @@ class TestArithmetic:
pol1 = [0]*i + [1]
val1 = lag.lagval(self.x, pol1)
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
pol2 = [0]*j + [1]
val2 = lag.lagval(self.x, pol2)
pol3 = lag.lagmul(pol1, pol2)
@@ -88,7 +88,7 @@ class TestArithmetic:
def test_lagdiv(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
ci = [0]*i + [1]
cj = [0]*j + [1]
tgt = lag.lagadd(ci, cj)
@@ -99,7 +99,7 @@ class TestArithmetic:
def test_lagpow(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
c = np.arange(i + 1)
tgt = reduce(lag.lagmul, [c]*j, np.array([1]))
res = lag.lagpow(c, j)
@@ -124,7 +124,7 @@ class TestEvaluation:
x = np.linspace(-1, 1)
y = [polyval(x, c) for c in Llist]
for i in range(7):
- msg = "At i=%d" % i
+ msg = f"At i={i}"
tgt = y[i]
res = lag.lagval(x, [0]*i + [1])
assert_almost_equal(res, tgt, err_msg=msg)
diff --git a/numpy/polynomial/tests/test_legendre.py b/numpy/polynomial/tests/test_legendre.py
index 8846ca6f2..a2a212c24 100644
--- a/numpy/polynomial/tests/test_legendre.py
+++ b/numpy/polynomial/tests/test_legendre.py
@@ -49,7 +49,7 @@ class TestArithmetic:
def test_legadd(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] += 1
@@ -59,7 +59,7 @@ class TestArithmetic:
def test_legsub(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] -= 1
@@ -81,7 +81,7 @@ class TestArithmetic:
pol1 = [0]*i + [1]
val1 = leg.legval(self.x, pol1)
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
pol2 = [0]*j + [1]
val2 = leg.legval(self.x, pol2)
pol3 = leg.legmul(pol1, pol2)
@@ -92,7 +92,7 @@ class TestArithmetic:
def test_legdiv(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
ci = [0]*i + [1]
cj = [0]*j + [1]
tgt = leg.legadd(ci, cj)
@@ -103,7 +103,7 @@ class TestArithmetic:
def test_legpow(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
c = np.arange(i + 1)
tgt = reduce(leg.legmul, [c]*j, np.array([1]))
res = leg.legpow(c, j)
@@ -128,7 +128,7 @@ class TestEvaluation:
x = np.linspace(-1, 1)
y = [polyval(x, c) for c in Llist]
for i in range(10):
- msg = "At i=%d" % i
+ msg = f"At i={i}"
tgt = y[i]
res = leg.legval(x, [0]*i + [1])
assert_almost_equal(res, tgt, err_msg=msg)
diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py
index 50973c480..5fd1a82a2 100644
--- a/numpy/polynomial/tests/test_polynomial.py
+++ b/numpy/polynomial/tests/test_polynomial.py
@@ -47,7 +47,7 @@ class TestArithmetic:
def test_polyadd(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] += 1
@@ -57,7 +57,7 @@ class TestArithmetic:
def test_polysub(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(max(i, j) + 1)
tgt[i] += 1
tgt[j] -= 1
@@ -75,7 +75,7 @@ class TestArithmetic:
def test_polymul(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
tgt = np.zeros(i + j + 1)
tgt[i + j] += 1
res = poly.polymul([0]*i + [1], [0]*j + [1])
@@ -94,7 +94,7 @@ class TestArithmetic:
# check rest.
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
ci = [0]*i + [1, 2]
cj = [0]*j + [1, 2]
tgt = poly.polyadd(ci, cj)
@@ -105,7 +105,7 @@ class TestArithmetic:
def test_polypow(self):
for i in range(5):
for j in range(5):
- msg = "At i=%d, j=%d" % (i, j)
+ msg = f"At i={i}, j={j}"
c = np.arange(i + 1)
tgt = reduce(poly.polymul, [c]*j, np.array([1]))
res = poly.polypow(c, j)