mathajax

Distance Metric - Euclidean Distance

The java program finds distance between two points by Euclidean distance metric and the points can be a scalar (single element) or a vector (more than one element).

The Euclidean distance between two points is measured based on Pythagoras theorem on right angle triangle.


         AC2  = AB2 + BC2

             AC2  - area of hypotenous 
             AB2 –  area of base
             BC2 –  area of  perpendicular

      Euclidean distance=  AC =  sqrt( AB^2 +BC^2)
 

Euclidean distance - on 1-D

The points x and y are scalar on 1-dimension and each point has one element. It is consider that the points on a measuring scale in case of 1-Dimension. The distance between these two points are measured by the following formula.

ID-Euclidean 





Euclidean distance - on 2-D

The points x and y are vector on 2-dimension and each point has two elements.It is consider that the points on a Cartesian 2-D coordinate graph in case of 2-Dimension. The distance between these two points are measured by the following formula.

2D-Euclidean 





Euclidean distance -Example on 2-D

A and B are two points on 2-D Cartesian coordinates and find distance between these two points A and B using euclidean distance equation.

    int A[] = { 2,3 };    // 2D vector  A 
    int B[] = { 2,1 };    // 2D vector B
    
   ||A-B||   =  sqrt (  (A[0]-B[0])^2  +  (A[1]-B[1])^2  )
   
              = sqrt ( (2-2)^2  + (3-1)^2 );
      
       = sqrt ( 0^2  + 2^2 ); 
      
       = sqrt ( 0  + 4 ); 
      
       = sqrt ( 4 );
      
      ||A-B||  = 2      
      
      Distance between vectors A=[2,3] and B=[2,1] is 2     

Euclidean distance - on N-D

The points x and y are vector on N-dimension and each point has N number of elements. It is consider that the points on a Cartesian 2-D coordinate graph in case of 2-Dimension. The distance between these two points are measured by the following one of the equation.

N-Dimensional Euclidean 

N-Dimensional Euclidean








Euclidean Distance - Java program

 
public class Euclidean {
 public static double distance(double x, double y) {  
  return  Math.sqrt( Math.pow(x-y,2.0) );  
 }
  
 public static double distance(int x, int y) {  
  return Math.sqrt( Math.pow(x-y,2) );          
 }
 
 
 public static double distance(double x[], double y[]) {
  
  double ds=0.0;
  for(int n=0;n<x.length;n++)
   ds += Math.pow(x[n]-y[n],2.0);
  
  ds=Math.sqrt(ds);
  return  ds;  
 }
  
 public static double distance(int x[], int y[]) {
  
  double ds=0.0;
  for(int n=0;n<x.length;n++)
   ds += Math.pow(x[n]-y[n],2.0);
  
  ds=Math.sqrt(ds);
  return  ds;            
 }
 
 
  public static double[] distance(int x[][], int y[]) {
  
    double ds[]=new double[x.length];
  for(int n=0;n<x.length;n++)
   ds[n]= distance(x[n],y);   
  return  ds;            
 }
 
  public static double[] distance(double x[][], double y[]) {
  
  double ds[]=new double[x.length];
  for(int n=0;n<x.length;n++)
   ds[n] = distance(x[n],y);   
  return  ds;            
 } 
}

Euclidean Distance - Main Java program

 
import java.util.Arrays;
public class EuclideanMain {

public static void main(String[] args) 
  {

       System.out.println("\nEuclidean Distance between
                        scalar integer x and  y " );
       int ix=20; 
       int iy=30;          
       double dist=Euclidean.distance(ix, iy);
       System.out.println("x="+ix +",y="+iy);
       System.out.println("Distance :" + dist);
  
       System.out.println("\nEuclidean Distance between 
                                scalar double x and  y " );
       double dx=2.6; 
       double dy=3.2;          
       double ds2=Euclidean.distance(dx, dy);
       System.out.println("x="+dx +",y="+dy);
       System.out.println("Distance :" + ds2);
     
       System.out.println("\n Euclidean Distance between two vectors x and y " );
      int aix[]={2,3,5};
      int aiy[]={3,5,7};          
      double ds3=Euclidean.distance(ix, iy);     
      System.out.println("x="+Arrays.toString(aix) +",y="          
                                      +Arrays.toString(aiy));
      System.out.println("Distance :" + ds3);
     

      System.out.println("\n Euclidean Distance between two 2D-vectors x and y " );
      double adx[]={2.4,3.1,5.2};
      double ady[]={3.1,5.6,7.8};      
      System.out.println("x="+Arrays.toString(adx) +",
                                      y="+Arrays.toString(ady));
      double ds4=Euclidean.distance(adx, ady);
      System.out.println("Distance :" + ds4);
     
      System.out.println("\n Euclidean Distance between two vectors x and y having N-dimensional element " );
      int aix2[][]={ {2,3,5} ,{1,7,4}};
      int aiy2[]={3,5,7};          
      double ds5[]=Euclidean.distance(aix2, aiy2);     
      System.out.println("x="+Arrays.toString(aix2[0]) + "," 
             + Arrays.toString(aix2[1]) +",y="+Arrays.toString(aiy2));
      System.out.println( "Distance :" + Arrays.toString(ds5));
     
     
      System.out.println("\n Euclidean Distance between two vectors x and y having N-dimensional element " );
      double adx2[][]={ {2.4,3.1,5.2},{1.2,7.2,4.5}};
      double ady2[]={3.1,5.6,7.8};          
      double ds6[]=Euclidean.distance(adx2, ady2);     
      System.out.println("x="+ Arrays.toString(adx2[0])+"," 
             +Arrays.toString(adx2[1])  +",y="+Arrays.toString(ady2));
      System.out.println( "Distance :" + Arrays.toString(ds6));   
          
 }
}


 

Euclidean Distance - Java program output


1D distance 
Euclidean Distance between scalar x and y 
x=20,y=30
Distance :10.0

Euclidean Distance between scalar  x and y  in datatype double 
x=2.6,y=3.2
Distance :0.6000

2D distance 
Euclidean Distance between two vectors x and y  in integer datatype
x=[2, 3],y=[3, 5]
Distance :2.2361

Euclidean Distance between two 2D vectors x and y  in double datatype
x=[2.4, 3.1],y=[3.1, 5.6]
Distance :2.5962

N-D distance 
Euclidean Distance between two vectors x and y having N-dimensional element  in integer datatype
x=[2, 3, 5],[1, 7, 4],y=[3, 5, 7]
Distance :[3.0, 4.1231]

Euclidean Distance between two vectors x and y having N-dimensional element  in double datatype
x=[2.4, 3.1, 5.2],[1.2, 7.2, 4.5],y=[3.1, 5.6, 7.8]
Distance :[3.6742, 4.1304]



Comments

Popular posts from this blog

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Matrix in Java

Vector norm