Here you can find the source of cosineSimilarity(double[] x, double[] y)
public static double cosineSimilarity(double[] x, double[] y)
//package com.java2s; //License from project: Apache License public class Main { public static double cosineSimilarity(double[] x, double[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Vector dimensions should be equal"); }//from w w w . j a v a 2 s. c o m double xySum = 0; double xSum = 0; double ySum = 0; for (int i = 0; i < x.length; i++) { xySum += x[i] * y[i]; xSum += x[i] * x[i]; ySum += y[i] * y[i]; } return xySum / (Math.sqrt(xSum) * Math.sqrt(ySum)); } }