The squared Euclidean distance.
15206
public static double squaredDistance(int[] x, int[] y) {
        if (x.length != y.length) {
            throw new IllegalArgumentException("Input vector sizes are different.");
        }

        double sum = 0.0;
        for (int i = 0; i < x.length; i++) {
            sum += sqr(x[i] - y[i]);
        }

        return sum;
    }
106310
def _distance_stats_sqr_fast_generic(x, y, dcov_function):
    """"""
    covariance_xy_sqr = dcov_function(x, y)
    variance_x_sqr = dcov_function(x, x)
    variance_y_sqr = dcov_function(y, y)
    denominator_sqr_signed = variance_x_sqr * variance_y_sqr
    denominator_sqr = np.absolute(denominator_sqr_signed)
    denominator = _sqrt(denominator_sqr)

    # Comparisons using a tolerance can change results if the
    # covariance has a similar order of magnitude
    if denominator == 0.0:
        correlation_xy_sqr = denominator.dtype.type(0)
    else:
        correlation_xy_sqr = covariance_xy_sqr / denominator

    return Stats(covariance_xy=covariance_xy_sqr,
                 correlation_xy=correlation_xy_sqr,
                 variance_x=variance_x_sqr,
                 variance_y=variance_y_sqr)
761638
def vq_nearest_neighbor(x, hparams):
  """"""
  bottleneck_size = 2**hparams.bottleneck_bits
  means = hparams.means
  x_norm_sq = tf.reduce_sum(tf.square(x), axis=-1, keepdims=True)
  means_norm_sq = tf.reduce_sum(tf.square(means), axis=-1, keepdims=True)
  scalar_prod = tf.matmul(x, means, transpose_b=True)
  dist = x_norm_sq + tf.transpose(means_norm_sq) - 2 * scalar_prod
  if hparams.bottleneck_kind == "em":
    x_means_idx = tf.multinomial(-dist, num_samples=hparams.num_samples)
    x_means_hot = tf.one_hot(
        x_means_idx, depth=bottleneck_size)
    x_means_hot = tf.reduce_mean(x_means_hot, axis=1)
  else:
    x_means_idx = tf.argmax(-dist, axis=-1)
    x_means_hot = tf.one_hot(x_means_idx, depth=bottleneck_size)
  x_means = tf.matmul(x_means_hot, means)
  e_loss = tf.reduce_mean(tf.squared_difference(x, tf.stop_gradient(x_means)))
  return x_means_hot, e_loss
647993
def distance_quat(p1, p2):
    
    if isinstance(p1, PoseStamped):
        p1 = pose_to_list(p1)
    if isinstance(p2, PoseStamped):
        p2 = pose_to_list(p2)
    if _is_indexable(p1) and _is_indexable(p1[0]):
        p1 = p1[1]
        p2 = p2[1]
    dotp = inner(array(p1), array(p2))
    return arccos(2 * dotp * dotp - 1)
310864
def disjoint_mst(X, num_spanning_trees=3, metric='euclidean'):
  ''''''
  D = pairwise_distances(X, metric=metric)
  if metric == 'precomputed':
    D = D.copy()
  mst = minimum_spanning_tree(D)
  W = mst.copy()
  for i in range(1, num_spanning_trees):
    ii,jj = mst.nonzero()
    D[ii,jj] = np.inf
    D[jj,ii] = np.inf
    mst = minimum_spanning_tree(D)
    W = W + mst
  # MSTs are all one-sided, so we symmetrize here
  return Graph.from_adj_matrix(W + W.T)
497411
def _u_distance_correlation_sqr_naive(x, y, exponent=1):
    """"""
    return _distance_sqr_stats_naive_generic(
        x, y,
        matrix_centered=_u_distance_matrix,
        product=u_product,
        exponent=exponent).correlation_xy
815600
def rgb_distance(rgb1, rgb2):
    '''
    
    '''
    return sum(map(lambda c: (c[0] - c[1]) ** 2,
        zip(rgb1, rgb2)))
1341739
function (r, a, sqrtassist) {
  var r1 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  var r2 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  var r3 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  var r4 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  var r5 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  var i=0;
  curve25519_sqrmodp(r2, a);					//  2 === 2 * 1
  curve25519_sqrmodp(r3, r2);					//  4 === 2 * 2
  curve25519_sqrmodp(r1, r3);					//  8 === 2 * 4
  curve25519_mulmodp(r3, r1, a);				//  9 === 8 + 1
  curve25519_mulmodp(r1, r3, r2);				// 11 === 9 + 2
  curve25519_sqrmodp(r2, r1);					// 22 === 2 * 11
  curve25519_mulmodp(r4, r2, r3);				// 31 === 22 + 9
 												//	=== 2^5   - 2^0
  curve25519_sqrmodp(r2, r4);					// 2^6   - 2^1
  curve25519_sqrmodp(r3, r2);					// 2^7   - 2^2
  curve25519_sqrmodp(r2, r3);					// 2^8   - 2^3
  curve25519_sqrmodp(r3, r2);					// 2^9   - 2^4
  curve25519_sqrmodp(r2, r3);					// 2^10  - 2^5
  curve25519_mulmodp(r3, r2, r4);				// 2^10  - 2^0
  curve25519_sqrmodp(r2, r3);					// 2^11  - 2^1
  curve25519_sqrmodp(r4, r2);					// 2^12  - 2^2
  for (i = 1; i < 5; i++) {
	curve25519_sqrmodp(r2, r4);
	curve25519_sqrmodp(r4, r2);
  } 											// 2^20  - 2^10
  curve25519_mulmodp(r2, r4, r3);				// 2^20  - 2^0
  curve25519_sqrmodp(r4, r2);					// 2^21  - 2^1
  curve25519_sqrmodp(r5, r4);					// 2^22  - 2^2
  for (i = 1; i < 10; i++) {
	curve25519_sqrmodp(r4, r5);
	curve25519_sqrmodp(r5, r4);
  } 											// 2^40  - 2^20
  curve25519_mulmodp(r4, r5, r2);				// 2^40  - 2^0
  for (i = 0; i < 5; i++) {
	curve25519_sqrmodp(r2, r4);
	curve25519_sqrmodp(r4, r2);
  } 											// 2^50  - 2^10
  curve25519_mulmodp(r2, r4, r3);				// 2^50  - 2^0
  curve25519_sqrmodp(r3, r2);					// 2^51  - 2^1
  curve25519_sqrmodp(r4, r3);					// 2^52  - 2^2
  for (i = 1; i < 25; i++) {
	curve25519_sqrmodp(r3, r4);
	curve25519_sqrmodp(r4, r3);
  } 											// 2^100 - 2^50
  curve25519_mulmodp(r3, r4, r2);				// 2^100 - 2^0
  curve25519_sqrmodp(r4, r3);					// 2^101 - 2^1
  curve25519_sqrmodp(r5, r4);					// 2^102 - 2^2
  for (i = 1; i < 50; i++) {
	curve25519_sqrmodp(r4, r5);
	curve25519_sqrmodp(r5, r4);
  } 											// 2^200 - 2^100
  curve25519_mulmodp(r4, r5, r3);				// 2^200 - 2^0
  for (i = 0; i < 25; i++) {
	curve25519_sqrmodp(r5, r4);
	curve25519_sqrmodp(r4, r5);
  } 											// 2^250 - 2^50
  curve25519_mulmodp(r3, r4, r2);				// 2^250 - 2^0
  curve25519_sqrmodp(r2, r3);					// 2^251 - 2^1
  curve25519_sqrmodp(r3, r2);					// 2^252 - 2^2
  if (sqrtassist === 1) {
	curve25519_mulmodp(r, a, r3);				// 2^252 - 3
  } else {
	curve25519_sqrmodp(r2, r3);					// 2^253 - 2^3
	curve25519_sqrmodp(r3, r2);					// 2^254 - 2^4
	curve25519_sqrmodp(r2, r3);					// 2^255 - 2^5
	curve25519_mulmodp(r, r2, r1);				// 2^255 - 21
  }
}
1209340
def _compute_simple_distance_term(self, C, rjb):
        
        return C["b4"] * np.log(np.sqrt(rjb ** 2. + C["h"] ** 2.))
568897
def vector_distance(v1, v2):
    """"""
    dist = 0
    for dim in v1:
        for x in v1[dim]:
            dd = int(v1[dim][x]) - int(v2[dim][x])
            dist = dist + dd**2
    return dist
228541
def distance(self, x):
        """"""

        d = x - self.ctr

        return np.sqrt(np.dot(np.dot(d, self.am), d))
1013178
def match_via_squared_difference(image, template, raw_tolerance=1, sq_diff_tolerance=0.1):
    
    h, w = image.shape
    th, tw = template.shape
    # fft based convolution enables fast matching of large images
    correlation = fftconvolve(image, template[::-1,::-1])
    # trim the returned image, fftconvolve returns an image of width: (Temp_w-1) + Im_w + (Temp_w -1), likewise height
    correlation = correlation[th-1:h, tw-1:w]
    # find images regions which are potentially matches
    match_position_dict = get_tiles_at_potential_match_regions(image, template, correlation, raw_tolerance=raw_tolerance)
    # bright spots in images can lead to false positivies- the normalisation carried out here eliminates those
    results = calculate_squared_differences(match_position_dict, correlation, template, sq_diff_tolerance=sq_diff_tolerance)
    return results
990907
public static double squaredDistance(SparseArray x, SparseArray y) {
        Iterator<SparseArray.Entry> it1 = x.iterator();
        Iterator<SparseArray.Entry> it2 = y.iterator();
        SparseArray.Entry e1 = it1.hasNext() ? it1.next() : null;
        SparseArray.Entry e2 = it2.hasNext() ? it2.next() : null;

        double sum = 0.0;
        while (e1 != null && e2 != null) {
            if (e1.i == e2.i) {
                sum += Math.sqr(e1.x - e2.x);
                e1 = it1.hasNext() ? it1.next() : null;
                e2 = it2.hasNext() ? it2.next() : null;
            } else if (e1.i > e2.i) {
                sum += Math.sqr(e2.x);
                e2 = it2.hasNext() ? it2.next() : null;
            } else {
                sum += Math.sqr(e1.x);
                e1 = it1.hasNext() ? it1.next() : null;
            }
        }
        
        while (it1.hasNext()) {
            sum += Math.sqr(it1.next().x);
        }

        while (it2.hasNext()) {
            sum += Math.sqr(it2.next().x);
        }
        
        return sum;
    }
372337
def euclidean(a, b):
    ""
    return np.linalg.norm(np.subtract(a, b))
144056
function distanceFromIntersectArea(r1, r2, overlap) {
        // handle complete overlapped circles
        if (Math.min(r1, r2) * Math.min(r1,r2) * Math.PI <= overlap + SMALL$1) {
            return Math.abs(r1 - r2);
        }

        return bisect(function(distance$$1) {
            return circleOverlap(r1, r2, distance$$1) - overlap;
        }, 0, r1 + r2);
    }
1209999
public static double euclidean(AssociativeArray a1, AssociativeArray a2) {
        Map<Object, Double> columnDistances = columnDistances(a1, a2, null);
        
        double distance = 0.0;
        for(double columnDistance : columnDistances.values()) {
            distance+=(columnDistance*columnDistance);
        }
        
        return Math.sqrt(distance);
    }
650799
def _distance_covariance_sqr_fast_generic(
        x, y, unbiased=False):  # pylint:disable=too-many-locals
    # This function has many locals so it can be compared
    # with the original algorithm.
    """"""
    x = np.asarray(x)
    y = np.asarray(y)

    x = np.ravel(x)
    y = np.ravel(y)

    n = x.shape[0]
    assert n > 3
    assert n == y.shape[0]
    temp = range(n)

    # Step 1
    ix0 = np.argsort(x)
    vx = x[ix0]

    ix = np.zeros(n, dtype=int)
    ix[ix0] = temp

    iy0 = np.argsort(y)
    vy = y[iy0]

    iy = np.zeros(n, dtype=int)
    iy[iy0] = temp

    # Step 2
    sx = np.cumsum(vx)
    sy = np.cumsum(vy)

    # Step 3
    alpha_x = ix
    alpha_y = iy

    beta_x = sx[ix] - vx[ix]
    beta_y = sy[iy] - vy[iy]

    # Step 4
    x_dot = np.sum(x)
    y_dot = np.sum(y)

    # Step 5
    a_i_dot = x_dot + (2 * alpha_x - n) * x - 2 * beta_x
    b_i_dot = y_dot + (2 * alpha_y - n) * y - 2 * beta_y

    sum_ab = np.sum(a_i_dot * b_i_dot)

    # Step 6
    a_dot_dot = 2 * np.sum(alpha_x * x) - 2 * np.sum(beta_x)
    b_dot_dot = 2 * np.sum(alpha_y * y) - 2 * np.sum(beta_y)

    # Step 7
    gamma_1 = _partial_sum_2d(x, y, np.ones(n, dtype=x.dtype))
    gamma_x = _partial_sum_2d(x, y, x)
    gamma_y = _partial_sum_2d(x, y, y)
    gamma_xy = _partial_sum_2d(x, y, x * y)

    # Step 8
    aijbij = np.sum(x * y * gamma_1 + gamma_xy - x * gamma_y - y * gamma_x)

    if unbiased:
        d3 = (n - 3)
        d2 = (n - 2)
        d1 = (n - 1)
    else:
        d3 = d2 = d1 = n

    # Step 9
    d_cov = (aijbij / n / d3 - 2 * sum_ab / n / d2 / d3 +
             a_dot_dot / n * b_dot_dot / d1 / d2 / d3)

    return d_cov