List of usage examples for com.google.common.collect TreeMultiset headMultiset
@Override
public SortedMultiset<E> headMultiset(@Nullable E upperBound, BoundType boundType)
From source file:org.apache.mahout.math.neighborhood.ProjectionSearch.java
/** * Returns the closest vector to the query. * When only one the nearest vector is needed, use this method, NOT search(query, limit) because * it's faster (less overhead).// www . java 2 s .c o m * * @param query the vector to search for * @param differentThanQuery if true, returns the closest vector different than the query (this * only matters if the query is among the searched vectors), otherwise, * returns the closest vector to the query (even the same vector). * @return the weighted vector closest to the query */ @Override public WeightedThing<Vector> searchFirst(Vector query, boolean differentThanQuery) { double bestDistance = Double.POSITIVE_INFINITY; Vector bestVector = null; Iterator<? extends Vector> projections = basisMatrix.iterator(); for (TreeMultiset<WeightedThing<Vector>> v : scalarProjections) { Vector basisVector = projections.next(); WeightedThing<Vector> projectedQuery = new WeightedThing<Vector>(query, query.dot(basisVector)); for (WeightedThing<Vector> candidate : Iterables.concat( Iterables.limit(v.tailMultiset(projectedQuery, BoundType.CLOSED), searchSize), Iterables.limit( v.headMultiset(projectedQuery, BoundType.OPEN).descendingMultiset(), searchSize))) { double distance = distanceMeasure.distance(query, candidate.getValue()); if (distance < bestDistance && (!differentThanQuery || !candidate.getValue().equals(query))) { bestDistance = distance; bestVector = candidate.getValue(); } } } return new WeightedThing<Vector>(bestVector, bestDistance); }
From source file:org.apache.mahout.math.neighborhood.ProjectionSearch.java
/** * Searches for the query vector returning the closest limit referenceVectors. * * @param query the vector to search for. * @param limit the number of results to return. * @return a list of Vectors wrapped in WeightedThings where the "thing"'s weight is the * distance.// w ww .ja v a2 s . c o m */ @Override public List<WeightedThing<Vector>> search(Vector query, int limit) { Set<Vector> candidates = Sets.newHashSet(); Iterator<? extends Vector> projections = basisMatrix.iterator(); for (TreeMultiset<WeightedThing<Vector>> v : scalarProjections) { Vector basisVector = projections.next(); WeightedThing<Vector> projectedQuery = new WeightedThing<Vector>(query, query.dot(basisVector)); for (WeightedThing<Vector> candidate : Iterables.concat( Iterables.limit(v.tailMultiset(projectedQuery, BoundType.CLOSED), searchSize), Iterables.limit( v.headMultiset(projectedQuery, BoundType.OPEN).descendingMultiset(), searchSize))) { candidates.add(candidate.getValue()); } } // If searchSize * scalarProjections.size() is small enough not to cause much memory pressure, // this is probably just as fast as a priority queue here. List<WeightedThing<Vector>> top = Lists.newArrayList(); for (Vector candidate : candidates) { top.add(new WeightedThing<Vector>(candidate, distanceMeasure.distance(query, candidate))); } Collections.sort(top); return top.subList(0, Math.min(limit, top.size())); }