diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2019-03-16 20:38:50 -0700 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2019-03-16 21:08:44 -0700 |
commit | ddbc31c2d71285f426dc16fab8ac2e00ffe6c1f8 (patch) | |
tree | a4f4a4e73eb900d1294ee56dd867da5143aeed31 /numpy/polynomial/polyutils.py | |
parent | 0764929543c85decde9d664367dbf7d8f137fe1f (diff) | |
download | numpy-ddbc31c2d71285f426dc16fab8ac2e00ffe6c1f8.tar.gz |
MAINT: Unify polynomial power functions
These power functions are all the same - the algorithm used does not care about the basis.
`polypow` and `chebpow` have some optimizations in their versions, which this maintains
Diffstat (limited to 'numpy/polynomial/polyutils.py')
-rw-r--r-- | numpy/polynomial/polyutils.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/numpy/polynomial/polyutils.py b/numpy/polynomial/polyutils.py index 1a73f47d8..5128e35e0 100644 --- a/numpy/polynomial/polyutils.py +++ b/numpy/polynomial/polyutils.py @@ -690,6 +690,39 @@ def _fit(vander_f, x, y, deg, rcond=None, full=False, w=None): return c +def _pow(mul_f, c, pow, maxpower): + """ + Helper function used to implement the ``<type>pow`` functions. + + Parameters + ---------- + vander_f : function(array_like, int) -> ndarray + The 1d vander function, such as ``polyvander`` + pow, maxpower : + See the ``<type>pow`` functions for more detail + mul_f : function(array_like, array_like) -> ndarray + The ``<type>mul`` function, such as ``polymul`` + """ + # c is a trimmed copy + [c] = as_series([c]) + power = int(pow) + if power != pow or power < 0: + raise ValueError("Power must be a non-negative integer.") + elif maxpower is not None and power > maxpower: + raise ValueError("Power is too large") + elif power == 0: + return np.array([1], dtype=c.dtype) + elif power == 1: + return c + 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): + prd = mul_f(prd, c) + return prd + + def _deprecate_as_int(x, desc): """ Like `operator.index`, but emits a deprecation warning when passed a float |