internal static double SumSquaredDiffs(DoubleArrayVector x, SparseVector y) { double sum = 0; int xlen = x.Length; int ylen = y.Length; int i = 0; int j = 0; while (i < xlen && j < ylen){ if (i == y.indices[j]){ double d = x.values[i++] - y.values[j++]; sum += d*d; } else if (i > y.indices[j]){ sum += y.values[j]*y.values[j]; ++j; } else{ sum += x.values[i]*x.values[i]; ++i; } } while (i < xlen){ sum += x.values[i]*x.values[i]; ++i; } while (j < ylen){ sum += y.values[j]*y.values[j]; ++j; } return sum; }
internal static double Dot(DoubleArrayVector x, SparseVector y) { double sum = 0; int xlen = x.Length; int ylen = y.Length; int i = 0; int j = 0; while (i < xlen && j < ylen){ if (i == y.indices[j]){ sum += x.values[i++]*y.values[j++]; } else{ if (i > y.indices[j]){ ++j; } else{ ++i; } } } return sum; }