Java cos cos(double[] v1, double[] v2)

Here you can find the source of cos(double[] v1, double[] v2)

Description

cos

License

Apache License

Declaration

public static double cos(double[] v1, double[] v2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static double cos(double[] v1, double[] v2) {
        double s = 0;
        s = Math.abs(multiple(v1, v2));
        double a = Math.pow(multiple(v1, v1) * multiple(v2, v2), 0.5);
        if (a == 0) {
            return -1.0;
        }/*  w w  w .  j av  a 2  s . com*/
        return s / a;
    }

    public static double multiple(double[] v1, double[] v2) {
        double v = 0;
        for (int i = 0; i < v1.length; i++) {
            v = v + v1[i] * v2[i];
        }
        return v;
    }

    public static double[][] multiple(double[][] x, double[][] y) {
        double[][] t = transpose(y);
        double[][] result = new double[x.length][];
        for (int i = 0; i < result.length; i++) {
            result[i] = new double[t.length];
            for (int j = 0; j < result[i].length; j++) {
                result[i][j] = multiple(x[i], t[j]);
            }
        }
        return result;
    }

    public static double[][] transpose(double[][] x) {
        double[][] t = new double[x[0].length][];
        for (int i = 0; i < t.length; i++) {
            t[i] = new double[x.length];
            for (int j = 0; j < t[i].length; j++) {
                t[i][j] = x[j][i];
            }
        }
        return t;
    }
}

Related

  1. cos(double a)
  2. cos(double anAngle)
  3. cos(double d)
  4. cos(double radians)
  5. cos(double[] v1, double[] v2)
  6. cos(final double value)
  7. cos(final float angle)
  8. cos(final float value)