public static long Dot(IndexedVector vec1, IndexedVector vec2) { if (ReferenceEquals(vec1, vec2)) { return(CbocModel.DotSelf(vec1)); } long product = 0; var cursor1 = 0; var cursor2 = 0; while (cursor1 < vec1.Count && cursor2 < vec2.Count) { var i1 = vec1.Index[cursor1]; var i2 = vec2.Index[cursor2]; if (i2 > i1) { cursor1++; } else if (i1 > i2) { cursor2++; } else { product += vec1.Values[cursor1++] * vec2.Values[cursor2++]; } } return(product); }
public float CosAngle(Vector vec1, Vector vec2) { var ivec1 = vec1 as IndexedVector; var ivec2 = vec2 as IndexedVector; if (ivec1 == null || ivec2 == null) { return(0); } long dotProduct = Dot(ivec1, ivec2); long dotSelf1 = CbocModel.DotSelf(vec1); long dotSelf2 = CbocModel.DotSelf(vec2); return((float)(dotProduct / (Math.Sqrt(dotSelf1) * Math.Sqrt(dotSelf2)))); }