Click here to Skip to main content
6,630,586 members and growing! (15,955 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Math     Intermediate License: The Code Project Open License (CPOL)

Quantitative Variable Distances

By saharkiz

An overview of different distances under the Minkowski distance.
C# (C# 1.0, C# 2.0, C# 3.0), .NET, Dev
Version:4 (See All)
Posted:6 Jan 2009
Views:6,108
Bookmarked:4 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 1.05 Rating: 3.50 out of 5

1
1 vote, 50.0%
2

3

4
1 vote, 50.0%
5

Introduction

In this article, I discuss and implement the following:

  • Minkowski Distance
  • Euclidean Distance
  • Manhattan Distance
  • Chebyshev Distance

The general metric for distance is the Minkowski distance. When lambda is equal to 1, it becomes the city block distance, and when lambda is equal to 2, it becomes the Euclidean distance. The special case is when lambda is equal to infinity (taking a limit), where it is considered as the Chebyshev distance.

MinkowskiDistance.gif

public static double MinkowskiDistance(double[] X, double[] Y, int order)
{
    int count = 0;
    double distance = 0.0;
    double sum = 0.0;
    if (X.GetUpperBound(0) != Y.GetUpperBound(0))
    {
        throw new System.ArgumentException("the number of elements" + 
                  " in X must match the number of elements in Y");
    }
    else
    {
        count = X.Length;
    }
    for (int i = 0; i < count; i++)
    {
        sum = sum + Math.Pow(Math.Abs(X[i] - Y[i]), order);
    }
    distance = Math.Pow(sum,(1/order));
    return distance;
}

Euclidean distance is the most common use of distance. When people talk about distance, this is what they are referring to. Euclidean distance, or simply 'distance', examines the root of square differences between the coordinates of a pair of objects. This is most generally known as the Pythagorean theorem.

EuclideanDistance.gif

public static double EuclideanDistance(double [] X, double []Y)
{
    int count = 0;
    double distance = 0.0;
    double sum = 0.0;
    if(X.GetUpperBound(0) != Y.GetUpperBound(0))
    {
        throw new System.ArgumentException("the number of elements" + 
                  " in X must match the number of elements in Y");
    }
    else
    {
        count = X.Length;
    }
    for (int i = 0; i < count; i++)
    {
        sum = sum + Math.Pow(Math.Abs(X[i] - Y[i]),2);
    }
    distance = Math.Sqrt(sum);
    return distance;
}

The taxicab metric is also known as rectilinear distance, L1 distance or L1 norm, city block distance, Manhattan distance, or Manhattan length, with the corresponding variations in the name of the geometry. It represents the distance between points in a city road grid. It examines the absolute differences between the coordinates of a pair of objects.

CityBlockDistance.gif

public static double ManhattanDistance(double [] X, double []Y)
{
    int count = 0;
    double distance = 0.0;
    double sum = 0.0;
    if(X.GetUpperBound(0) != Y.GetUpperBound(0))
    {
        throw new System.ArgumentException("the number of elements" + 
                  " in X must match the number of elements in Y");
    }
    else
    {
        count = X.Length;
    }
    for (int i = 0; i < count; i++)
    {
        sum = sum + Math.Abs(X[i] - Y[i]);
    }
    distance = sum;
    return distance;
}

Chebyshev distance is also called the Maximum value distance, defined on a vector space where the distance between two vectors is the greatest of their differences along any coordinate dimension. In other words, it examines the absolute magnitude of the differences between the coordinates of a pair of objects.

ChebyshevDistance.gif

public static double ChebyshevDistance(double[] X, double[] Y)
{
    int count = 0;
    if (X.GetUpperBound(0) != Y.GetUpperBound(0))
    {
        throw new System.ArgumentException("the number of elements" + 
                  " in X must match the number of elements in Y");
    }
    else
    {
        count = X.Length;
    }
    double[] newData = new double[count];
    for (int i = 0; i < count; i++)
    {
        newData[i] = Math.Abs(X[i] - Y[i]);
    }
    double max = double.MinValue;
    foreach (double num in newData)
    {
        if (num > max)
        {
            max = num;
        }
    }
    return max;
}

Sources

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

saharkiz


Member
My name : Aresh Saharkhiz.
Origin: Unknown

Education : BS Computer Science
MS Computer Science
Interests : Genetic Programming
Neural Networks
Game AI
Programming: (language == statementEnd(semicolon)


http://sites.google.com/site/docaresh

Skill:
Flash
Carrara 3D
PHP,ASP,ASP.NET
J2SE
Occupation: Web Developer
Location: Philippines Philippines

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralMy vote of 2 PinmemberARon_10:47 6 Jan '09  
GeneralRe: My vote of 2 Pinmemberaresguya20:12 6 Jan '09  
Generaldouble max = double.MinValue; Pinmemberaxelriet7:58 6 Jan '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jan 2009
Editor: Smitha Vijayan
Copyright 2009 by saharkiz
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project