mathajax

Chebyshev distance between two points

The java program finds distance between two points using chebyshev distance equation. The points can be a scalar or a vector.
1D distance metric
It measures distance between two points by absolute difference between them. Each point has one element.

2-Dimensional distance metric
It measures distance between two points (2D vector) by selecting maximum absolute difference on 2D vector element absolute difference.

N-Dimensional distance metric
It measures distance between two N-Dimensional vector by selecting maximum absolute difference on the two N-D vector element.






Example Calculation on 2D vectors


This is an example calculation shown below explain how to find the distance between two vectors using Chebyshev distance formula. A vector,array of elements declared and initialized in Java using one dimensional array.


        int A[] = { 2 ,6 };    // 2D vector A 
 
 int B[] = { 4,1 };    // 2D vector B
 
 max  -   operators finds  greatest value in a array 
 
   find  distance between two vectors A and B using 
              Chebyshev distance equation
   
   ||A-B||     =  max { abs(A[0]-B[0])  ,  abs(A[1]-B[1]) } 
   
               =  max { abs(2-4) , abs(6-1) };
      
        =  max { abs(-2) , abs(5) }; 
      
        =  max {2 , 5}; 
      
   ||A-B||     =  5 ;
      
   
      
   Chebyshev Distance between vectors A=[2,6] and B=[4,1] is 5     





public class Chebyshev {

 public static double distance(double x, double y) {  
  return  Math.abs(x-y);  
 }
  
 public static double distance(int x, int y) {  
  return Math.abs(x-y);          
 }
 
 
 public static double distance(double x[], double y[]) {
  
  double ds=0.0; 
  for(int n=0;n<x.length;n++)      
    ds=Math.max(ds,Math.abs(x[n]-y[n]));
  
  return  ds;  
 }
  
 public static double distance(int x[], int y[]) {
  
  double ds=0.0;
  for(int n=0;n<x.length;n++) {
   ds=Math.max(ds,Math.abs(x[n]-y[n]));
  }
  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;            
 }

}




import java.util.Arrays;

public class ChebyshevMain {

public static void main(String[] args) 
  {  
    System.out.println("\n1D - Distance on integer" );
    System.out.println("Chebyshev Distance between scalar 
                       int x and y" );
          
    int ix=20; 
    int iy=30;          
    double dist=Chebyshev.distance(ix, iy);
    System.out.println("x="+ix +",y="+iy);
    System.out.println("Distance :" + dist);
  
    System.out.println("\n1D - Distance on double " );
    System.out.println("Chebyshev Distance between scalar 
                                 double x and y" );
    double dx=2.6; 
    double dy=3.2;          
    double ds2=Chebyshev.distance(dx, dy);
    System.out.println("x="+dx +",y="+dy);
    System.out.println("Distance :" + ds2);
     
     
    System.out.println("\n2D - Distance on integer" );
    System.out.println("Chebyshev Distance between 
                      vector int x and y");
    int aix[]={2,3};
    int aiy[]={3,5};          
    double ds3=Chebyshev.distance(ix, iy);     
    System.out.println("x="+Arrays.toString(aix) 
                   +",y="+Arrays.toString(aiy));
    System.out.println("Distance :" + ds3);
     

    System.out.println("\n2D - Distance on integer" );
    System.out.println("Chebyshev Distance between 
                             vector double x and y");     
    double adx[]={2.4,3.1};
    double ady[]={3.1,5.6};      
    System.out.println("x="+Arrays.toString(adx) 
                  +",y="+Arrays.toString(ady)); 
     double ds4=Chebyshev.distance(adx, ady);
     System.out.println("Distance :" + ds4);
     
     System.out.println("nChebyshev Distance between 
               set of int vector x and vector int y" );
    int aix2[][]={ {2,3,5} ,{1,7,4}};
    int aiy2[]={3,5,7};          
    double ds5[]=Chebyshev.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("nChebyshev Distance between 
    set of vector double x and a vector double y " );
    double adx2[][]={ {2.4,3.1,5.2},{1.2,7.2,4.5}};
    double ady2[]={3.1,5.6,7.8};          
    double ds6[]=Chebyshev.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) );   
     
 }

}


Output
1D - Distance on integer
Chebyshev Distance between scalar int x and y
x=20,y=30
Distance :10.0

1D - Distance on double
Chebyshev Distance between scalar double x and y
x=2.6,y=3.2
Distance :0.6000000000000001

2D - Distance on integer
Chebyshev Distance between vector int x and y
x=[2, 3],y=[3, 5]
Distance :2.0

2D - Distance on double
Chebyshev Distance between vector double x and y
x=[2.4, 3.1],y=[3.1, 5.6]
Distance :2.4999999999999996

Chebyshev Distance between set of int vector x and vector int y
x=[2, 3, 5],[1, 7, 4],y=[3, 5, 7]
Distance :[2.0, 3.0]

Chebyshev Distance between set of vector double x 
                      and a vector double y 
x=[2.4, 3.1, 5.2],[1.2, 7.2, 4.5],y=[3.1, 5.6, 7.8]
Distance :[2.5999999999999996, 3.3]

Comments

Popular posts from this blog

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Complex number Multiplication and Division

Solving System of Linear Equations by Gauss Jordan Elimination