/** {@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); } }
/** * 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; }
private Entry e = new Entry();
private Entry e = new Entry();
/** * 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); }
/** {@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); } }
/** {@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); } }
/** * 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; }
/** {@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 = Cloner.clone(data); 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 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 Math.sqrt(d); }
/** * 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 = Math.max(Math.abs(e.getValue() - v.getEntry(e.getIndex())), d); } return d; }
/** {@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 = Cloner.clone(data); 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>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; }
/** * 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>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 Math.sqrt(sum); }
/** * 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 += Math.abs(e.getValue() - v.getEntry(e.getIndex())); } return d; }
/** * 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; }
/** * 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; }
/** * 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; }