Skip to content

Instantly share code, notes, and snippets.

@shelajev
Created July 23, 2021 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shelajev/73c893c69127044d207e2a53e33aeea5 to your computer and use it in GitHub Desktop.
Save shelajev/73c893c69127044d207e2a53e33aeea5 to your computer and use it in GitHub Desktop.
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