From 55c5a54db79687dc7b8fcdc60e307627b5c2c36d Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 25 Nov 2017 22:36:40 -0800 Subject: [PATCH] Vectorize and document simple_linear_interpolation. --- lib/matplotlib/cbook/__init__.py | 36 +++++++++++++++++--------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 94db12478fe8..0b6a4968b113 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1557,24 +1557,26 @@ def get_siblings(self, a): def simple_linear_interpolation(a, steps): - if steps == 1: - return a - - steps = int(np.floor(steps)) - new_length = ((len(a) - 1) * steps) + 1 - new_shape = list(a.shape) - new_shape[0] = new_length - result = np.zeros(new_shape, a.dtype) - - result[0] = a[0] - a0 = a[0:-1] - a1 = a[1:] - delta = ((a1 - a0) / steps) - for i in range(1, steps): - result[i::steps] = delta * i + a0 - result[steps::steps] = a1 + """ + Resample an array with ``steps - 1`` points between original point pairs. - return result + Parameters + ---------- + a : array, shape (n, ...) + steps : int + + Returns + ------- + array, shape ``((n - 1) * steps + 1, ...)`` + + Along each column of *a*, ``(steps - 1)`` points are introduced between + each original values; the values are linearly interpolated. + """ + fps = a.reshape((len(a), -1)) + xp = np.arange(len(a)) * steps + x = np.arange((len(a) - 1) * steps + 1) + return (np.column_stack([np.interp(x, xp, fp) for fp in fps.T]) + .reshape((len(x),) + a.shape[1:])) @deprecated('2.1', alternative='shutil.rmtree')