Returns the euclidian distance between the current cluster and the one
 given as parameter
 Params:
 +cluster+: Cluster object
 +method+: 0 or 1
 method = 0: do not into condseideration duplicate values
 method = 1: average linkage clusterization
765159
def distance(cluster, method = 0)
      d = 0
      norm = 0

      cluster.objects.each do |elem1|
        objects.each do |elem2|
          if method == 1
            d += elem1[1] * elem2[1] * (elem1[0] - elem2[0]).abs
            norm += elem1[1] * elem2[1]
          else
            d += (elem1[0] - elem2[0]).abs
            norm = cluster.objects.length * objects.length
          end
        end
      end

      # group average distance
      d /= (norm + 0.0)
    end
244623
private double evaluateClusters(ArrayList<PROCLUSCluster> clusters, long[][] dimensions, Relation<V> database) {
    double result = 0;
    for(int i = 0; i < dimensions.length; i++) {
      PROCLUSCluster c_i = clusters.get(i);
      double[] centroid_i = c_i.centroid;

      long[] dims_i = dimensions[i];
      double w_i = 0;
      for(int d = BitsUtil.nextSetBit(dims_i, 0); d >= 0; d = BitsUtil.nextSetBit(dims_i, d + 1)) {
        w_i += avgDistance(centroid_i, c_i.objectIDs, database, d);
      }

      w_i /= dimensions.length;
      result += c_i.objectIDs.size() * w_i;
    }

    return result / database.size();
  }
1308020
def get_distance_term(self, C, rrup):
        
        f_p = C["c1"] * rrup
        idx = np.logical_and(rrup > self.CONSTANTS["r1"],
                             rrup <= self.CONSTANTS["r2"])
        f_p[idx] = (C["c1"] * self.CONSTANTS["r1"]) +\
            C["c2"] * (rrup[idx] - self.CONSTANTS["r1"])
        idx = rrup > self.CONSTANTS["r2"]
        f_p[idx] = C["c1"] * self.CONSTANTS["r1"] +\
            C["c2"] * (self.CONSTANTS["r2"] - self.CONSTANTS["r1"]) +\
            C["c3"] * (rrup[idx] - self.CONSTANTS["r2"])
        return f_p
1004664
def perimeter(self):
        '''
        
        '''
        return sum([a.distance(b) for a, b in self.pairs()])
126617
def path_distance(points):
    
    vecs = np.diff(points, axis=0)[:, :3]
    d2 = [np.dot(p, p) for p in vecs]
    return np.sum(np.sqrt(d2))
220934
def point_segment_distance(point, segment):
    
    assert isinstance(point, Point), \
        "point is not of type Point, but of %r" % type(point)
    dx = segment.p2.x - segment.p1.x
    dy = segment.p2.y - segment.p1.y
    if dx == dy == 0:  # the segment's just a point
        return point.dist_to(segment.p1)

    if dx == 0:  # It's a straight vertical line
        p_below_p1 = point.y <= segment.p1.y and segment.p1.y <= segment.p2.y
        p_below_p2 = point.y <= segment.p2.y and segment.p2.y <= segment.p1.y
        p_above_p2 = segment.p1.y <= segment.p2.y and segment.p2.y <= point.y
        p_above_p1 = segment.p2.y <= segment.p1.y and segment.p1.y <= point.y
        if p_below_p1 or p_above_p1:
            return point.dist_to(segment.p1)
        elif p_below_p2 or p_above_p2:
            return point.dist_to(segment.p2)

    if dy == 0:  # It's a straight horizontal line
        p_left_p1 = point.x <= segment.p1.x and segment.p1.x <= segment.p2.x
        p_left_p2 = point.x <= segment.p2.x and segment.p2.x <= segment.p1.x
        p_right_p2 = segment.p1.x <= segment.p2.x and segment.p2.x <= point.x
        p_right_p1 = segment.p2.x <= segment.p1.x and segment.p1.x <= point.x
        if p_left_p1 or p_right_p1:
            return point.dist_to(segment.p1)
        elif p_left_p2 or p_right_p2:
            return point.dist_to(segment.p2)

    # Calculate the t that minimizes the distance.
    t = ((point.x - segment.p1.x) * dx + (point.y - segment.p1.y) * dy) / \
        (dx * dx + dy * dy)

    # See if this represents one of the segment's
    # end points or a point in the middle.
    if t < 0:
        dx = point.x - segment.p1.x
        dy = point.y - segment.p1.y
    elif t > 1:
        dx = point.x - segment.p2.x
        dy = point.y - segment.p2.y
    else:
        near_x = segment.p1.x + t * dx
        near_y = segment.p1.y + t * dy
        dx = point.x - near_x
        dy = point.y - near_y
    return math.hypot(dx, dy)
724013
def distance(self, other):
        
        return distance((self.separation, self.pa), (other.separation, other.pa))
644736
private double[] get1ToNDistances(Instance sample, Instances samples){
		double distances[] = new double[samples.numInstances()];
		for (int i=0; i<samples.numInstances(); i++){
			distances[i] = this.getDistance(sample, samples.get(i));
		}
		return distances;
	}
1019565
function getSimilarity(type, n1, n2, attributes) {
  var attr = function attr(n, i) {
    return attributes[i](n);
  }; // nb negative because similarity should have an inverse relationship to distance


  return -clusteringDistance(type, attributes.length, function (i) {
    return attr(n1, i);
  }, function (i) {
    return attr(n2, i);
  }, n1, n2);
}
939302
def _get_distance_segment_coefficients(self, rval):
        
        # Get distance segment ends
        nsites = len(rval)
        # Equation 12a
        f_0 = np.log10(self.CONSTS["r0"] / rval)
        f_0[rval > self.CONSTS["r0"]] = 0.0

        # Equation 12b
        f_1 = np.log10(rval)
        f_1[rval > self.CONSTS["r1"]] = np.log10(self.CONSTS["r1"])
        # Equation 12c
        f_2 = np.log10(rval / self.CONSTS["r2"])
        f_2[rval <= self.CONSTS["r2"]] = 0.0
        return f_0, f_1, f_2
1038426
function distanceFrom(Centroids) {
                var distances = [];

                for(var i = 1; i <= this.Observations.rows(); i++) {
                    var distance = [];

                    for(var j = 1; j <= Centroids.rows(); j++) {
                        distance.push(this.Observations.row(i).distanceFrom(Centroids.row(j)));
                    }

                    distances.push(distance);
                }

                return $M(distances);
            }
1224654
def cluster_nodes(self, tol=0.2):
        
        lattice = self.structure.lattice

        vfcoords = [v.frac_coords for v in self.vnodes]

        # Manually generate the distance matrix (which needs to take into
        # account PBC.
        dist_matrix = np.array(lattice.get_all_distances(vfcoords, vfcoords))
        dist_matrix = (dist_matrix + dist_matrix.T) / 2
        for i in range(len(dist_matrix)):
            dist_matrix[i, i] = 0
        condensed_m = squareform(dist_matrix)
        z = linkage(condensed_m)
        cn = fcluster(z, tol, criterion="distance")
        merged_vnodes = []
        for n in set(cn):
            poly_indices = set()
            frac_coords = []
            for i, j in enumerate(np.where(cn == n)[0]):
                poly_indices.update(self.vnodes[j].polyhedron_indices)
                if i == 0:
                    frac_coords.append(self.vnodes[j].frac_coords)
                else:
                    fcoords = self.vnodes[j].frac_coords
                    # We need the image to combine the frac_coords properly.
                    d, image = lattice.get_distance_and_image(frac_coords[0],
                                                              fcoords)
                    frac_coords.append(fcoords + image)
            merged_vnodes.append(
                VoronoiPolyhedron(lattice, np.average(frac_coords, axis=0),
                                  poly_indices, self.coords))
        self.vnodes = merged_vnodes
        logger.debug("%d vertices after combination." % len(self.vnodes))
26740
public static int numPoints(Clustering<? extends MeanModel> clustering) {
    int n = 0;
    for(Cluster<? extends MeanModel> aCluster : clustering.getAllClusters()) {
      n += aCluster.size();
    }
    return n;
  }
9275
private float calculateClusterQuality (ArrayList<Instance> docs,
			HashSparseVector centroid) {
		float quality = 0.0f;
		HashSparseVector c = centroid;

		for (int i = 0; i < docs.size(); ++i) {
			Instance doc = docs.get(i);

			quality += c.distanceEuclidean((HashSparseVector) doc.getData());
		}

		return quality;
	}
458774
@Override
    public double getInclusionProbability(Instance instance) {
        //trivial cluster
        if(N == 1){
            double distance = 0.0;
            for (int i = 0; i < LS.length; i++) {
                double d = LS[i] - instance.value(i);
                distance += d * d;
            }
            distance = Math.sqrt(distance);
            if( distance < EPSILON )
                return 1.0;
            return 0.0;
        }
        else{
            double dist = calcNormalizedDistance(instance.toDoubleArray());
            if(dist <= getRadius()){
                return 1;
            }
            else{
                return 0;
            }
//            double res = AuxiliaryFunctions.distanceProbabilty(dist, LS.length);
//            return res;
        }
    }
980034
@Reference(authors = "D. Pelleg, A. Moore", //
      title = "X-means: Extending K-means with Efficient Estimation on the Number of Clusters", //
      booktitle = "Proc. 17th Int. Conf. on Machine Learning (ICML 2000)", //
      url = "http://www.pelleg.org/shared/hp/download/xmeans.ps", //
      bibkey = "DBLP:conf/icml/PellegM00")
  public static <V extends NumberVector> double logLikelihood(Relation<V> relation, Clustering<? extends MeanModel> clustering, NumberVectorDistanceFunction<? super V> distanceFunction) {
    List<? extends Cluster<? extends MeanModel>> clusters = clustering.getAllClusters();
    // number of clusters
    final int m = clusters.size();

    // number of objects in the clustering
    int n = 0;
    // cluster sizes
    int[] n_i = new int[m];
    // total variance
    double d = 0.;
    // variances
    double[] d_i = new double[m];

    // Iterate over clusters:
    Iterator<? extends Cluster<? extends MeanModel>> it = clusters.iterator();
    for(int i = 0; it.hasNext(); ++i) {
      Cluster<? extends MeanModel> cluster = it.next();
      n += n_i[i] = cluster.size();
      d += d_i[i] = varianceOfCluster(cluster, distanceFunction, relation);
    }

    // No remaining variance, if every point is on its own:
    if(n <= m) {
      return Double.NEGATIVE_INFINITY;
    }

    // Total variance (corrected for bias)
    final double logv = FastMath.log(d / (n - m));

    final int dim = RelationUtil.dimensionality(relation);
    // log likelihood of this clustering
    double logLikelihood = 0.;

    // Aggregate
    for(int i = 0; i < m; i++) {
      logLikelihood += n_i[i] * FastMath.log(n_i[i]) // Post. entropy Rn log Rn
          - n_i[i] * .5 * MathUtil.LOGTWOPI // Rn/2 log2pi
          - n_i[i] * dim * .5 * logv // Rn M/2 log sigma^2
          - (d_i[i] - m) * .5; // (Rn-K)/2
    }
    logLikelihood -= n * FastMath.log(n); // Prior entropy, sum_i Rn log R
    return logLikelihood;
  }
1078393
def get_distance_term(self, C, dists, rup):
        
        x_ij = dists.rrup
        gn_exp = np.exp(C["c1"] + 6.5 * C["c2"])
        g_n = C["gcrN"] * np.log(self.CONSTANTS["xcro"] + 30. + gn_exp) *\
            np.ones_like(x_ij)
        idx = x_ij <= 30.0
        if np.any(idx):
            g_n[idx] = C["gcrN"] * np.log(self.CONSTANTS["xcro"] +
                                          x_ij[idx] + gn_exp)
        c_m = min(rup.mag, self.CONSTANTS["m_c"])
        r_ij = self.CONSTANTS["xcro"] + x_ij + np.exp(C["c1"] + C["c2"] * c_m)
        return C["gUM"] * np.log(r_ij) +\
            C["gcrL"] * np.log(x_ij + 200.0) +\
            g_n + C["eum"] * x_ij + C["ecrV"] * dists.rvolc + C["gamma_S"]