Java List Cosine vectorCosine(List array1, List array2)

Here you can find the source of vectorCosine(List array1, List array2)

Description

vector Cosine

License

Open Source License

Declaration

public static double vectorCosine(List<Double> array1, List<Double> array2) 

Method Source Code


//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));
    }
}