use of de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction in project elki by elki-project.
the class LinearScanEuclideanDistanceRangeQuery method linearScan.
/**
* Main loop for linear scan,
*
* @param relation Data relation
* @param iter Iterator
* @param obj Query object
* @param range Query radius
* @param result Output data structure
*/
private void linearScan(Relation<? extends O> relation, DBIDIter iter, O obj, double range, ModifiableDoubleDBIDList result) {
final SquaredEuclideanDistanceFunction squared = SquaredEuclideanDistanceFunction.STATIC;
// Avoid a loss in numerical precision when using the squared radius:
final double upper = range * 1.0000001;
// This should be more precise, but slower:
// upper = MathUtil.floatToDoubleUpper((float)range);
final double sqrange = upper * upper;
while (iter.valid()) {
final double sqdistance = squared.distance(obj, relation.get(iter));
if (sqdistance <= sqrange) {
final double dist = FastMath.sqrt(sqdistance);
if (dist <= range) {
// double check, as we increased the radius above
result.add(dist, iter);
}
}
iter.advance();
}
}
use of de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction in project elki by elki-project.
the class KMeansSort method assignToNearestCluster.
/**
* Reassign objects, but only if their bounds indicate it is necessary to do
* so.
*
* @param relation Data
* @param means Current means
* @param clusters Current clusters
* @param assignment Cluster assignment
* @param varsum Variance sum counter
* @param cdist Centroid distances
* @param cnum Centroid nearest neighbors
* @param diststat Distance statistics
* @return true when the object was reassigned
*/
private boolean assignToNearestCluster(Relation<V> relation, double[][] means, List<ModifiableDBIDs> clusters, WritableIntegerDataStore assignment, double[] varsum, double[][] cdist, int[][] cnum, LongStatistic diststat) {
assert (k == means.length);
long dists = 0;
boolean changed = false;
// Reset all clusters
Arrays.fill(varsum, 0.);
for (ModifiableDBIDs cluster : clusters) {
cluster.clear();
}
double mult = (distanceFunction instanceof SquaredEuclideanDistanceFunction) ? 4 : 2;
for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
final int cur = assignment.intValue(iditer), ini = cur >= 0 ? cur : 0;
// Distance to current mean:
V fv = relation.get(iditer);
double mindist = distanceFunction.distance(fv, DoubleVector.wrap(means[ini]));
++dists;
final double threshold = mult * mindist;
int minIndex = ini;
for (int i : cnum[ini]) {
if (cdist[minIndex][i] >= threshold) {
// All following can only be worse.
break;
}
double dist = distanceFunction.distance(fv, DoubleVector.wrap(means[i]));
++dists;
if (dist < mindist) {
minIndex = i;
mindist = dist;
}
}
varsum[minIndex] += mindist;
clusters.get(minIndex).add(iditer);
changed |= assignment.putInt(iditer, minIndex) != minIndex;
}
// Increment distance computations counter.
if (diststat != null) {
diststat.increment(dists);
}
return changed;
}
use of de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction in project elki by elki-project.
the class KMeansCompare method assignToNearestCluster.
/**
* Reassign objects, but only if their bounds indicate it is necessary to do
* so.
*
* @param relation Data
* @param means Current means
* @param clusters Current clusters
* @param assignment Cluster assignment
* @param varsum Variance sum counter
* @param cdist Centroid distances
* @param diststat Distance statistics
* @return true when the object was reassigned
*/
private boolean assignToNearestCluster(Relation<V> relation, double[][] means, List<ModifiableDBIDs> clusters, WritableIntegerDataStore assignment, double[] varsum, double[][] cdist, LongStatistic diststat) {
assert (k == means.length);
long dists = 0;
boolean changed = false;
// Reset all clusters
Arrays.fill(varsum, 0.);
for (ModifiableDBIDs cluster : clusters) {
cluster.clear();
}
double mult = (distanceFunction instanceof SquaredEuclideanDistanceFunction) ? 4 : 2;
for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
final int cur = assignment.intValue(iditer), ini = cur >= 0 ? cur : 0;
// Distance to current mean:
V fv = relation.get(iditer);
double mindist = distanceFunction.distance(fv, DoubleVector.wrap(means[ini]));
++dists;
final double thresh = mult * mindist;
int minIndex = ini;
for (int i = 0; i < k; i++) {
if (i == ini || cdist[minIndex][i] >= thresh) {
// Compare pruning
continue;
}
double dist = distanceFunction.distance(fv, DoubleVector.wrap(means[i]));
++dists;
if (dist < mindist) {
minIndex = i;
mindist = dist;
}
}
varsum[minIndex] += mindist;
clusters.get(minIndex).add(iditer);
changed |= assignment.putInt(iditer, minIndex) != minIndex;
}
// Increment distance computations counter.
if (diststat != null) {
diststat.increment(dists);
}
return changed;
}
Aggregations