diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2019-03-02 15:44:25 -0800 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2019-03-11 22:26:43 -0700 |
commit | 1bb279ad4f25d987155106ee6f82ba7fc83ce5a0 (patch) | |
tree | d5bc7027dd67bc96f06da27af3211d1cc2693bc7 /numpy/polynomial/polyutils.py | |
parent | 379995dcc10fd9c58336420bd995b6d8a1b61288 (diff) | |
download | numpy-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/polynomial/polyutils.py')
-rw-r--r-- | numpy/polynomial/polyutils.py | 30 |
1 files changed, 30 insertions, 0 deletions
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] |