[Scipy-svn] r5268 - in trunk/scipy/interpolate: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Tue Dec 16 04:35:01 EST 2008


Author: ptvirtan
Date: 2008-12-16 03:34:47 -0600 (Tue, 16 Dec 2008)
New Revision: 5268

Modified:
   trunk/scipy/interpolate/interpolate.py
   trunk/scipy/interpolate/tests/test_interpolate.py
Log:
interp1d: Fix spurious casting of complex data to real. Fail early with complex data + splines -- these don't work yet due to _fitpack.bisplev being written in C. Add tests with complex data

Modified: trunk/scipy/interpolate/interpolate.py
===================================================================
--- trunk/scipy/interpolate/interpolate.py	2008-12-15 19:01:36 UTC (rev 5267)
+++ trunk/scipy/interpolate/interpolate.py	2008-12-16 09:34:47 UTC (rev 5268)
@@ -217,7 +217,7 @@
             raise ValueError("the y array must have at least one dimension.")
 
         # Force-cast y to a floating-point type, if it's not yet one
-        if not isinstance(y.dtype.type, np.inexact):
+        if not issubclass(y.dtype.type, np.inexact):
             y = y.astype(np.float_)
 
         # Normalize the axis to ensure that it is positive.
@@ -248,6 +248,9 @@
             self._call = self._call_spline
             self._spline = splmake(x,oriented_y,order=order)
 
+            if issubclass(y.dtype.type, np.complexfloating):
+                raise ValueError("Input data must be real for spline interpolation")
+
         len_x = len(x)
         if len_x != len_y:
             raise ValueError("x and y arrays must be equal in length along "

Modified: trunk/scipy/interpolate/tests/test_interpolate.py
===================================================================
--- trunk/scipy/interpolate/tests/test_interpolate.py	2008-12-15 19:01:36 UTC (rev 5267)
+++ trunk/scipy/interpolate/tests/test_interpolate.py	2008-12-16 09:34:47 UTC (rev 5268)
@@ -292,6 +292,35 @@
             yield self._nd_check_interp, kind
             yield self._nd_check_shape, kind
 
+    def _check_complex(self, dtype=np.complex_, kind='linear', fail=False):
+        x = np.arange(10).astype(np.int_)
+        y = np.arange(10).astype(np.int_) * (1 + 2j)
+        y = y.astype(dtype)
+        if fail:
+            assert_raises(ValueError, interp1d, x, y, kind=kind)
+        else:
+            c = interp1d(x, y, kind=kind)
+            assert_array_almost_equal(y[:-1], c(x)[:-1])
+        assert (y.dtype == dtype) or not issubclass(y.dtype.type, np.inexact)
+
+    def test_complex(self):
+        for kind in ('linear', 'nearest'):
+            yield self._check_complex, np.complex64, kind
+            yield self._check_complex, np.complex128, kind
+            yield self._check_complex, np.float32, kind
+            yield self._check_complex, np.float64, kind
+
+        # The spline methods can't handle complex values, because the code
+        # for _fitpack.bispleval is written in C and is not type-agnostic.
+        #
+        # Check that a ValueError is raised if one attempts to interpolate
+        # complex data using these routines.
+        for kind in ('cubic', 'slinear', 'quadratic', 'zero'):
+            yield self._check_complex, np.complex64, kind, True
+            yield self._check_complex, np.complex128, kind, True
+            yield self._check_complex, np.float32, kind
+            yield self._check_complex, np.float64, kind
+
     @dec.knownfailureif(True, "zero-order splines fail for the last point")
     def test_nd_zero_spline(self):
         # zero-order splines don't get the last point right,




More information about the Scipy-svn mailing list