Here you can find the source of cosineSimilarity(double[] vector1, double[] vector2)
private static double cosineSimilarity(double[] vector1, double[] vector2)
//package com.java2s; //License from project: Open Source License public class Main { private static double cosineSimilarity(double[] vector1, double[] vector2) { double dotProduct = dotProduct(vector1, vector2); double euclideanDist = euclideanDistance(vector1) * euclideanDistance(vector2); return dotProduct / euclideanDist; }/*from w w w . j av a 2s .c om*/ private static double dotProduct(double[] vector1, double[] vector2) { double result = 0.0; for (int i = 0; i < vector1.length; i++) { result += vector1[i] * vector2[i]; } return result; } private static double euclideanDistance(double[] vector) { double result = 0.0; for (double aVector : vector) { result += aVector * aVector; } return Math.sqrt(result); } }