com.github.TKnudsen.ComplexDataObject.model.distanceMeasure.Double.WeightedMinkowskiDistance Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of complex-data-object Show documentation
Show all versions of complex-data-object Show documentation
A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.
The newest version!
package com.github.TKnudsen.ComplexDataObject.model.distanceMeasure.Double;
import java.util.List;
import com.github.TKnudsen.ComplexDataObject.model.distanceMeasure.WeightedDistanceMeasure;
/**
*
* Title: WeightedMinkowskiDistance
*
*
*
* Description:
*
*
*
* Copyright: (c) 2016-2017 Juergen Bernard
*
*
* @author Christian Ritter
* @version 1.01
*/
public class WeightedMinkowskiDistance extends WeightedDistanceMeasure {
private double exponent;
public WeightedMinkowskiDistance(List weights) {
this(weights, 2.0);
}
public WeightedMinkowskiDistance(List weights, double nullValue) {
super(weights, nullValue);
}
public WeightedMinkowskiDistance(List weights, double nullValue, double exponent) {
super(weights, nullValue);
this.exponent = exponent;
}
@Override
public String getDescription() {
return "WeightedMinkowskiDistanceMeasure";
}
@Override
public double getDistance(double[] o1, double[] o2) {
if (o1.length != o2.length || o1.length != getWeights().size())
throw new IllegalArgumentException(getName() + ": given arrays have different length");
int length = o1.length;
double result = 0;
for (int i = 0; i < length; i++) {
if (!Double.isNaN(o1[i] + o2[i]))
result += getWeights().get(i) * Math.pow(Math.abs((o1[i] - o2[i])), exponent);
else
result += getNullValue() * getWeights().get(i);
}
return Math.pow(result, 1 / exponent);
}
@Override
public String getName() {
return "WeightedMinkowskiDistanceMeasure";
}
}