Skip to content

Instantly share code, notes, and snippets.

@engleangs
Created July 20, 2019 09:10
Show Gist options
  • Save engleangs/df7d7df2eb37e56933a963af6edc1b0e to your computer and use it in GitHub Desktop.
Save engleangs/df7d7df2eb37e56933a963af6edc1b0e to your computer and use it in GitHub Desktop.
DistanceCalculator using Euclidean formula
package com.demo.knn;
/**
* @author Engleang
* Distance calculator using Euclidean fomula
*
* */
public final class DistanceCalculator {
public final static double getDistance(double[] data1, double[] data2) {
if (data1.length != data2.length) {
throw new IllegalArgumentException(" Data length mismatched ! ");
}
double sum = 0;
for(int i =0;i<data1.length;i++) {
sum += Math.pow( data1[i] - data2[i],2);
}
return Math.sqrt( sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment