[Scipy-svn] r4692 - in trunk/scipy/cluster: . src

scipy-svn@scip... scipy-svn@scip...
Fri Sep 5 13:05:58 CDT 2008


Author: damian.eads
Date: 2008-09-05 13:05:54 -0500 (Fri, 05 Sep 2008)
New Revision: 4692

Modified:
   trunk/scipy/cluster/distance.py
   trunk/scipy/cluster/src/distance.c
   trunk/scipy/cluster/src/distance.h
Log:
Added some code for computing distances between two sets of vectors, instead of pairwise distances.

Modified: trunk/scipy/cluster/distance.py
===================================================================
--- trunk/scipy/cluster/distance.py	2008-09-05 14:18:40 UTC (rev 4691)
+++ trunk/scipy/cluster/distance.py	2008-09-05 18:05:54 UTC (rev 4692)
@@ -9,8 +9,12 @@
 +------------------+-------------------------------------------------+
 |*Function*        | *Description*                                   |
 +------------------+-------------------------------------------------+
-|pdist             | computes distances between observation pairs.   |
+|pdist             | pairwise distances between observation          |
+|                  | vectors.                                        |
 +------------------+-------------------------------------------------+
+|cdist             | distances between between two collections of    |
+|                  | observation vectors.                            |
++------------------+-------------------------------------------------+
 |squareform        | converts a square distance matrix to a          |
 |                  | condensed one and vice versa.                   |
 +------------------+-------------------------------------------------+

Modified: trunk/scipy/cluster/src/distance.c
===================================================================
--- trunk/scipy/cluster/src/distance.c	2008-09-05 14:18:40 UTC (rev 4691)
+++ trunk/scipy/cluster/src/distance.c	2008-09-05 18:05:54 UTC (rev 4692)
@@ -618,3 +618,301 @@
     }
   }
 }
+
+
+/** cdist */
+
+void cdist_euclidean(const double *XA,
+		     const double *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = euclidean_distance(u, v, n);
+    }
+  }
+}
+
+void cdist_mahalanobis(const double *XA,
+		       const double *XB,
+		       const double *covinv,
+		       double *dm, int mA, int mB, int n) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  double *dimbuf1, *dimbuf2;
+  dimbuf1 = (double*)malloc(sizeof(double) * 2 * n);
+  dimbuf2 = dimbuf1 + n;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = mahalanobis_distance(u, v, covinv, dimbuf1, dimbuf2, n);
+    }
+  }
+  dimbuf2 = 0;
+  free(dimbuf1);
+}
+
+void cdist_bray_curtis(const double *XA, const double *XB,
+		       double *dm, int mA, int mB, int n) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = bray_curtis_distance(u, v, n);
+    }
+  }
+}
+
+void cdist_canberra(const double *XA,
+		    const double *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = canberra_distance(u, v, n);
+    }
+  }
+}
+
+void cdist_hamming(const double *XA,
+		   const double *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = hamming_distance(u, v, n);
+    }
+  }
+}
+
+void cdist_hamming_bool(const char *XA,
+			const char *XB, const char *X, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const char *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = hamming_distance_bool(u, v, n);
+    }
+  }
+}
+
+void cdist_jaccard(const double *XA,
+		   const double *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = jaccard_distance(u, v, n);
+    }
+  }
+}
+
+void cdist_jaccard_bool(const char *XA,
+			const char *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const char *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = jaccard_distance_bool(u, v, n);
+    }
+  }
+}
+
+
+void cdist_chebyshev(const double *XA,
+		     const double *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = chebyshev_distance(u, v, n);
+    }
+  }
+}
+
+void cdist_cosine(const double *XA,
+		  const double *XB, double *dm, int mA, int mB, int n,
+		  const double *normsA, const double *normsB) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = cosine_distance(u, v, n, normsA[i], normsB[j]);
+    }
+  }
+}
+
+void cdist_seuclidean(const double *XA,
+		      const double *XB,
+		      const double *var,
+		      double *dm, int mA, int mB, int n) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = seuclidean_distance(var, u, v, n);
+    }
+  }
+}
+
+void cdist_city_block(const double *XA, const double *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = city_block_distance(u, v, n);
+    }
+  }
+}
+
+void cdist_minkowski(const double *XA, const double *XB, double *dm, int mA, int mB, int n, double p) {
+  int i, j;
+  const double *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = minkowski_distance(u, v, n, p);
+    }
+  }
+}
+
+void cdist_yule_bool(const char *XA, const char *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const char *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = yule_distance_bool(u, v, n);
+    }
+  }
+}
+
+void cdist_matching_bool(const char *XA, const char *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const char *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = matching_distance_bool(u, v, n);
+    }
+  }
+}
+
+void cdist_dice_bool(const char *XA, const char *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const char *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = dice_distance_bool(u, v, n);
+    }
+  }
+}
+
+void cdist_rogerstanimoto_bool(const char *XA, const char *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const char *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = rogerstanimoto_distance_bool(u, v, n);
+    }
+  }
+}
+
+void cdist_russellrao_bool(const char *XA, const char *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const char *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = russellrao_distance_bool(u, v, n);
+    }
+  }
+}
+
+void cdist_kulsinski_bool(const char *XA, const char *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const char *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = kulsinski_distance_bool(u, v, n);
+    }
+  }
+}
+
+void cdist_sokalsneath_bool(const char *XA, const char *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const char *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = sokalsneath_distance_bool(u, v, n);
+    }
+  }
+}
+
+void cdist_sokalmichener_bool(const char *XA, const char *XB, double *dm, int mA, int mB, int n) {
+  int i, j;
+  const char *u, *v;
+  double *it = dm;
+  for (i = 0; i < mA; i++) {
+    for (j = 0; j < mB; j++, it++) {
+      u = XA + (n * i);
+      v = XB + (n * j);
+      *it = sokalmichener_distance_bool(u, v, n);
+    }
+  }
+}

Modified: trunk/scipy/cluster/src/distance.h
===================================================================
--- trunk/scipy/cluster/src/distance.h	2008-09-05 14:18:40 UTC (rev 4691)
+++ trunk/scipy/cluster/src/distance.h	2008-09-05 18:05:54 UTC (rev 4692)
@@ -63,4 +63,51 @@
 void pdist_sokalmichener_bool(const char *X, double *dm, int m, int n);
 void pdist_sokalsneath_bool(const char *X, double *dm, int m, int n);
 
+void cdist_euclidean(const double *XA, const double *XB, double *dm, int mA, int mB, int n);
+void cdist_mahalanobis(const double *XA, const double *XB,
+		       const double *covinv,
+		       double *dm, int mA, int mB, int n);
+void cdist_bray_curtis(const double *XA, const double *XB,
+		       double *dm, int mA, int mB, int n);
+void cdist_canberra(const double *XA,
+		    const double *XB, double *dm, int mA, int mB, int n);
+void cdist_hamming(const double *XA,
+		   const double *XB, double *dm, int mA, int mB, int n);
+void cdist_hamming_bool(const char *XA,
+			const char *XB, const char *X, double *dm,
+			int mA, int mB, int n);
+void cdist_jaccard(const double *XA,
+		   const double *XB, double *dm, int mA, int mB, int n);
+void cdist_jaccard_bool(const char *XA,
+			const char *XB, double *dm, int mA, int mB, int n);
+void cdist_chebyshev(const double *XA,
+		     const double *XB, double *dm, int mA, int mB, int n);
+void cdist_cosine(const double *XA,
+		  const double *XB, double *dm, int mA, int mB, int n,
+		  const double *normsA, const double *normsB);
+void cdist_seuclidean(const double *XA,
+		      const double *XB,
+		      const double *var,
+		      double *dm, int mA, int mB, int n);
+void cdist_city_block(const double *XA, const double *XB, double *dm,
+		      int mA, int mB, int n);
+void cdist_minkowski(const double *XA, const double *XB, double *dm,
+		     int mA, int mB, int n, double p);
+void cdist_yule_bool(const char *XA, const char *XB, double *dm,
+		     int mA, int mB, int n);
+void cdist_matching_bool(const char *XA, const char *XB, double *dm,
+			 int mA, int mB, int n);
+void cdist_dice_bool(const char *XA, const char *XB, double *dm,
+		     int mA, int mB, int n);
+void cdist_rogerstanimoto_bool(const char *XA, const char *XB, double *dm,
+			       int mA, int mB, int n);
+void cdist_russellrao_bool(const char *XA, const char *XB, double *dm,
+			   int mA, int mB, int n);
+void cdist_kulsinski_bool(const char *XA, const char *XB, double *dm,
+			  int mA, int mB, int n);
+void cdist_sokalsneath_bool(const char *XA, const char *XB, double *dm,
+			    int mA, int mB, int n);
+void cdist_sokalmichener_bool(const char *XA, const char *XB, double *dm,
+			      int mA, int mB, int n);
+
 #endif



More information about the Scipy-svn mailing list