[Scipy-svn] r4674 - in trunk/scipy/cluster: src tests

scipy-svn@scip... scipy-svn@scip...
Mon Aug 25 07:11:05 CDT 2008


Author: damian.eads
Date: 2008-08-25 07:10:57 -0500 (Mon, 25 Aug 2008)
New Revision: 4674

Modified:
   trunk/scipy/cluster/src/distance.c
   trunk/scipy/cluster/tests/test_distance.py
   trunk/scipy/cluster/tests/test_hierarchy.py
Log:
Fixed bug in C version of Canberra.

Modified: trunk/scipy/cluster/src/distance.c
===================================================================
--- trunk/scipy/cluster/src/distance.c	2008-08-24 21:05:23 UTC (rev 4673)
+++ trunk/scipy/cluster/src/distance.c	2008-08-25 12:10:57 UTC (rev 4674)
@@ -73,11 +73,13 @@
 
 static inline double canberra_distance(const double *u, const double *v, int n) {
   int i;
-  double s = 0.0;
+  double snum = 0.0, sdenom_u = 0.0, sdenom_v = 0.0;
   for (i = 0; i < n; i++) {
-    s += (fabs(u[i] - v[i]) / (fabs(u[i]) + fabs(v[i])));
+    snum += fabs(u[i] - v[i]);
+    sdenom_u += fabs(u[i]);
+    sdenom_v += fabs(v[i]);
   }
-  return s;
+  return snum / (sdenom_u + sdenom_v);
 }
 
 static inline double bray_curtis_distance(const double *u, const double *v, int n) {

Modified: trunk/scipy/cluster/tests/test_distance.py
===================================================================
--- trunk/scipy/cluster/tests/test_distance.py	2008-08-24 21:05:23 UTC (rev 4673)
+++ trunk/scipy/cluster/tests/test_distance.py	2008-08-25 12:10:57 UTC (rev 4674)
@@ -944,6 +944,17 @@
         print np.abs(y1-y2).max()
         self.failUnless(within_tol(y1, y2, eps))
 
+    def test_pdist_canberra_match(self):
+        "Tests pdist(X, 'canberra') to see if the two implementations match on random double input data."
+        D = eo['iris']
+        print D.shape, D.dtype
+        eps = 1e-10
+        y1 = pdist(D, "canberra")
+        y2 = pdist(D, "test_canberra")
+        print np.abs(y1-y2).max()
+        self.failUnless(within_tol(y1, y2, eps))
+
+
 def within_tol(a, b, tol):
     return np.abs(a - b).max() < tol
 

Modified: trunk/scipy/cluster/tests/test_hierarchy.py
===================================================================
--- trunk/scipy/cluster/tests/test_hierarchy.py	2008-08-24 21:05:23 UTC (rev 4673)
+++ trunk/scipy/cluster/tests/test_hierarchy.py	2008-08-25 12:10:57 UTC (rev 4674)
@@ -43,11 +43,11 @@
 from scipy.cluster.distance import squareform, pdist, matching, jaccard, dice, sokalsneath, rogerstanimoto, russellrao, yule, numobs_dm, numobs_y
 
 _tdist = np.array([[0,    662,  877,  255,  412,  996],
-                      [662,  0,    295,  468,  268,  400],
-                      [877,  295,  0,    754,  564,  138],
-                      [255,  468,  754,  0,    219,  869],
-                      [412,  268,  564,  219,  0,    669],
-                      [996,  400,  138,  869,  669,  0  ]], dtype='double')
+                   [662,  0,    295,  468,  268,  400],
+                   [877,  295,  0,    754,  564,  138],
+                   [255,  468,  754,  0,    219,  869],
+                   [412,  268,  564,  219,  0,    669],
+                   [996,  400,  138,  869,  669,  0  ]], dtype='double')
 
 _ytdist = squareform(_tdist)
 



More information about the Scipy-svn mailing list