framework/oryx-common/src/main/java/com/cloudera/oryx/common/math/VectorMath.java
public static double cosineSimilarity(float[] x, float[] y, double normY) {
    int length = x.length;
    double dot = 0.0;
    double totalXSq = 0.0;
    for (int i = 0; i < length; i++) {
      double xi = x[i];
      totalXSq += xi * xi;
      dot += xi * y[i];
    }
    return dot / (Math.sqrt(totalXSq) * normY);
  }
public static double cosineSimilarity(float[] x, float[] y, double normY) {
    int length = x.length;
    double dot = 0.0;
    double totalXSq = 0.0;
    for (int i = 0; i < length; i++) {
      double xi = x[i];
      totalXSq += xi * xi;
      dot += xi * y[i];
    }
    return dot / (Math.sqrt(totalXSq) * normY);
  }
public static double cosineSimilarity ( float [ ] x , float [ ] y , double normY ) { int length = x . length ; double dot = 0.0 ; double totalXSq = 0.0 ; for ( int i = 0 ; i < length ; i ++ ) { double xi = x [ i ] ; totalXSq += xi * xi ; dot += xi * y [ i ] ; } return dot / ( Math . sqrt ( totalXSq ) * normY ) ; }
Computes cosine similarity of values in two given arrays, when the norm of one array is
known in advance, which is a not-uncommon case.

@param x one array
@param y the other array
@param normY norm of y
@return cosine similarity = dot(x,y) / (norm(x) * norm(y))
Computes cosine similarity of values in two given arrays when the norm of one array is known in advance which is a not - uncommon case .