QUICK CLUSTER: Remove sqrt from Euclidean distance calculations
authorJohn Darrington <john@darrington.wattle.id.au>
Fri, 13 Nov 2015 12:57:38 +0000 (13:57 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Fri, 13 Nov 2015 19:39:20 +0000 (20:39 +0100)
The Euclidean distance is used only for comparison with other Euclidean
distances.  Since it is invariant that (sqrt(x) < sqrt(y))  === (x < y)
for all non-negative x,y  it is a waste of effort calculating the sqrt.

src/language/stats/quick-cluster.c

index 4f34e4f0a8ef0eabcb955d4eb1588d634264f33e..0a2b75de252b7e9e7f5405c923a2700fbb15d05b 100644 (file)
@@ -20,7 +20,6 @@
 #include <gsl/gsl_permutation.h>
 #include <gsl/gsl_sort_vector.h>
 #include <gsl/gsl_statistics.h>
-#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -187,7 +186,7 @@ matrix_mindist (const gsl_matrix *m, int *mn, int *mm)
        }
     }
 
-  return sqrt (mindist);
+  return mindist;
 }
 
 
@@ -220,7 +219,7 @@ dist_from_case (const struct Kmeans *kmeans, const struct ccase *c, const struct
       dist += pow2 (gsl_matrix_get (kmeans->centers, which, j) - val->f);
     }
  
-  return sqrt (dist);
+  return dist;
 }
 
 /* Return the minimum distance of the group WHICH and all other groups */
@@ -247,7 +246,7 @@ min_dist_from (const struct Kmeans *kmeans, const struct qc *qc, int which)
        }
     }
 
-  return sqrt (mindist);
+  return mindist;
 }
 
 
@@ -349,7 +348,6 @@ kmeans_get_nearest_group (const struct Kmeans *kmeans, struct ccase *c, const st
 
          dist += pow2 (gsl_matrix_get (kmeans->centers, i, j) - val->f);
        }
-      dist = sqrt (dist);
 
       if (dist < mindist0)
        {