Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public static double correlate(double[] a, double[] b) {
if (a.length != b.length) {
return Double.NaN;
}
double partial[] = new double[4];
int i = 0;
for ( ; i + 3 < a.length; i += 4) {
partial[0] += a[i+0] * b[i+0];
partial[1] += a[i+1] * b[i+1];
partial[2] += a[i+2] * b[i+2];
partial[3] += a[i+3] * b[i+3];
}
double correlation = partial[0] + partial[1] + partial[2] + partial[3];
for ( ; i < a.length; i++) {
correlation += a[i] * b[i];
}
return correlation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment