Here you can find the source of cosine_similarity(double[] vec1, double[] vec2)
public static double cosine_similarity(double[] vec1, double[] vec2)
//package com.java2s; //License from project: Apache License public class Main { public static double cosine_similarity(double[] vec1, double[] vec2) { double cosim = vector_dot(vec1, vec2) / (vector_norm(vec1) * vector_norm(vec2)); return cosim; }//from w w w. j a va 2s . c o m public static double vector_dot(double[] vec1, double[] vec2) { double sum = 0; for (int i = 0; i < vec1.length && i < vec2.length; i++) sum += vec1[i] * vec2[i]; return sum; } public static double vector_norm(double[] vec) { double sum = 0; for (double v : vec) sum += v * v; return Math.sqrt(sum); } }