use of de.lmu.ifi.dbs.elki.data.NumberVector in project elki by elki-project.
the class WeightedEuclideanDistanceFunction method minDist.
@Override
public double minDist(SpatialComparable mbr1, SpatialComparable mbr2) {
final int dim1 = mbr1.getDimensionality(), dim2 = mbr2.getDimensionality();
final int mindim = (dim1 < dim2) ? dim1 : dim2;
final NumberVector v1 = (mbr1 instanceof NumberVector) ? (NumberVector) mbr1 : null;
final NumberVector v2 = (mbr2 instanceof NumberVector) ? (NumberVector) mbr2 : null;
double agg = //
(v1 != null) ? //
(v2 != null) ? preDistance(v1, v2, 0, mindim) : preDistanceVM(v1, mbr2, 0, mindim) : (v2 != null) ? preDistanceVM(v2, mbr1, 0, mindim) : preDistanceMBR(mbr1, mbr2, 0, mindim);
// first object has more dimensions.
if (dim1 > mindim) {
agg += (v1 != null) ? preNorm(v1, mindim, dim1) : preNormMBR(mbr1, mindim, dim1);
}
// second object has more dimensions.
if (dim2 > mindim) {
agg += (v2 != null) ? preNorm(v2, mindim, dim2) : preNormMBR(mbr2, mindim, dim2);
}
return FastMath.sqrt(agg);
}
use of de.lmu.ifi.dbs.elki.data.NumberVector in project elki by elki-project.
the class MaximumDistanceFunction method minDist.
@Override
public double minDist(SpatialComparable mbr1, SpatialComparable mbr2) {
final int dim1 = mbr1.getDimensionality(), dim2 = mbr2.getDimensionality();
final int mindim = (dim1 < dim2) ? dim1 : dim2;
final NumberVector v1 = (mbr1 instanceof NumberVector) ? (NumberVector) mbr1 : null;
final NumberVector v2 = (mbr2 instanceof NumberVector) ? (NumberVector) mbr2 : null;
double agg = //
(v1 != null) ? //
(v2 != null) ? preDistance(v1, v2, 0, mindim) : preDistanceVM(v1, mbr2, 0, mindim) : (v2 != null) ? preDistanceVM(v2, mbr1, 0, mindim) : preDistanceMBR(mbr1, mbr2, 0, mindim);
// first object has more dimensions.
if (dim1 > mindim) {
double b = (v1 != null) ? preNorm(v1, mindim, dim1) : preNormMBR(mbr1, mindim, dim1);
agg = agg >= b ? agg : b;
}
// second object has more dimensions.
if (dim2 > mindim) {
double b = (v2 != null) ? preNorm(v2, mindim, dim2) : preNormMBR(mbr2, mindim, dim2);
agg = agg >= b ? agg : b;
}
return agg;
}
use of de.lmu.ifi.dbs.elki.data.NumberVector in project elki by elki-project.
the class SquaredEuclideanDistanceFunction method minDist.
@Override
public double minDist(SpatialComparable mbr1, SpatialComparable mbr2) {
final int dim1 = mbr1.getDimensionality(), dim2 = mbr2.getDimensionality();
final int mindim = (dim1 < dim2) ? dim1 : dim2;
final NumberVector v1 = (mbr1 instanceof NumberVector) ? (NumberVector) mbr1 : null;
final NumberVector v2 = (mbr2 instanceof NumberVector) ? (NumberVector) mbr2 : null;
double agg = //
(v1 != null) ? //
(v2 != null) ? preDistance(v1, v2, 0, mindim) : preDistanceVM(v1, mbr2, 0, mindim) : (v2 != null) ? preDistanceVM(v2, mbr1, 0, mindim) : preDistanceMBR(mbr1, mbr2, 0, mindim);
// first object has more dimensions.
if (dim1 > mindim) {
agg += (v1 != null) ? preNorm(v1, mindim, dim1) : preNormMBR(mbr1, mindim, dim1);
}
// second object has more dimensions.
if (dim2 > mindim) {
agg += (v2 != null) ? preNorm(v2, mindim, dim2) : preNormMBR(mbr2, mindim, dim2);
}
return agg;
}
use of de.lmu.ifi.dbs.elki.data.NumberVector in project elki by elki-project.
the class IntegerRankTieNormalization method filter.
@Override
public MultipleObjectsBundle filter(MultipleObjectsBundle objects) {
final int len = objects.dataLength();
MultipleObjectsBundle bundle = new MultipleObjectsBundle();
int[] order = new int[len];
for (int i = 0; i < len; i++) {
order[i] = i;
}
Sorter comparator = new Sorter();
for (int r = 0; r < objects.metaLength(); r++) {
final SimpleTypeInformation<?> type = objects.meta(r);
final List<?> column = objects.getColumn(r);
if (!TypeUtil.NUMBER_VECTOR_FIELD.isAssignableFromType(type)) {
bundle.appendColumn(type, column);
continue;
}
@SuppressWarnings("unchecked") final List<? extends NumberVector> castColumn = (List<? extends NumberVector>) column;
// Get the replacement type information
final int dim = ((VectorFieldTypeInformation<?>) type).getDimensionality();
final VectorFieldTypeInformation<IntegerVector> outType = new VectorFieldTypeInformation<>(IntegerVector.STATIC, dim);
// Output vectors
int[][] posvecs = new int[len][dim];
// Sort for each dimension
for (int d = 0; d < dim; d++) {
// Sort
comparator.setup(castColumn, d);
IntegerArrayQuickSort.sort(order, comparator);
// Transfer positions to output vectors
for (int sta = 0; sta < order.length; ) {
double v = castColumn.get(order[sta]).doubleValue(d);
// Compute ties
int end = sta + 1;
while (end < order.length && !(v < castColumn.get(order[end]).doubleValue(d))) {
end++;
}
final int pos = (sta + end - 1);
for (int i = sta; i < end; i++) {
posvecs[order[i]][d] = pos;
}
sta = end;
}
}
// Prepare output data
final List<IntegerVector> outColumn = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
outColumn.add(new IntegerVector(posvecs[i]));
}
bundle.appendColumn(outType, outColumn);
}
return bundle;
}
use of de.lmu.ifi.dbs.elki.data.NumberVector in project elki by elki-project.
the class FastMultidimensionalScalingTransform method filter.
@Override
public MultipleObjectsBundle filter(MultipleObjectsBundle objects) {
final int size = objects.dataLength();
if (size == 0) {
return objects;
}
MultipleObjectsBundle bundle = new MultipleObjectsBundle();
for (int r = 0; r < objects.metaLength(); r++) {
SimpleTypeInformation<? extends Object> type = objects.meta(r);
@SuppressWarnings("unchecked") final List<Object> column = (List<Object>) objects.getColumn(r);
// Not supported column (e.g. labels):
if (!dist.getInputTypeRestriction().isAssignableFromType(type)) {
bundle.appendColumn(type, column);
continue;
}
// Get the replacement type information
@SuppressWarnings("unchecked") final List<I> castColumn = (List<I>) column;
NumberVector.Factory<? extends NumberVector> factory = null;
{
if (type instanceof VectorFieldTypeInformation) {
final VectorFieldTypeInformation<?> ctype = (VectorFieldTypeInformation<?>) type;
// Note two-step cast, to make stricter compilers happy.
@SuppressWarnings("unchecked") final VectorFieldTypeInformation<? extends NumberVector> vtype = (VectorFieldTypeInformation<? extends NumberVector>) ctype;
factory = FilterUtil.guessFactory(vtype);
} else {
factory = DoubleVector.FACTORY;
}
bundle.appendColumn(new VectorFieldTypeInformation<>(factory, tdim), castColumn);
}
// Compute distance matrix.
double[][] imat = computeSquaredDistanceMatrix(castColumn, dist);
doubleCenterSymmetric(imat);
// Find eigenvectors.
{
double[][] evs = new double[tdim][size];
double[] lambda = new double[tdim];
findEigenVectors(imat, evs, lambda);
// Undo squared, unless we were given a squared distance function:
if (!dist.isSquared()) {
for (int i = 0; i < tdim; i++) {
lambda[i] = FastMath.sqrt(Math.abs(lambda[i]));
}
}
// Project each data point to the new coordinates.
double[] buf = new double[tdim];
for (int i = 0; i < size; i++) {
for (int d = 0; d < tdim; d++) {
buf[d] = lambda[d] * evs[d][i];
}
column.set(i, factory.newNumberVector(buf));
}
}
}
return bundle;
}
Aggregations