Interface DistanceMeasure<T>

Type Parameters:
T - the type of object
All Known Implementing Classes:
CachedDistanceMeasure, DistanceMeasure.ApacheDistanceMeasure
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface DistanceMeasure<T>
Interface for computing the distance between two objects. A distance measure must be:
  • Non-negative - compute(A, B) >= 0 for all A and B
  • Zero identity - compute(A, B) == 0 when A == B
  • Finite - compute(A, B) < Inf && compute(A, B) != NaN
Additionally, while not a requirement, distance measures should be symmetric, where compute(A, B) == compute(B, A). Be mindful if caching distances that non-symmetric measures must store both directions.

Implementation Note: While we use the Apache Commons Math clustering code behind the scenes, this interface is distinct from Apache's DistanceMeasure in order to (1) support generic types; and (2) provide a functional interface to simplify use with lambdas and method references. The static methods return a distance measure compatible with both.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    DistanceMeasure.ApacheDistanceMeasure<T extends org.apache.commons.math3.ml.clustering.Clusterable>
    Wraps one of the Apache Commons Math distance measures to be compatible with our generic distance measure.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends org.apache.commons.math3.ml.clustering.Clusterable>
    DistanceMeasure.ApacheDistanceMeasure<T>
    Returns the Chebyshev distance measure, also known as the Linf-norm.
    double
    compute(T first, T second)
    Returns the distance between the two objects.
    static <T extends org.apache.commons.math3.ml.clustering.Clusterable>
    DistanceMeasure.ApacheDistanceMeasure<T>
    Returns the Euclidean distance measure, also known as the L2-norm.
    static <T extends org.apache.commons.math3.ml.clustering.Clusterable>
    DistanceMeasure.ApacheDistanceMeasure<T>
    Returns the Manhattan distance measure, also known as the L1-norm.
  • Method Details

    • compute

      double compute(T first, T second)
      Returns the distance between the two objects.
      Parameters:
      first - the first object
      second - the second object
      Returns:
      the distance
    • euclideanDistance

      static <T extends org.apache.commons.math3.ml.clustering.Clusterable> DistanceMeasure.ApacheDistanceMeasure<T> euclideanDistance()
      Returns the Euclidean distance measure, also known as the L2-norm.
      Type Parameters:
      T - the type, which must be Clusterable
      Returns:
      the distance measure
    • manhattanDistance

      static <T extends org.apache.commons.math3.ml.clustering.Clusterable> DistanceMeasure.ApacheDistanceMeasure<T> manhattanDistance()
      Returns the Manhattan distance measure, also known as the L1-norm.
      Type Parameters:
      T - the type, which must be Clusterable
      Returns:
      the distance measure
    • chebyshevDistance

      static <T extends org.apache.commons.math3.ml.clustering.Clusterable> DistanceMeasure.ApacheDistanceMeasure<T> chebyshevDistance()
      Returns the Chebyshev distance measure, also known as the Linf-norm.
      Type Parameters:
      T - the type, which must be Clusterable
      Returns:
      the distance measure