summaryrefslogtreecommitdiff
path: root/numpy/polynomial
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2014-08-04 14:32:34 -0600
committerCharles Harris <charlesr.harris@gmail.com>2014-08-04 14:32:34 -0600
commit192f025ce5280dffbdd361e3ee0645aa08764156 (patch)
tree5d396aa22ec11fcb3000600d8e43d1ee1bfec7f6 /numpy/polynomial
parent74bd706d6fccb0512b276a038274680fd6568b75 (diff)
parentf9c6398d4f021e9509e19157a0d5ee72b27a8c2a (diff)
downloadnumpy-192f025ce5280dffbdd361e3ee0645aa08764156.tar.gz
Merge pull request #4924 from charris/pep8-numpy-polynomial
Pep8 numpy polynomial
Diffstat (limited to 'numpy/polynomial')
-rw-r--r--numpy/polynomial/__init__.py2
-rw-r--r--numpy/polynomial/_polybase.py4
-rw-r--r--numpy/polynomial/chebyshev.py127
-rw-r--r--numpy/polynomial/hermite.py117
-rw-r--r--numpy/polynomial/hermite_e.py118
-rw-r--r--numpy/polynomial/laguerre.py119
-rw-r--r--numpy/polynomial/legendre.py121
-rw-r--r--numpy/polynomial/polynomial.py90
-rw-r--r--numpy/polynomial/polyutils.py68
-rw-r--r--numpy/polynomial/tests/test_chebyshev.py18
-rw-r--r--numpy/polynomial/tests/test_classes.py28
-rw-r--r--numpy/polynomial/tests/test_hermite.py17
-rw-r--r--numpy/polynomial/tests/test_hermite_e.py21
-rw-r--r--numpy/polynomial/tests/test_laguerre.py17
-rw-r--r--numpy/polynomial/tests/test_legendre.py17
-rw-r--r--numpy/polynomial/tests/test_polynomial.py16
-rw-r--r--numpy/polynomial/tests/test_polyutils.py8
17 files changed, 460 insertions, 448 deletions
diff --git a/numpy/polynomial/__init__.py b/numpy/polynomial/__init__.py
index e9ca387c3..1200d1c8d 100644
--- a/numpy/polynomial/__init__.py
+++ b/numpy/polynomial/__init__.py
@@ -15,8 +15,6 @@ information can be found in the docstring for the module of interest.
"""
from __future__ import division, absolute_import, print_function
-import warnings
-
from .polynomial import Polynomial
from .chebyshev import Chebyshev
from .legendre import Legendre
diff --git a/numpy/polynomial/_polybase.py b/numpy/polynomial/_polybase.py
index 23608c74a..234b509aa 100644
--- a/numpy/polynomial/_polybase.py
+++ b/numpy/polynomial/_polybase.py
@@ -374,7 +374,7 @@ class ABCPolyBase(object):
return quo, rem
def __pow__(self, other):
- coef = self._pow(self.coef, other, maxpower = self.maxpower)
+ coef = self._pow(self.coef, other, maxpower=self.maxpower)
res = self.__class__(coef, self.domain, self.window)
return res
@@ -721,8 +721,6 @@ class ABCPolyBase(object):
y = self(x)
return x, y
-
-
@classmethod
def fit(cls, x, y, deg, domain=None, rcond=None, full=False, w=None,
window=None):
diff --git a/numpy/polynomial/chebyshev.py b/numpy/polynomial/chebyshev.py
index b4acbbeab..f213ab3fd 100644
--- a/numpy/polynomial/chebyshev.py
+++ b/numpy/polynomial/chebyshev.py
@@ -94,13 +94,14 @@ import numpy.linalg as la
from . import polyutils as pu
from ._polybase import ABCPolyBase
-__all__ = ['chebzero', 'chebone', 'chebx', 'chebdomain', 'chebline',
- 'chebadd', 'chebsub', 'chebmulx', 'chebmul', 'chebdiv', 'chebpow',
- 'chebval', 'chebder', 'chebint', 'cheb2poly', 'poly2cheb',
- 'chebfromroots', 'chebvander', 'chebfit', 'chebtrim', 'chebroots',
- 'chebpts1', 'chebpts2', 'Chebyshev', 'chebval2d', 'chebval3d',
- 'chebgrid2d', 'chebgrid3d', 'chebvander2d', 'chebvander3d',
- 'chebcompanion', 'chebgauss', 'chebweight']
+__all__ = [
+ 'chebzero', 'chebone', 'chebx', 'chebdomain', 'chebline', 'chebadd',
+ 'chebsub', 'chebmulx', 'chebmul', 'chebdiv', 'chebpow', 'chebval',
+ 'chebder', 'chebint', 'cheb2poly', 'poly2cheb', 'chebfromroots',
+ 'chebvander', 'chebfit', 'chebtrim', 'chebroots', 'chebpts1',
+ 'chebpts2', 'Chebyshev', 'chebval2d', 'chebval3d', 'chebgrid2d',
+ 'chebgrid3d', 'chebvander2d', 'chebvander3d', 'chebcompanion',
+ 'chebgauss', 'chebweight']
chebtrim = pu.trimcoef
@@ -109,7 +110,7 @@ chebtrim = pu.trimcoef
# functions and do minimal error checking.
#
-def _cseries_to_zseries(c) :
+def _cseries_to_zseries(c):
"""Covert Chebyshev series to z-series.
Covert a Chebyshev series to the equivalent z-series. The result is
@@ -134,7 +135,7 @@ def _cseries_to_zseries(c) :
return zs + zs[::-1]
-def _zseries_to_cseries(zs) :
+def _zseries_to_cseries(zs):
"""Covert z-series to a Chebyshev series.
Covert a z series to the equivalent Chebyshev series. The result is
@@ -159,7 +160,7 @@ def _zseries_to_cseries(zs) :
return c
-def _zseries_mul(z1, z2) :
+def _zseries_mul(z1, z2):
"""Multiply two z-series.
Multiply two z-series to produce a z-series.
@@ -186,7 +187,7 @@ def _zseries_mul(z1, z2) :
return np.convolve(z1, z2)
-def _zseries_div(z1, z2) :
+def _zseries_div(z1, z2):
"""Divide the first z-series by the second.
Divide `z1` by `z2` and return the quotient and remainder as z-series.
@@ -223,19 +224,19 @@ def _zseries_div(z1, z2) :
z2 = z2.copy()
len1 = len(z1)
len2 = len(z2)
- if len2 == 1 :
+ if len2 == 1:
z1 /= z2
return z1, z1[:1]*0
- elif len1 < len2 :
+ elif len1 < len2:
return z1[:1]*0, z1
- else :
+ else:
dlen = len1 - len2
scl = z2[0]
z2 /= scl
quo = np.empty(dlen + 1, dtype=z1.dtype)
i = 0
j = dlen
- while i < j :
+ while i < j:
r = z1[i]
quo[i] = z1[i]
quo[dlen - i] = r
@@ -253,7 +254,7 @@ def _zseries_div(z1, z2) :
return quo, rem
-def _zseries_der(zs) :
+def _zseries_der(zs):
"""Differentiate a z-series.
The derivative is with respect to x, not z. This is achieved using the
@@ -285,7 +286,7 @@ def _zseries_der(zs) :
return d
-def _zseries_int(zs) :
+def _zseries_int(zs):
"""Integrate a z-series.
The integral is with respect to x, not z. This is achieved by a change
@@ -323,7 +324,7 @@ def _zseries_int(zs) :
#
-def poly2cheb(pol) :
+def poly2cheb(pol):
"""
Convert a polynomial to a Chebyshev series.
@@ -368,12 +369,12 @@ def poly2cheb(pol) :
[pol] = pu.as_series([pol])
deg = len(pol) - 1
res = 0
- for i in range(deg, -1, -1) :
+ for i in range(deg, -1, -1):
res = chebadd(chebmulx(res), pol[i])
return res
-def cheb2poly(c) :
+def cheb2poly(c):
"""
Convert a Chebyshev series to a polynomial.
@@ -427,7 +428,7 @@ def cheb2poly(c) :
c0 = c[-2]
c1 = c[-1]
# i is the current degree of c1
- for i in range(n - 1, 1, -1) :
+ for i in range(n - 1, 1, -1):
tmp = c0
c0 = polysub(c[i - 2], c1)
c1 = polyadd(tmp, polymulx(c1)*2)
@@ -452,7 +453,7 @@ chebone = np.array([1])
chebx = np.array([0, 1])
-def chebline(off, scl) :
+def chebline(off, scl):
"""
Chebyshev series whose graph is a straight line.
@@ -482,13 +483,13 @@ def chebline(off, scl) :
-3.0
"""
- if scl != 0 :
+ if scl != 0:
return np.array([off, scl])
- else :
+ else:
return np.array([off])
-def chebfromroots(roots) :
+def chebfromroots(roots):
"""
Generate a Chebyshev series with given roots.
@@ -537,9 +538,9 @@ def chebfromroots(roots) :
array([ 1.5+0.j, 0.0+0.j, 0.5+0.j])
"""
- if len(roots) == 0 :
+ if len(roots) == 0:
return np.ones(1)
- else :
+ else:
[roots] = pu.as_series([roots], trim=False)
roots.sort()
p = [chebline(-r, 1) for r in roots]
@@ -595,10 +596,10 @@ def chebadd(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] += c2
ret = c1
- else :
+ else:
c2[:c1.size] += c1
ret = c2
return pu.trimseq(ret)
@@ -647,10 +648,10 @@ def chebsub(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] -= c2
ret = c1
- else :
+ else:
c2 = -c2
c2[:c1.size] += c1
ret = c2
@@ -794,16 +795,16 @@ def chebdiv(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if c2[-1] == 0 :
+ if c2[-1] == 0:
raise ZeroDivisionError()
lc1 = len(c1)
lc2 = len(c2)
- if lc1 < lc2 :
+ if lc1 < lc2:
return c1[:1]*0, c1
- elif lc2 == 1 :
+ elif lc2 == 1:
return c1/c2[-1], c1[:1]*0
- else :
+ else:
z1 = _cseries_to_zseries(c1)
z2 = _cseries_to_zseries(c2)
quo, rem = _zseries_div(z1, z2)
@@ -812,7 +813,7 @@ def chebdiv(c1, c2):
return quo, rem
-def chebpow(c, pow, maxpower=16) :
+def chebpow(c, pow, maxpower=16):
"""Raise a Chebyshev series to a power.
Returns the Chebyshev series `c` raised to the power `pow`. The
@@ -846,25 +847,25 @@ def chebpow(c, pow, maxpower=16) :
# c is a trimmed copy
[c] = pu.as_series([c])
power = int(pow)
- if power != pow or power < 0 :
+ if power != pow or power < 0:
raise ValueError("Power must be a non-negative integer.")
- elif maxpower is not None and power > maxpower :
+ elif maxpower is not None and power > maxpower:
raise ValueError("Power is too large")
- elif power == 0 :
+ elif power == 0:
return np.array([1], dtype=c.dtype)
- elif power == 1 :
+ elif power == 1:
return c
- else :
+ else:
# This can be made more efficient by using powers of two
# in the usual way.
zs = _cseries_to_zseries(c)
prd = zs
- for i in range(2, power + 1) :
+ for i in range(2, power + 1):
prd = np.convolve(prd, zs)
return _zseries_to_cseries(prd)
-def chebder(c, m=1, scl=1, axis=0) :
+def chebder(c, m=1, scl=1, axis=0):
"""
Differentiate a Chebyshev series.
@@ -1057,9 +1058,9 @@ def chebint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
if cnt != m:
raise ValueError("The order of integration must be integer")
- if cnt < 0 :
+ if cnt < 0:
raise ValueError("The order of integration must be non-negative")
- if len(k) > cnt :
+ if len(k) > cnt:
raise ValueError("Too many integration constants")
if iaxis != axis:
raise ValueError("The axis must be integer")
@@ -1073,7 +1074,7 @@ def chebint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
c = np.rollaxis(c, iaxis)
k = list(k) + [0]*(cnt - len(k))
- for i in range(cnt) :
+ for i in range(cnt):
n = len(c)
c *= scl
if n == 1 and np.all(c[0] == 0):
@@ -1162,19 +1163,19 @@ def chebval(x, c, tensor=True):
if isinstance(x, (tuple, list)):
x = np.asarray(x)
if isinstance(x, np.ndarray) and tensor:
- c = c.reshape(c.shape + (1,)*x.ndim)
+ c = c.reshape(c.shape + (1,)*x.ndim)
- if len(c) == 1 :
+ if len(c) == 1:
c0 = c[0]
c1 = 0
- elif len(c) == 2 :
+ elif len(c) == 2:
c0 = c[0]
c1 = c[1]
- else :
+ else:
x2 = 2*x
c0 = c[-2]
c1 = c[-1]
- for i in range(3, len(c) + 1) :
+ for i in range(3, len(c) + 1):
tmp = c0
c0 = c[-i] - c1
c1 = tmp + c1*x2
@@ -1410,7 +1411,7 @@ def chebgrid3d(x, y, z, c):
return c
-def chebvander(x, deg) :
+def chebvander(x, deg):
"""Pseudo-Vandermonde matrix of given degree.
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
@@ -1457,15 +1458,15 @@ def chebvander(x, deg) :
v = np.empty(dims, dtype=dtyp)
# Use forward recursion to generate the entries.
v[0] = x*0 + 1
- if ideg > 0 :
+ if ideg > 0:
x2 = 2*x
v[1] = x
- for i in range(2, ideg + 1) :
+ for i in range(2, ideg + 1):
v[i] = v[i-1]*x2 - v[i-2]
return np.rollaxis(v, 0, v.ndim)
-def chebvander2d(x, y, deg) :
+def chebvander2d(x, y, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1528,7 +1529,7 @@ def chebvander2d(x, y, deg) :
return v.reshape(v.shape[:-2] + (-1,))
-def chebvander3d(x, y, z, deg) :
+def chebvander3d(x, y, z, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1714,13 +1715,13 @@ def chebfit(x, y, deg, rcond=None, full=False, w=None):
y = np.asarray(y) + 0.0
# check arguments.
- if deg < 0 :
+ if deg < 0:
raise ValueError("expected deg >= 0")
if x.ndim != 1:
raise TypeError("expected 1D vector for x")
if x.size == 0:
raise TypeError("expected non-empty vector for x")
- if y.ndim < 1 or y.ndim > 2 :
+ if y.ndim < 1 or y.ndim > 2:
raise TypeError("expected 1D or 2D array for y")
if len(x) != len(y):
raise TypeError("expected x and y to have same length")
@@ -1740,7 +1741,7 @@ def chebfit(x, y, deg, rcond=None, full=False, w=None):
rhs = rhs * w
# set rcond
- if rcond is None :
+ if rcond is None:
rcond = len(x)*np.finfo(x.dtype).eps
# Determine the norms of the design matrix columns.
@@ -1759,9 +1760,9 @@ def chebfit(x, y, deg, rcond=None, full=False, w=None):
msg = "The fit may be poorly conditioned"
warnings.warn(msg, pu.RankWarning)
- if full :
+ if full:
return c, [resids, rank, s, rcond]
- else :
+ else:
return c
@@ -1916,8 +1917,8 @@ def chebweight(x):
The weight function of the Chebyshev polynomials.
The weight function is :math:`1/\sqrt{1 - x^2}` and the interval of
- integration is :math:`[-1, 1]`. The Chebyshev polynomials are orthogonal, but
- not normalized, with respect to this weight function.
+ integration is :math:`[-1, 1]`. The Chebyshev polynomials are
+ orthogonal, but not normalized, with respect to this weight function.
Parameters
----------
diff --git a/numpy/polynomial/hermite.py b/numpy/polynomial/hermite.py
index 43ede58ac..1fd49d774 100644
--- a/numpy/polynomial/hermite.py
+++ b/numpy/polynomial/hermite.py
@@ -66,18 +66,18 @@ import numpy.linalg as la
from . import polyutils as pu
from ._polybase import ABCPolyBase
-__all__ = ['hermzero', 'hermone', 'hermx', 'hermdomain', 'hermline',
- 'hermadd', 'hermsub', 'hermmulx', 'hermmul', 'hermdiv', 'hermpow',
- 'hermval', 'hermder', 'hermint', 'herm2poly', 'poly2herm',
- 'hermfromroots', 'hermvander', 'hermfit', 'hermtrim', 'hermroots',
- 'Hermite', 'hermval2d', 'hermval3d', 'hermgrid2d', 'hermgrid3d',
- 'hermvander2d', 'hermvander3d', 'hermcompanion', 'hermgauss',
- 'hermweight']
+__all__ = [
+ 'hermzero', 'hermone', 'hermx', 'hermdomain', 'hermline', 'hermadd',
+ 'hermsub', 'hermmulx', 'hermmul', 'hermdiv', 'hermpow', 'hermval',
+ 'hermder', 'hermint', 'herm2poly', 'poly2herm', 'hermfromroots',
+ 'hermvander', 'hermfit', 'hermtrim', 'hermroots', 'Hermite',
+ 'hermval2d', 'hermval3d', 'hermgrid2d', 'hermgrid3d', 'hermvander2d',
+ 'hermvander3d', 'hermcompanion', 'hermgauss', 'hermweight']
hermtrim = pu.trimcoef
-def poly2herm(pol) :
+def poly2herm(pol):
"""
poly2herm(pol)
@@ -118,12 +118,12 @@ def poly2herm(pol) :
[pol] = pu.as_series([pol])
deg = len(pol) - 1
res = 0
- for i in range(deg, -1, -1) :
+ for i in range(deg, -1, -1):
res = hermadd(hermmulx(res), pol[i])
return res
-def herm2poly(c) :
+def herm2poly(c):
"""
Convert a Hermite series to a polynomial.
@@ -174,7 +174,7 @@ def herm2poly(c) :
c0 = c[-2]
c1 = c[-1]
# i is the current degree of c1
- for i in range(n - 1, 1, -1) :
+ for i in range(n - 1, 1, -1):
tmp = c0
c0 = polysub(c[i - 2], c1*(2*(i - 1)))
c1 = polyadd(tmp, polymulx(c1)*2)
@@ -198,7 +198,7 @@ hermone = np.array([1])
hermx = np.array([0, 1/2])
-def hermline(off, scl) :
+def hermline(off, scl):
"""
Hermite series whose graph is a straight line.
@@ -228,13 +228,13 @@ def hermline(off, scl) :
5.0
"""
- if scl != 0 :
+ if scl != 0:
return np.array([off, scl/2])
- else :
+ else:
return np.array([off])
-def hermfromroots(roots) :
+def hermfromroots(roots):
"""
Generate a Hermite series with given roots.
@@ -284,9 +284,9 @@ def hermfromroots(roots) :
array([ 0.+0.j, 0.+0.j])
"""
- if len(roots) == 0 :
+ if len(roots) == 0:
return np.ones(1)
- else :
+ else:
[roots] = pu.as_series([roots], trim=False)
roots.sort()
p = [hermline(-r, 1) for r in roots]
@@ -340,10 +340,10 @@ def hermadd(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] += c2
ret = c1
- else :
+ else:
c2[:c1.size] += c1
ret = c2
return pu.trimseq(ret)
@@ -388,10 +388,10 @@ def hermsub(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] -= c2
ret = c1
- else :
+ else:
c2 = -c2
c2[:c1.size] += c1
ret = c2
@@ -501,13 +501,13 @@ def hermmul(c1, c2):
elif len(c) == 2:
c0 = c[0]*xs
c1 = c[1]*xs
- else :
+ else:
nd = len(c)
c0 = c[-2]*xs
c1 = c[-1]*xs
- for i in range(3, len(c) + 1) :
+ for i in range(3, len(c) + 1):
tmp = c0
- nd = nd - 1
+ nd = nd - 1
c0 = hermsub(c[-i]*xs, c1*(2*(nd - 1)))
c1 = hermadd(tmp, hermmulx(c1)*2)
return hermadd(c0, hermmulx(c1)*2)
@@ -560,16 +560,16 @@ def hermdiv(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if c2[-1] == 0 :
+ if c2[-1] == 0:
raise ZeroDivisionError()
lc1 = len(c1)
lc2 = len(c2)
- if lc1 < lc2 :
+ if lc1 < lc2:
return c1[:1]*0, c1
- elif lc2 == 1 :
+ elif lc2 == 1:
return c1/c2[-1], c1[:1]*0
- else :
+ else:
quo = np.empty(lc1 - lc2 + 1, dtype=c1.dtype)
rem = c1
for i in range(lc1 - lc2, - 1, -1):
@@ -580,7 +580,7 @@ def hermdiv(c1, c2):
return quo, pu.trimseq(rem)
-def hermpow(c, pow, maxpower=16) :
+def hermpow(c, pow, maxpower=16):
"""Raise a Hermite series to a power.
Returns the Hermite series `c` raised to the power `pow`. The
@@ -617,24 +617,24 @@ def hermpow(c, pow, maxpower=16) :
# c is a trimmed copy
[c] = pu.as_series([c])
power = int(pow)
- if power != pow or power < 0 :
+ if power != pow or power < 0:
raise ValueError("Power must be a non-negative integer.")
- elif maxpower is not None and power > maxpower :
+ elif maxpower is not None and power > maxpower:
raise ValueError("Power is too large")
- elif power == 0 :
+ elif power == 0:
return np.array([1], dtype=c.dtype)
- elif power == 1 :
+ elif power == 1:
return c
- else :
+ else:
# This can be made more efficient by using powers of two
# in the usual way.
prd = c
- for i in range(2, power + 1) :
+ for i in range(2, power + 1):
prd = hermmul(prd, c)
return prd
-def hermder(c, m=1, scl=1, axis=0) :
+def hermder(c, m=1, scl=1, axis=0):
"""
Differentiate a Hermite series.
@@ -712,7 +712,7 @@ def hermder(c, m=1, scl=1, axis=0) :
n = len(c)
if cnt >= n:
c = c[:1]*0
- else :
+ else:
for i in range(cnt):
n = n - 1
c *= scl
@@ -816,9 +816,9 @@ def hermint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
if cnt != m:
raise ValueError("The order of integration must be integer")
- if cnt < 0 :
+ if cnt < 0:
raise ValueError("The order of integration must be non-negative")
- if len(k) > cnt :
+ if len(k) > cnt:
raise ValueError("Too many integration constants")
if iaxis != axis:
raise ValueError("The axis must be integer")
@@ -832,7 +832,7 @@ def hermint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
c = np.rollaxis(c, iaxis)
k = list(k) + [0]*(cnt - len(k))
- for i in range(cnt) :
+ for i in range(cnt):
n = len(c)
c *= scl
if n == 1 and np.all(c[0] == 0):
@@ -924,22 +924,22 @@ def hermval(x, c, tensor=True):
if isinstance(x, (tuple, list)):
x = np.asarray(x)
if isinstance(x, np.ndarray) and tensor:
- c = c.reshape(c.shape + (1,)*x.ndim)
+ c = c.reshape(c.shape + (1,)*x.ndim)
x2 = x*2
- if len(c) == 1 :
+ if len(c) == 1:
c0 = c[0]
c1 = 0
- elif len(c) == 2 :
+ elif len(c) == 2:
c0 = c[0]
c1 = c[1]
- else :
+ else:
nd = len(c)
c0 = c[-2]
c1 = c[-1]
- for i in range(3, len(c) + 1) :
+ for i in range(3, len(c) + 1):
tmp = c0
- nd = nd - 1
+ nd = nd - 1
c0 = c[-i] - c1*(2*(nd - 1))
c1 = tmp + c1*x2
return c0 + c1*x2
@@ -1174,7 +1174,7 @@ def hermgrid3d(x, y, z, c):
return c
-def hermvander(x, deg) :
+def hermvander(x, deg):
"""Pseudo-Vandermonde matrix of given degree.
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
@@ -1229,15 +1229,15 @@ def hermvander(x, deg) :
dtyp = x.dtype
v = np.empty(dims, dtype=dtyp)
v[0] = x*0 + 1
- if ideg > 0 :
+ if ideg > 0:
x2 = x*2
v[1] = x2
- for i in range(2, ideg + 1) :
+ for i in range(2, ideg + 1):
v[i] = (v[i-1]*x2 - v[i-2]*(2*(i - 1)))
return np.rollaxis(v, 0, v.ndim)
-def hermvander2d(x, y, deg) :
+def hermvander2d(x, y, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1300,7 +1300,7 @@ def hermvander2d(x, y, deg) :
return v.reshape(v.shape[:-2] + (-1,))
-def hermvander3d(x, y, z, deg) :
+def hermvander3d(x, y, z, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1491,13 +1491,13 @@ def hermfit(x, y, deg, rcond=None, full=False, w=None):
y = np.asarray(y) + 0.0
# check arguments.
- if deg < 0 :
+ if deg < 0:
raise ValueError("expected deg >= 0")
if x.ndim != 1:
raise TypeError("expected 1D vector for x")
if x.size == 0:
raise TypeError("expected non-empty vector for x")
- if y.ndim < 1 or y.ndim > 2 :
+ if y.ndim < 1 or y.ndim > 2:
raise TypeError("expected 1D or 2D array for y")
if len(x) != len(y):
raise TypeError("expected x and y to have same length")
@@ -1517,7 +1517,7 @@ def hermfit(x, y, deg, rcond=None, full=False, w=None):
rhs = rhs * w
# set rcond
- if rcond is None :
+ if rcond is None:
rcond = len(x)*np.finfo(x.dtype).eps
# Determine the norms of the design matrix columns.
@@ -1536,9 +1536,9 @@ def hermfit(x, y, deg, rcond=None, full=False, w=None):
msg = "The fit may be poorly conditioned"
warnings.warn(msg, pu.RankWarning)
- if full :
+ if full:
return c, [resids, rank, s, rcond]
- else :
+ else:
return c
@@ -1568,7 +1568,6 @@ def hermcompanion(c):
.. versionadded::1.7.0
"""
- accprod = np.multiply.accumulate
# c is a trimmed copy
[c] = pu.as_series([c])
if len(c) < 2:
@@ -1636,9 +1635,9 @@ def hermroots(c):
"""
# c is a trimmed copy
[c] = pu.as_series([c])
- if len(c) <= 1 :
+ if len(c) <= 1:
return np.array([], dtype=c.dtype)
- if len(c) == 2 :
+ if len(c) == 2:
return np.array([-.5*c[0]/c[1]])
m = hermcompanion(c)
diff --git a/numpy/polynomial/hermite_e.py b/numpy/polynomial/hermite_e.py
index 874b42470..6e33dc0bc 100644
--- a/numpy/polynomial/hermite_e.py
+++ b/numpy/polynomial/hermite_e.py
@@ -66,18 +66,19 @@ import numpy.linalg as la
from . import polyutils as pu
from ._polybase import ABCPolyBase
-__all__ = ['hermezero', 'hermeone', 'hermex', 'hermedomain', 'hermeline',
- 'hermeadd', 'hermesub', 'hermemulx', 'hermemul', 'hermediv', 'hermpow',
- 'hermeval',
- 'hermeder', 'hermeint', 'herme2poly', 'poly2herme', 'hermefromroots',
- 'hermevander', 'hermefit', 'hermetrim', 'hermeroots', 'HermiteE',
- 'hermeval2d', 'hermeval3d', 'hermegrid2d', 'hermegrid3d', 'hermevander2d',
- 'hermevander3d', 'hermecompanion', 'hermegauss', 'hermeweight']
+__all__ = [
+ 'hermezero', 'hermeone', 'hermex', 'hermedomain', 'hermeline',
+ 'hermeadd', 'hermesub', 'hermemulx', 'hermemul', 'hermediv',
+ 'hermepow', 'hermeval', 'hermeder', 'hermeint', 'herme2poly',
+ 'poly2herme', 'hermefromroots', 'hermevander', 'hermefit', 'hermetrim',
+ 'hermeroots', 'HermiteE', 'hermeval2d', 'hermeval3d', 'hermegrid2d',
+ 'hermegrid3d', 'hermevander2d', 'hermevander3d', 'hermecompanion',
+ 'hermegauss', 'hermeweight']
hermetrim = pu.trimcoef
-def poly2herme(pol) :
+def poly2herme(pol):
"""
poly2herme(pol)
@@ -118,12 +119,12 @@ def poly2herme(pol) :
[pol] = pu.as_series([pol])
deg = len(pol) - 1
res = 0
- for i in range(deg, -1, -1) :
+ for i in range(deg, -1, -1):
res = hermeadd(hermemulx(res), pol[i])
return res
-def herme2poly(c) :
+def herme2poly(c):
"""
Convert a Hermite series to a polynomial.
@@ -173,7 +174,7 @@ def herme2poly(c) :
c0 = c[-2]
c1 = c[-1]
# i is the current degree of c1
- for i in range(n - 1, 1, -1) :
+ for i in range(n - 1, 1, -1):
tmp = c0
c0 = polysub(c[i - 2], c1*(i - 1))
c1 = polyadd(tmp, polymulx(c1))
@@ -197,7 +198,7 @@ hermeone = np.array([1])
hermex = np.array([0, 1])
-def hermeline(off, scl) :
+def hermeline(off, scl):
"""
Hermite series whose graph is a straight line.
@@ -228,13 +229,13 @@ def hermeline(off, scl) :
5.0
"""
- if scl != 0 :
+ if scl != 0:
return np.array([off, scl])
- else :
+ else:
return np.array([off])
-def hermefromroots(roots) :
+def hermefromroots(roots):
"""
Generate a HermiteE series with given roots.
@@ -284,9 +285,9 @@ def hermefromroots(roots) :
array([ 0.+0.j, 0.+0.j])
"""
- if len(roots) == 0 :
+ if len(roots) == 0:
return np.ones(1)
- else :
+ else:
[roots] = pu.as_series([roots], trim=False)
roots.sort()
p = [hermeline(-r, 1) for r in roots]
@@ -340,10 +341,10 @@ def hermeadd(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] += c2
ret = c1
- else :
+ else:
c2[:c1.size] += c1
ret = c2
return pu.trimseq(ret)
@@ -388,10 +389,10 @@ def hermesub(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] -= c2
ret = c1
- else :
+ else:
c2 = -c2
c2[:c1.size] += c1
ret = c2
@@ -501,13 +502,13 @@ def hermemul(c1, c2):
elif len(c) == 2:
c0 = c[0]*xs
c1 = c[1]*xs
- else :
+ else:
nd = len(c)
c0 = c[-2]*xs
c1 = c[-1]*xs
- for i in range(3, len(c) + 1) :
+ for i in range(3, len(c) + 1):
tmp = c0
- nd = nd - 1
+ nd = nd - 1
c0 = hermesub(c[-i]*xs, c1*(nd - 1))
c1 = hermeadd(tmp, hermemulx(c1))
return hermeadd(c0, hermemulx(c1))
@@ -558,16 +559,16 @@ def hermediv(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if c2[-1] == 0 :
+ if c2[-1] == 0:
raise ZeroDivisionError()
lc1 = len(c1)
lc2 = len(c2)
- if lc1 < lc2 :
+ if lc1 < lc2:
return c1[:1]*0, c1
- elif lc2 == 1 :
+ elif lc2 == 1:
return c1/c2[-1], c1[:1]*0
- else :
+ else:
quo = np.empty(lc1 - lc2 + 1, dtype=c1.dtype)
rem = c1
for i in range(lc1 - lc2, - 1, -1):
@@ -578,7 +579,7 @@ def hermediv(c1, c2):
return quo, pu.trimseq(rem)
-def hermepow(c, pow, maxpower=16) :
+def hermepow(c, pow, maxpower=16):
"""Raise a Hermite series to a power.
Returns the Hermite series `c` raised to the power `pow`. The
@@ -615,24 +616,24 @@ def hermepow(c, pow, maxpower=16) :
# c is a trimmed copy
[c] = pu.as_series([c])
power = int(pow)
- if power != pow or power < 0 :
+ if power != pow or power < 0:
raise ValueError("Power must be a non-negative integer.")
- elif maxpower is not None and power > maxpower :
+ elif maxpower is not None and power > maxpower:
raise ValueError("Power is too large")
- elif power == 0 :
+ elif power == 0:
return np.array([1], dtype=c.dtype)
- elif power == 1 :
+ elif power == 1:
return c
- else :
+ else:
# This can be made more efficient by using powers of two
# in the usual way.
prd = c
- for i in range(2, power + 1) :
+ for i in range(2, power + 1):
prd = hermemul(prd, c)
return prd
-def hermeder(c, m=1, scl=1, axis=0) :
+def hermeder(c, m=1, scl=1, axis=0):
"""
Differentiate a Hermite_e series.
@@ -710,7 +711,7 @@ def hermeder(c, m=1, scl=1, axis=0) :
n = len(c)
if cnt >= n:
return c[:1]*0
- else :
+ else:
for i in range(cnt):
n = n - 1
c *= scl
@@ -814,9 +815,9 @@ def hermeint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
if cnt != m:
raise ValueError("The order of integration must be integer")
- if cnt < 0 :
+ if cnt < 0:
raise ValueError("The order of integration must be non-negative")
- if len(k) > cnt :
+ if len(k) > cnt:
raise ValueError("Too many integration constants")
if iaxis != axis:
raise ValueError("The axis must be integer")
@@ -830,7 +831,7 @@ def hermeint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
c = np.rollaxis(c, iaxis)
k = list(k) + [0]*(cnt - len(k))
- for i in range(cnt) :
+ for i in range(cnt):
n = len(c)
c *= scl
if n == 1 and np.all(c[0] == 0):
@@ -922,21 +923,21 @@ def hermeval(x, c, tensor=True):
if isinstance(x, (tuple, list)):
x = np.asarray(x)
if isinstance(x, np.ndarray) and tensor:
- c = c.reshape(c.shape + (1,)*x.ndim)
+ c = c.reshape(c.shape + (1,)*x.ndim)
- if len(c) == 1 :
+ if len(c) == 1:
c0 = c[0]
c1 = 0
- elif len(c) == 2 :
+ elif len(c) == 2:
c0 = c[0]
c1 = c[1]
- else :
+ else:
nd = len(c)
c0 = c[-2]
c1 = c[-1]
- for i in range(3, len(c) + 1) :
+ for i in range(3, len(c) + 1):
tmp = c0
- nd = nd - 1
+ nd = nd - 1
c0 = c[-i] - c1*(nd - 1)
c1 = tmp + c1*x
return c0 + c1*x
@@ -1171,7 +1172,7 @@ def hermegrid3d(x, y, z, c):
return c
-def hermevander(x, deg) :
+def hermevander(x, deg):
"""Pseudo-Vandermonde matrix of given degree.
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
@@ -1226,14 +1227,14 @@ def hermevander(x, deg) :
dtyp = x.dtype
v = np.empty(dims, dtype=dtyp)
v[0] = x*0 + 1
- if ideg > 0 :
+ if ideg > 0:
v[1] = x
- for i in range(2, ideg + 1) :
+ for i in range(2, ideg + 1):
v[i] = (v[i-1]*x - v[i-2]*(i - 1))
return np.rollaxis(v, 0, v.ndim)
-def hermevander2d(x, y, deg) :
+def hermevander2d(x, y, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1296,7 +1297,7 @@ def hermevander2d(x, y, deg) :
return v.reshape(v.shape[:-2] + (-1,))
-def hermevander3d(x, y, z, deg) :
+def hermevander3d(x, y, z, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1487,13 +1488,13 @@ def hermefit(x, y, deg, rcond=None, full=False, w=None):
y = np.asarray(y) + 0.0
# check arguments.
- if deg < 0 :
+ if deg < 0:
raise ValueError("expected deg >= 0")
if x.ndim != 1:
raise TypeError("expected 1D vector for x")
if x.size == 0:
raise TypeError("expected non-empty vector for x")
- if y.ndim < 1 or y.ndim > 2 :
+ if y.ndim < 1 or y.ndim > 2:
raise TypeError("expected 1D or 2D array for y")
if len(x) != len(y):
raise TypeError("expected x and y to have same length")
@@ -1513,7 +1514,7 @@ def hermefit(x, y, deg, rcond=None, full=False, w=None):
rhs = rhs * w
# set rcond
- if rcond is None :
+ if rcond is None:
rcond = len(x)*np.finfo(x.dtype).eps
# Determine the norms of the design matrix columns.
@@ -1532,9 +1533,9 @@ def hermefit(x, y, deg, rcond=None, full=False, w=None):
msg = "The fit may be poorly conditioned"
warnings.warn(msg, pu.RankWarning)
- if full :
+ if full:
return c, [resids, rank, s, rcond]
- else :
+ else:
return c
@@ -1565,7 +1566,6 @@ def hermecompanion(c):
.. versionadded::1.7.0
"""
- accprod = np.multiply.accumulate
# c is a trimmed copy
[c] = pu.as_series([c])
if len(c) < 2:
@@ -1633,9 +1633,9 @@ def hermeroots(c):
"""
# c is a trimmed copy
[c] = pu.as_series([c])
- if len(c) <= 1 :
+ if len(c) <= 1:
return np.array([], dtype=c.dtype)
- if len(c) == 2 :
+ if len(c) == 2:
return np.array([-c[0]/c[1]])
m = hermecompanion(c)
diff --git a/numpy/polynomial/laguerre.py b/numpy/polynomial/laguerre.py
index 9d88162ce..8d2705d5d 100644
--- a/numpy/polynomial/laguerre.py
+++ b/numpy/polynomial/laguerre.py
@@ -66,17 +66,18 @@ import numpy.linalg as la
from . import polyutils as pu
from ._polybase import ABCPolyBase
-__all__ = ['lagzero', 'lagone', 'lagx', 'lagdomain', 'lagline',
- 'lagadd', 'lagsub', 'lagmulx', 'lagmul', 'lagdiv', 'lagpow',
- 'lagval', 'lagder', 'lagint', 'lag2poly', 'poly2lag', 'lagfromroots',
- 'lagvander', 'lagfit', 'lagtrim', 'lagroots', 'Laguerre', 'lagval2d',
- 'lagval3d', 'laggrid2d', 'laggrid3d', 'lagvander2d', 'lagvander3d',
- 'lagcompanion', 'laggauss', 'lagweight']
+__all__ = [
+ 'lagzero', 'lagone', 'lagx', 'lagdomain', 'lagline', 'lagadd',
+ 'lagsub', 'lagmulx', 'lagmul', 'lagdiv', 'lagpow', 'lagval', 'lagder',
+ 'lagint', 'lag2poly', 'poly2lag', 'lagfromroots', 'lagvander',
+ 'lagfit', 'lagtrim', 'lagroots', 'Laguerre', 'lagval2d', 'lagval3d',
+ 'laggrid2d', 'laggrid3d', 'lagvander2d', 'lagvander3d', 'lagcompanion',
+ 'laggauss', 'lagweight']
lagtrim = pu.trimcoef
-def poly2lag(pol) :
+def poly2lag(pol):
"""
poly2lag(pol)
@@ -117,12 +118,12 @@ def poly2lag(pol) :
[pol] = pu.as_series([pol])
deg = len(pol) - 1
res = 0
- for i in range(deg, -1, -1) :
+ for i in range(deg, -1, -1):
res = lagadd(lagmulx(res), pol[i])
return res
-def lag2poly(c) :
+def lag2poly(c):
"""
Convert a Laguerre series to a polynomial.
@@ -194,7 +195,7 @@ lagone = np.array([1])
lagx = np.array([1, -1])
-def lagline(off, scl) :
+def lagline(off, scl):
"""
Laguerre series whose graph is a straight line.
@@ -224,13 +225,13 @@ def lagline(off, scl) :
5.0
"""
- if scl != 0 :
+ if scl != 0:
return np.array([off + scl, -scl])
- else :
+ else:
return np.array([off])
-def lagfromroots(roots) :
+def lagfromroots(roots):
"""
Generate a Laguerre series with given roots.
@@ -280,9 +281,9 @@ def lagfromroots(roots) :
array([ 0.+0.j, 0.+0.j])
"""
- if len(roots) == 0 :
+ if len(roots) == 0:
return np.ones(1)
- else :
+ else:
[roots] = pu.as_series([roots], trim=False)
roots.sort()
p = [lagline(-r, 1) for r in roots]
@@ -337,10 +338,10 @@ def lagadd(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] += c2
ret = c1
- else :
+ else:
c2[:c1.size] += c1
ret = c2
return pu.trimseq(ret)
@@ -385,10 +386,10 @@ def lagsub(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] -= c2
ret = c1
- else :
+ else:
c2 = -c2
c2[:c1.size] += c1
ret = c2
@@ -499,13 +500,13 @@ def lagmul(c1, c2):
elif len(c) == 2:
c0 = c[0]*xs
c1 = c[1]*xs
- else :
+ else:
nd = len(c)
c0 = c[-2]*xs
c1 = c[-1]*xs
- for i in range(3, len(c) + 1) :
+ for i in range(3, len(c) + 1):
tmp = c0
- nd = nd - 1
+ nd = nd - 1
c0 = lagsub(c[-i]*xs, (c1*(nd - 1))/nd)
c1 = lagadd(tmp, lagsub((2*nd - 1)*c1, lagmulx(c1))/nd)
return lagadd(c0, lagsub(c1, lagmulx(c1)))
@@ -556,16 +557,16 @@ def lagdiv(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if c2[-1] == 0 :
+ if c2[-1] == 0:
raise ZeroDivisionError()
lc1 = len(c1)
lc2 = len(c2)
- if lc1 < lc2 :
+ if lc1 < lc2:
return c1[:1]*0, c1
- elif lc2 == 1 :
+ elif lc2 == 1:
return c1/c2[-1], c1[:1]*0
- else :
+ else:
quo = np.empty(lc1 - lc2 + 1, dtype=c1.dtype)
rem = c1
for i in range(lc1 - lc2, - 1, -1):
@@ -576,7 +577,7 @@ def lagdiv(c1, c2):
return quo, pu.trimseq(rem)
-def lagpow(c, pow, maxpower=16) :
+def lagpow(c, pow, maxpower=16):
"""Raise a Laguerre series to a power.
Returns the Laguerre series `c` raised to the power `pow`. The
@@ -613,24 +614,24 @@ def lagpow(c, pow, maxpower=16) :
# c is a trimmed copy
[c] = pu.as_series([c])
power = int(pow)
- if power != pow or power < 0 :
+ if power != pow or power < 0:
raise ValueError("Power must be a non-negative integer.")
- elif maxpower is not None and power > maxpower :
+ elif maxpower is not None and power > maxpower:
raise ValueError("Power is too large")
- elif power == 0 :
+ elif power == 0:
return np.array([1], dtype=c.dtype)
- elif power == 1 :
+ elif power == 1:
return c
- else :
+ else:
# This can be made more efficient by using powers of two
# in the usual way.
prd = c
- for i in range(2, power + 1) :
+ for i in range(2, power + 1):
prd = lagmul(prd, c)
return prd
-def lagder(c, m=1, scl=1, axis=0) :
+def lagder(c, m=1, scl=1, axis=0):
"""
Differentiate a Laguerre series.
@@ -708,7 +709,7 @@ def lagder(c, m=1, scl=1, axis=0) :
n = len(c)
if cnt >= n:
c = c[:1]*0
- else :
+ else:
for i in range(cnt):
n = n - 1
c *= scl
@@ -815,9 +816,9 @@ def lagint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
if cnt != m:
raise ValueError("The order of integration must be integer")
- if cnt < 0 :
+ if cnt < 0:
raise ValueError("The order of integration must be non-negative")
- if len(k) > cnt :
+ if len(k) > cnt:
raise ValueError("Too many integration constants")
if iaxis != axis:
raise ValueError("The axis must be integer")
@@ -831,7 +832,7 @@ def lagint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
c = np.rollaxis(c, iaxis)
k = list(k) + [0]*(cnt - len(k))
- for i in range(cnt) :
+ for i in range(cnt):
n = len(c)
c *= scl
if n == 1 and np.all(c[0] == 0):
@@ -924,22 +925,21 @@ def lagval(x, c, tensor=True):
if isinstance(x, (tuple, list)):
x = np.asarray(x)
if isinstance(x, np.ndarray) and tensor:
- c = c.reshape(c.shape + (1,)*x.ndim)
+ c = c.reshape(c.shape + (1,)*x.ndim)
-
- if len(c) == 1 :
+ if len(c) == 1:
c0 = c[0]
c1 = 0
- elif len(c) == 2 :
+ elif len(c) == 2:
c0 = c[0]
c1 = c[1]
- else :
+ else:
nd = len(c)
c0 = c[-2]
c1 = c[-1]
- for i in range(3, len(c) + 1) :
+ for i in range(3, len(c) + 1):
tmp = c0
- nd = nd - 1
+ nd = nd - 1
c0 = c[-i] - (c1*(nd - 1))/nd
c1 = tmp + (c1*((2*nd - 1) - x))/nd
return c0 + c1*(1 - x)
@@ -1174,7 +1174,7 @@ def laggrid3d(x, y, z, c):
return c
-def lagvander(x, deg) :
+def lagvander(x, deg):
"""Pseudo-Vandermonde matrix of given degree.
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
@@ -1229,14 +1229,14 @@ def lagvander(x, deg) :
dtyp = x.dtype
v = np.empty(dims, dtype=dtyp)
v[0] = x*0 + 1
- if ideg > 0 :
+ if ideg > 0:
v[1] = 1 - x
- for i in range(2, ideg + 1) :
+ for i in range(2, ideg + 1):
v[i] = (v[i-1]*(2*i - 1 - x) - v[i-2]*(i - 1))/i
return np.rollaxis(v, 0, v.ndim)
-def lagvander2d(x, y, deg) :
+def lagvander2d(x, y, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1299,7 +1299,7 @@ def lagvander2d(x, y, deg) :
return v.reshape(v.shape[:-2] + (-1,))
-def lagvander3d(x, y, z, deg) :
+def lagvander3d(x, y, z, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1490,13 +1490,13 @@ def lagfit(x, y, deg, rcond=None, full=False, w=None):
y = np.asarray(y) + 0.0
# check arguments.
- if deg < 0 :
+ if deg < 0:
raise ValueError("expected deg >= 0")
if x.ndim != 1:
raise TypeError("expected 1D vector for x")
if x.size == 0:
raise TypeError("expected non-empty vector for x")
- if y.ndim < 1 or y.ndim > 2 :
+ if y.ndim < 1 or y.ndim > 2:
raise TypeError("expected 1D or 2D array for y")
if len(x) != len(y):
raise TypeError("expected x and y to have same length")
@@ -1516,7 +1516,7 @@ def lagfit(x, y, deg, rcond=None, full=False, w=None):
rhs = rhs * w
# set rcond
- if rcond is None :
+ if rcond is None:
rcond = len(x)*np.finfo(x.dtype).eps
# Determine the norms of the design matrix columns.
@@ -1535,9 +1535,9 @@ def lagfit(x, y, deg, rcond=None, full=False, w=None):
msg = "The fit may be poorly conditioned"
warnings.warn(msg, pu.RankWarning)
- if full :
+ if full:
return c, [resids, rank, s, rcond]
- else :
+ else:
return c
@@ -1566,7 +1566,6 @@ def lagcompanion(c):
.. versionadded::1.7.0
"""
- accprod = np.multiply.accumulate
# c is a trimmed copy
[c] = pu.as_series([c])
if len(c) < 2:
@@ -1634,9 +1633,9 @@ def lagroots(c):
"""
# c is a trimmed copy
[c] = pu.as_series([c])
- if len(c) <= 1 :
+ if len(c) <= 1:
return np.array([], dtype=c.dtype)
- if len(c) == 2 :
+ if len(c) == 2:
return np.array([1 + c[0]/c[1]])
m = lagcompanion(c)
@@ -1651,8 +1650,8 @@ def laggauss(deg):
Computes the sample points and weights for Gauss-Laguerre quadrature.
These sample points and weights will correctly integrate polynomials of
- degree :math:`2*deg - 1` or less over the interval :math:`[0, \inf]` with the
- weight function :math:`f(x) = \exp(-x)`.
+ degree :math:`2*deg - 1` or less over the interval :math:`[0, \inf]`
+ with the weight function :math:`f(x) = \exp(-x)`.
Parameters
----------
diff --git a/numpy/polynomial/legendre.py b/numpy/polynomial/legendre.py
index 58c130b7e..d2de28269 100644
--- a/numpy/polynomial/legendre.py
+++ b/numpy/polynomial/legendre.py
@@ -90,17 +90,18 @@ import numpy.linalg as la
from . import polyutils as pu
from ._polybase import ABCPolyBase
-__all__ = ['legzero', 'legone', 'legx', 'legdomain', 'legline',
- 'legadd', 'legsub', 'legmulx', 'legmul', 'legdiv', 'legpow', 'legval',
- 'legder', 'legint', 'leg2poly', 'poly2leg', 'legfromroots',
- 'legvander', 'legfit', 'legtrim', 'legroots', 'Legendre', 'legval2d',
- 'legval3d', 'leggrid2d', 'leggrid3d', 'legvander2d', 'legvander3d',
- 'legcompanion', 'leggauss', 'legweight']
+__all__ = [
+ 'legzero', 'legone', 'legx', 'legdomain', 'legline', 'legadd',
+ 'legsub', 'legmulx', 'legmul', 'legdiv', 'legpow', 'legval', 'legder',
+ 'legint', 'leg2poly', 'poly2leg', 'legfromroots', 'legvander',
+ 'legfit', 'legtrim', 'legroots', 'Legendre', 'legval2d', 'legval3d',
+ 'leggrid2d', 'leggrid3d', 'legvander2d', 'legvander3d', 'legcompanion',
+ 'leggauss', 'legweight']
legtrim = pu.trimcoef
-def poly2leg(pol) :
+def poly2leg(pol):
"""
Convert a polynomial to a Legendre series.
@@ -143,12 +144,12 @@ def poly2leg(pol) :
[pol] = pu.as_series([pol])
deg = len(pol) - 1
res = 0
- for i in range(deg, -1, -1) :
+ for i in range(deg, -1, -1):
res = legadd(legmulx(res), pol[i])
return res
-def leg2poly(c) :
+def leg2poly(c):
"""
Convert a Legendre series to a polynomial.
@@ -202,7 +203,7 @@ def leg2poly(c) :
c0 = c[-2]
c1 = c[-1]
# i is the current degree of c1
- for i in range(n - 1, 1, -1) :
+ for i in range(n - 1, 1, -1):
tmp = c0
c0 = polysub(c[i - 2], (c1*(i - 1))/i)
c1 = polyadd(tmp, (polymulx(c1)*(2*i - 1))/i)
@@ -226,7 +227,7 @@ legone = np.array([1])
legx = np.array([0, 1])
-def legline(off, scl) :
+def legline(off, scl):
"""
Legendre series whose graph is a straight line.
@@ -256,13 +257,13 @@ def legline(off, scl) :
-3.0
"""
- if scl != 0 :
+ if scl != 0:
return np.array([off, scl])
- else :
+ else:
return np.array([off])
-def legfromroots(roots) :
+def legfromroots(roots):
"""
Generate a Legendre series with given roots.
@@ -311,9 +312,9 @@ def legfromroots(roots) :
array([ 1.33333333+0.j, 0.00000000+0.j, 0.66666667+0.j])
"""
- if len(roots) == 0 :
+ if len(roots) == 0:
return np.ones(1)
- else :
+ else:
[roots] = pu.as_series([roots], trim=False)
roots.sort()
p = [legline(-r, 1) for r in roots]
@@ -369,10 +370,10 @@ def legadd(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] += c2
ret = c1
- else :
+ else:
c2[:c1.size] += c1
ret = c2
return pu.trimseq(ret)
@@ -421,10 +422,10 @@ def legsub(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] -= c2
ret = c1
- else :
+ else:
c2 = -c2
c2[:c1.size] += c1
ret = c2
@@ -533,13 +534,13 @@ def legmul(c1, c2):
elif len(c) == 2:
c0 = c[0]*xs
c1 = c[1]*xs
- else :
+ else:
nd = len(c)
c0 = c[-2]*xs
c1 = c[-1]*xs
- for i in range(3, len(c) + 1) :
+ for i in range(3, len(c) + 1):
tmp = c0
- nd = nd - 1
+ nd = nd - 1
c0 = legsub(c[-i]*xs, (c1*(nd - 1))/nd)
c1 = legadd(tmp, (legmulx(c1)*(2*nd - 1))/nd)
return legadd(c0, legmulx(c1))
@@ -593,16 +594,16 @@ def legdiv(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if c2[-1] == 0 :
+ if c2[-1] == 0:
raise ZeroDivisionError()
lc1 = len(c1)
lc2 = len(c2)
- if lc1 < lc2 :
+ if lc1 < lc2:
return c1[:1]*0, c1
- elif lc2 == 1 :
+ elif lc2 == 1:
return c1/c2[-1], c1[:1]*0
- else :
+ else:
quo = np.empty(lc1 - lc2 + 1, dtype=c1.dtype)
rem = c1
for i in range(lc1 - lc2, - 1, -1):
@@ -613,7 +614,7 @@ def legdiv(c1, c2):
return quo, pu.trimseq(rem)
-def legpow(c, pow, maxpower=16) :
+def legpow(c, pow, maxpower=16):
"""Raise a Legendre series to a power.
Returns the Legendre series `c` raised to the power `pow`. The
@@ -647,24 +648,24 @@ def legpow(c, pow, maxpower=16) :
# c is a trimmed copy
[c] = pu.as_series([c])
power = int(pow)
- if power != pow or power < 0 :
+ if power != pow or power < 0:
raise ValueError("Power must be a non-negative integer.")
- elif maxpower is not None and power > maxpower :
+ elif maxpower is not None and power > maxpower:
raise ValueError("Power is too large")
- elif power == 0 :
+ elif power == 0:
return np.array([1], dtype=c.dtype)
- elif power == 1 :
+ elif power == 1:
return c
- else :
+ else:
# This can be made more efficient by using powers of two
# in the usual way.
prd = c
- for i in range(2, power + 1) :
+ for i in range(2, power + 1):
prd = legmul(prd, c)
return prd
-def legder(c, m=1, scl=1, axis=0) :
+def legder(c, m=1, scl=1, axis=0):
"""
Differentiate a Legendre series.
@@ -747,7 +748,7 @@ def legder(c, m=1, scl=1, axis=0) :
n = len(c)
if cnt >= n:
c = c[:1]*0
- else :
+ else:
for i in range(cnt):
n = n - 1
c *= scl
@@ -857,9 +858,9 @@ def legint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
if cnt != m:
raise ValueError("The order of integration must be integer")
- if cnt < 0 :
+ if cnt < 0:
raise ValueError("The order of integration must be non-negative")
- if len(k) > cnt :
+ if len(k) > cnt:
raise ValueError("Too many integration constants")
if iaxis != axis:
raise ValueError("The axis must be integer")
@@ -873,7 +874,7 @@ def legint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
c = np.rollaxis(c, iaxis)
k = list(k) + [0]*(cnt - len(k))
- for i in range(cnt) :
+ for i in range(cnt):
n = len(c)
c *= scl
if n == 1 and np.all(c[0] == 0):
@@ -964,19 +965,19 @@ def legval(x, c, tensor=True):
if isinstance(x, np.ndarray) and tensor:
c = c.reshape(c.shape + (1,)*x.ndim)
- if len(c) == 1 :
+ if len(c) == 1:
c0 = c[0]
c1 = 0
- elif len(c) == 2 :
+ elif len(c) == 2:
c0 = c[0]
c1 = c[1]
- else :
+ else:
nd = len(c)
c0 = c[-2]
c1 = c[-1]
- for i in range(3, len(c) + 1) :
+ for i in range(3, len(c) + 1):
tmp = c0
- nd = nd - 1
+ nd = nd - 1
c0 = c[-i] - (c1*(nd - 1))/nd
c1 = tmp + (c1*x*(2*nd - 1))/nd
return c0 + c1*x
@@ -1211,7 +1212,7 @@ def leggrid3d(x, y, z, c):
return c
-def legvander(x, deg) :
+def legvander(x, deg):
"""Pseudo-Vandermonde matrix of given degree.
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
@@ -1259,14 +1260,14 @@ def legvander(x, deg) :
# Use forward recursion to generate the entries. This is not as accurate
# as reverse recursion in this application but it is more efficient.
v[0] = x*0 + 1
- if ideg > 0 :
+ if ideg > 0:
v[1] = x
- for i in range(2, ideg + 1) :
+ for i in range(2, ideg + 1):
v[i] = (v[i-1]*x*(2*i - 1) - v[i-2]*(i - 1))/i
return np.rollaxis(v, 0, v.ndim)
-def legvander2d(x, y, deg) :
+def legvander2d(x, y, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1329,7 +1330,7 @@ def legvander2d(x, y, deg) :
return v.reshape(v.shape[:-2] + (-1,))
-def legvander3d(x, y, z, deg) :
+def legvander3d(x, y, z, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1515,13 +1516,13 @@ def legfit(x, y, deg, rcond=None, full=False, w=None):
y = np.asarray(y) + 0.0
# check arguments.
- if deg < 0 :
+ if deg < 0:
raise ValueError("expected deg >= 0")
if x.ndim != 1:
raise TypeError("expected 1D vector for x")
if x.size == 0:
raise TypeError("expected non-empty vector for x")
- if y.ndim < 1 or y.ndim > 2 :
+ if y.ndim < 1 or y.ndim > 2:
raise TypeError("expected 1D or 2D array for y")
if len(x) != len(y):
raise TypeError("expected x and y to have same length")
@@ -1541,7 +1542,7 @@ def legfit(x, y, deg, rcond=None, full=False, w=None):
rhs = rhs * w
# set rcond
- if rcond is None :
+ if rcond is None:
rcond = len(x)*np.finfo(x.dtype).eps
# Determine the norms of the design matrix columns.
@@ -1560,9 +1561,9 @@ def legfit(x, y, deg, rcond=None, full=False, w=None):
msg = "The fit may be poorly conditioned"
warnings.warn(msg, pu.RankWarning)
- if full :
+ if full:
return c, [resids, rank, s, rcond]
- else :
+ else:
return c
@@ -1637,11 +1638,11 @@ def legroots(c):
-----
The root estimates are obtained as the eigenvalues of the companion
matrix, Roots far from the origin of the complex plane may have large
- errors due to the numerical instability of the series for such
- values. Roots with multiplicity greater than 1 will also show larger
- errors as the value of the series near such points is relatively
- insensitive to errors in the roots. Isolated roots near the origin can
- be improved by a few iterations of Newton's method.
+ errors due to the numerical instability of the series for such values.
+ Roots with multiplicity greater than 1 will also show larger errors as
+ the value of the series near such points is relatively insensitive to
+ errors in the roots. Isolated roots near the origin can be improved by
+ a few iterations of Newton's method.
The Legendre series basis polynomials aren't powers of ``x`` so the
results of this function may seem unintuitive.
@@ -1649,7 +1650,7 @@ def legroots(c):
Examples
--------
>>> import numpy.polynomial.legendre as leg
- >>> leg.legroots((1, 2, 3, 4)) # 4L_3 + 3L_2 + 2L_1 + 1L_0 has only real roots
+ >>> leg.legroots((1, 2, 3, 4)) # 4L_3 + 3L_2 + 2L_1 + 1L_0, all real roots
array([-0.85099543, -0.11407192, 0.51506735])
"""
diff --git a/numpy/polynomial/polynomial.py b/numpy/polynomial/polynomial.py
index 92cc83821..60e339a1d 100644
--- a/numpy/polynomial/polynomial.py
+++ b/numpy/polynomial/polynomial.py
@@ -55,11 +55,12 @@ See Also
"""
from __future__ import division, absolute_import, print_function
-__all__ = ['polyzero', 'polyone', 'polyx', 'polydomain', 'polyline',
- 'polyadd', 'polysub', 'polymulx', 'polymul', 'polydiv', 'polypow',
- 'polyval', 'polyder', 'polyint', 'polyfromroots', 'polyvander',
- 'polyfit', 'polytrim', 'polyroots', 'Polynomial', 'polyval2d',
- 'polyval3d', 'polygrid2d', 'polygrid3d', 'polyvander2d', 'polyvander3d']
+__all__ = [
+ 'polyzero', 'polyone', 'polyx', 'polydomain', 'polyline', 'polyadd',
+ 'polysub', 'polymulx', 'polymul', 'polydiv', 'polypow', 'polyval',
+ 'polyder', 'polyint', 'polyfromroots', 'polyvander', 'polyfit',
+ 'polytrim', 'polyroots', 'Polynomial', 'polyval2d', 'polyval3d',
+ 'polygrid2d', 'polygrid3d', 'polyvander2d', 'polyvander3d']
import warnings
import numpy as np
@@ -92,7 +93,7 @@ polyx = np.array([0, 1])
#
-def polyline(off, scl) :
+def polyline(off, scl):
"""
Returns an array representing a linear polynomial.
@@ -120,13 +121,13 @@ def polyline(off, scl) :
0.0
"""
- if scl != 0 :
+ if scl != 0:
return np.array([off, scl])
- else :
+ else:
return np.array([off])
-def polyfromroots(roots) :
+def polyfromroots(roots):
"""
Generate a monic polynomial with given roots.
@@ -184,9 +185,9 @@ def polyfromroots(roots) :
array([ 1.+0.j, 0.+0.j, 1.+0.j])
"""
- if len(roots) == 0 :
+ if len(roots) == 0:
return np.ones(1)
- else :
+ else:
[roots] = pu.as_series([roots], trim=False)
roots.sort()
p = [polyline(-r, 1) for r in roots]
@@ -236,10 +237,10 @@ def polyadd(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] += c2
ret = c1
- else :
+ else:
c2[:c1.size] += c1
ret = c2
return pu.trimseq(ret)
@@ -281,10 +282,10 @@ def polysub(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if len(c1) > len(c2) :
+ if len(c1) > len(c2):
c1[:c2.size] -= c2
ret = c1
- else :
+ else:
c2 = -c2
c2[:c1.size] += c1
ret = c2
@@ -400,29 +401,29 @@ def polydiv(c1, c2):
"""
# c1, c2 are trimmed copies
[c1, c2] = pu.as_series([c1, c2])
- if c2[-1] == 0 :
+ if c2[-1] == 0:
raise ZeroDivisionError()
len1 = len(c1)
len2 = len(c2)
- if len2 == 1 :
+ if len2 == 1:
return c1/c2[-1], c1[:1]*0
- elif len1 < len2 :
+ elif len1 < len2:
return c1[:1]*0, c1
- else :
+ else:
dlen = len1 - len2
scl = c2[-1]
- c2 = c2[:-1]/scl
+ c2 = c2[:-1]/scl
i = dlen
j = len1 - 1
- while i >= 0 :
+ while i >= 0:
c1[i:j] -= c2*c1[j]
i -= 1
j -= 1
return c1[j+1:]/scl, pu.trimseq(c1[:j+1])
-def polypow(c, pow, maxpower=None) :
+def polypow(c, pow, maxpower=None):
"""Raise a polynomial to a power.
Returns the polynomial `c` raised to the power `pow`. The argument
@@ -456,19 +457,19 @@ def polypow(c, pow, maxpower=None) :
# c is a trimmed copy
[c] = pu.as_series([c])
power = int(pow)
- if power != pow or power < 0 :
+ if power != pow or power < 0:
raise ValueError("Power must be a non-negative integer.")
- elif maxpower is not None and power > maxpower :
+ elif maxpower is not None and power > maxpower:
raise ValueError("Power is too large")
- elif power == 0 :
+ elif power == 0:
return np.array([1], dtype=c.dtype)
- elif power == 1 :
+ elif power == 1:
return c
- else :
+ else:
# This can be made more efficient by using powers of two
# in the usual way.
prd = c
- for i in range(2, power + 1) :
+ for i in range(2, power + 1):
prd = np.convolve(prd, c)
return prd
@@ -550,7 +551,7 @@ def polyder(c, m=1, scl=1, axis=0):
n = len(c)
if cnt >= n:
c = c[:1]*0
- else :
+ else:
for i in range(cnt):
n = n - 1
c *= scl
@@ -650,9 +651,9 @@ def polyint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
if cnt != m:
raise ValueError("The order of integration must be integer")
- if cnt < 0 :
+ if cnt < 0:
raise ValueError("The order of integration must be non-negative")
- if len(k) > cnt :
+ if len(k) > cnt:
raise ValueError("Too many integration constants")
if iaxis != axis:
raise ValueError("The axis must be integer")
@@ -661,7 +662,6 @@ def polyint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
if iaxis < 0:
iaxis += c.ndim
-
if cnt == 0:
return c
@@ -775,7 +775,7 @@ def polyval(x, c, tensor=True):
c = c.reshape(c.shape + (1,)*x.ndim)
c0 = c[-1] + x*0
- for i in range(2, len(c) + 1) :
+ for i in range(2, len(c) + 1):
c0 = c[-i] + c0*x
return c0
@@ -1010,7 +1010,7 @@ def polygrid3d(x, y, z, c):
return c
-def polyvander(x, deg) :
+def polyvander(x, deg):
"""Vandermonde matrix of given degree.
Returns the Vandermonde matrix of degree `deg` and sample points
@@ -1059,14 +1059,14 @@ def polyvander(x, deg) :
dtyp = x.dtype
v = np.empty(dims, dtype=dtyp)
v[0] = x*0 + 1
- if ideg > 0 :
+ if ideg > 0:
v[1] = x
- for i in range(2, ideg + 1) :
+ for i in range(2, ideg + 1):
v[i] = v[i-1]*x
return np.rollaxis(v, 0, v.ndim)
-def polyvander2d(x, y, deg) :
+def polyvander2d(x, y, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1126,7 +1126,7 @@ def polyvander2d(x, y, deg) :
return v.reshape(v.shape[:-2] + (-1,))
-def polyvander3d(x, y, z, deg) :
+def polyvander3d(x, y, z, deg):
"""Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
@@ -1254,7 +1254,7 @@ def polyfit(x, y, deg, rcond=None, full=False, w=None):
rcond -- value of `rcond`.
For more details, see `linalg.lstsq`.
-
+
Raises
------
RankWarning
@@ -1337,13 +1337,13 @@ def polyfit(x, y, deg, rcond=None, full=False, w=None):
y = np.asarray(y) + 0.0
# check arguments.
- if deg < 0 :
+ if deg < 0:
raise ValueError("expected deg >= 0")
if x.ndim != 1:
raise TypeError("expected 1D vector for x")
if x.size == 0:
raise TypeError("expected non-empty vector for x")
- if y.ndim < 1 or y.ndim > 2 :
+ if y.ndim < 1 or y.ndim > 2:
raise TypeError("expected 1D or 2D array for y")
if len(x) != len(y):
raise TypeError("expected x and y to have same length")
@@ -1363,7 +1363,7 @@ def polyfit(x, y, deg, rcond=None, full=False, w=None):
rhs = rhs * w
# set rcond
- if rcond is None :
+ if rcond is None:
rcond = len(x)*np.finfo(x.dtype).eps
# Determine the norms of the design matrix columns.
@@ -1382,9 +1382,9 @@ def polyfit(x, y, deg, rcond=None, full=False, w=None):
msg = "The fit may be poorly conditioned"
warnings.warn(msg, pu.RankWarning)
- if full :
+ if full:
return c, [resids, rank, s, rcond]
- else :
+ else:
return c
@@ -1415,7 +1415,7 @@ def polycompanion(c):
"""
# c is a trimmed copy
[c] = pu.as_series([c])
- if len(c) < 2 :
+ if len(c) < 2:
raise ValueError('Series must have maximum degree of at least 1.')
if len(c) == 2:
return np.array([[-c[0]/c[1]]])
diff --git a/numpy/polynomial/polyutils.py b/numpy/polynomial/polyutils.py
index 99f508521..9348559ed 100644
--- a/numpy/polynomial/polyutils.py
+++ b/numpy/polynomial/polyutils.py
@@ -45,27 +45,25 @@ Functions
"""
from __future__ import division, absolute_import, print_function
-__all__ = ['RankWarning', 'PolyError', 'PolyDomainError', 'as_series',
- 'trimseq', 'trimcoef', 'getdomain', 'mapdomain', 'mapparms',
- 'PolyBase']
-
-import warnings
import numpy as np
-import sys
+
+__all__ = [
+ 'RankWarning', 'PolyError', 'PolyDomainError', 'as_series', 'trimseq',
+ 'trimcoef', 'getdomain', 'mapdomain', 'mapparms', 'PolyBase']
#
# Warnings and Exceptions
#
-class RankWarning(UserWarning) :
+class RankWarning(UserWarning):
"""Issued by chebfit when the design matrix is rank deficient."""
pass
-class PolyError(Exception) :
+class PolyError(Exception):
"""Base class for errors in this module."""
pass
-class PolyDomainError(PolyError) :
+class PolyDomainError(PolyError):
"""Issued by the generic Poly class when two domains don't match.
This is raised when an binary operation is passed Poly objects with
@@ -78,7 +76,7 @@ class PolyDomainError(PolyError) :
# Base class for all polynomial types
#
-class PolyBase(object) :
+class PolyBase(object):
"""
Base class for all polynomial types.
@@ -93,7 +91,7 @@ class PolyBase(object) :
#
# Helper functions to convert inputs to 1-D arrays
#
-def trimseq(seq) :
+def trimseq(seq):
"""Remove small Poly series coefficients.
Parameters
@@ -114,16 +112,16 @@ def trimseq(seq) :
Do not lose the type info if the sequence contains unknown objects.
"""
- if len(seq) == 0 :
+ if len(seq) == 0:
return seq
- else :
- for i in range(len(seq) - 1, -1, -1) :
- if seq[i] != 0 :
+ else:
+ for i in range(len(seq) - 1, -1, -1):
+ if seq[i] != 0:
break
return seq[:i+1]
-def as_series(alist, trim=True) :
+def as_series(alist, trim=True):
"""
Return argument as a list of 1-d arrays.
@@ -165,32 +163,32 @@ def as_series(alist, trim=True) :
"""
arrays = [np.array(a, ndmin=1, copy=0) for a in alist]
- if min([a.size for a in arrays]) == 0 :
+ if min([a.size for a in arrays]) == 0:
raise ValueError("Coefficient array is empty")
- if any([a.ndim != 1 for a in arrays]) :
+ if any([a.ndim != 1 for a in arrays]):
raise ValueError("Coefficient array is not 1-d")
- if trim :
+ if trim:
arrays = [trimseq(a) for a in arrays]
- if any([a.dtype == np.dtype(object) for a in arrays]) :
+ if any([a.dtype == np.dtype(object) for a in arrays]):
ret = []
- for a in arrays :
- if a.dtype != np.dtype(object) :
+ for a in arrays:
+ if a.dtype != np.dtype(object):
tmp = np.empty(len(a), dtype=np.dtype(object))
tmp[:] = a[:]
ret.append(tmp)
- else :
+ else:
ret.append(a.copy())
- else :
- try :
+ else:
+ try:
dtype = np.common_type(*arrays)
- except :
+ except:
raise ValueError("Coefficient arrays have no common type")
ret = [np.array(a, copy=1, dtype=dtype) for a in arrays]
return ret
-def trimcoef(c, tol=0) :
+def trimcoef(c, tol=0):
"""
Remove "small" "trailing" coefficients from a polynomial.
@@ -234,17 +232,17 @@ def trimcoef(c, tol=0) :
array([ 0.0003+0.j , 0.0010-0.001j])
"""
- if tol < 0 :
+ if tol < 0:
raise ValueError("tol must be non-negative")
[c] = as_series([c])
[ind] = np.where(np.abs(c) > tol)
- if len(ind) == 0 :
+ if len(ind) == 0:
return c[:1]*0
- else :
+ else:
return c[:ind[-1] + 1].copy()
-def getdomain(x) :
+def getdomain(x):
"""
Return a domain suitable for given abscissae.
@@ -283,14 +281,14 @@ def getdomain(x) :
"""
[x] = as_series([x], trim=False)
- if x.dtype.char in np.typecodes['Complex'] :
+ if x.dtype.char in np.typecodes['Complex']:
rmin, rmax = x.real.min(), x.real.max()
imin, imax = x.imag.min(), x.imag.max()
return np.array((complex(rmin, imin), complex(rmax, imax)))
- else :
+ else:
return np.array((x.min(), x.max()))
-def mapparms(old, new) :
+def mapparms(old, new):
"""
Linear map parameters between domains.
@@ -337,7 +335,7 @@ def mapparms(old, new) :
scl = newlen/oldlen
return off, scl
-def mapdomain(x, old, new) :
+def mapdomain(x, old, new):
"""
Apply linear map to input points.
diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py
index 82c3ba9ea..a596905f6 100644
--- a/numpy/polynomial/tests/test_chebyshev.py
+++ b/numpy/polynomial/tests/test_chebyshev.py
@@ -400,14 +400,14 @@ class TestFitting(TestCase):
return x*(x - 1)*(x - 2)
# Test exceptions
- assert_raises(ValueError, cheb.chebfit, [1], [1], -1)
- assert_raises(TypeError, cheb.chebfit, [[1]], [1], 0)
- assert_raises(TypeError, cheb.chebfit, [], [1], 0)
- assert_raises(TypeError, cheb.chebfit, [1], [[[1]]], 0)
- assert_raises(TypeError, cheb.chebfit, [1, 2], [1], 0)
- assert_raises(TypeError, cheb.chebfit, [1], [1, 2], 0)
- assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[[1]])
- assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[1, 1])
+ assert_raises(ValueError, cheb.chebfit, [1], [1], -1)
+ assert_raises(TypeError, cheb.chebfit, [[1]], [1], 0)
+ assert_raises(TypeError, cheb.chebfit, [], [1], 0)
+ assert_raises(TypeError, cheb.chebfit, [1], [[[1]]], 0)
+ assert_raises(TypeError, cheb.chebfit, [1, 2], [1], 0)
+ assert_raises(TypeError, cheb.chebfit, [1], [1, 2], 0)
+ assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[[1]])
+ assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[1, 1])
# Test fit
x = np.linspace(0, 2)
@@ -532,7 +532,7 @@ class TestMisc(TestCase):
assert_almost_equal(cheb.chebpts1(2), tgt)
tgt = [-0.86602540378443871, 0, 0.86602540378443871]
assert_almost_equal(cheb.chebpts1(3), tgt)
- tgt = [-0.9238795325, -0.3826834323, 0.3826834323, 0.9238795325]
+ tgt = [-0.9238795325, -0.3826834323, 0.3826834323, 0.9238795325]
assert_almost_equal(cheb.chebpts1(4), tgt)
def test_chebpts2(self):
diff --git a/numpy/polynomial/tests/test_classes.py b/numpy/polynomial/tests/test_classes.py
index f9134b8c1..cd5a54687 100644
--- a/numpy/polynomial/tests/test_classes.py
+++ b/numpy/polynomial/tests/test_classes.py
@@ -10,12 +10,10 @@ from numbers import Number
import numpy as np
from numpy.polynomial import (
- Polynomial, Legendre, Chebyshev, Laguerre,
- Hermite, HermiteE)
+ Polynomial, Legendre, Chebyshev, Laguerre, Hermite, HermiteE)
from numpy.testing import (
- TestCase, assert_almost_equal, assert_raises,
- assert_equal, assert_, run_module_suite, dec)
-from numpy.testing.noseclasses import KnownFailure
+ assert_almost_equal, assert_raises, assert_equal, assert_,
+ run_module_suite)
from numpy.compat import long
@@ -410,6 +408,9 @@ def check_roots(Poly):
d = Poly.domain + random((2,))*.25
w = Poly.window + random((2,))*.25
tgt = np.sort(random((5,)))
+ res = np.sort(Poly.fromroots(tgt, domain=d, window=w).roots())
+ assert_almost_equal(res, tgt)
+ # default domain and window
res = np.sort(Poly.fromroots(tgt).roots())
assert_almost_equal(res, tgt)
@@ -468,6 +469,12 @@ def check_deriv(Poly):
p3 = p1.integ(1, k=[1])
assert_almost_equal(p2.deriv(1).coef, p3.coef)
assert_almost_equal(p2.deriv(2).coef, p1.coef)
+ # default domain and window
+ p1 = Poly([1, 2, 3])
+ p2 = p1.integ(2, k=[1, 2])
+ p3 = p1.integ(1, k=[1])
+ assert_almost_equal(p2.deriv(1).coef, p3.coef)
+ assert_almost_equal(p2.deriv(2).coef, p1.coef)
def check_linspace(Poly):
@@ -491,11 +498,18 @@ def check_linspace(Poly):
def check_pow(Poly):
d = Poly.domain + random((2,))*.25
w = Poly.window + random((2,))*.25
- tgt = Poly([1], domain=d, window=d)
- tst = Poly([1, 2, 3], domain=d, window=d)
+ tgt = Poly([1], domain=d, window=w)
+ tst = Poly([1, 2, 3], domain=d, window=w)
+ for i in range(5):
+ assert_poly_almost_equal(tst**i, tgt)
+ tgt = tgt * tst
+ # default domain and window
+ tgt = Poly([1])
+ tst = Poly([1, 2, 3])
for i in range(5):
assert_poly_almost_equal(tst**i, tgt)
tgt = tgt * tst
+ # check error for invalid powers
assert_raises(ValueError, op.pow, tgt, 1.5)
assert_raises(ValueError, op.pow, tgt, -1)
diff --git a/numpy/polynomial/tests/test_hermite.py b/numpy/polynomial/tests/test_hermite.py
index ac60007d1..e67625a88 100644
--- a/numpy/polynomial/tests/test_hermite.py
+++ b/numpy/polynomial/tests/test_hermite.py
@@ -119,7 +119,6 @@ class TestEvaluation(TestCase):
y = [polyval(x, c) for c in Hlist]
for i in range(10):
msg = "At i=%d" % i
- ser = np.zeros
tgt = y[i]
res = herm.hermval(x, [0]*i + [1])
assert_almost_equal(res, tgt, err_msg=msg)
@@ -389,14 +388,14 @@ class TestFitting(TestCase):
return x*(x - 1)*(x - 2)
# Test exceptions
- assert_raises(ValueError, herm.hermfit, [1], [1], -1)
- assert_raises(TypeError, herm.hermfit, [[1]], [1], 0)
- assert_raises(TypeError, herm.hermfit, [], [1], 0)
- assert_raises(TypeError, herm.hermfit, [1], [[[1]]], 0)
- assert_raises(TypeError, herm.hermfit, [1, 2], [1], 0)
- assert_raises(TypeError, herm.hermfit, [1], [1, 2], 0)
- assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[[1]])
- assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[1, 1])
+ assert_raises(ValueError, herm.hermfit, [1], [1], -1)
+ assert_raises(TypeError, herm.hermfit, [[1]], [1], 0)
+ assert_raises(TypeError, herm.hermfit, [], [1], 0)
+ assert_raises(TypeError, herm.hermfit, [1], [[[1]]], 0)
+ assert_raises(TypeError, herm.hermfit, [1, 2], [1], 0)
+ assert_raises(TypeError, herm.hermfit, [1], [1, 2], 0)
+ assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[[1]])
+ assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[1, 1])
# Test fit
x = np.linspace(0, 2)
diff --git a/numpy/polynomial/tests/test_hermite_e.py b/numpy/polynomial/tests/test_hermite_e.py
index 5341dc7ff..f8601a828 100644
--- a/numpy/polynomial/tests/test_hermite_e.py
+++ b/numpy/polynomial/tests/test_hermite_e.py
@@ -6,7 +6,9 @@ from __future__ import division, absolute_import, print_function
import numpy as np
import numpy.polynomial.hermite_e as herme
from numpy.polynomial.polynomial import polyval
-from numpy.testing import *
+from numpy.testing import (
+ TestCase, assert_almost_equal, assert_raises,
+ assert_equal, assert_, run_module_suite)
He0 = np.array([1])
He1 = np.array([0, 1])
@@ -117,7 +119,6 @@ class TestEvaluation(TestCase):
y = [polyval(x, c) for c in Helist]
for i in range(10):
msg = "At i=%d" % i
- ser = np.zeros
tgt = y[i]
res = herme.hermeval(x, [0]*i + [1])
assert_almost_equal(res, tgt, err_msg=msg)
@@ -388,14 +389,14 @@ class TestFitting(TestCase):
return x*(x - 1)*(x - 2)
# Test exceptions
- assert_raises(ValueError, herme.hermefit, [1], [1], -1)
- assert_raises(TypeError, herme.hermefit, [[1]], [1], 0)
- assert_raises(TypeError, herme.hermefit, [], [1], 0)
- assert_raises(TypeError, herme.hermefit, [1], [[[1]]], 0)
- assert_raises(TypeError, herme.hermefit, [1, 2], [1], 0)
- assert_raises(TypeError, herme.hermefit, [1], [1, 2], 0)
- assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[[1]])
- assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[1, 1])
+ assert_raises(ValueError, herme.hermefit, [1], [1], -1)
+ assert_raises(TypeError, herme.hermefit, [[1]], [1], 0)
+ assert_raises(TypeError, herme.hermefit, [], [1], 0)
+ assert_raises(TypeError, herme.hermefit, [1], [[[1]]], 0)
+ assert_raises(TypeError, herme.hermefit, [1, 2], [1], 0)
+ assert_raises(TypeError, herme.hermefit, [1], [1, 2], 0)
+ assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[[1]])
+ assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[1, 1])
# Test fit
x = np.linspace(0, 2)
diff --git a/numpy/polynomial/tests/test_laguerre.py b/numpy/polynomial/tests/test_laguerre.py
index b3d8fe5ee..1dc57a960 100644
--- a/numpy/polynomial/tests/test_laguerre.py
+++ b/numpy/polynomial/tests/test_laguerre.py
@@ -116,7 +116,6 @@ class TestEvaluation(TestCase):
y = [polyval(x, c) for c in Llist]
for i in range(7):
msg = "At i=%d" % i
- ser = np.zeros
tgt = y[i]
res = lag.lagval(x, [0]*i + [1])
assert_almost_equal(res, tgt, err_msg=msg)
@@ -386,14 +385,14 @@ class TestFitting(TestCase):
return x*(x - 1)*(x - 2)
# Test exceptions
- assert_raises(ValueError, lag.lagfit, [1], [1], -1)
- assert_raises(TypeError, lag.lagfit, [[1]], [1], 0)
- assert_raises(TypeError, lag.lagfit, [], [1], 0)
- assert_raises(TypeError, lag.lagfit, [1], [[[1]]], 0)
- assert_raises(TypeError, lag.lagfit, [1, 2], [1], 0)
- assert_raises(TypeError, lag.lagfit, [1], [1, 2], 0)
- assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[[1]])
- assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[1, 1])
+ assert_raises(ValueError, lag.lagfit, [1], [1], -1)
+ assert_raises(TypeError, lag.lagfit, [[1]], [1], 0)
+ assert_raises(TypeError, lag.lagfit, [], [1], 0)
+ assert_raises(TypeError, lag.lagfit, [1], [[[1]]], 0)
+ assert_raises(TypeError, lag.lagfit, [1, 2], [1], 0)
+ assert_raises(TypeError, lag.lagfit, [1], [1, 2], 0)
+ assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[[1]])
+ assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[1, 1])
# Test fit
x = np.linspace(0, 2)
diff --git a/numpy/polynomial/tests/test_legendre.py b/numpy/polynomial/tests/test_legendre.py
index e248f005d..8ac1feb58 100644
--- a/numpy/polynomial/tests/test_legendre.py
+++ b/numpy/polynomial/tests/test_legendre.py
@@ -120,7 +120,6 @@ class TestEvaluation(TestCase):
y = [polyval(x, c) for c in Llist]
for i in range(10):
msg = "At i=%d" % i
- ser = np.zeros
tgt = y[i]
res = leg.legval(x, [0]*i + [1])
assert_almost_equal(res, tgt, err_msg=msg)
@@ -390,14 +389,14 @@ class TestFitting(TestCase):
return x*(x - 1)*(x - 2)
# Test exceptions
- assert_raises(ValueError, leg.legfit, [1], [1], -1)
- assert_raises(TypeError, leg.legfit, [[1]], [1], 0)
- assert_raises(TypeError, leg.legfit, [], [1], 0)
- assert_raises(TypeError, leg.legfit, [1], [[[1]]], 0)
- assert_raises(TypeError, leg.legfit, [1, 2], [1], 0)
- assert_raises(TypeError, leg.legfit, [1], [1, 2], 0)
- assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[[1]])
- assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[1, 1])
+ assert_raises(ValueError, leg.legfit, [1], [1], -1)
+ assert_raises(TypeError, leg.legfit, [[1]], [1], 0)
+ assert_raises(TypeError, leg.legfit, [], [1], 0)
+ assert_raises(TypeError, leg.legfit, [1], [[[1]]], 0)
+ assert_raises(TypeError, leg.legfit, [1, 2], [1], 0)
+ assert_raises(TypeError, leg.legfit, [1], [1, 2], 0)
+ assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[[1]])
+ assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[1, 1])
# Test fit
x = np.linspace(0, 2)
diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py
index 77092cd2f..c806a8497 100644
--- a/numpy/polynomial/tests/test_polynomial.py
+++ b/numpy/polynomial/tests/test_polynomial.py
@@ -420,14 +420,14 @@ class TestMisc(TestCase):
return x*(x - 1)*(x - 2)
# Test exceptions
- assert_raises(ValueError, poly.polyfit, [1], [1], -1)
- assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
- assert_raises(TypeError, poly.polyfit, [], [1], 0)
- assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
- assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
- assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
- assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]])
- assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1])
+ assert_raises(ValueError, poly.polyfit, [1], [1], -1)
+ assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
+ assert_raises(TypeError, poly.polyfit, [], [1], 0)
+ assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
+ assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
+ assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
+ assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]])
+ assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1])
# Test fit
x = np.linspace(0, 2)
diff --git a/numpy/polynomial/tests/test_polyutils.py b/numpy/polynomial/tests/test_polyutils.py
index c77ee2435..974e2e09a 100644
--- a/numpy/polynomial/tests/test_polyutils.py
+++ b/numpy/polynomial/tests/test_polyutils.py
@@ -5,7 +5,9 @@ from __future__ import division, absolute_import, print_function
import numpy as np
import numpy.polynomial.polyutils as pu
-from numpy.testing import *
+from numpy.testing import (
+ TestCase, assert_almost_equal, assert_raises,
+ assert_equal, assert_, run_module_suite)
class TestMisc(TestCase):
@@ -101,3 +103,7 @@ class TestDomain(TestCase):
tgt = [-1 + 1j, 1 - 1j]
res = pu.mapparms(dom1, dom2)
assert_almost_equal(res, tgt)
+
+
+if __name__ == "__main__":
+ run_module_suite()