Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2cb05d

Browse files
authored
Merge pull request #9860 from anntzer/simple-linear-interp
Vectorize and document simple_linear_interpolation.
2 parents 045c951 + 55c5a54 commit a2cb05d

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,24 +1557,26 @@ def get_siblings(self, a):
15571557

15581558

15591559
def simple_linear_interpolation(a, steps):
1560-
if steps == 1:
1561-
return a
1562-
1563-
steps = int(np.floor(steps))
1564-
new_length = ((len(a) - 1) * steps) + 1
1565-
new_shape = list(a.shape)
1566-
new_shape[0] = new_length
1567-
result = np.zeros(new_shape, a.dtype)
1568-
1569-
result[0] = a[0]
1570-
a0 = a[0:-1]
1571-
a1 = a[1:]
1572-
delta = ((a1 - a0) / steps)
1573-
for i in range(1, steps):
1574-
result[i::steps] = delta * i + a0
1575-
result[steps::steps] = a1
1560+
"""
1561+
Resample an array with ``steps - 1`` points between original point pairs.
15761562
1577-
return result
1563+
Parameters
1564+
----------
1565+
a : array, shape (n, ...)
1566+
steps : int
1567+
1568+
Returns
1569+
-------
1570+
array, shape ``((n - 1) * steps + 1, ...)``
1571+
1572+
Along each column of *a*, ``(steps - 1)`` points are introduced between
1573+
each original values; the values are linearly interpolated.
1574+
"""
1575+
fps = a.reshape((len(a), -1))
1576+
xp = np.arange(len(a)) * steps
1577+
x = np.arange((len(a) - 1) * steps + 1)
1578+
return (np.column_stack([np.interp(x, xp, fp) for fp in fps.T])
1579+
.reshape((len(x),) + a.shape[1:]))
15781580

15791581

15801582
@deprecated('2.1', alternative='shutil.rmtree')

0 commit comments

Comments
 (0)