/** * Returns the L<sub>1</sub> norm of the vector. * <p>The L<sub>1</sub> norm is the sum of the absolute * values of the elements.</p> * * @return the norm. * @see #getNorm() * @see #getLInfNorm() * @see #getL1Distance(RealVector) */ public double getL1Norm() { double norm = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); norm += FastMath.abs(e.getValue()); } return norm; }
/** * Returns the L<sub>2</sub> norm of the vector. * <p>The L<sub>2</sub> norm is the root of the sum of * the squared elements.</p> * * @return the norm. * @see #getL1Norm() * @see #getLInfNorm() * @see #getDistance(RealVector) */ public double getNorm() { double sum = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final double value = e.getValue(); sum += value * value; } return FastMath.sqrt(sum); }
/** * Returns the L<sub>∞</sub> norm of the vector. * <p>The L<sub>∞</sub> norm is the max of the absolute * values of the elements.</p> * * @return the norm. * @see #getNorm() * @see #getL1Norm() * @see #getLInfDistance(RealVector) */ public double getLInfNorm() { double norm = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); norm = FastMath.max(norm, FastMath.abs(e.getValue())); } return norm; }
/** * Advance an entry up to the next nonzero one. * * @param e entry to advance. */ protected void advance(Entry e) { if (e == null) { return; } do { e.setIndex(e.getIndex() + 1); } while (e.getIndex() < dim && e.getValue() == 0); if (e.getIndex() >= dim) { e.setIndex(-1); } }
/** * Advance an entry up to the next nonzero one. * * @param e entry to advance. */ protected void advance(Entry e) { if (e == null) { return; } do { e.setIndex(e.getIndex() + 1); } while (e.getIndex() < dim && e.getValue() == 0); if (e.getIndex() >= dim) { e.setIndex(-1); } }
public static Map<Integer, Double> vectorToMap(RealVector vector) { Map<Integer, Double> mapVector = new HashMap<>(); Iterator<RealVector.Entry> iter = vector.sparseIterator(); while (iter.hasNext()) { RealVector.Entry entry = iter.next(); mapVector.put(entry.getIndex(), entry.getValue()); } return mapVector; }
/** Simple constructor. */ protected SparseEntryIterator() { dim = getDimension(); current = new Entry(); next = new Entry(); if (next.getValue() == 0) { advance(next); } }
/** Simple constructor. */ protected SparseEntryIterator() { dim = getDimension(); current = new Entry(); next = new Entry(); if (next.getValue() == 0) { advance(next); } }
/** {@inheritDoc} */ @Override public ArrayRealVector add(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; final int dim = vData.length; checkVectorDimensions(dim); ArrayRealVector result = new ArrayRealVector(dim); double[] resultData = result.data; for (int i = 0; i < dim; i++) { resultData[i] = data[i] + vData[i]; } return result; } else { checkVectorDimensions(v); double[] out = data.clone(); Iterator<Entry> it = v.iterator(); while (it.hasNext()) { final Entry e = it.next(); out[e.getIndex()] += e.getValue(); } return new ArrayRealVector(out, false); } }
/** {@inheritDoc} */ @Override public ArrayRealVector subtract(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; final int dim = vData.length; checkVectorDimensions(dim); ArrayRealVector result = new ArrayRealVector(dim); double[] resultData = result.data; for (int i = 0; i < dim; i++) { resultData[i] = data[i] - vData[i]; } return result; } else { checkVectorDimensions(v); double[] out = data.clone(); Iterator<Entry> it = v.iterator(); while (it.hasNext()) { final Entry e = it.next(); out[e.getIndex()] -= e.getValue(); } return new ArrayRealVector(out, false); } }
/** * Distance between two vectors. * <p>This method computes the distance consistent with * L<sub>∞</sub> norm, i.e. the max of the absolute values of * element differences.</p> * * @param v Vector to which distance is requested. * @return the distance between two vectors. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * @see #getDistance(RealVector) * @see #getL1Distance(RealVector) * @see #getLInfNorm() */ public double getLInfDistance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); d = FastMath.max(FastMath.abs(e.getValue() - v.getEntry(e.getIndex())), d); } return d; }
/** * Distance between two vectors. * <p>This method computes the distance consistent with the * L<sub>2</sub> norm, i.e. the square root of the sum of * element differences, or Euclidean distance.</p> * * @param v Vector to which distance is requested. * @return the distance between two vectors. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * @see #getL1Distance(RealVector) * @see #getLInfDistance(RealVector) * @see #getNorm() */ public double getDistance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final double diff = e.getValue() - v.getEntry(e.getIndex()); d += diff * diff; } return FastMath.sqrt(d); }
/** * Distance between two vectors. * <p>This method computes the distance consistent with * L<sub>1</sub> norm, i.e. the sum of the absolute values of * the elements differences.</p> * * @param v Vector to which distance is requested. * @return the distance between two vectors. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public double getL1Distance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); d += FastMath.abs(e.getValue() - v.getEntry(e.getIndex())); } return d; }
/** * Subtract {@code v} from this vector. * Returns a new vector. Does not change instance data. * * @param v Vector to be subtracted. * @return {@code this} - {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public RealVector subtract(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); RealVector result = v.mapMultiply(-1d); Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final int index = e.getIndex(); result.setEntry(index, e.getValue() + result.getEntry(index)); } return result; }
/** * Acts as if it is implemented as: * <pre> * Entry e = null; * for(Iterator<Entry> it = iterator(); it.hasNext(); e = it.next()) { * e.setValue(function.value(e.getValue())); * } * </pre> * Entries of this vector are modified in-place by this method. * * @param function Function to apply to each entry. * @return a reference to this vector. */ public RealVector mapToSelf(UnivariateFunction function) { Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); e.setValue(function.value(e.getValue())); } return this; }
/** * Compute the sum of this vector and {@code v}. * Returns a new vector. Does not change instance data. * * @param v Vector to be added. * @return {@code this} + {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public RealVector add(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); RealVector result = v.copy(); Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final int index = e.getIndex(); result.setEntry(index, e.getValue() + result.getEntry(index)); } return result; }
/** * Get the index of the maximum entry. * * @return the index of the maximum entry or -1 if vector length is 0 * or all entries are {@code NaN} */ public int getMaxIndex() { int maxIndex = -1; double maxValue = Double.NEGATIVE_INFINITY; Iterator<Entry> iterator = iterator(); while (iterator.hasNext()) { final Entry entry = iterator.next(); if (entry.getValue() >= maxValue) { maxIndex = entry.getIndex(); maxValue = entry.getValue(); } } return maxIndex; }
/** * Get the index of the minimum entry. * * @return the index of the minimum entry or -1 if vector length is 0 * or all entries are {@code NaN}. */ public int getMinIndex() { int minIndex = -1; double minValue = Double.POSITIVE_INFINITY; Iterator<Entry> iterator = iterator(); while (iterator.hasNext()) { final Entry entry = iterator.next(); if (entry.getValue() <= minValue) { minIndex = entry.getIndex(); minValue = entry.getValue(); } } return minIndex; }
/** * Advance an entry up to the next nonzero one. * * @param e entry to advance. */ protected void advance(Entry e) { if (e == null) { return; } do { e.setIndex(e.getIndex() + 1); } while (e.getIndex() < dim && e.getValue() == 0); if (e.getIndex() >= dim) { e.setIndex(-1); } }
/** Simple constructor. */ protected SparseEntryIterator() { dim = getDimension(); current = new Entry(); next = new Entry(); if (next.getValue() == 0) { advance(next); } }