compute similarity with distance measurement
337143
def _similarity_distance(s1, s2, ignore):
    '''
    
    '''
    g = 0.0
    try:
        g_ = cosine(_flat_sum_array(_get_wv(s1, ignore)), _flat_sum_array(_get_wv(s2, ignore)))
        if is_digit(g_): g = g_
    except: pass

    u = _nearby_levenshtein_distance(s1, s2)
    logging.debug("g: %s, u: %s" % (g, u))
    if u >= 0.99:
        r = 1.0
    elif u > 0.9:
        r = _similarity_smooth(g, 0.05, u, 0.05)
    elif u > 0.8:
        r = _similarity_smooth(g, 0.1, u, 0.2)
    elif u > 0.4:
        r = _similarity_smooth(g, 0.2, u, 0.15)
    elif u > 0.2:
        r = _similarity_smooth(g, 0.3, u, 0.1)
    else:
        r = _similarity_smooth(g, 0.4, u, 0)

    if r < 0: r = abs(r)
    r = min(r, 1.0)
    return float("%.3f" % r)
969158
def distance(p1, p2):
    

    def xyz(some_pose):
        if isinstance(some_pose, PoseStamped):
            return some_pose.pose.position.x, some_pose.pose.position.y, some_pose.pose.position.z
        elif isinstance(some_pose, Pose):
            return some_pose.position.x, some_pose.position.y, some_pose.position.z
        elif _is_indexable(some_pose[0]):
            return some_pose[0][0], some_pose[0][1], some_pose[0][2]
        else:
            return some_pose[0], some_pose[1], some_pose[2]

    x1, y1, z1 = xyz(p1)
    x2, y2, z2 = xyz(p2)

    x = x1 - x2
    y = y1 - y2
    z = z1 - z2
    return sqrt(x * x + y * y + z * z)
577873
def similarity(self, other):
        
        sim = self.Similarity()
        total = 0.0

        # Calculate similarity ratio for each attribute
        cname = self.__class__.__name__
        for aname, weight in self.attributes.items():

            attr1 = getattr(self, aname, None)
            attr2 = getattr(other, aname, None)
            self.log(attr1, attr2, '%', cname=cname, aname=aname)

            # Similarity is ignored if None on both objects
            if attr1 is None and attr2 is None:
                self.log(attr1, attr2, '%', cname=cname, aname=aname,
                         result="attributes are both None")
                continue

            # Similarity is 0 if either attribute is non-Comparable
            if not all((isinstance(attr1, Comparable),
                        isinstance(attr2, Comparable))):
                self.log(attr1, attr2, '%', cname=cname, aname=aname,
                         result="attributes not Comparable")
                total += weight
                continue

            # Calculate similarity between the attributes
            attr_sim = (attr1 % attr2)
            self.log(attr1, attr2, '%', cname=cname, aname=aname,
                     result=attr_sim)

            # Add the similarity to the total
            sim += attr_sim * weight
            total += weight

        # Scale the similarity so the total is 1.0
        if total:
            sim *= (1.0 / total)

        return sim
1152763
def _compute_distance_scaling(self, C, rrup, mag):
        
        rscale1 = rrup + C["c2"] * (10.0 ** (C["c3"] * mag))
        return -np.log10(rscale1) - (C["c4"] * rrup)
483751
def _compute_diff(existing, expected):

    '''
    
    '''

    diff = {}

    for key in ['location', 'contact', 'chassis_id']:
        if existing.get(key) != expected.get(key):
            _create_diff(diff,
                         _valid_str,
                         key,
                         existing.get(key),
                         expected.get(key))

    for key in ['community']:  # for the moment only onen
        if existing.get(key) != expected.get(key):
            _create_diff(diff,
                         _valid_dict,
                         key,
                         existing.get(key),
                         expected.get(key))

    return diff
216354
def init_distance_ttable(wordlist, distance_function):
    
    n = len(wordlist)
    t_table = numpy.zeros((n, n + 1))

    # Initialize P(c|r) accordingly
    for r, w in enumerate(wordlist):
        for c, v in enumerate(wordlist):
            if c < r:
                t_table[r, c] = t_table[c, r]  # Similarity is symmetric
            else:
                t_table[r, c] = distance_function(w, v) + 0.001  # For backoff
    t_table[:, n] = numpy.mean(t_table[:, :-1], axis=1)

    # Normalize
    t_totals = numpy.sum(t_table, axis=0)
    for i, t_total in enumerate(t_totals.tolist()):
        t_table[:, i] /= t_total
    return t_table
1019544
public void similar(IMatrix U, IMatrix result) {
        if ((result.rows != U.columns) || (result.columns != U.columns)) result.reshape(U.columns, U.columns);

        double realsum, imagsum, realinnersum, imaginnersum;
        for (int i = 0; i < U.columns; i++)
            for (int j = 0; j < U.columns; j++) {
                realsum = 0d;
                imagsum = 0d;
                for (int k = 0; k < U.columns; k++) {
                    realinnersum = 0d;
                    imaginnersum = 0d;
                    for (int l = 0; l < U.columns; l++) {
                        realinnersum += realmatrix[k][l] * U.realmatrix[l][j] - imagmatrix[k][l] * U.imagmatrix[l][j];
                        imaginnersum += realmatrix[k][l] * U.imagmatrix[l][j] + imagmatrix[k][l] * U.realmatrix[l][j];
                    }
                    realsum += U.realmatrix[k][i] * realinnersum - U.imagmatrix[k][i] * imaginnersum;
                    imagsum += U.realmatrix[k][i] * imaginnersum + U.imagmatrix[k][i] * realinnersum;
                }
                result.realmatrix[i][j] = realsum;
                result.imagmatrix[i][j] = imagsum;
            }
    }
395270
def similar(self, similarity):
        
        pattern = Pattern(self.path)
        pattern.similarity = similarity
        return pattern
318022
def _cdist_scipy(x, y, exponent=1):
    """"""
    metric = 'euclidean'

    if exponent != 1:
        metric = 'sqeuclidean'

    distances = _spatial.distance.cdist(x, y, metric=metric)

    if exponent != 1:
        distances **= exponent / 2

    return distances
821658
def _calculate_similarity(c):
    
    ma = {}
    for idc in c:
        set1 = _get_seqs(c[idc])
        [ma.update({(idc, idc2): _common(set1, _get_seqs(c[idc2]), idc, idc2)}) for idc2 in c if idc != idc2 and (idc2, idc) not in ma]
    # logger.debug("_calculate_similarity_ %s" % ma)
    return ma
1034766
def _compute_distance_term(self, C, rhypo):
        
        return (C["c2"] * rhypo) + (C["c3"] * np.log10(rhypo))
866046
function fastSimilarity(newPeaks2, from, to) {
    array1Extract = extract(array1, from, to);
    array2Extract = newPeaks2;
    if (common & COMMON_SECOND) array1Extract = getCommonArray(array1Extract, array2Extract, widthBottom);
    normalize(array1Extract);
    var diff = calculateDiff();
    return calculateOverlapFromDiff(diff);
  }
559871
def _compute_distance_scaling(self, C, rhypo):
        
        return C["c"] * np.log10(np.sqrt((rhypo ** 2.) + (C["h"] ** 2.))) +\
            (C["d"] * rhypo)
956221
def compute_distance_matrix(points):
    
    n = len(points)
    return [[1000 * great_circle_distance(points[i], points[j])
             for j in range(n)] for i in range(n)]
942041
def similarity(self, other):
        """"""
        logging.debug("comparing %r and %r...", self.stripped, other.stripped)
        ratio = SequenceMatcher(a=self.stripped, b=other.stripped).ratio()
        similarity = self.Similarity(ratio)
        return similarity
1374199
def similarity(self, other):
        """"""
        if self.magnitude == 0 or other.magnitude == 0:
            return 0

        return self.dot(other) / self.magnitude
874983
@Override
    public double similarity(Vertex<V> vertex1, Vertex<V> vertex2) {
        return similarity(vertex1.vertexID(), vertex2.vertexID());
    }