Java Utililty Methods Cosine Similarity

List of utility methods to do Cosine Similarity

Description

The list of methods to do Cosine Similarity are organized into topic(s).

Method

doublecosDegrees(double angleInDegrees)
cos Degrees
return Math.cos(Math.toRadians(angleInDegrees));
floatcosDistance(float[] v1, float[] v2)
Calculate cosine distance of two vectors
assert v1.length == v2.length;
float dotProduct = dotProduct(v1, v2);
float sumNorm = vectorNorm(v1) * vectorNorm(v2);
return dotProduct / sumNorm;
doublecosine_similarity(double[] vec1, double[] vec2)
cosinsimilarity
double cosim = vector_dot(vec1, vec2) / (vector_norm(vec1) * vector_norm(vec2));
return cosim;
doublecosineLawGetDegree(double a, double b, double c)
cosine Law Get Degree
return Math.acos((a * a + b * b - c * c) / (2 * a * b));
doublecosineSim(double[] a, double[] b)
Calculate the cosine similarity between two vectors
if (a == null || b == null || a.length < 1 || b.length < 1 || a.length != b.length)
    return Double.NaN;
double sum = 0.0, sum_a = 0, sum_b = 0;
for (int i = 0; i < a.length; i++) {
    sum += a[i] * b[i];
    sum_a += a[i] * a[i];
    sum_b += b[i] * b[i];
double val = Math.sqrt(sum_a) * Math.sqrt(sum_b);
return sum / val;
doublecosineSimilarity(double[] vector1, double[] vector2)
cosine Similarity
double dotProduct = dotProduct(vector1, vector2);
double euclideanDist = euclideanDistance(vector1) * euclideanDistance(vector2);
return dotProduct / euclideanDist;
doublecosineSimilarity(double[] x, double[] y)
cosine Similarity
if (x.length != y.length) {
    throw new IllegalArgumentException("Vector dimensions should be equal");
double xySum = 0;
double xSum = 0;
double ySum = 0;
for (int i = 0; i < x.length; i++) {
    xySum += x[i] * y[i];
...
doublecosineSimilarity(float[] f1, float[] f2)
cosine Similarity
float num = 0, den1 = 0, den2 = 0;
for (int i = 0; i < f1.length; i++) {
    num += f1[i] * f2[i];
    den1 += sq(f1[i]);
    den2 += sq(f2[i]);
return num / (Math.sqrt(den1) * Math.sqrt(den2));
doublecosineSimilarity(float[] vectorA, float[] vectorB)
cosine Similarity
double dotProduct = 0.0;
double normA = 0.0;
double normB = 0.0;
for (int i = 0; i < vectorA.length; i++) {
    dotProduct += vectorA[i] * vectorB[i];
    normA += Math.pow(vectorA[i], 2);
    normB += Math.pow(vectorB[i], 2);
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
doublecosineSimilarity(String[] tkn0, String[] tkn1)
cosine Similarity
HashMap<String, int[]> map = new HashMap<String, int[]>();
for (int i = 0; i < tkn0.length; i++) {
    String t = tkn0[i].toLowerCase();
    if (!map.containsKey(t)) {
        map.put(t, new int[2]);
    map.get(t)[0]++;
for (int i = 0; i < tkn1.length; i++) {
    String t = tkn1[i].toLowerCase();
    if (!map.containsKey(t)) {
        map.put(t, new int[2]);
    map.get(t)[1]++;
double dot = 0;
double norma = 0;
double normb = 0;
for (Entry<String, int[]> e : map.entrySet()) {
    int[] v = e.getValue();
    dot += v[0] * v[1];
    norma += v[0] * v[0];
    normb += v[1] * v[1];
norma = Math.sqrt(norma);
normb = Math.sqrt(normb);
if (dot == 0) {
    return 0;
} else {
    return dot / (norma * normb);