Here you can find the source of vectorCosine(List
public static double vectorCosine(List<Double> array1, List<Double> array2)
//package com.java2s; import java.util.List; public class Main { public static double vectorCosine(List<Double> array1, List<Double> array2) { if (array1.size() != array2.size()) throw new RuntimeException( "Cannot compute cosine of arrays of differnt sizes: " + array1.size() + " " + array2.size()); double dotProd = 0.0; double lsq1 = 0.0; double lsq2 = 0.0; for (int i = 0; i < array1.size(); ++i) { dotProd += array1.get(i) * array2.get(i); lsq1 += array1.get(i) * array1.get(i); lsq2 += array2.get(i) * array2.get(i); }/*from www. j a va 2 s.co m*/ return dotProd / (Math.sqrt(lsq1) * Math.sqrt(lsq2)); } }