Enum Metric
- All Implemented Interfaces:
Serializable,Comparable<Metric>,java.lang.constant.Constable
Each enum constant holds a specific metric implementation, providing a type-safe way to calculate the distance between two points in a multidimensional space.
- See Also:
-
MetricDefinition
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>> -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionRepresents the Cosine distance metric, implemented byMetricDefinition.CosineMetric.Dot product similarity, implemented byMetricDefinition.DotProductMetricRepresents the Euclidean distance metric, implemented byMetricDefinition.EuclideanMetric.Represents the squared Euclidean distance metric, implemented byMetricDefinition.EuclideanSquareMetric.Represents the Manhattan distance metric, implemented byMetricDefinition.ManhattanMetric. -
Method Summary
Modifier and TypeMethodDescriptiondoubledistance(double[] vectorData1, double[] vectorData2) doubledistance(RealVector vector1, RealVector vector2) Calculates a distance between two n-dimensional vectors.default booleanConvenience method that returns if all properties of a metric required to be a true metric are satisfied.booleanbooleanbooleanbooleantoString()static MetricReturns the enum constant of this type with the specified name.static Metric[]values()Returns an array containing the constants of this enum type, in the order they are declared.
-
Enum Constant Details
-
MANHATTAN_METRIC
Represents the Manhattan distance metric, implemented byMetricDefinition.ManhattanMetric.This metric calculates a distance overlaying the multidimensional space with a grid-like structure only allowing orthogonal lines. In 2D this resembles the street structure in Manhattan where one would have to go
xblocks north/south andyblocks east/west leading to a total distance ofx + y.- See Also:
-
MetricDefinition.ManhattanMetric
-
EUCLIDEAN_METRIC
Represents the Euclidean distance metric, implemented byMetricDefinition.EuclideanMetric.This metric calculates the "ordinary" straight-line distance between two points in Euclidean space. The distance is the square root of the sum of the squared differences between the corresponding coordinates of the two points.
- See Also:
-
MetricDefinition.EuclideanMetric
-
EUCLIDEAN_SQUARE_METRIC
Represents the squared Euclidean distance metric, implemented byMetricDefinition.EuclideanSquareMetric.This metric calculates the sum of the squared differences between the coordinates of two vectors, defined as
sum((p_i - q_i)^2). It is computationally less expensive than the standard Euclidean distance because it avoids the final square root operation.This is often preferred in algorithms where comparing distances is more important than the actual distance value, such as in clustering algorithms, as it preserves the relative ordering of distances.
- See Also:
-
- Squared Euclidean distance
MetricDefinition.EuclideanSquareMetric
-
COSINE_METRIC
Represents the Cosine distance metric, implemented byMetricDefinition.CosineMetric.This metric calculates a "distance" between two vectors
v1andv2that ranges between0.0dand2.0dthat corresponds to1 - cos(v1, v2), meaning that ifv1 == v2, the distance is0while ifv1is orthogonal tov2it is1.- See Also:
-
MetricDefinition.CosineMetric
-
DOT_PRODUCT_METRIC
Dot product similarity, implemented byMetricDefinition.DotProductMetricThis metric calculates the inverted dot product of two vectors. It is not a true metric as several properties of true metrics do not hold, for instance this metric can be negative.
- See Also:
-
- Dot Product
MetricDefinition.DotProductMetric
-
-
Method Details
-
values
Returns an array containing the constants of this enum type, in the order they are declared.- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum type has no constant with the specified nameNullPointerException- if the argument is null
-
satisfiesZeroSelfDistance
public boolean satisfiesZeroSelfDistance() -
satisfiesPositivity
public boolean satisfiesPositivity() -
satisfiesSymmetry
public boolean satisfiesSymmetry() -
satisfiesTriangleInequality
public boolean satisfiesTriangleInequality() -
distance
public double distance(@Nonnull double[] vectorData1, @Nonnull double[] vectorData2) -
distance
Calculates a distance between two n-dimensional vectors.The two vectors are represented as arrays of
Doubleand must be of the same length (i.e., have the same number of dimensions).- Parameters:
vector1- the first vector. Must not be null.vector2- the second vector. Must not be null and must have the same length asvector1.- Returns:
- the calculated distance as a
double. - Throws:
IllegalArgumentException- if the vectors have different lengths.NullPointerException- if eithervector1orvector2is null.
-
toString
-
isTrueMetric
default boolean isTrueMetric()Convenience method that returns if all properties of a metric required to be a true metric are satisfied.- Returns:
trueiff this metric is a true metric.
-