mathajax

Metric - Minkowski distance between two points

The java program finds distance between two points using minkowski distance equation.
when power is set P=1, minkowski metric results as same as manhattan distance equation and when set P=2, minkowski metric results as same as euclidean distance equation.

1-Dimensional distance
It measures the distance between two points. Each point lies on a line and has one element.

2-Dimensional distance
It measures the distance between two points. Each point lies on 2D cartesian plane and has 2 elements.

N-Dimensional distance
It measures the distance between two points and each point has N elements.







Example Calculation

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


        int A[] = { 5 ,3 };    // 2D vector A 
 
 int B[] = { 2,1 };    // 2D vector B
 
 int P =3; 
 
 P th power sqrt cab be converted into exponential power 1/P 
 
    

find distance between two vectors A and B using Minkowski distance equation.

||A-B|| = pow ( ((A[0]-B[0])^P + (A[1]-B[1])^P), 1/P ) = pow ( ((5-2)^3 + (3-1)^3) ,0.333 ); = pow ( ( 5^3 + 2^3 ),0.333 ); = pow ( (125 + 8),0.333 ); = pow ( 133,0.333 ); ||A-B|| = 5.096 Minkowski Distance between vectors A=[5,3] and B=[2,1] is 5.096





public class Minkowski 
{
 
 public static double distance(double x, double y,double p) {  
  return  Math.pow(Math.pow(x-y,p),1/p);  
 }
  
 public static double distance(int x, int y,double p) {  
  return   Math.pow(Math.pow(x-y,p),1/p);          
 }
 
 
public static double distance(double x[], double y[],double p) {
  
  double ds=0.0;
  for(int n=0;n<x.length;n++)
   ds += Math.pow(x[n]-y[n],p);
  
  ds = Math.pow(ds, 1/p);
  return  ds;  
 }
  
public static double distance(int x[], int y[],double p) 
{
  
  double ds=0.0;
  for(int n=0;n<x.length;n++)
   ds += Math.pow(x[n]-y[n],p);;
   
  ds = Math.pow(ds, 1/p); 
  return  ds;            
  }
 
 
public static double[] distance(int x[][], int y[],double p) {
  
    double ds[]=new double[x.length];
  for(int n=0;n<x.length;n++)
   ds[n]= distance(x[n],y,p);   
  return  ds;            
 }
 
public static double[] distance(double x[][], double y[],double p) {
  
  double ds[]=new double[x.length];
  for(int n=0;n<x.length;n++)
   ds[n] = distance(x[n],y,p);   
  return  ds;            
 }
 
}




import java.util.Arrays;
public class MinkowskiMain {

 public static void main(String[] args) 
   {    
       double p=3.0; 
           
       System.out.println("\n1D - Distance on integer" );
       System.out.println("Minkowski Distance between 
           scalar int x and y" );
 int ix=20; 
        int iy=30;          
        double dist=Minkowski.distance(ix, iy,p);
        System.out.println("x="+ix +",y="+iy);
 System.out.println("Distance :" + dist);
  
 System.out.println("\n1D - Distance on double " );
 System.out.println("Minkowski Distance between 
            scalar double x and y" );
 double dx=2.6; 
 double dy=3.2;          
 double ds2=Minkowski.distance(dx, dy,p);
 System.out.println("x="+dx +",y="+dy);
 System.out.println("Distance :" + ds2);
     
 System.out.println("2D - Distance on integer" );
 System.out.println("nMinkowski Distance between 
            int vector x and y " );
 int aix[]={2,3};
        int aiy[]={3,5};          
 double ds3=Minkowski.distance(ix, iy,p);     
 System.out.println("x="+Arrays.toString(aix) 
              +",y="+Arrays.toString(aiy));
 System.out.println("Distance :" + ds3);
     
 System.out.println("\n2 Dimensional - distance on double " );
        System.out.println("Minkowski Distance between 
              double vector 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=Minkowski.distance(adx, ady,p);
 System.out.println("Distance :" + ds4);
     
 System.out.println("\nN Dimensional Distance on double" );
 System.out.println("Minkowski Distance between 
        set of int vectors x and int vector y " );
 int aix2[][]={ {2,3,5} ,{1,7,4}};
 int aiy2[]={3,5,7};            
 double ds5[]=Minkowski.distance(aix2, aiy2,p);     
 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("\nMinkowski Distance between 
  set of double vector x and a double vector 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[]=Minkowski.distance(adx2, ady2,p);     
 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
Minkowski Distance between scalar int x and y
x=20,y=30
Distance :10.0

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

Minkowski Distance between int vector x and y 
x=[2, 3],y=[3, 5]
Distance :10.0

2 Dimensional - distance on double
Minkowski Distance between double vector x and y 
x=[2.4, 3.1],y=[3.1, 5.6]
Distance :2.5961509971494334

N Dimensional Distance on double
Minkowski Distance between set of int vectors x 
          and int vector y 
x=[2, 3, 5],[1, 7, 4],y=[3, 5, 7]
Distance :[3.0, 4.123105625617661]

Minkowski Distance between set of double vector x 
          and a double vector y 
x=[2.4, 3.1, 5.2],[1.2, 7.2, 4.5],y=[3.1, 5.6, 7.8]
Distance :[3.674234614174767, 4.1303752856126765]

Comments

Popular posts from this blog

Matrix Forward and Back Substitution

Chebyshev distance between two points

Solve System of Linear Equations by LU Decompose

Complex number Multiplication and Division

Matrix Determinant, Matrix Adjoint and Matrix Inverse