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

scipy-svn@scip... scipy-svn@scip...
Sat Nov 1 10:12:24 CDT 2008


Author: ptvirtan
Date: 2008-11-01 10:12:11 -0500 (Sat, 01 Nov 2008)
New Revision: 4895

Modified:
   trunk/scipy/interpolate/rbf.py
   trunk/scipy/interpolate/tests/test_rbf.py
Log:
interpolate.Rbf: make epsilon behave for 'gaussian' similarly as for multiquadrics

Also add a test that checks that for a simple 1D smooth test case, Rbf builds
a regular approximation that is valid also away from nodes.

Modified: trunk/scipy/interpolate/rbf.py
===================================================================
--- trunk/scipy/interpolate/rbf.py	2008-11-01 15:00:54 UTC (rev 4894)
+++ trunk/scipy/interpolate/rbf.py	2008-11-01 15:12:11 UTC (rev 4895)
@@ -64,7 +64,7 @@
 
             'multiquadric': sqrt((r/self.epsilon)**2 + 1)
             'inverse multiquadric': 1.0/sqrt((r/self.epsilon)**2 + 1)
-            'gaussian': exp(-(self.epsilon*r)**2)
+            'gaussian': exp(-(r/self.epsilon)**2)
             'linear': r
             'cubic': r**3
             'quintic': r**5
@@ -105,7 +105,7 @@
         elif self.function.lower() == 'inverse multiquadric':
             return 1.0/sqrt((1.0/self.epsilon*r)**2 + 1)
         elif self.function.lower() == 'gaussian':
-            return exp(-(self.epsilon*r)**2)
+            return exp(-(1.0/self.epsilon*r)**2)
         elif self.function.lower() == 'linear':
             return r
         elif self.function.lower() == 'cubic':

Modified: trunk/scipy/interpolate/tests/test_rbf.py
===================================================================
--- trunk/scipy/interpolate/tests/test_rbf.py	2008-11-01 15:00:54 UTC (rev 4894)
+++ trunk/scipy/interpolate/tests/test_rbf.py	2008-11-01 15:12:11 UTC (rev 4895)
@@ -3,13 +3,14 @@
 """ Test functions for rbf module """
 
 from numpy.testing import assert_array_almost_equal, assert_almost_equal
-from numpy import linspace, sin, random, exp
+from numpy import linspace, sin, random, exp, log10
 from scipy.interpolate.rbf import Rbf
 
 FUNCTIONS = ('multiquadric', 'inverse multiquadric', 'gaussian',
              'cubic', 'quintic', 'thin-plate', 'linear')
 
-def check_rbf1d(function):
+def check_rbf1d_interpolation(function):
+    """Check that the Rbf function interpolates throught the nodes (1D)"""
     x = linspace(0,10,9)
     y = sin(x)
     rbf = Rbf(x, y, function=function)
@@ -17,7 +18,8 @@
     assert_array_almost_equal(y, yi)
     assert_almost_equal(rbf(float(x[0])), y[0])
 
-def check_rbf2d(function):
+def check_rbf2d_interpolation(function):
+    """Check that the Rbf function interpolates throught the nodes (2D)"""
     x = random.rand(50,1)*4-2
     y = random.rand(50,1)*4-2
     z = x*exp(-x**2-1j*y**2)
@@ -26,7 +28,8 @@
     zi.shape = x.shape
     assert_array_almost_equal(z, zi)
 
-def check_rbf3d(function):
+def check_rbf3d_interpolation(function):
+    """Check that the Rbf function interpolates throught the nodes (3D)"""
     x = random.rand(50,1)*4-2
     y = random.rand(50,1)*4-2
     z = random.rand(50,1)*4-2
@@ -38,6 +41,35 @@
 
 def test_rbf_interpolation():
     for function in FUNCTIONS:
-        yield check_rbf1d, function
-        yield check_rbf2d, function
-        yield check_rbf3d, function
+        yield check_rbf1d_interpolation, function
+        yield check_rbf2d_interpolation, function
+        yield check_rbf3d_interpolation, function
+
+def check_rbf1d_regularity(function, atol):
+    """Check that the Rbf function approximates a smooth function well away
+    from the nodes."""
+    x = linspace(0, 10, 9)
+    y = sin(x)
+    rbf = Rbf(x, y, function=function)
+    xi = linspace(0, 10, 100)
+    yi = rbf(xi)
+    #import matplotlib.pyplot as plt
+    #plt.figure()
+    #plt.plot(x, y, 'o', xi, sin(xi), ':', xi, yi, '-')
+    #plt.title(function)
+    #plt.show()
+    assert_array_almost_equal(yi, sin(xi), decimal=-log10(atol),
+                              err_msg="abs-diff: %f" % abs(yi - sin(xi)).max())
+
+def test_rbf_regularity():
+    tolerances = {
+        'multiquadric': 0.05,
+        'inverse multiquadric': 0.01,
+        'gaussian': 0.01,
+        'cubic': 0.1,
+        'quintic': 0.1,
+        'thin-plate': 0.1,
+        'linear': 0.2
+    }
+    for function in FUNCTIONS:
+        yield check_rbf1d_regularity, function, tolerances.get(function, 1e-2)



More information about the Scipy-svn mailing list