double sample(double u) { if (u < limit) { List<WeightedThing<Integer>> steps = Lists.newArrayList(); limit = 1; int i = 0; while (u / 20 < limit) { double pdf = pd.probability(i); limit -= pdf; steps.add(new WeightedThing<>(i, pdf)); i++; } steps.add(new WeightedThing<>(steps.size(), limit)); partial = new Multinomial<>(steps); } return partial.sample(u); } }
/** * 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). * * @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; for (Vector row : referenceVectors) { double distance = distanceMeasure.distance(query, row); if (distance < bestDistance && (!differentThanQuery || !row.equals(query))) { bestDistance = distance; bestVector = row; } } return new WeightedThing<Vector>(bestVector, bestDistance); }
double sample(double u) { if (u < limit) { List<WeightedThing<Integer>> steps = Lists.newArrayList(); limit = 1; int i = 0; while (u / 20 < limit) { double pdf = pd.probability(i); limit -= pdf; steps.add(new WeightedThing<>(i, pdf)); i++; } steps.add(new WeightedThing<>(steps.size(), limit)); partial = new Multinomial<>(steps); } return partial.sample(u); } }
/** * 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). * * @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; for (Vector row : referenceVectors) { double distance = distanceMeasure.distance(query, row); if (distance < bestDistance && (!differentThanQuery || !row.equals(query))) { bestDistance = distance; bestVector = row; } } return new WeightedThing<Vector>(bestVector, bestDistance); }
/** * 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). * * @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; for (Vector row : referenceVectors) { double distance = distanceMeasure.distance(query, row); if (distance < bestDistance && (!differentThanQuery || !row.equals(query))) { bestDistance = distance; bestVector = row; } } return new WeightedThing<>(bestVector, bestDistance); }
@Override public List<WeightedThing<Vector>> search(Vector query, int limit) { PriorityQueue<WeightedThing<Vector>> top = searchInternal(query); List<WeightedThing<Vector>> results = Lists.newArrayListWithExpectedSize(top.size()); while (top.size() != 0) { WeightedThing<Vector> wv = top.pop(); results.add(new WeightedThing<Vector>(((HashedVector) wv.getValue()).getVector(), wv.getWeight())); } Collections.reverse(results); if (limit < results.size()) { results = results.subList(0, limit); } return results; }
@Override public List<WeightedThing<Vector>> search(Vector query, int limit) { PriorityQueue<WeightedThing<Vector>> top = searchInternal(query); List<WeightedThing<Vector>> results = Lists.newArrayListWithExpectedSize(top.size()); while (top.size() != 0) { WeightedThing<Vector> wv = top.pop(); results.add(new WeightedThing<Vector>(((HashedVector) wv.getValue()).getVector(), wv.getWeight())); } Collections.reverse(results); if (limit < results.size()) { results = results.subList(0, limit); } return results; }
protected static WeightedThing<Vector> removeHash(WeightedThing<Vector> input) { return new WeightedThing<Vector>(((HashedVector) input.getValue()).getVector(), input.getWeight()); }
@Override public List<WeightedThing<Vector>> search(Vector query, int limit) { PriorityQueue<WeightedThing<Vector>> top = searchInternal(query); List<WeightedThing<Vector>> results = Lists.newArrayListWithExpectedSize(top.size()); while (top.size() != 0) { WeightedThing<Vector> wv = top.pop(); results.add(new WeightedThing<>(((HashedVector) wv.getValue()).getVector(), wv.getWeight())); } Collections.reverse(results); if (limit < results.size()) { results = results.subList(0, limit); } return results; }
protected static WeightedThing<Vector> removeHash(WeightedThing<Vector> input) { return new WeightedThing<>(((HashedVector) input.getValue()).getVector(), input.getWeight()); }
for (int i = 0; i < basisMatrix.numRows(); ++i) { List<WeightedThing<Vector>> currProjections = scalarProjections.get(i); WeightedThing<Vector> searchedThing = new WeightedThing<Vector>(projection.get(i)); int middle = Collections.binarySearch(currProjections, searchedThing); if (middle < 0) {
for (int i = 0; i < basisMatrix.numRows(); ++i) { List<WeightedThing<Vector>> currProjections = scalarProjections.get(i); WeightedThing<Vector> searchedThing = new WeightedThing<Vector>(projection.get(i)); int middle = Collections.binarySearch(currProjections, searchedThing); if (middle < 0) {
for (int i = 0; i < basisMatrix.numRows(); ++i) { List<WeightedThing<Vector>> currProjections = scalarProjections.get(i); WeightedThing<Vector> searchedThing = new WeightedThing<>(projection.get(i)); int middle = Collections.binarySearch(currProjections, searchedThing); if (middle < 0) {
@Override public boolean remove(Vector vector, double epsilon) { WeightedThing<Vector> toRemove = searchFirst(vector, false); if (toRemove.getWeight() < epsilon) { Iterator<? extends Vector> basisVectors = basisMatrix.iterator(); for (TreeMultiset<WeightedThing<Vector>> projection : scalarProjections) { if (!projection.remove(new WeightedThing<Vector>(vector, vector.dot(basisVectors.next())))) { throw new RuntimeException("Internal inconsistency in ProjectionSearch"); } } return true; } else { return false; } }
protected static WeightedThing<Vector> removeHash(WeightedThing<Vector> input) { return new WeightedThing<Vector>(((HashedVector) input.getValue()).getVector(), input.getWeight()); }
@Override public boolean remove(Vector vector, double epsilon) { WeightedThing<Vector> toRemove = searchFirst(vector, false); if (toRemove.getWeight() < epsilon) { Iterator<? extends Vector> basisVectors = basisMatrix.iterator(); for (TreeMultiset<WeightedThing<Vector>> projection : scalarProjections) { if (!projection.remove(new WeightedThing<Vector>(vector, vector.dot(basisVectors.next())))) { throw new RuntimeException("Internal inconsistency in ProjectionSearch"); } } return true; } else { return false; } }
@Override public boolean remove(Vector vector, double epsilon) { WeightedThing<Vector> toRemove = searchFirst(vector, false); if (toRemove.getWeight() < epsilon) { Iterator<? extends Vector> basisVectors = basisMatrix.iterator(); for (TreeMultiset<WeightedThing<Vector>> projection : scalarProjections) { if (!projection.remove(new WeightedThing<>(vector, vector.dot(basisVectors.next())))) { throw new RuntimeException("Internal inconsistency in ProjectionSearch"); } } return true; } else { return false; } }
/** * Adds a WeightedVector into the set of projections for later searching. * @param vector The WeightedVector to add. */ @Override public void add(Vector vector) { initialize(vector.size()); Vector projection = basisMatrix.times(vector); // Add the the new vector and the projected distance to each set separately. int i = 0; for (TreeMultiset<WeightedThing<Vector>> s : scalarProjections) { s.add(new WeightedThing<Vector>(vector, projection.get(i++))); } int numVectors = scalarProjections.get(0).size(); for (TreeMultiset<WeightedThing<Vector>> s : scalarProjections) { Preconditions.checkArgument(s.size() == numVectors, "Number of vectors in projection sets " + "differ"); double firstWeight = s.firstEntry().getElement().getWeight(); for (WeightedThing<Vector> w : s) { Preconditions.checkArgument(firstWeight <= w.getWeight(), "Weights not in non-decreasing " + "order"); firstWeight = w.getWeight(); } } }
/** * Adds a WeightedVector into the set of projections for later searching. * @param vector The WeightedVector to add. */ @Override public void add(Vector vector) { initialize(vector.size()); Vector projection = basisMatrix.times(vector); // Add the the new vector and the projected distance to each set separately. int i = 0; for (TreeMultiset<WeightedThing<Vector>> s : scalarProjections) { s.add(new WeightedThing<>(vector, projection.get(i++))); } int numVectors = scalarProjections.get(0).size(); for (TreeMultiset<WeightedThing<Vector>> s : scalarProjections) { Preconditions.checkArgument(s.size() == numVectors, "Number of vectors in projection sets " + "differ"); double firstWeight = s.firstEntry().getElement().getWeight(); for (WeightedThing<Vector> w : s) { Preconditions.checkArgument(firstWeight <= w.getWeight(), "Weights not in non-decreasing " + "order"); firstWeight = w.getWeight(); } } }
/** * Adds a WeightedVector into the set of projections for later searching. * @param vector The WeightedVector to add. */ @Override public void add(Vector vector) { initialize(vector.size()); Vector projection = basisMatrix.times(vector); // Add the the new vector and the projected distance to each set separately. int i = 0; for (TreeMultiset<WeightedThing<Vector>> s : scalarProjections) { s.add(new WeightedThing<Vector>(vector, projection.get(i++))); } int numVectors = scalarProjections.get(0).size(); for (TreeMultiset<WeightedThing<Vector>> s : scalarProjections) { Preconditions.checkArgument(s.size() == numVectors, "Number of vectors in projection sets " + "differ"); double firstWeight = s.firstEntry().getElement().getWeight(); for (WeightedThing<Vector> w : s) { Preconditions.checkArgument(firstWeight <= w.getWeight(), "Weights not in non-decreasing " + "order"); firstWeight = w.getWeight(); } } }