cosineSimilarity function
Measures the cosine of the angle between two vectors in a vector space. It ranges from -1 to 1, where 1 represents identical vectors, 0 represents orthogonal vectors, and -1 represents vectors that are diametrically opposed.
Implementation
double cosineSimilarity(final List<double> a, final List<double> b) {
double p = 0;
double p2 = 0;
double q2 = 0;
for (int i = 0; i < a.length; i++) {
p += a[i] * b[i];
p2 += a[i] * a[i];
q2 += b[i] * b[i];
}
return p / sqrt(p2 * q2);
}