/** {@inheritDoc} */ public OpenMapRealVector mapMultiplyToSelf(double d) { Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); entries.put(iter.key(), iter.value() * d); } return this; }
/** {@inheritDoc} */ public OpenMapRealVector mapPowToSelf(double d) { Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); entries.put(iter.key(), Math.pow(iter.value(), d)); } return this; }
/** {@inheritDoc} */ public OpenMapRealVector mapSqrtToSelf() { Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); entries.put(iter.key(), Math.sqrt(iter.value())); } return this; }
/** {@inheritDoc} */ public double getL1Norm() { double res = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res += Math.abs(iter.value()); } return res; }
/** {@inheritDoc} */ public double getLInfNorm() { double max = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); max += iter.value(); } return max; }
/** {@inheritDoc} */ public double getNorm() { double res = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res += iter.value() * iter.value(); } return Math.sqrt(res); }
/** * Optimized method to append a OpenMapRealVector. * * @param v vector to append * @return The result of appending <code>v</code> to self */ public OpenMapRealVector append(OpenMapRealVector v) { OpenMapRealVector res = new OpenMapRealVector(this, v.getDimension()); Iterator iter = v.entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key() + virtualSize, iter.value()); } return res; }
/** {@inheritDoc} */ public double[] getData() { double[] res = new double[virtualSize]; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res[iter.key()] = iter.value(); } return res; }
/** {@inheritDoc} */ public double dotProduct(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); double res = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res += v.getEntry(iter.key()) * iter.value(); } return res; }
/** {@inheritDoc} */ public boolean isNaN() { Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); if (Double.isNaN(iter.value())) { return true; } } return false; }
/** {@inheritDoc} */ public void unitize() { double norm = getNorm(); if (isZero(norm)) { throw MathRuntimeException.createArithmeticException("cannot normalize a zero norm vector"); } Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); entries.put(iter.key(), iter.value() / norm); } }
/** {@inheritDoc} */ public double dotProduct(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); double res = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { int idx = iter.key(); double value = 0; if (idx < v.length) { value = v[idx]; } res += value * iter.value(); } return res; }
/** {@inheritDoc} */ public RealMatrix outerProduct(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); RealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize); Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int row = iter.key(); double value = iter.value(); for (int col = 0; col < virtualSize; col++) { res.setEntry(row, col, value * v[col]); } } return res; }
/** * Optimized method to compute the outer product. * * @param v The vector to comput the outer product on * @return The outer product of <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public OpenMapRealMatrix outerproduct(OpenMapRealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); OpenMapRealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize); Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); Iterator iter2 = v.getEntries().iterator(); while (iter2.hasNext()) { iter2.advance(); res.setEntry(iter.key(), iter2.key(), iter.value() * iter2.value()); } } return res; }
/** {@inheritDoc} */ public OpenMapRealVector getSubVector(int index, int n) throws MatrixIndexException { checkIndex(index); checkIndex(index + n - 1); OpenMapRealVector res = new OpenMapRealVector(n); int end = index + n; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (key >= index && key < end) { res.setEntry(key - index, iter.value()); } } return res; }
/** {@inheritDoc} */ public boolean isInfinite() { boolean infiniteFound = false; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); final double value = iter.value(); if (Double.isNaN(value)) { return false; } if (Double.isInfinite(value)) { infiniteFound = true; } } return infiniteFound; }
/** * {@inheritDoc} * * <p>Implementation Note: This works on exact values, and as a result it is possible for {@code * a.subtract(b)} to be the zero vector, while {@code a.hashCode() != b.hashCode()}. */ @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(epsilon); result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + virtualSize; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); temp = Double.doubleToLongBits(iter.value()); result = prime * result + (int) (temp ^ (temp >> 32)); } return result; }
/** {@inheritDoc} */ public RealMatrix outerProduct(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); if (v instanceof OpenMapRealVector) { return outerproduct((OpenMapRealVector) v); } RealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize); Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int row = iter.key(); for (int col = 0; col < virtualSize; col++) { res.setEntry(row, col, iter.value() * v.getEntry(col)); } } return res; }
/** * Optimized method to compute LInfDistance. * * @param v The vector to compute from * @return the LInfDistance */ private double getLInfDistance(OpenMapRealVector v) { double max = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); double delta = Math.abs(iter.value() - v.getEntry(iter.key())); if (delta > max) { max = delta; } } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { if (iter.value() > max) { max = iter.value(); } } } return max; }
/** {@inheritDoc} */ public OpenMapRealVector ebeDivide(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); OpenMapRealVector res = new OpenMapRealVector(this); Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key())); } return res; }
/** {@inheritDoc} */ public OpenMapRealVector ebeMultiply(double[] v) throws IllegalArgumentException { checkVectorDimensions(v.length); OpenMapRealVector res = new OpenMapRealVector(this); Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value() * v[iter.key()]); } return res; }
/** * Optimized method to compute distance. * * @param v The vector to compute distance to * @return The distance from <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public double getDistance(OpenMapRealVector v) throws IllegalArgumentException { 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 Math.sqrt(res); }
/** * Implementation Note: This performs an exact comparison, and as a result it is possible for * {@code a.subtract(b}} to be the zero vector, while {@code a.equals(b) == false}. {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof OpenMapRealVector)) { return false; } OpenMapRealVector other = (OpenMapRealVector) obj; if (virtualSize != other.virtualSize) { return false; } if (Double.doubleToLongBits(epsilon) != Double.doubleToLongBits(other.epsilon)) { return false; } Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); double test = other.getEntry(iter.key()); if (Double.doubleToLongBits(test) != Double.doubleToLongBits(iter.value())) { return false; } } iter = other.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); double test = iter.value(); if (Double.doubleToLongBits(test) != Double.doubleToLongBits(getEntry(iter.key()))) { return false; } } return true; }
/** * Optimized method to add two OpenMapRealVectors. * * @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()); OpenMapRealVector res = (OpenMapRealVector) 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; }