diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2010-05-07 02:25:08 +0000 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2010-05-07 02:25:08 +0000 |
commit | b66a200a4a1e98f1955c8a774e4ebfb4588dab5b (patch) | |
tree | 26252c2c011e0f6a168c63e5c38537d36672a8b3 /numpy/lib | |
parent | 20757e2fe36792e67613e3231e10eaa109660fea (diff) | |
download | numpy-b66a200a4a1e98f1955c8a774e4ebfb4588dab5b.tar.gz |
BUG: Make interp handle zero dimensional ndarrays as interpolation
points. Add some tests for interp. Fixes ticket #1177.
unc_api.txt.tmp
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/function_base.py | 14 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 30 |
2 files changed, 37 insertions, 7 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 3f293ae4c..680103924 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -55,7 +55,7 @@ def histogram(a, bins=10, range=None, normed=False, weights=None): range : (float, float), optional The lower and upper range of the bins. If not provided, range is simply ``(a.min(), a.max())``. Values outside the range are - ignored. + ignored. normed : bool, optional If False, the result will contain the number of samples in each bin. If True, the result is the value of the @@ -68,7 +68,7 @@ def histogram(a, bins=10, range=None, normed=False, weights=None): only contributes its associated weight towards the bin count (instead of 1). If `normed` is True, the weights are normalized, so that the integral of the density over the range remains 1 - + Returns ------- hist : array @@ -76,7 +76,7 @@ def histogram(a, bins=10, range=None, normed=False, weights=None): description of the possible semantics. bin_edges : array of dtype float Return the bin edges ``(length(hist)+1)``. - + See Also -------- @@ -112,7 +112,7 @@ def histogram(a, bins=10, range=None, normed=False, weights=None): 1.0 """ - + a = asarray(a) if weights is not None: weights = asarray(weights) @@ -985,6 +985,8 @@ def interp(x, xp, fp, left=None, right=None): """ if isinstance(x, (float, int, number)): return compiled_interp([x], xp, fp, left, right).item() + elif isinstance(x, np.ndarray) and x.ndim == 0: + return compiled_interp([x], xp, fp, left, right).item() else: return compiled_interp(x, xp, fp, left, right) @@ -2043,7 +2045,7 @@ def blackman(M): >>> plt.xlabel("Sample") <matplotlib.text.Text object at 0x...> >>> plt.show() - + >>> plt.figure() <matplotlib.figure.Figure object at 0x...> >>> A = fft(window, 2048) / 25.5 @@ -2152,7 +2154,7 @@ def bartlett(M): >>> plt.xlabel("Sample") <matplotlib.text.Text object at 0x...> >>> plt.show() - + >>> plt.figure() <matplotlib.figure.Figure object at 0x...> >>> A = fft(window, 2048) / 25.5 diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 6381c0a72..10e3790ea 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -69,7 +69,7 @@ class TestAverage(TestCase): actual = average(y, weights=w) desired = (arange(10) ** 2).sum()*1. / arange(10).sum() assert_almost_equal(actual, desired) - + y1 = array([[1, 2, 3], [4, 5, 6]]) w0 = [1, 2] actual = average(y1, weights=w0, axis=0) @@ -934,6 +934,34 @@ class TestBincount(TestCase): assert_array_equal(y, np.array([0, 0.2, 0.5, 0, 0.5, 0.1])) +class TestInterp(TestCase): + def test_basic(self): + x = np.linspace(0, 1, 5) + y = np.linspace(0, 1, 5) + x0 = np.linspace(0, 1, 50) + assert_almost_equal(np.interp(x0, x, y), x0) + + def test_scalar_interpolation_point(self): + x = np.linspace(0, 1, 5) + y = np.linspace(0, 1, 5) + x0 = 0 + assert_almost_equal(np.interp(x0, x, y), x0) + x0 = .3 + assert_almost_equal(np.interp(x0, x, y), x0) + x0 = np.float32(.3) + assert_almost_equal(np.interp(x0, x, y), x0) + x0 = np.float64(.3) + assert_almost_equal(np.interp(x0, x, y), x0) + + def test_zero_dimensional_interpolation_point(self): + x = np.linspace(0, 1, 5) + y = np.linspace(0, 1, 5) + x0 = np.array(.3) + assert_almost_equal(np.interp(x0, x, y), x0) + x0 = np.array(.3, dtype=object) + assert_almost_equal(np.interp(x0, x, y), .3) + + def compare_results(res, desired): for i in range(len(desired)): assert_array_equal(res[i], desired[i]) |