euclidean norm


See On Github

Data

Tags

euclid, length

Source Code

/**
  * Written by: ghostsnstuff
  * Last Updated: March 22, 2014
  * Source: http://en.wikipedia.org/wiki/Norm_(mathematics)#Euclidean_norm
  * Logic: square each element in the vector -> summate all squared values -> take the square root of the sum
  */
class EuclideanNorm {
  static double euclidean_norm(int[] vec) {
    double sum = 0;
    for(int i = 0; i < vec.length; i++) {
      sum += vec[i] * vec[i];
    }
    return Math.sqrt(sum);
  }
}
class EuclideanNormTest extends EuclideanNorm {
  public static void main(String[] args) {
    int[] v = {1,2,3,4};
    System.out.println(euclidean_norm(v)); // 5.477225575051661
  }
}