I am trying to find the cosine similarity between 2 vectors (x,y Points) and I am making some silly error that I cannot nail down. Pardone me am a newbie and sorry if I am making a very simple error (which I very likely am).
Thanks for your help
public static double GetCosineSimilarity(List<Point> V1, List<Point> V2)
{
double sim = 0.0d;
int N = 0;
N = ((V2.Count < V1.Count)?V2.Count : V1.Count);
double dotX = 0.0d; double dotY = 0.0d;
double magX = 0.0d; double magY = 0.0d;
for (int n = 0; n < N; n++)
{
dotX += V1[n].X * V2[n].X;
dotY += V1[n].Y * V2[n].Y;
magX += Math.Pow(V1[n].X, 2);
magY += Math.Pow(V1[n].Y, 2);
}
return (dotX + dotY)/(Math.Sqrt(magX) * Math.Sqrt(magY));
}
Edit: Apart from syntax, my question was also to do with the logical construct given I am dealing with Vectors of differing lengths. Also, how is the above generalizable to vectors of m dimensions. Thanks