summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2019-03-02 15:44:25 -0800
committerEric Wieser <wieser.eric@gmail.com>2019-03-11 22:26:43 -0700
commit1bb279ad4f25d987155106ee6f82ba7fc83ce5a0 (patch)
treed5bc7027dd67bc96f06da27af3211d1cc2693bc7 /numpy
parent379995dcc10fd9c58336420bd995b6d8a1b61288 (diff)
downloadnumpy-1bb279ad4f25d987155106ee6f82ba7fc83ce5a0.tar.gz
MAINT: Move duplicate implementations of ABCPolyBase._fromroots into polyutils
Every implementation is the same right now, other than calling different line / mul functions. Found by LGTM.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/polynomial/chebyshev.py16
-rw-r--r--numpy/polynomial/hermite.py16
-rw-r--r--numpy/polynomial/hermite_e.py16
-rw-r--r--numpy/polynomial/laguerre.py16
-rw-r--r--numpy/polynomial/legendre.py16
-rw-r--r--numpy/polynomial/polynomial.py16
-rw-r--r--numpy/polynomial/polyutils.py30
7 files changed, 36 insertions, 90 deletions
diff --git a/numpy/polynomial/chebyshev.py b/numpy/polynomial/chebyshev.py
index e8efe1cb9..70527ec79 100644
--- a/numpy/polynomial/chebyshev.py
+++ b/numpy/polynomial/chebyshev.py
@@ -541,21 +541,7 @@ def chebfromroots(roots):
array([1.5+0.j, 0. +0.j, 0.5+0.j])
"""
- if len(roots) == 0:
- return np.ones(1)
- else:
- [roots] = pu.as_series([roots], trim=False)
- roots.sort()
- p = [chebline(-r, 1) for r in roots]
- n = len(p)
- while n > 1:
- m, r = divmod(n, 2)
- tmp = [chebmul(p[i], p[i+m]) for i in range(m)]
- if r:
- tmp[0] = chebmul(tmp[0], p[-1])
- p = tmp
- n = m
- return p[0]
+ return pu._fromroots(chebline, chebmul, roots)
def chebadd(c1, c2):
diff --git a/numpy/polynomial/hermite.py b/numpy/polynomial/hermite.py
index 93c9fc564..1dd2563bb 100644
--- a/numpy/polynomial/hermite.py
+++ b/numpy/polynomial/hermite.py
@@ -286,21 +286,7 @@ def hermfromroots(roots):
array([0.+0.j, 0.+0.j])
"""
- if len(roots) == 0:
- return np.ones(1)
- else:
- [roots] = pu.as_series([roots], trim=False)
- roots.sort()
- p = [hermline(-r, 1) for r in roots]
- n = len(p)
- while n > 1:
- m, r = divmod(n, 2)
- tmp = [hermmul(p[i], p[i+m]) for i in range(m)]
- if r:
- tmp[0] = hermmul(tmp[0], p[-1])
- p = tmp
- n = m
- return p[0]
+ return pu._fromroots(hermline, hermmul, roots)
def hermadd(c1, c2):
diff --git a/numpy/polynomial/hermite_e.py b/numpy/polynomial/hermite_e.py
index f6ba64301..282d5da90 100644
--- a/numpy/polynomial/hermite_e.py
+++ b/numpy/polynomial/hermite_e.py
@@ -287,21 +287,7 @@ def hermefromroots(roots):
array([0.+0.j, 0.+0.j])
"""
- if len(roots) == 0:
- return np.ones(1)
- else:
- [roots] = pu.as_series([roots], trim=False)
- roots.sort()
- p = [hermeline(-r, 1) for r in roots]
- n = len(p)
- while n > 1:
- m, r = divmod(n, 2)
- tmp = [hermemul(p[i], p[i+m]) for i in range(m)]
- if r:
- tmp[0] = hermemul(tmp[0], p[-1])
- p = tmp
- n = m
- return p[0]
+ return pu._fromroots(hermeline, hermemul, roots)
def hermeadd(c1, c2):
diff --git a/numpy/polynomial/laguerre.py b/numpy/polynomial/laguerre.py
index fbd8e8ab7..337c71dd3 100644
--- a/numpy/polynomial/laguerre.py
+++ b/numpy/polynomial/laguerre.py
@@ -283,21 +283,7 @@ def lagfromroots(roots):
array([0.+0.j, 0.+0.j])
"""
- if len(roots) == 0:
- return np.ones(1)
- else:
- [roots] = pu.as_series([roots], trim=False)
- roots.sort()
- p = [lagline(-r, 1) for r in roots]
- n = len(p)
- while n > 1:
- m, r = divmod(n, 2)
- tmp = [lagmul(p[i], p[i+m]) for i in range(m)]
- if r:
- tmp[0] = lagmul(tmp[0], p[-1])
- p = tmp
- n = m
- return p[0]
+ return pu._fromroots(lagline, lagmul, roots)
def lagadd(c1, c2):
diff --git a/numpy/polynomial/legendre.py b/numpy/polynomial/legendre.py
index b5e4c9bef..0037f3ad0 100644
--- a/numpy/polynomial/legendre.py
+++ b/numpy/polynomial/legendre.py
@@ -314,21 +314,7 @@ def legfromroots(roots):
array([ 1.33333333+0.j, 0.00000000+0.j, 0.66666667+0.j]) # may vary
"""
- if len(roots) == 0:
- return np.ones(1)
- else:
- [roots] = pu.as_series([roots], trim=False)
- roots.sort()
- p = [legline(-r, 1) for r in roots]
- n = len(p)
- while n > 1:
- m, r = divmod(n, 2)
- tmp = [legmul(p[i], p[i+m]) for i in range(m)]
- if r:
- tmp[0] = legmul(tmp[0], p[-1])
- p = tmp
- n = m
- return p[0]
+ return pu._fromroots(legline, legmul, roots)
def legadd(c1, c2):
diff --git a/numpy/polynomial/polynomial.py b/numpy/polynomial/polynomial.py
index 3cb6765da..2b6c9fd0a 100644
--- a/numpy/polynomial/polynomial.py
+++ b/numpy/polynomial/polynomial.py
@@ -188,21 +188,7 @@ def polyfromroots(roots):
array([1.+0.j, 0.+0.j, 1.+0.j])
"""
- if len(roots) == 0:
- return np.ones(1)
- else:
- [roots] = pu.as_series([roots], trim=False)
- roots.sort()
- p = [polyline(-r, 1) for r in roots]
- n = len(p)
- while n > 1:
- m, r = divmod(n, 2)
- tmp = [polymul(p[i], p[i+m]) for i in range(m)]
- if r:
- tmp[0] = polymul(tmp[0], p[-1])
- p = tmp
- n = m
- return p[0]
+ return pu._fromroots(polyline, polymul, roots)
def polyadd(c1, c2):
diff --git a/numpy/polynomial/polyutils.py b/numpy/polynomial/polyutils.py
index 4054d77ff..9482ed89f 100644
--- a/numpy/polynomial/polyutils.py
+++ b/numpy/polynomial/polyutils.py
@@ -459,3 +459,33 @@ def _vander3d(vander_f, x, y, z, deg):
vz = vander_f(z, degz)
v = vx[..., None, None]*vy[..., None,:, None]*vz[..., None, None,:]
return v.reshape(v.shape[:-3] + (-1,))
+
+
+def _fromroots(line_f, mul_f, roots):
+ """
+ Helper function used to implement the ``<type>fromroots`` functions.
+
+ Parameters
+ ----------
+ line_f : function(float, float) -> ndarray
+ The ``<type>line`` function, such as ``polyline``
+ mul_f : function(array_like, array_like) -> ndarray
+ The ``<type>mul`` function, such as ``polymul``
+ roots :
+ See the ``<type>fromroots`` functions for more detail
+ """
+ if len(roots) == 0:
+ return np.ones(1)
+ else:
+ [roots] = as_series([roots], trim=False)
+ roots.sort()
+ p = [line_f(-r, 1) for r in roots]
+ n = len(p)
+ while n > 1:
+ m, r = divmod(n, 2)
+ tmp = [mul_f(p[i], p[i+m]) for i in range(m)]
+ if r:
+ tmp[0] = mul_f(tmp[0], p[-1])
+ p = tmp
+ n = m
+ return p[0]