// *** ISimilarity<SparseVector<double>.ReadOnly> interface implementation *** public double GetSimilarity(SparseVector <double> .ReadOnly a, SparseVector <double> .ReadOnly b) { Utils.ThrowException(a == null ? new ArgumentNullException("a") : null); Utils.ThrowException(b == null ? new ArgumentNullException("b") : null); double dotProd = 0; int i = 0, j = 0; int aCount = a.Count; Utils.ThrowException(aCount == 0 ? new ArgumentValueException("a") : null); int bCount = b.Count; Utils.ThrowException(bCount == 0 ? new ArgumentValueException("b") : null); ArrayList <int> aIdx = a.Inner.InnerIdx; ArrayList <double> aDat = a.Inner.InnerDat; ArrayList <int> bIdx = b.Inner.InnerIdx; ArrayList <double> bDat = b.Inner.InnerDat; int aIdx_i = aCount == 0 ? 0 : aIdx[0]; int bIdx_j = bCount == 0 ? 0 : bIdx[0]; while (true) { if (aIdx_i < bIdx_j) { if (++i == aCount) { break; } aIdx_i = aIdx[i]; } else if (aIdx_i > bIdx_j) { if (++j == bCount) { break; } bIdx_j = bIdx[j]; } else { dotProd += aDat[i] * bDat[j]; if (++i == aCount || ++j == bCount) { break; } aIdx_i = aIdx[i]; bIdx_j = bIdx[j]; } } double aLen = ModelUtils.GetVecLenL2(a); Utils.ThrowException(aLen == 0 ? new ArgumentValueException("a") : null); double bLen = ModelUtils.GetVecLenL2(b); Utils.ThrowException(bLen == 0 ? new ArgumentValueException("b") : null); double lenMult = aLen * bLen; return(dotProd / lenMult); }
// *** ISimilarity<SparseVector<double>.ReadOnly> interface implementation *** public double GetSimilarity(SparseVector <double> .ReadOnly a, SparseVector <double> .ReadOnly b) { Utils.ThrowException(a == null ? new ArgumentNullException("a") : null); Utils.ThrowException(b == null ? new ArgumentNullException("b") : null); double dot_prod = 0; int i = 0, j = 0; int a_count = a.Count; Utils.ThrowException(a_count == 0 ? new ArgumentValueException("a") : null); int b_count = b.Count; Utils.ThrowException(b_count == 0 ? new ArgumentValueException("b") : null); ArrayList <int> a_idx = a.Inner.InnerIdx; ArrayList <double> a_dat = a.Inner.InnerDat; ArrayList <int> b_idx = b.Inner.InnerIdx; ArrayList <double> b_dat = b.Inner.InnerDat; int a_idx_i = a_count == 0 ? 0 : a_idx[0]; int b_idx_j = b_count == 0 ? 0 : b_idx[0]; while (true) { if (a_idx_i < b_idx_j) { if (++i == a_count) { break; } a_idx_i = a_idx[i]; } else if (a_idx_i > b_idx_j) { if (++j == b_count) { break; } b_idx_j = b_idx[j]; } else { dot_prod += a_dat[i] * b_dat[j]; if (++i == a_count || ++j == b_count) { break; } a_idx_i = a_idx[i]; b_idx_j = b_idx[j]; } } double len_a = ModelUtils.GetVecLenL2(a); Utils.ThrowException(len_a == 0 ? new ArgumentValueException("a") : null); double len_b = ModelUtils.GetVecLenL2(b); Utils.ThrowException(len_b == 0 ? new ArgumentValueException("b") : null); double len_mult = len_a * len_b; return(dot_prod / len_mult); }