I have two double arrays a and b and want to calculate the cosine similarity between them. My code looks like this:
double [][] target = new double [1][65000];
double [][] compare = new double [1][65000];
double dotProduct = dot(target[0], compare[0]);
double eucledianDist = norm2(target) * norm2(compare);
double output = dotProduct / eucledianDist;
private double norm2(double[][] a){
double sum = 0;
for (int i = 0; i < a[0].length; i++){
sum = sum + a[0][i] * a[0][i];
}
return Math.sqrt(sum);
}
private double dot(double[] a, double [] b){
double sum = 0;
for(int i = 0; i < a.length; i ++){
sum += a[i] * b[i];
}
return sum;
}
Is there any way to speed up computation time?