/** * Create from an array, specifying zero tolerance. * Only non-zero entries will be stored. * * @param values Set of values to create from. * @param epsilon Tolerance below which a value is considered zero. */ public OpenMapRealVector(double[] values, double epsilon) { virtualSize = values.length; entries = new OpenIntToDoubleHashMap(0.0); this.epsilon = epsilon; for (int key = 0; key < values.length; key++) { double value = values[key]; if (!isDefaultValue(value)) { entries.put(key, value); } } }
/** * Put a value associated with a key in the map. * @param key key to which value is associated * @param value value to put in the map * @return previous value associated with the key */ public double put(final int key, final double value) { int index = findInsertionIndex(key); double previous = missingEntries; boolean newMapping = true; if (index < 0) { index = changeIndexSign(index); previous = values[index]; newMapping = false; } keys[index] = key; states[index] = FULL; values[index] = value; if (newMapping) { ++size; if (shouldGrowTable()) { growTable(); } ++count; } return previous; }
/** * Build an empty map with specified size. * @param expectedSize expected number of elements in the map * @param missingEntries value to return when a missing entry is fetched */ public OpenIntToDoubleHashMap(final int expectedSize, final double missingEntries) { final int capacity = computeCapacity(expectedSize); keys = new int[capacity]; values = new double[capacity]; states = new byte[capacity]; this.missingEntries = missingEntries; mask = capacity - 1; }
/** * Remove the value associated with a key. * @param key key to which the value is associated * @return removed value */ public double remove(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return doRemove(index); } if (states[index] == FREE) { return missingEntries; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return doRemove(index); } } return missingEntries; }
final int hash = hashOf(key); int index = hash & mask; if (states[index] == FREE) { return index; } else if (states[index] == FULL && keys[index] == key) { return changeIndexSign(index); int perturb = perturb(hash); int j = index; if (states[index] == FULL) { while (true) { j = probe(perturb, j); index = j & mask; perturb >>= PERTURB_SHIFT; return changeIndexSign(index); j = probe(perturb, j); index = j & mask; return firstRemoved; } else if (states[index] == FULL && keys[index] == key) { return changeIndexSign(index);
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) { iterator.advance(); final double value = iterator.value(); for (int j = 0; j < outCols; ++j) { final int rightKey = m.computeKey(k, j); if (m.entries.containsKey(rightKey)) { final int outKey = out.computeKey(i, j); final double outValue = out.entries.get(outKey) + value * m.entries.get(rightKey); if (outValue == 0.0) { out.entries.remove(outKey); } else { out.entries.put(outKey, outValue);
/** {@inheritDoc} */ @Override public void addToEntry(int row, int column, double increment) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); final int key = computeKey(row, column); final double value = entries.get(key) + increment; if (value == 0.0) { entries.remove(key); } else { entries.put(key, value); } }
/** * Optimized method to add two OpenMapRealVectors. * It copies the larger vector, then iterates over the smaller. * * @param v Vector to add. * @return the sum of {@code this} and {@code v}. * @throws DimensionMismatchException if the dimensions do not match. */ public OpenMapRealVector add(OpenMapRealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); boolean copyThis = entries.size() > v.entries.size(); OpenMapRealVector res = copyThis ? this.copy() : v.copy(); Iterator iter = copyThis ? v.entries.iterator() : entries.iterator(); OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries; while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (randomAccess.containsKey(key)) { res.setEntry(key, randomAccess.get(key) + iter.value()); } else { res.setEntry(key, iter.value()); } } return res; }
/** * Optimized method to compute distance. * * @param v Vector to compute distance to. * @return the distance from {@code this} and {@code v}. * @throws DimensionMismatchException if the dimensions do not match. */ public double getDistance(OpenMapRealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); Iterator iter = entries.iterator(); double res = 0; while (iter.hasNext()) { iter.advance(); int key = iter.key(); double delta; delta = iter.value() - v.getEntry(key); res += delta * delta; } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { final double value = iter.value(); res += value * value; } } return FastMath.sqrt(res); }
/** {@inheritDoc} */ @Override public void setEntry(int index, double value) throws OutOfRangeException { checkIndex(index); if (!isDefaultValue(value)) { entries.put(index, value); } else if (entries.containsKey(index)) { entries.remove(index); } }
/** * Optimized method to subtract OpenMapRealVectors. * * @param v Vector to subtract from {@code this}. * @return the difference of {@code this} and {@code v}. * @throws DimensionMismatchException if the dimensions do not match. */ public OpenMapRealVector subtract(OpenMapRealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); OpenMapRealVector res = copy(); Iterator iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (entries.containsKey(key)) { res.setEntry(key, entries.get(key) - iter.value()); } else { res.setEntry(key, -iter.value()); } } return res; }
/** * Construct a vector of zeroes, specifying zero tolerance. * * @param dimension Size of the vector. * @param epsilon Tolerance below which a value considered zero. */ public OpenMapRealVector(int dimension, double epsilon) { virtualSize = dimension; entries = new OpenIntToDoubleHashMap(0.0); this.epsilon = epsilon; }
/** Simple constructor. */ protected OpenMapSparseIterator() { iter = entries.iterator(); current = new OpenMapEntry(iter); }
/** {@inheritDoc} */ @Override public void unitize() throws MathArithmeticException { double norm = getNorm(); if (isDefaultValue(norm)) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); entries.put(iter.key(), iter.value() / norm); } }
/** {@inheritDoc} */ @Override public double getEntry(int index) throws OutOfRangeException { checkIndex(index); return entries.get(index); }
/** * Find the index at which a key should be inserted * @param key key to lookup * @return index at which key should be inserted */ private int findInsertionIndex(final int key) { return findInsertionIndex(keys, states, key, mask); }
/** * Get the stored value associated with the given key * @param key key associated with the data * @return data associated with the key */ public double get(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return values[index]; } if (states[index] == FREE) { return missingEntries; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return values[index]; } } return missingEntries; }
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) { iterator.advance(); final double value = iterator.value(); for (int j = 0; j < outCols; ++j) { final int rightKey = m.computeKey(k, j); if (m.entries.containsKey(rightKey)) { final int outKey = out.computeKey(i, j); final double outValue = out.entries.get(outKey) + value * m.entries.get(rightKey); if (outValue == 0.0) { out.entries.remove(outKey); } else { out.entries.put(outKey, outValue);
/** {@inheritDoc} */ @Override public void multiplyEntry(int row, int column, double factor) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); final int key = computeKey(row, column); final double value = entries.get(key) * factor; if (value == 0.0) { entries.remove(key); } else { entries.put(key, value); } }
/** * Optimized method to add two OpenMapRealVectors. * It copies the larger vector, then iterates over the smaller. * * @param v Vector to add. * @return the sum of {@code this} and {@code v}. * @throws DimensionMismatchException if the dimensions do not match. */ public OpenMapRealVector add(OpenMapRealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); boolean copyThis = entries.size() > v.entries.size(); OpenMapRealVector res = copyThis ? this.copy() : v.copy(); Iterator iter = copyThis ? v.entries.iterator() : entries.iterator(); OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries; while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (randomAccess.containsKey(key)) { res.setEntry(key, randomAccess.get(key) + iter.value()); } else { res.setEntry(key, iter.value()); } } return res; }