/** * 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; }
/** * 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; }
/** {@inheritDoc} */ public Entry next() { int index = next.getIndex(); if (index < 0) { throw new NoSuchElementException(); } current.setIndex(index); advance(next); return current; }
/** {@inheritDoc} */ public Entry next() { int index = next.getIndex(); if (index < 0) { throw new NoSuchElementException(); } current.setIndex(index); advance(next); return current; }
/** * Get the value of the entry. * * @return the value of the entry. */ public double getValue() { return getEntry(getIndex()); }
/** * Set the value of the entry. * * @param value New value for the entry. */ public void setValue(double value) { setEntry(getIndex(), value); }
/** * Get the value of the entry. * * @return the value of the entry. */ public double getValue() { return getEntry(getIndex()); }
/** {@inheritDoc} */ public Entry next() { int index = next.getIndex(); if (index < 0) { throw new NoSuchElementException(); } current.setIndex(index); advance(next); return current; }
/** * Set the value of the entry. * * @param value New value for the entry. */ public void setValue(double value) { setEntry(getIndex(), value); }
/** {@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; }
/** * 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; }
/** * 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; }
/** {@inheritDoc} */ public boolean hasNext() { return next.getIndex() >= 0; }
/** * Set the value of the entry. * * @param value New value for the entry. */ public void setValue(double value) { setEntry(getIndex(), value); }
/** * Get the value of the entry. * * @return the value of the entry. */ public double getValue() { return getEntry(getIndex()); }