/** {@inheritDoc} */ public double getEntry(int index) throws MatrixIndexException { checkIndex(index); return entries.get(index); }
/** {@inheritDoc} */ @Override public double getDistance(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); double res = 0; for (int i = 0; i < v.length; i++) { double delta = entries.get(i) - v[i]; res += delta * delta; } return Math.sqrt(res); }
/** {@inheritDoc} */ public double getEntry(int index) throws MatrixIndexException { checkIndex(index); return entries.get(index); }
/** {@inheritDoc} */ @Override public double getDistance(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); double res = 0; for (int i = 0; i < v.length; i++) { double delta = entries.get(i) - v[i]; res += delta * delta; } return FastMath.sqrt(res); }
/** {@inheritDoc} */ @Override public double getEntry(int row, int column) throws MatrixIndexException { MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); return entries.get(computeKey(row, column)); }
/** {@inheritDoc} */ @Override public double getEntry(int row, int column) throws MatrixIndexException { MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); return entries.get(computeKey(row, column)); }
/** {@inheritDoc} */ @Override public void addToEntry(int row, int column, double increment) throws MatrixIndexException { 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); } }
/** {@inheritDoc} */ @Override public void multiplyEntry(int row, int column, double factor) throws MatrixIndexException { 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); } }
/** {@inheritDoc} */ @Override public OpenMapRealVector subtract(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); OpenMapRealVector res = new OpenMapRealVector(this); for (int i = 0; i < v.length; i++) { if (entries.containsKey(i)) { res.setEntry(i, entries.get(i) - v[i]); } else { res.setEntry(i, -v[i]); } } return res; }
/** {@inheritDoc} */ @Override public OpenMapRealVector subtract(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); OpenMapRealVector res = new OpenMapRealVector(this); for (int i = 0; i < v.length; i++) { if (entries.containsKey(i)) { res.setEntry(i, entries.get(i) - v[i]); } else { res.setEntry(i, -v[i]); } } return res; }
/** {@inheritDoc} */ @Override public void multiplyEntry(int row, int column, double factor) throws MatrixIndexException { 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); } }
/** {@inheritDoc} */ @Override public void addToEntry(int row, int column, double increment) throws MatrixIndexException { 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 compute the dot product with an OpenMapRealVector. * Iterates over the smaller of the two. * @param v The vector to compute the dot product with * @return The dot product of <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public double dotProduct(OpenMapRealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); boolean thisIsSmaller = entries.size() < v.entries.size(); Iterator iter = thisIsSmaller ? entries.iterator() : v.entries.iterator(); OpenIntToDoubleHashMap larger = thisIsSmaller ? v.entries : entries; double d = 0; while(iter.hasNext()) { iter.advance(); d += iter.value() * larger.get(iter.key()); } return d; }
/** * Optimized method to compute the dot product with an OpenMapRealVector. * Iterates over the smaller of the two. * @param v The vector to compute the dot product with * @return The dot product of <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public double dotProduct(OpenMapRealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); boolean thisIsSmaller = entries.size() < v.entries.size(); Iterator iter = thisIsSmaller ? entries.iterator() : v.entries.iterator(); OpenIntToDoubleHashMap larger = thisIsSmaller ? v.entries : entries; double d = 0; while(iter.hasNext()) { iter.advance(); d += iter.value() * larger.get(iter.key()); } return d; }
/** * Optimized method to subtract OpenMapRealVectors. * @param v The vector to subtract from <code>this</code> * @return The difference of <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public OpenMapRealVector subtract(OpenMapRealVector v) throws IllegalArgumentException{ 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; }
/** * Optimized method to subtract OpenMapRealVectors. * @param v The vector to subtract from <code>this</code> * @return The difference of <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public OpenMapRealVector subtract(OpenMapRealVector v) throws IllegalArgumentException{ 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; }
/** * Optimized method to add two OpenMapRealVectors. Copies the larger vector, iterates over the smaller. * @param v Vector to add with * @return The sum of <code>this</code> with <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public OpenMapRealVector add(OpenMapRealVector v) throws IllegalArgumentException{ 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 add two OpenMapRealVectors. Copies the larger vector, iterates over the smaller. * @param v Vector to add with * @return The sum of <code>this</code> with <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public OpenMapRealVector add(OpenMapRealVector v) throws IllegalArgumentException{ 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; }