RealVector.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.math4.legacy.linear;

  18. import java.util.Iterator;
  19. import java.util.NoSuchElementException;

  20. import org.apache.commons.math4.legacy.analysis.FunctionUtils;
  21. import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
  22. import org.apache.commons.math4.legacy.analysis.function.Add;
  23. import org.apache.commons.math4.legacy.analysis.function.Divide;
  24. import org.apache.commons.math4.legacy.analysis.function.Multiply;
  25. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  26. import org.apache.commons.math4.legacy.exception.MathArithmeticException;
  27. import org.apache.commons.math4.legacy.exception.MathUnsupportedOperationException;
  28. import org.apache.commons.math4.legacy.exception.NotPositiveException;
  29. import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
  30. import org.apache.commons.math4.legacy.exception.OutOfRangeException;
  31. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
  32. import org.apache.commons.math4.core.jdkmath.JdkMath;

  33. /**
  34.  * Class defining a real-valued vector with basic algebraic operations.
  35.  * <p>
  36.  * vector element indexing is 0-based -- e.g., {@code getEntry(0)}
  37.  * returns the first element of the vector.
  38.  * </p>
  39.  * <p>
  40.  * The {@code code map} and {@code mapToSelf} methods operate
  41.  * on vectors element-wise, i.e. they perform the same operation (adding a scalar,
  42.  * applying a function ...) on each element in turn. The {@code map}
  43.  * versions create a new vector to hold the result and do not change the instance.
  44.  * The {@code mapToSelf} version uses the instance itself to store the
  45.  * results, so the instance is changed by this method. In all cases, the result
  46.  * vector is returned by the methods, allowing the <i>fluent API</i>
  47.  * style, like this:
  48.  * </p>
  49.  * <pre>
  50.  *   RealVector result = v.mapAddToSelf(3.4).mapToSelf(new Tan()).mapToSelf(new Power(2.3));
  51.  * </pre>
  52.  *
  53.  * @since 2.1
  54.  */
  55. public abstract class RealVector {
  56.     /**
  57.      * Returns the size of the vector.
  58.      *
  59.      * @return the size of this vector.
  60.      */
  61.     public abstract int getDimension();

  62.     /**
  63.      * Return the entry at the specified index.
  64.      *
  65.      * @param index Index location of entry to be fetched.
  66.      * @return the vector entry at {@code index}.
  67.      * @throws OutOfRangeException if the index is not valid.
  68.      * @see #setEntry(int, double)
  69.      */
  70.     public abstract double getEntry(int index) throws OutOfRangeException;

  71.     /**
  72.      * Set a single element.
  73.      *
  74.      * @param index element index.
  75.      * @param value new value for the element.
  76.      * @throws OutOfRangeException if the index is not valid.
  77.      * @see #getEntry(int)
  78.      */
  79.     public abstract void setEntry(int index, double value)
  80.         throws OutOfRangeException;

  81.     /**
  82.      * Change an entry at the specified index.
  83.      *
  84.      * @param index Index location of entry to be set.
  85.      * @param increment Value to add to the vector entry.
  86.      * @throws OutOfRangeException if the index is not valid.
  87.      * @since 3.0
  88.      */
  89.     public void addToEntry(int index, double increment)
  90.         throws OutOfRangeException {
  91.         setEntry(index, getEntry(index) + increment);
  92.     }

  93.     /**
  94.      * Construct a new vector by appending a vector to this vector.
  95.      *
  96.      * @param v vector to append to this one.
  97.      * @return a new vector.
  98.      */
  99.     public abstract RealVector append(RealVector v);

  100.     /**
  101.      * Construct a new vector by appending a double to this vector.
  102.      *
  103.      * @param d double to append.
  104.      * @return a new vector.
  105.      */
  106.     public abstract RealVector append(double d);

  107.     /**
  108.      * Get a subvector from consecutive elements.
  109.      *
  110.      * @param index index of first element.
  111.      * @param n number of elements to be retrieved.
  112.      * @return a vector containing n elements.
  113.      * @throws OutOfRangeException if the index is not valid.
  114.      * @throws NotPositiveException if the number of elements is not positive.
  115.      */
  116.     public abstract RealVector getSubVector(int index, int n)
  117.         throws NotPositiveException, OutOfRangeException;

  118.     /**
  119.      * Set a sequence of consecutive elements.
  120.      *
  121.      * @param index index of first element to be set.
  122.      * @param v vector containing the values to set.
  123.      * @throws OutOfRangeException if the index is not valid.
  124.      */
  125.     public abstract void setSubVector(int index, RealVector v)
  126.         throws OutOfRangeException;

  127.     /**
  128.      * Check whether any coordinate of this vector is {@code NaN}.
  129.      *
  130.      * @return {@code true} if any coordinate of this vector is {@code NaN},
  131.      * {@code false} otherwise.
  132.      */
  133.     public abstract boolean isNaN();

  134.     /**
  135.      * Check whether any coordinate of this vector is infinite and none are {@code NaN}.
  136.      *
  137.      * @return {@code true} if any coordinate of this vector is infinite and
  138.      * none are {@code NaN}, {@code false} otherwise.
  139.      */
  140.     public abstract boolean isInfinite();

  141.     /**
  142.      * Check if instance and specified vectors have the same dimension.
  143.      *
  144.      * @param v Vector to compare instance with.
  145.      * @throws DimensionMismatchException if the vectors do not
  146.      * have the same dimension.
  147.      */
  148.     protected void checkVectorDimensions(RealVector v)
  149.         throws DimensionMismatchException {
  150.         checkVectorDimensions(v.getDimension());
  151.     }

  152.     /**
  153.      * Check if instance dimension is equal to some expected value.
  154.      *
  155.      * @param n Expected dimension.
  156.      * @throws DimensionMismatchException if the dimension is
  157.      * inconsistent with the vector size.
  158.      */
  159.     protected void checkVectorDimensions(int n)
  160.         throws DimensionMismatchException {
  161.         int d = getDimension();
  162.         if (d != n) {
  163.             throw new DimensionMismatchException(d, n);
  164.         }
  165.     }

  166.     /**
  167.      * Check if an index is valid.
  168.      *
  169.      * @param index Index to check.
  170.      * @exception OutOfRangeException if {@code index} is not valid.
  171.      */
  172.     protected void checkIndex(final int index) throws OutOfRangeException {
  173.         if (index < 0 ||
  174.             index >= getDimension()) {
  175.             throw new OutOfRangeException(LocalizedFormats.INDEX,
  176.                                           index, 0, getDimension() - 1);
  177.         }
  178.     }

  179.     /**
  180.      * Checks that the indices of a subvector are valid.
  181.      *
  182.      * @param start the index of the first entry of the subvector
  183.      * @param end the index of the last entry of the subvector (inclusive)
  184.      * @throws OutOfRangeException if {@code start} of {@code end} are not valid
  185.      * @throws NumberIsTooSmallException if {@code end < start}
  186.      * @since 3.1
  187.      */
  188.     protected void checkIndices(final int start, final int end)
  189.         throws NumberIsTooSmallException, OutOfRangeException {
  190.         final int dim = getDimension();
  191.         if (start < 0 || start >= dim) {
  192.             throw new OutOfRangeException(LocalizedFormats.INDEX, start, 0,
  193.                                           dim - 1);
  194.         }
  195.         if (end < 0 || end >= dim) {
  196.             throw new OutOfRangeException(LocalizedFormats.INDEX, end, 0,
  197.                                           dim - 1);
  198.         }
  199.         if (end < start) {
  200.             // TODO Use more specific error message
  201.             throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW,
  202.                                                 end, start, false);
  203.         }
  204.     }

  205.     /**
  206.      * Compute the sum of this vector and {@code v}.
  207.      * Returns a new vector. Does not change instance data.
  208.      *
  209.      * @param v Vector to be added.
  210.      * @return {@code this} + {@code v}.
  211.      * @throws DimensionMismatchException if {@code v} is not the same size as
  212.      * {@code this} vector.
  213.      */
  214.     public RealVector add(RealVector v) throws DimensionMismatchException {
  215.         checkVectorDimensions(v);
  216.         RealVector result = v.copy();
  217.         Iterator<Entry> it = iterator();
  218.         while (it.hasNext()) {
  219.             final Entry e = it.next();
  220.             final int index = e.getIndex();
  221.             result.setEntry(index, e.getValue() + result.getEntry(index));
  222.         }
  223.         return result;
  224.     }

  225.     /**
  226.      * Subtract {@code v} from this vector.
  227.      * Returns a new vector. Does not change instance data.
  228.      *
  229.      * @param v Vector to be subtracted.
  230.      * @return {@code this} - {@code v}.
  231.      * @throws DimensionMismatchException if {@code v} is not the same size as
  232.      * {@code this} vector.
  233.      */
  234.     public RealVector subtract(RealVector v) throws DimensionMismatchException {
  235.         checkVectorDimensions(v);
  236.         RealVector result = v.mapMultiply(-1d);
  237.         Iterator<Entry> it = iterator();
  238.         while (it.hasNext()) {
  239.             final Entry e = it.next();
  240.             final int index = e.getIndex();
  241.             result.setEntry(index, e.getValue() + result.getEntry(index));
  242.         }
  243.         return result;
  244.     }

  245.     /**
  246.      * Add a value to each entry.
  247.      * Returns a new vector. Does not change instance data.
  248.      *
  249.      * @param d Value to be added to each entry.
  250.      * @return {@code this} + {@code d}.
  251.      */
  252.     public RealVector mapAdd(double d) {
  253.         return copy().mapAddToSelf(d);
  254.     }

  255.     /**
  256.      * Add a value to each entry.
  257.      * The instance is changed in-place.
  258.      *
  259.      * @param d Value to be added to each entry.
  260.      * @return {@code this}.
  261.      */
  262.     public RealVector mapAddToSelf(double d) {
  263.         if (d != 0) {
  264.             return mapToSelf(FunctionUtils.fix2ndArgument(new Add(), d));
  265.         }
  266.         return this;
  267.     }

  268.     /**
  269.      * Returns a (deep) copy of this vector.
  270.      *
  271.      * @return a vector copy.
  272.      */
  273.     public abstract RealVector copy();

  274.     /**
  275.      * Compute the dot product of this vector with {@code v}.
  276.      *
  277.      * @param v Vector with which dot product should be computed
  278.      * @return the scalar dot product between this instance and {@code v}.
  279.      * @throws DimensionMismatchException if {@code v} is not the same size as
  280.      * {@code this} vector.
  281.      */
  282.     public double dotProduct(RealVector v) throws DimensionMismatchException {
  283.         checkVectorDimensions(v);
  284.         double d = 0;
  285.         final int n = getDimension();
  286.         for (int i = 0; i < n; i++) {
  287.             d += getEntry(i) * v.getEntry(i);
  288.         }
  289.         return d;
  290.     }

  291.     /**
  292.      * Computes the cosine of the angle between this vector and the
  293.      * argument.
  294.      *
  295.      * @param v Vector.
  296.      * @return the cosine of the angle between this vector and {@code v}.
  297.      * @throws MathArithmeticException if {@code this} or {@code v} is the null
  298.      * vector
  299.      * @throws DimensionMismatchException if the dimensions of {@code this} and
  300.      * {@code v} do not match
  301.      */
  302.     public double cosine(RealVector v) throws DimensionMismatchException,
  303.         MathArithmeticException {
  304.         final double norm = getNorm();
  305.         final double vNorm = v.getNorm();

  306.         if (norm == 0 ||
  307.             vNorm == 0) {
  308.             throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
  309.         }
  310.         return dotProduct(v) / (norm * vNorm);
  311.     }

  312.     /**
  313.      * Element-by-element division.
  314.      *
  315.      * @param v Vector by which instance elements must be divided.
  316.      * @return a vector containing this[i] / v[i] for all i.
  317.      * @throws DimensionMismatchException if {@code v} is not the same size as
  318.      * {@code this} vector.
  319.      */
  320.     public abstract RealVector ebeDivide(RealVector v)
  321.         throws DimensionMismatchException;

  322.     /**
  323.      * Element-by-element multiplication.
  324.      *
  325.      * @param v Vector by which instance elements must be multiplied
  326.      * @return a vector containing this[i] * v[i] for all i.
  327.      * @throws DimensionMismatchException if {@code v} is not the same size as
  328.      * {@code this} vector.
  329.      */
  330.     public abstract RealVector ebeMultiply(RealVector v)
  331.         throws DimensionMismatchException;

  332.     /**
  333.      * Distance between two vectors.
  334.      * <p>This method computes the distance consistent with the
  335.      * L<sub>2</sub> norm, i.e. the square root of the sum of
  336.      * element differences, or Euclidean distance.</p>
  337.      *
  338.      * @param v Vector to which distance is requested.
  339.      * @return the distance between two vectors.
  340.      * @throws DimensionMismatchException if {@code v} is not the same size as
  341.      * {@code this} vector.
  342.      * @see #getL1Distance(RealVector)
  343.      * @see #getLInfDistance(RealVector)
  344.      * @see #getNorm()
  345.      */
  346.     public double getDistance(RealVector v) throws DimensionMismatchException {
  347.         checkVectorDimensions(v);
  348.         double d = 0;
  349.         Iterator<Entry> it = iterator();
  350.         while (it.hasNext()) {
  351.             final Entry e = it.next();
  352.             final double diff = e.getValue() - v.getEntry(e.getIndex());
  353.             d += diff * diff;
  354.         }
  355.         return JdkMath.sqrt(d);
  356.     }

  357.     /**
  358.      * Returns the L<sub>2</sub> norm of the vector.
  359.      * <p>The L<sub>2</sub> norm is the root of the sum of
  360.      * the squared elements.</p>
  361.      *
  362.      * @return the norm.
  363.      * @see #getL1Norm()
  364.      * @see #getLInfNorm()
  365.      * @see #getDistance(RealVector)
  366.      */
  367.     public double getNorm() {
  368.         double sum = 0;
  369.         Iterator<Entry> it = iterator();
  370.         while (it.hasNext()) {
  371.             final Entry e = it.next();
  372.             final double value = e.getValue();
  373.             sum += value * value;
  374.         }
  375.         return JdkMath.sqrt(sum);
  376.     }

  377.     /**
  378.      * Returns the L<sub>1</sub> norm of the vector.
  379.      * <p>The L<sub>1</sub> norm is the sum of the absolute
  380.      * values of the elements.</p>
  381.      *
  382.      * @return the norm.
  383.      * @see #getNorm()
  384.      * @see #getLInfNorm()
  385.      * @see #getL1Distance(RealVector)
  386.      */
  387.     public double getL1Norm() {
  388.         double norm = 0;
  389.         Iterator<Entry> it = iterator();
  390.         while (it.hasNext()) {
  391.             final Entry e = it.next();
  392.             norm += JdkMath.abs(e.getValue());
  393.         }
  394.         return norm;
  395.     }

  396.     /**
  397.      * Returns the L<sub>&infin;</sub> norm of the vector.
  398.      * <p>The L<sub>&infin;</sub> norm is the max of the absolute
  399.      * values of the elements.</p>
  400.      *
  401.      * @return the norm.
  402.      * @see #getNorm()
  403.      * @see #getL1Norm()
  404.      * @see #getLInfDistance(RealVector)
  405.      */
  406.     public double getLInfNorm() {
  407.         double norm = 0;
  408.         Iterator<Entry> it = iterator();
  409.         while (it.hasNext()) {
  410.             final Entry e = it.next();
  411.             norm = JdkMath.max(norm, JdkMath.abs(e.getValue()));
  412.         }
  413.         return norm;
  414.     }

  415.     /**
  416.      * Distance between two vectors.
  417.      * <p>This method computes the distance consistent with
  418.      * L<sub>1</sub> norm, i.e. the sum of the absolute values of
  419.      * the elements differences.</p>
  420.      *
  421.      * @param v Vector to which distance is requested.
  422.      * @return the distance between two vectors.
  423.      * @throws DimensionMismatchException if {@code v} is not the same size as
  424.      * {@code this} vector.
  425.      */
  426.     public double getL1Distance(RealVector v)
  427.         throws DimensionMismatchException {
  428.         checkVectorDimensions(v);
  429.         double d = 0;
  430.         Iterator<Entry> it = iterator();
  431.         while (it.hasNext()) {
  432.             final Entry e = it.next();
  433.             d += JdkMath.abs(e.getValue() - v.getEntry(e.getIndex()));
  434.         }
  435.         return d;
  436.     }

  437.     /**
  438.      * Distance between two vectors.
  439.      * <p>This method computes the distance consistent with
  440.      * L<sub>&infin;</sub> norm, i.e. the max of the absolute values of
  441.      * element differences.</p>
  442.      *
  443.      * @param v Vector to which distance is requested.
  444.      * @return the distance between two vectors.
  445.      * @throws DimensionMismatchException if {@code v} is not the same size as
  446.      * {@code this} vector.
  447.      * @see #getDistance(RealVector)
  448.      * @see #getL1Distance(RealVector)
  449.      * @see #getLInfNorm()
  450.      */
  451.     public double getLInfDistance(RealVector v)
  452.         throws DimensionMismatchException {
  453.         checkVectorDimensions(v);
  454.         double d = 0;
  455.         Iterator<Entry> it = iterator();
  456.         while (it.hasNext()) {
  457.             final Entry e = it.next();
  458.             d = JdkMath.max(JdkMath.abs(e.getValue() - v.getEntry(e.getIndex())), d);
  459.         }
  460.         return d;
  461.     }

  462.     /**
  463.      * Get the index of the minimum entry.
  464.      *
  465.      * @return the index of the minimum entry or -1 if vector length is 0
  466.      * or all entries are {@code NaN}.
  467.      */
  468.     public int getMinIndex() {
  469.         int minIndex    = -1;
  470.         double minValue = Double.POSITIVE_INFINITY;
  471.         Iterator<Entry> iterator = iterator();
  472.         while (iterator.hasNext()) {
  473.             final Entry entry = iterator.next();
  474.             if (entry.getValue() <= minValue) {
  475.                 minIndex = entry.getIndex();
  476.                 minValue = entry.getValue();
  477.             }
  478.         }
  479.         return minIndex;
  480.     }

  481.     /**
  482.      * Get the value of the minimum entry.
  483.      *
  484.      * @return the value of the minimum entry or {@code NaN} if all
  485.      * entries are {@code NaN}.
  486.      */
  487.     public double getMinValue() {
  488.         final int minIndex = getMinIndex();
  489.         return minIndex < 0 ? Double.NaN : getEntry(minIndex);
  490.     }

  491.     /**
  492.      * Get the index of the maximum entry.
  493.      *
  494.      * @return the index of the maximum entry or -1 if vector length is 0
  495.      * or all entries are {@code NaN}
  496.      */
  497.     public int getMaxIndex() {
  498.         int maxIndex    = -1;
  499.         double maxValue = Double.NEGATIVE_INFINITY;
  500.         Iterator<Entry> iterator = iterator();
  501.         while (iterator.hasNext()) {
  502.             final Entry entry = iterator.next();
  503.             if (entry.getValue() >= maxValue) {
  504.                 maxIndex = entry.getIndex();
  505.                 maxValue = entry.getValue();
  506.             }
  507.         }
  508.         return maxIndex;
  509.     }

  510.     /**
  511.      * Get the value of the maximum entry.
  512.      *
  513.      * @return the value of the maximum entry or {@code NaN} if all
  514.      * entries are {@code NaN}.
  515.      */
  516.     public double getMaxValue() {
  517.         final int maxIndex = getMaxIndex();
  518.         return maxIndex < 0 ? Double.NaN : getEntry(maxIndex);
  519.     }


  520.     /**
  521.      * Multiply each entry by the argument. Returns a new vector.
  522.      * Does not change instance data.
  523.      *
  524.      * @param d Multiplication factor.
  525.      * @return {@code this} * {@code d}.
  526.      */
  527.     public RealVector mapMultiply(double d) {
  528.         return copy().mapMultiplyToSelf(d);
  529.     }

  530.     /**
  531.      * Multiply each entry.
  532.      * The instance is changed in-place.
  533.      *
  534.      * @param d Multiplication factor.
  535.      * @return {@code this}.
  536.      */
  537.     public RealVector mapMultiplyToSelf(double d){
  538.         return mapToSelf(FunctionUtils.fix2ndArgument(new Multiply(), d));
  539.     }

  540.     /**
  541.      * Subtract a value from each entry. Returns a new vector.
  542.      * Does not change instance data.
  543.      *
  544.      * @param d Value to be subtracted.
  545.      * @return {@code this} - {@code d}.
  546.      */
  547.     public RealVector mapSubtract(double d) {
  548.         return copy().mapSubtractToSelf(d);
  549.     }

  550.     /**
  551.      * Subtract a value from each entry.
  552.      * The instance is changed in-place.
  553.      *
  554.      * @param d Value to be subtracted.
  555.      * @return {@code this}.
  556.      */
  557.     public RealVector mapSubtractToSelf(double d){
  558.         return mapAddToSelf(-d);
  559.     }

  560.     /**
  561.      * Divide each entry by the argument. Returns a new vector.
  562.      * Does not change instance data.
  563.      *
  564.      * @param d Value to divide by.
  565.      * @return {@code this} / {@code d}.
  566.      */
  567.     public RealVector mapDivide(double d) {
  568.         return copy().mapDivideToSelf(d);
  569.     }

  570.     /**
  571.      * Divide each entry by the argument.
  572.      * The instance is changed in-place.
  573.      *
  574.      * @param d Value to divide by.
  575.      * @return {@code this}.
  576.      */
  577.     public RealVector mapDivideToSelf(double d){
  578.         return mapToSelf(FunctionUtils.fix2ndArgument(new Divide(), d));
  579.     }

  580.     /**
  581.      * Compute the outer product.
  582.      *
  583.      * @param v Vector with which outer product should be computed.
  584.      * @return the matrix outer product between this instance and {@code v}.
  585.      */
  586.     public RealMatrix outerProduct(RealVector v) {
  587.         final int m = this.getDimension();
  588.         final int n = v.getDimension();
  589.         final RealMatrix product;
  590.         if (v instanceof SparseRealVector || this instanceof SparseRealVector) {
  591.             product = new OpenMapRealMatrix(m, n);
  592.         } else {
  593.             product = new Array2DRowRealMatrix(m, n);
  594.         }
  595.         for (int i = 0; i < m; i++) {
  596.             for (int j = 0; j < n; j++) {
  597.                 product.setEntry(i, j, this.getEntry(i) * v.getEntry(j));
  598.             }
  599.         }
  600.         return product;
  601.     }

  602.     /**
  603.      * Find the orthogonal projection of this vector onto another vector.
  604.      *
  605.      * @param v vector onto which instance must be projected.
  606.      * @return projection of the instance onto {@code v}.
  607.      * @throws DimensionMismatchException if {@code v} is not the same size as
  608.      * {@code this} vector.
  609.      * @throws MathArithmeticException if {@code this} or {@code v} is the null
  610.      * vector
  611.      */
  612.     public RealVector projection(final RealVector v)
  613.         throws DimensionMismatchException, MathArithmeticException {
  614.         final double norm2 = v.dotProduct(v);
  615.         if (norm2 == 0.0) {
  616.             throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
  617.         }
  618.         return v.mapMultiply(dotProduct(v) / v.dotProduct(v));
  619.     }

  620.     /**
  621.      * Set all elements to a single value.
  622.      *
  623.      * @param value Single value to set for all elements.
  624.      */
  625.     public void set(double value) {
  626.         Iterator<Entry> it = iterator();
  627.         while (it.hasNext()) {
  628.             final Entry e = it.next();
  629.             e.setValue(value);
  630.         }
  631.     }

  632.     /**
  633.      * Convert the vector to an array of {@code double}s.
  634.      * The array is independent from this vector data: the elements
  635.      * are copied.
  636.      *
  637.      * @return an array containing a copy of the vector elements.
  638.      */
  639.     public double[] toArray() {
  640.         int dim = getDimension();
  641.         double[] values = new double[dim];
  642.         for (int i = 0; i < dim; i++) {
  643.             values[i] = getEntry(i);
  644.         }
  645.         return values;
  646.     }

  647.     /**
  648.      * Creates a unit vector pointing in the direction of this vector.
  649.      * The instance is not changed by this method.
  650.      *
  651.      * @return a unit vector pointing in direction of this vector.
  652.      * @throws MathArithmeticException if the norm is zero.
  653.      */
  654.     public RealVector unitVector() throws MathArithmeticException {
  655.         final double norm = getNorm();
  656.         if (norm == 0) {
  657.             throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
  658.         }
  659.         return mapDivide(norm);
  660.     }

  661.     /**
  662.      * Converts this vector into a unit vector.
  663.      * The instance itself is changed by this method.
  664.      *
  665.      * @throws MathArithmeticException if the norm is zero.
  666.      */
  667.     public void unitize() throws MathArithmeticException {
  668.         final double norm = getNorm();
  669.         if (norm == 0) {
  670.             throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
  671.         }
  672.         mapDivideToSelf(getNorm());
  673.     }

  674.     /**
  675.      * Create a sparse iterator over the vector, which may omit some entries.
  676.      * The ommitted entries are either exact zeroes (for dense implementations)
  677.      * or are the entries which are not stored (for real sparse vectors).
  678.      * No guarantees are made about order of iteration.
  679.      *
  680.      * <p>Note: derived classes are required to return an {@link Iterator} that
  681.      * returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()}
  682.      * returns {@code true}.</p>
  683.      *
  684.      * @return a sparse iterator.
  685.      */
  686.     public Iterator<Entry> sparseIterator() {
  687.         return new SparseEntryIterator();
  688.     }

  689.     /**
  690.      * Generic dense iterator. Iteration is in increasing order
  691.      * of the vector index.
  692.      *
  693.      * <p>Note: derived classes are required to return an {@link Iterator} that
  694.      * returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()}
  695.      * returns {@code true}.</p>
  696.      *
  697.      * @return a dense iterator.
  698.      */
  699.     public Iterator<Entry> iterator() {
  700.         final int dim = getDimension();
  701.         return new Iterator<Entry>() {

  702.             /** Current index. */
  703.             private int i;

  704.             /** Current entry. */
  705.             private Entry e = new Entry();

  706.             /** {@inheritDoc} */
  707.             @Override
  708.             public boolean hasNext() {
  709.                 return i < dim;
  710.             }

  711.             /** {@inheritDoc} */
  712.             @Override
  713.             public Entry next() {
  714.                 if (i < dim) {
  715.                     e.setIndex(i++);
  716.                     return e;
  717.                 } else {
  718.                     throw new NoSuchElementException();
  719.                 }
  720.             }

  721.             /**
  722.              * {@inheritDoc}
  723.              *
  724.              * @throws MathUnsupportedOperationException in all circumstances.
  725.              */
  726.             @Override
  727.             public void remove() throws MathUnsupportedOperationException {
  728.                 throw new MathUnsupportedOperationException();
  729.             }
  730.         };
  731.     }

  732.     /**
  733.      * Acts as if implemented as:
  734.      * <pre>
  735.      *  return copy().mapToSelf(function);
  736.      * </pre>
  737.      * Returns a new vector. Does not change instance data.
  738.      *
  739.      * @param function Function to apply to each entry.
  740.      * @return a new vector.
  741.      */
  742.     public RealVector map(UnivariateFunction function) {
  743.         return copy().mapToSelf(function);
  744.     }

  745.     /**
  746.      * Acts as if it is implemented as:
  747.      * {@code
  748.      *  Entry e = null;
  749.      *  for(Iterator<Entry> it = iterator(); it.hasNext(); e = it.next()) {
  750.      *      e.setValue(function.value(e.getValue()));
  751.      *  }
  752.      * }
  753.      * Entries of this vector are modified in-place by this method.
  754.      *
  755.      * @param function Function to apply to each entry.
  756.      * @return a reference to this vector.
  757.      */
  758.     public RealVector mapToSelf(UnivariateFunction function) {
  759.         Iterator<Entry> it = iterator();
  760.         while (it.hasNext()) {
  761.             final Entry e = it.next();
  762.             e.setValue(function.value(e.getValue()));
  763.         }
  764.         return this;
  765.     }

  766.     /**
  767.      * Returns a new vector representing {@code a * this + b * y}, the linear
  768.      * combination of {@code this} and {@code y}.
  769.      * Returns a new vector. Does not change instance data.
  770.      *
  771.      * @param a Coefficient of {@code this}.
  772.      * @param b Coefficient of {@code y}.
  773.      * @param y Vector with which {@code this} is linearly combined.
  774.      * @return a vector containing {@code a * this[i] + b * y[i]} for all
  775.      * {@code i}.
  776.      * @throws DimensionMismatchException if {@code y} is not the same size as
  777.      * {@code this} vector.
  778.      */
  779.     public RealVector combine(double a, double b, RealVector y)
  780.         throws DimensionMismatchException {
  781.         return copy().combineToSelf(a, b, y);
  782.     }

  783.     /**
  784.      * Updates {@code this} with the linear combination of {@code this} and
  785.      * {@code y}.
  786.      *
  787.      * @param a Weight of {@code this}.
  788.      * @param b Weight of {@code y}.
  789.      * @param y Vector with which {@code this} is linearly combined.
  790.      * @return {@code this}, with components equal to
  791.      * {@code a * this[i] + b * y[i]} for all {@code i}.
  792.      * @throws DimensionMismatchException if {@code y} is not the same size as
  793.      * {@code this} vector.
  794.      */
  795.     public RealVector combineToSelf(double a, double b, RealVector y)
  796.         throws DimensionMismatchException {
  797.         checkVectorDimensions(y);
  798.         for (int i = 0; i < getDimension(); i++) {
  799.             final double xi = getEntry(i);
  800.             final double yi = y.getEntry(i);
  801.             setEntry(i, a * xi + b * yi);
  802.         }
  803.         return this;
  804.     }

  805.     /**
  806.      * Visits (but does not alter) all entries of this vector in default order
  807.      * (increasing index).
  808.      *
  809.      * @param visitor the visitor to be used to process the entries of this
  810.      * vector
  811.      * @return the value returned by {@link RealVectorPreservingVisitor#end()}
  812.      * at the end of the walk
  813.      * @since 3.1
  814.      */
  815.     public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor) {
  816.         final int dim = getDimension();
  817.         visitor.start(dim, 0, dim - 1);
  818.         for (int i = 0; i < dim; i++) {
  819.             visitor.visit(i, getEntry(i));
  820.         }
  821.         return visitor.end();
  822.     }

  823.     /**
  824.      * Visits (but does not alter) some entries of this vector in default order
  825.      * (increasing index).
  826.      *
  827.      * @param visitor visitor to be used to process the entries of this vector
  828.      * @param start the index of the first entry to be visited
  829.      * @param end the index of the last entry to be visited (inclusive)
  830.      * @return the value returned by {@link RealVectorPreservingVisitor#end()}
  831.      * at the end of the walk
  832.      * @throws NumberIsTooSmallException if {@code end < start}.
  833.      * @throws OutOfRangeException if the indices are not valid.
  834.      * @since 3.1
  835.      */
  836.     public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor,
  837.                                      final int start, final int end)
  838.         throws NumberIsTooSmallException, OutOfRangeException {
  839.         checkIndices(start, end);
  840.         visitor.start(getDimension(), start, end);
  841.         for (int i = start; i <= end; i++) {
  842.             visitor.visit(i, getEntry(i));
  843.         }
  844.         return visitor.end();
  845.     }

  846.     /**
  847.      * Visits (but does not alter) all entries of this vector in optimized
  848.      * order. The order in which the entries are visited is selected so as to
  849.      * lead to the most efficient implementation; it might depend on the
  850.      * concrete implementation of this abstract class.
  851.      *
  852.      * @param visitor the visitor to be used to process the entries of this
  853.      * vector
  854.      * @return the value returned by {@link RealVectorPreservingVisitor#end()}
  855.      * at the end of the walk
  856.      * @since 3.1
  857.      */
  858.     public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor) {
  859.         return walkInDefaultOrder(visitor);
  860.     }

  861.     /**
  862.      * Visits (but does not alter) some entries of this vector in optimized
  863.      * order. The order in which the entries are visited is selected so as to
  864.      * lead to the most efficient implementation; it might depend on the
  865.      * concrete implementation of this abstract class.
  866.      *
  867.      * @param visitor visitor to be used to process the entries of this vector
  868.      * @param start the index of the first entry to be visited
  869.      * @param end the index of the last entry to be visited (inclusive)
  870.      * @return the value returned by {@link RealVectorPreservingVisitor#end()}
  871.      * at the end of the walk
  872.      * @throws NumberIsTooSmallException if {@code end < start}.
  873.      * @throws OutOfRangeException if the indices are not valid.
  874.      * @since 3.1
  875.      */
  876.     public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor,
  877.                                        final int start, final int end)
  878.         throws NumberIsTooSmallException, OutOfRangeException {
  879.         return walkInDefaultOrder(visitor, start, end);
  880.     }

  881.     /**
  882.      * Visits (and possibly alters) all entries of this vector in default order
  883.      * (increasing index).
  884.      *
  885.      * @param visitor the visitor to be used to process and modify the entries
  886.      * of this vector
  887.      * @return the value returned by {@link RealVectorChangingVisitor#end()}
  888.      * at the end of the walk
  889.      * @since 3.1
  890.      */
  891.     public double walkInDefaultOrder(final RealVectorChangingVisitor visitor) {
  892.         final int dim = getDimension();
  893.         visitor.start(dim, 0, dim - 1);
  894.         for (int i = 0; i < dim; i++) {
  895.             setEntry(i, visitor.visit(i, getEntry(i)));
  896.         }
  897.         return visitor.end();
  898.     }

  899.     /**
  900.      * Visits (and possibly alters) some entries of this vector in default order
  901.      * (increasing index).
  902.      *
  903.      * @param visitor visitor to be used to process the entries of this vector
  904.      * @param start the index of the first entry to be visited
  905.      * @param end the index of the last entry to be visited (inclusive)
  906.      * @return the value returned by {@link RealVectorChangingVisitor#end()}
  907.      * at the end of the walk
  908.      * @throws NumberIsTooSmallException if {@code end < start}.
  909.      * @throws OutOfRangeException if the indices are not valid.
  910.      * @since 3.1
  911.      */
  912.     public double walkInDefaultOrder(final RealVectorChangingVisitor visitor,
  913.                               final int start, final int end)
  914.         throws NumberIsTooSmallException, OutOfRangeException {
  915.         checkIndices(start, end);
  916.         visitor.start(getDimension(), start, end);
  917.         for (int i = start; i <= end; i++) {
  918.             setEntry(i, visitor.visit(i, getEntry(i)));
  919.         }
  920.         return visitor.end();
  921.     }

  922.     /**
  923.      * Visits (and possibly alters) all entries of this vector in optimized
  924.      * order. The order in which the entries are visited is selected so as to
  925.      * lead to the most efficient implementation; it might depend on the
  926.      * concrete implementation of this abstract class.
  927.      *
  928.      * @param visitor the visitor to be used to process the entries of this
  929.      * vector
  930.      * @return the value returned by {@link RealVectorChangingVisitor#end()}
  931.      * at the end of the walk
  932.      * @since 3.1
  933.      */
  934.     public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor) {
  935.         return walkInDefaultOrder(visitor);
  936.     }

  937.     /**
  938.      * Visits (and possibly change) some entries of this vector in optimized
  939.      * order. The order in which the entries are visited is selected so as to
  940.      * lead to the most efficient implementation; it might depend on the
  941.      * concrete implementation of this abstract class.
  942.      *
  943.      * @param visitor visitor to be used to process the entries of this vector
  944.      * @param start the index of the first entry to be visited
  945.      * @param end the index of the last entry to be visited (inclusive)
  946.      * @return the value returned by {@link RealVectorChangingVisitor#end()}
  947.      * at the end of the walk
  948.      * @throws NumberIsTooSmallException if {@code end < start}.
  949.      * @throws OutOfRangeException if the indices are not valid.
  950.      * @since 3.1
  951.      */
  952.     public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor,
  953.                                        final int start, final int end)
  954.         throws NumberIsTooSmallException, OutOfRangeException {
  955.         return walkInDefaultOrder(visitor, start, end);
  956.     }

  957.     /** An entry in the vector. */
  958.     protected class Entry {
  959.         /** Index of this entry. */
  960.         private int index;

  961.         /** Simple constructor. */
  962.         public Entry() {
  963.             setIndex(0);
  964.         }

  965.         /**
  966.          * Get the value of the entry.
  967.          *
  968.          * @return the value of the entry.
  969.          */
  970.         public double getValue() {
  971.             return getEntry(getIndex());
  972.         }

  973.         /**
  974.          * Set the value of the entry.
  975.          *
  976.          * @param value New value for the entry.
  977.          */
  978.         public void setValue(double value) {
  979.             setEntry(getIndex(), value);
  980.         }

  981.         /**
  982.          * Get the index of the entry.
  983.          *
  984.          * @return the index of the entry.
  985.          */
  986.         public int getIndex() {
  987.             return index;
  988.         }

  989.         /**
  990.          * Set the index of the entry.
  991.          *
  992.          * @param index New index for the entry.
  993.          */
  994.         public void setIndex(int index) {
  995.             this.index = index;
  996.         }
  997.     }

  998.     /**
  999.      * <p>
  1000.      * Test for the equality of two real vectors. If all coordinates of two real
  1001.      * vectors are exactly the same, and none are {@code NaN}, the two real
  1002.      * vectors are considered to be equal. {@code NaN} coordinates are
  1003.      * considered to affect globally the vector and be equals to each other -
  1004.      * i.e, if either (or all) coordinates of the real vector are equal to
  1005.      * {@code NaN}, the real vector is equal to a vector with all {@code NaN}
  1006.      * coordinates.
  1007.      * </p>
  1008.      * <p>
  1009.      * This method <em>must</em> be overridden by concrete subclasses of
  1010.      * {@link RealVector} (the current implementation throws an exception).
  1011.      * </p>
  1012.      *
  1013.      * @param other Object to test for equality.
  1014.      * @return {@code true} if two vector objects are equal, {@code false} if
  1015.      * {@code other} is null, not an instance of {@code RealVector}, or
  1016.      * not equal to this {@code RealVector} instance.
  1017.      * @throws MathUnsupportedOperationException if this method is not
  1018.      * overridden.
  1019.      */
  1020.     @Override
  1021.     public boolean equals(Object other)
  1022.         throws MathUnsupportedOperationException {
  1023.         throw new MathUnsupportedOperationException();
  1024.     }

  1025.     /**
  1026.      * {@inheritDoc}. This method <em>must</em> be overridden by concrete
  1027.      * subclasses of {@link RealVector} (current implementation throws an
  1028.      * exception).
  1029.      *
  1030.      * @throws MathUnsupportedOperationException if this method is not
  1031.      * overridden.
  1032.      */
  1033.     @Override
  1034.     public int hashCode() throws MathUnsupportedOperationException {
  1035.         throw new MathUnsupportedOperationException();
  1036.     }

  1037.     /**
  1038.      * This class should rarely be used, but is here to provide
  1039.      * a default implementation of sparseIterator(), which is implemented
  1040.      * by walking over the entries, skipping those that are zero.
  1041.      *
  1042.      * Concrete subclasses which are SparseVector implementations should
  1043.      * make their own sparse iterator, rather than using this one.
  1044.      *
  1045.      * This implementation might be useful for ArrayRealVector, when expensive
  1046.      * operations which preserve the default value are to be done on the entries,
  1047.      * and the fraction of non-default values is small (i.e. someone took a
  1048.      * SparseVector, and passed it into the copy-constructor of ArrayRealVector)

  1049.      */
  1050.     protected class SparseEntryIterator implements Iterator<Entry> {
  1051.         /** Dimension of the vector. */
  1052.         private final int dim;
  1053.         /** Last entry returned by {@link #next()}. */
  1054.         private Entry current;
  1055.         /** Next entry for {@link #next()} to return. */
  1056.         private Entry next;

  1057.         /** Simple constructor. */
  1058.         protected SparseEntryIterator() {
  1059.             dim = getDimension();
  1060.             current = new Entry();
  1061.             next = new Entry();
  1062.             if (next.getValue() == 0) {
  1063.                 advance(next);
  1064.             }
  1065.         }

  1066.         /**
  1067.          * Advance an entry up to the next nonzero one.
  1068.          *
  1069.          * @param e entry to advance.
  1070.          */
  1071.         protected void advance(Entry e) {
  1072.             if (e == null) {
  1073.                 return;
  1074.             }
  1075.             do {
  1076.                 e.setIndex(e.getIndex() + 1);
  1077.             } while (e.getIndex() < dim && e.getValue() == 0);
  1078.             if (e.getIndex() >= dim) {
  1079.                 e.setIndex(-1);
  1080.             }
  1081.         }

  1082.         /** {@inheritDoc} */
  1083.         @Override
  1084.         public boolean hasNext() {
  1085.             return next.getIndex() >= 0;
  1086.         }

  1087.         /** {@inheritDoc} */
  1088.         @Override
  1089.         public Entry next() {
  1090.             int index = next.getIndex();
  1091.             if (index < 0) {
  1092.                 throw new NoSuchElementException();
  1093.             }
  1094.             current.setIndex(index);
  1095.             advance(next);
  1096.             return current;
  1097.         }

  1098.         /**
  1099.          * {@inheritDoc}
  1100.          *
  1101.          * @throws MathUnsupportedOperationException in all circumstances.
  1102.          */
  1103.         @Override
  1104.         public void remove() throws MathUnsupportedOperationException {
  1105.             throw new MathUnsupportedOperationException();
  1106.         }
  1107.     }

  1108.     /**
  1109.      * Returns an unmodifiable view of the specified vector.
  1110.      * The returned vector has read-only access. An attempt to modify it will
  1111.      * result in a {@link MathUnsupportedOperationException}. However, the
  1112.      * returned vector is <em>not</em> immutable, since any modification of
  1113.      * {@code v} will also change the returned view.
  1114.      * For example, in the following piece of code
  1115.      * <pre>
  1116.      *     RealVector v = new ArrayRealVector(2);
  1117.      *     RealVector w = RealVector.unmodifiableRealVector(v);
  1118.      *     v.setEntry(0, 1.2);
  1119.      *     v.setEntry(1, -3.4);
  1120.      * </pre>
  1121.      * the changes will be seen in the {@code w} view of {@code v}.
  1122.      *
  1123.      * @param v Vector for which an unmodifiable view is to be returned.
  1124.      * @return an unmodifiable view of {@code v}.
  1125.      */
  1126.     public static RealVector unmodifiableRealVector(final RealVector v) {
  1127.         /*
  1128.          * This anonymous class is an implementation of {@link RealVector}
  1129.          * with read-only access.
  1130.          * It wraps any {@link RealVector}, and exposes all methods which
  1131.          * do not modify it. Invoking methods which should normally result
  1132.          * in the modification of the calling {@link RealVector} results in
  1133.          * a {@link MathUnsupportedOperationException}. It should be noted
  1134.          * that {@link UnmodifiableVector} is <em>not</em> immutable.
  1135.          */
  1136.         return new RealVector() {
  1137.             /**
  1138.              * {@inheritDoc}
  1139.              *
  1140.              * @throws MathUnsupportedOperationException in all circumstances.
  1141.              */
  1142.             @Override
  1143.             public RealVector mapToSelf(UnivariateFunction function)
  1144.                 throws MathUnsupportedOperationException {
  1145.                 throw new MathUnsupportedOperationException();
  1146.             }

  1147.             /** {@inheritDoc} */
  1148.             @Override
  1149.             public RealVector map(UnivariateFunction function) {
  1150.                 return v.map(function);
  1151.             }

  1152.             /** {@inheritDoc} */
  1153.             @Override
  1154.             public Iterator<Entry> iterator() {
  1155.                 final Iterator<Entry> i = v.iterator();
  1156.                 return new Iterator<Entry>() {
  1157.                     /** The current entry. */
  1158.                     private final UnmodifiableEntry e = new UnmodifiableEntry();

  1159.                     /** {@inheritDoc} */
  1160.                     @Override
  1161.                     public boolean hasNext() {
  1162.                         return i.hasNext();
  1163.                     }

  1164.                     /** {@inheritDoc} */
  1165.                     @Override
  1166.                     public Entry next() {
  1167.                         e.setIndex(i.next().getIndex());
  1168.                         return e;
  1169.                     }

  1170.                     /**
  1171.                      * {@inheritDoc}
  1172.                      *
  1173.                      * @throws MathUnsupportedOperationException in all
  1174.                      * circumstances.
  1175.                      */
  1176.                     @Override
  1177.                     public void remove() throws MathUnsupportedOperationException {
  1178.                         throw new MathUnsupportedOperationException();
  1179.                     }
  1180.                 };
  1181.             }

  1182.             /** {@inheritDoc} */
  1183.             @Override
  1184.             public Iterator<Entry> sparseIterator() {
  1185.                 final Iterator<Entry> i = v.sparseIterator();

  1186.                 return new Iterator<Entry>() {
  1187.                     /** The current entry. */
  1188.                     private final UnmodifiableEntry e = new UnmodifiableEntry();

  1189.                     /** {@inheritDoc} */
  1190.                     @Override
  1191.                     public boolean hasNext() {
  1192.                         return i.hasNext();
  1193.                     }

  1194.                     /** {@inheritDoc} */
  1195.                     @Override
  1196.                     public Entry next() {
  1197.                         e.setIndex(i.next().getIndex());
  1198.                         return e;
  1199.                     }

  1200.                     /**
  1201.                      * {@inheritDoc}
  1202.                      *
  1203.                      * @throws MathUnsupportedOperationException in all
  1204.                      * circumstances.
  1205.                      */
  1206.                     @Override
  1207.                     public void remove()
  1208.                         throws MathUnsupportedOperationException {
  1209.                         throw new MathUnsupportedOperationException();
  1210.                     }
  1211.                 };
  1212.             }

  1213.             /** {@inheritDoc} */
  1214.             @Override
  1215.             public RealVector copy() {
  1216.                 return v.copy();
  1217.             }

  1218.             /** {@inheritDoc} */
  1219.             @Override
  1220.             public RealVector add(RealVector w)
  1221.                 throws DimensionMismatchException {
  1222.                 return v.add(w);
  1223.             }

  1224.             /** {@inheritDoc} */
  1225.             @Override
  1226.             public RealVector subtract(RealVector w)
  1227.                 throws DimensionMismatchException {
  1228.                 return v.subtract(w);
  1229.             }

  1230.             /** {@inheritDoc} */
  1231.             @Override
  1232.             public RealVector mapAdd(double d) {
  1233.                 return v.mapAdd(d);
  1234.             }

  1235.             /**
  1236.              * {@inheritDoc}
  1237.              *
  1238.              * @throws MathUnsupportedOperationException in all
  1239.              * circumstances.
  1240.              */
  1241.             @Override
  1242.             public RealVector mapAddToSelf(double d)
  1243.                 throws MathUnsupportedOperationException {
  1244.                 throw new MathUnsupportedOperationException();
  1245.             }

  1246.             /** {@inheritDoc} */
  1247.             @Override
  1248.             public RealVector mapSubtract(double d) {
  1249.                 return v.mapSubtract(d);
  1250.             }

  1251.             /**
  1252.              * {@inheritDoc}
  1253.              *
  1254.              * @throws MathUnsupportedOperationException in all
  1255.              * circumstances.
  1256.              */
  1257.             @Override
  1258.             public RealVector mapSubtractToSelf(double d)
  1259.                 throws MathUnsupportedOperationException {
  1260.                 throw new MathUnsupportedOperationException();
  1261.             }

  1262.             /** {@inheritDoc} */
  1263.             @Override
  1264.             public RealVector mapMultiply(double d) {
  1265.                 return v.mapMultiply(d);
  1266.             }

  1267.             /**
  1268.              * {@inheritDoc}
  1269.              *
  1270.              * @throws MathUnsupportedOperationException in all
  1271.              * circumstances.
  1272.              */
  1273.             @Override
  1274.             public RealVector mapMultiplyToSelf(double d)
  1275.                 throws MathUnsupportedOperationException {
  1276.                 throw new MathUnsupportedOperationException();
  1277.             }

  1278.             /** {@inheritDoc} */
  1279.             @Override
  1280.             public RealVector mapDivide(double d) {
  1281.                 return v.mapDivide(d);
  1282.             }

  1283.             /**
  1284.              * {@inheritDoc}
  1285.              *
  1286.              * @throws MathUnsupportedOperationException in all
  1287.              * circumstances.
  1288.              */
  1289.             @Override
  1290.             public RealVector mapDivideToSelf(double d)
  1291.                 throws MathUnsupportedOperationException {
  1292.                 throw new MathUnsupportedOperationException();
  1293.             }

  1294.             /** {@inheritDoc} */
  1295.             @Override
  1296.             public RealVector ebeMultiply(RealVector w)
  1297.                 throws DimensionMismatchException {
  1298.                 return v.ebeMultiply(w);
  1299.             }

  1300.             /** {@inheritDoc} */
  1301.             @Override
  1302.             public RealVector ebeDivide(RealVector w)
  1303.                 throws DimensionMismatchException {
  1304.                 return v.ebeDivide(w);
  1305.             }

  1306.             /** {@inheritDoc} */
  1307.             @Override
  1308.             public double dotProduct(RealVector w)
  1309.                 throws DimensionMismatchException {
  1310.                 return v.dotProduct(w);
  1311.             }

  1312.             /** {@inheritDoc} */
  1313.             @Override
  1314.             public double cosine(RealVector w)
  1315.                 throws DimensionMismatchException, MathArithmeticException {
  1316.                 return v.cosine(w);
  1317.             }

  1318.             /** {@inheritDoc} */
  1319.             @Override
  1320.             public double getNorm() {
  1321.                 return v.getNorm();
  1322.             }

  1323.             /** {@inheritDoc} */
  1324.             @Override
  1325.             public double getL1Norm() {
  1326.                 return v.getL1Norm();
  1327.             }

  1328.             /** {@inheritDoc} */
  1329.             @Override
  1330.             public double getLInfNorm() {
  1331.                 return v.getLInfNorm();
  1332.             }

  1333.             /** {@inheritDoc} */
  1334.             @Override
  1335.             public double getDistance(RealVector w)
  1336.                 throws DimensionMismatchException {
  1337.                 return v.getDistance(w);
  1338.             }

  1339.             /** {@inheritDoc} */
  1340.             @Override
  1341.             public double getL1Distance(RealVector w)
  1342.                 throws DimensionMismatchException {
  1343.                 return v.getL1Distance(w);
  1344.             }

  1345.             /** {@inheritDoc} */
  1346.             @Override
  1347.             public double getLInfDistance(RealVector w)
  1348.                 throws DimensionMismatchException {
  1349.                 return v.getLInfDistance(w);
  1350.             }

  1351.             /** {@inheritDoc} */
  1352.             @Override
  1353.             public RealVector unitVector() throws MathArithmeticException {
  1354.                 return v.unitVector();
  1355.             }

  1356.             /**
  1357.              * {@inheritDoc}
  1358.              *
  1359.              * @throws MathUnsupportedOperationException in all
  1360.              * circumstances.
  1361.              */
  1362.             @Override
  1363.             public void unitize() throws MathUnsupportedOperationException {
  1364.                 throw new MathUnsupportedOperationException();
  1365.             }

  1366.             /** {@inheritDoc} */
  1367.             @Override
  1368.             public RealMatrix outerProduct(RealVector w) {
  1369.                 return v.outerProduct(w);
  1370.             }

  1371.             /** {@inheritDoc} */
  1372.             @Override
  1373.             public double getEntry(int index) throws OutOfRangeException {
  1374.                 return v.getEntry(index);
  1375.             }

  1376.             /**
  1377.              * {@inheritDoc}
  1378.              *
  1379.              * @throws MathUnsupportedOperationException in all
  1380.              * circumstances.
  1381.              */
  1382.             @Override
  1383.             public void setEntry(int index, double value)
  1384.                 throws MathUnsupportedOperationException {
  1385.                 throw new MathUnsupportedOperationException();
  1386.             }

  1387.             /**
  1388.              * {@inheritDoc}
  1389.              *
  1390.              * @throws MathUnsupportedOperationException in all
  1391.              * circumstances.
  1392.              */
  1393.             @Override
  1394.             public void addToEntry(int index, double value)
  1395.                 throws MathUnsupportedOperationException {
  1396.                 throw new MathUnsupportedOperationException();
  1397.             }

  1398.             /** {@inheritDoc} */
  1399.             @Override
  1400.             public int getDimension() {
  1401.                 return v.getDimension();
  1402.             }

  1403.             /** {@inheritDoc} */
  1404.             @Override
  1405.             public RealVector append(RealVector w) {
  1406.                 return v.append(w);
  1407.             }

  1408.             /** {@inheritDoc} */
  1409.             @Override
  1410.             public RealVector append(double d) {
  1411.                 return v.append(d);
  1412.             }

  1413.             /** {@inheritDoc} */
  1414.             @Override
  1415.             public RealVector getSubVector(int index, int n)
  1416.                 throws OutOfRangeException, NotPositiveException {
  1417.                 return v.getSubVector(index, n);
  1418.             }

  1419.             /**
  1420.              * {@inheritDoc}
  1421.              *
  1422.              * @throws MathUnsupportedOperationException in all
  1423.              * circumstances.
  1424.              */
  1425.             @Override
  1426.             public void setSubVector(int index, RealVector w)
  1427.                 throws MathUnsupportedOperationException {
  1428.                 throw new MathUnsupportedOperationException();
  1429.             }

  1430.             /**
  1431.              * {@inheritDoc}
  1432.              *
  1433.              * @throws MathUnsupportedOperationException in all
  1434.              * circumstances.
  1435.              */
  1436.             @Override
  1437.             public void set(double value)
  1438.                 throws MathUnsupportedOperationException {
  1439.                 throw new MathUnsupportedOperationException();
  1440.             }

  1441.             /** {@inheritDoc} */
  1442.             @Override
  1443.             public double[] toArray() {
  1444.                 return v.toArray();
  1445.             }

  1446.             /** {@inheritDoc} */
  1447.             @Override
  1448.             public boolean isNaN() {
  1449.                 return v.isNaN();
  1450.             }

  1451.             /** {@inheritDoc} */
  1452.             @Override
  1453.             public boolean isInfinite() {
  1454.                 return v.isInfinite();
  1455.             }

  1456.             /** {@inheritDoc} */
  1457.             @Override
  1458.             public RealVector combine(double a, double b, RealVector y)
  1459.                 throws DimensionMismatchException {
  1460.                 return v.combine(a, b, y);
  1461.             }

  1462.             /**
  1463.              * {@inheritDoc}
  1464.              *
  1465.              * @throws MathUnsupportedOperationException in all
  1466.              * circumstances.
  1467.              */
  1468.             @Override
  1469.             public RealVector combineToSelf(double a, double b, RealVector y)
  1470.                 throws MathUnsupportedOperationException {
  1471.                 throw new MathUnsupportedOperationException();
  1472.             }

  1473.             /** An entry in the vector. */
  1474.             class UnmodifiableEntry extends Entry {
  1475.                 /** {@inheritDoc} */
  1476.                 @Override
  1477.                 public double getValue() {
  1478.                     return v.getEntry(getIndex());
  1479.                 }

  1480.                 /**
  1481.                  * {@inheritDoc}
  1482.                  *
  1483.                  * @throws MathUnsupportedOperationException in all
  1484.                  * circumstances.
  1485.                  */
  1486.                 @Override
  1487.                 public void setValue(double value)
  1488.                     throws MathUnsupportedOperationException {
  1489.                     throw new MathUnsupportedOperationException();
  1490.                 }
  1491.             }
  1492.         };
  1493.     }
  1494. }