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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.linear;

  22. import java.util.Iterator;
  23. import java.util.NoSuchElementException;

  24. import org.hipparchus.analysis.FunctionUtils;
  25. import org.hipparchus.analysis.UnivariateFunction;
  26. import org.hipparchus.analysis.function.Add;
  27. import org.hipparchus.analysis.function.Divide;
  28. import org.hipparchus.analysis.function.Multiply;
  29. import org.hipparchus.exception.LocalizedCoreFormats;
  30. import org.hipparchus.exception.MathIllegalArgumentException;
  31. import org.hipparchus.exception.MathRuntimeException;
  32. import org.hipparchus.util.FastMath;

  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.  */
  54. public abstract class RealVector {

  55.     /** Empty constructor.
  56.      * <p>
  57.      * This constructor is not strictly necessary, but it prevents spurious
  58.      * javadoc warnings with JDK 18 and later.
  59.      * </p>
  60.      * @since 3.0
  61.      */
  62.     protected RealVector() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  63.         // nothing to do
  64.     }

  65.     /**
  66.      * Returns the size of the vector.
  67.      *
  68.      * @return the size of this vector.
  69.      */
  70.     public abstract int getDimension();

  71.     /**
  72.      * Return the entry at the specified index.
  73.      *
  74.      * @param index Index location of entry to be fetched.
  75.      * @return the vector entry at {@code index}.
  76.      * @throws MathIllegalArgumentException if the index is not valid.
  77.      * @see #setEntry(int, double)
  78.      */
  79.     public abstract double getEntry(int index) throws MathIllegalArgumentException;

  80.     /**
  81.      * Set a single element.
  82.      *
  83.      * @param index element index.
  84.      * @param value new value for the element.
  85.      * @throws MathIllegalArgumentException if the index is not valid.
  86.      * @see #getEntry(int)
  87.      */
  88.     public abstract void setEntry(int index, double value)
  89.         throws MathIllegalArgumentException;

  90.     /**
  91.      * Change an entry at the specified index.
  92.      *
  93.      * @param index Index location of entry to be set.
  94.      * @param increment Value to add to the vector entry.
  95.      * @throws MathIllegalArgumentException if the index is not valid.
  96.      */
  97.     public void addToEntry(int index, double increment)
  98.         throws MathIllegalArgumentException {
  99.         setEntry(index, getEntry(index) + increment);
  100.     }

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

  108.     /**
  109.      * Construct a new vector by appending a double to this vector.
  110.      *
  111.      * @param d double to append.
  112.      * @return a new vector.
  113.      */
  114.     public abstract RealVector append(double d);

  115.     /**
  116.      * Get a subvector from consecutive elements.
  117.      *
  118.      * @param index index of first element.
  119.      * @param n number of elements to be retrieved.
  120.      * @return a vector containing n elements.
  121.      * @throws MathIllegalArgumentException if the index is not valid.
  122.      * @throws MathIllegalArgumentException if the number of elements is not positive.
  123.      */
  124.     public abstract RealVector getSubVector(int index, int n)
  125.         throws MathIllegalArgumentException;

  126.     /**
  127.      * Set a sequence of consecutive elements.
  128.      *
  129.      * @param index index of first element to be set.
  130.      * @param v vector containing the values to set.
  131.      * @throws MathIllegalArgumentException if the index is not valid.
  132.      */
  133.     public abstract void setSubVector(int index, RealVector v)
  134.         throws MathIllegalArgumentException;

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

  142.     /**
  143.      * Check whether any coordinate of this vector is infinite and none are {@code NaN}.
  144.      *
  145.      * @return {@code true} if any coordinate of this vector is infinite and
  146.      * none are {@code NaN}, {@code false} otherwise.
  147.      */
  148.     public abstract boolean isInfinite();

  149.     /**
  150.      * Check if instance and specified vectors have the same dimension.
  151.      *
  152.      * @param v Vector to compare instance with.
  153.      * @throws MathIllegalArgumentException if the vectors do not
  154.      * have the same dimension.
  155.      */
  156.     protected void checkVectorDimensions(RealVector v)
  157.         throws MathIllegalArgumentException {
  158.         checkVectorDimensions(v.getDimension());
  159.     }

  160.     /**
  161.      * Check if instance dimension is equal to some expected value.
  162.      *
  163.      * @param n Expected dimension.
  164.      * @throws MathIllegalArgumentException if the dimension is
  165.      * inconsistent with the vector size.
  166.      */
  167.     protected void checkVectorDimensions(int n)
  168.         throws MathIllegalArgumentException {
  169.         int d = getDimension();
  170.         if (d != n) {
  171.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  172.                                                    d, n);
  173.         }
  174.     }

  175.     /**
  176.      * Check if an index is valid.
  177.      *
  178.      * @param index Index to check.
  179.      * @exception MathIllegalArgumentException if {@code index} is not valid.
  180.      */
  181.     protected void checkIndex(final int index) throws MathIllegalArgumentException {
  182.         if (index < 0 ||
  183.             index >= getDimension()) {
  184.             throw new MathIllegalArgumentException(LocalizedCoreFormats.INDEX,
  185.                                           index, 0, getDimension() - 1);
  186.         }
  187.     }

  188.     /**
  189.      * Checks that the indices of a subvector are valid.
  190.      *
  191.      * @param start the index of the first entry of the subvector
  192.      * @param end the index of the last entry of the subvector (inclusive)
  193.      * @throws MathIllegalArgumentException if {@code start} of {@code end} are not valid
  194.      * @throws MathIllegalArgumentException if {@code end < start}
  195.      */
  196.     protected void checkIndices(final int start, final int end)
  197.         throws MathIllegalArgumentException {
  198.         final int dim = getDimension();
  199.         if ((start < 0) || (start >= dim)) {
  200.             throw new MathIllegalArgumentException(LocalizedCoreFormats.INDEX, start, 0,
  201.                                           dim - 1);
  202.         }
  203.         if ((end < 0) || (end >= dim)) {
  204.             throw new MathIllegalArgumentException(LocalizedCoreFormats.INDEX, end, 0,
  205.                                           dim - 1);
  206.         }
  207.         if (end < start) {
  208.             // TODO Use more specific error message
  209.             throw new MathIllegalArgumentException(LocalizedCoreFormats.INITIAL_ROW_AFTER_FINAL_ROW,
  210.                                                 end, start, false);
  211.         }
  212.     }

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

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

  253.     /**
  254.      * Add a value to each entry.
  255.      * Returns a new vector. Does not change instance data.
  256.      *
  257.      * @param d Value to be added to each entry.
  258.      * @return {@code this} + {@code d}.
  259.      */
  260.     public RealVector mapAdd(double d) {
  261.         return copy().mapAddToSelf(d);
  262.     }

  263.     /**
  264.      * Add a value to each entry.
  265.      * The instance is changed in-place.
  266.      *
  267.      * @param d Value to be added to each entry.
  268.      * @return {@code this}.
  269.      */
  270.     public RealVector mapAddToSelf(double d) {
  271.         if (d != 0) {
  272.             return mapToSelf(FunctionUtils.fix2ndArgument(new Add(), d));
  273.         }
  274.         return this;
  275.     }

  276.     /**
  277.      * Returns a (deep) copy of this vector.
  278.      *
  279.      * @return a vector copy.
  280.      */
  281.     public abstract RealVector copy();

  282.     /**
  283.      * Compute the dot product of this vector with {@code v}.
  284.      *
  285.      * @param v Vector with which dot product should be computed
  286.      * @return the scalar dot product between this instance and {@code v}.
  287.      * @throws MathIllegalArgumentException if {@code v} is not the same size as
  288.      * {@code this} vector.
  289.      */
  290.     public double dotProduct(RealVector v) throws MathIllegalArgumentException {
  291.         checkVectorDimensions(v);
  292.         double d = 0;
  293.         final int n = getDimension();
  294.         for (int i = 0; i < n; i++) {
  295.             d += getEntry(i) * v.getEntry(i);
  296.         }
  297.         return d;
  298.     }

  299.     /**
  300.      * Computes the cosine of the angle between this vector and the
  301.      * argument.
  302.      *
  303.      * @param v Vector.
  304.      * @return the cosine of the angle between this vector and {@code v}.
  305.      * @throws MathRuntimeException if {@code this} or {@code v} is the null
  306.      * vector
  307.      * @throws MathIllegalArgumentException if the dimensions of {@code this} and
  308.      * {@code v} do not match
  309.      */
  310.     public double cosine(RealVector v) throws MathIllegalArgumentException,
  311.         MathRuntimeException {
  312.         final double norm = getNorm();
  313.         final double vNorm = v.getNorm();

  314.         if (norm == 0 ||
  315.             vNorm == 0) {
  316.             throw new MathRuntimeException(LocalizedCoreFormats.ZERO_NORM);
  317.         }
  318.         return dotProduct(v) / (norm * vNorm);
  319.     }

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

  330.     /**
  331.      * Element-by-element multiplication.
  332.      *
  333.      * @param v Vector by which instance elements must be multiplied
  334.      * @return a vector containing this[i] * v[i] for all i.
  335.      * @throws MathIllegalArgumentException if {@code v} is not the same size as
  336.      * {@code this} vector.
  337.      */
  338.     public abstract RealVector ebeMultiply(RealVector v)
  339.         throws MathIllegalArgumentException;

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

  365.     /**
  366.      * Returns the L<sub>2</sub> norm of the vector.
  367.      * <p>The L<sub>2</sub> norm is the root of the sum of
  368.      * the squared elements.</p>
  369.      *
  370.      * @return the norm.
  371.      * @see #getL1Norm()
  372.      * @see #getLInfNorm()
  373.      * @see #getDistance(RealVector)
  374.      */
  375.     public double getNorm() {
  376.         double sum = 0;
  377.         Iterator<Entry> it = iterator();
  378.         while (it.hasNext()) {
  379.             final Entry e = it.next();
  380.             final double value = e.getValue();
  381.             sum += value * value;
  382.         }
  383.         return FastMath.sqrt(sum);
  384.     }

  385.     /**
  386.      * Returns the L<sub>1</sub> norm of the vector.
  387.      * <p>The L<sub>1</sub> norm is the sum of the absolute
  388.      * values of the elements.</p>
  389.      *
  390.      * @return the norm.
  391.      * @see #getNorm()
  392.      * @see #getLInfNorm()
  393.      * @see #getL1Distance(RealVector)
  394.      */
  395.     public double getL1Norm() {
  396.         double norm = 0;
  397.         Iterator<Entry> it = iterator();
  398.         while (it.hasNext()) {
  399.             final Entry e = it.next();
  400.             norm += FastMath.abs(e.getValue());
  401.         }
  402.         return norm;
  403.     }

  404.     /**
  405.      * Returns the L<sub>&infin;</sub> norm of the vector.
  406.      * <p>The L<sub>&infin;</sub> norm is the max of the absolute
  407.      * values of the elements.</p>
  408.      *
  409.      * @return the norm.
  410.      * @see #getNorm()
  411.      * @see #getL1Norm()
  412.      * @see #getLInfDistance(RealVector)
  413.      */
  414.     public double getLInfNorm() {
  415.         double norm = 0;
  416.         Iterator<Entry> it = iterator();
  417.         while (it.hasNext()) {
  418.             final Entry e = it.next();
  419.             norm = FastMath.max(norm, FastMath.abs(e.getValue()));
  420.         }
  421.         return norm;
  422.     }

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

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

  470.     /**
  471.      * Get the index of the minimum entry.
  472.      *
  473.      * @return the index of the minimum entry or -1 if vector length is 0
  474.      * or all entries are {@code NaN}.
  475.      */
  476.     public int getMinIndex() {
  477.         int minIndex    = -1;
  478.         double minValue = Double.POSITIVE_INFINITY;
  479.         Iterator<Entry> iterator = iterator();
  480.         while (iterator.hasNext()) {
  481.             final Entry entry = iterator.next();
  482.             if (entry.getValue() <= minValue) {
  483.                 minIndex = entry.getIndex();
  484.                 minValue = entry.getValue();
  485.             }
  486.         }
  487.         return minIndex;
  488.     }

  489.     /**
  490.      * Get the value of the minimum entry.
  491.      *
  492.      * @return the value of the minimum entry or {@code NaN} if all
  493.      * entries are {@code NaN}.
  494.      */
  495.     public double getMinValue() {
  496.         final int minIndex = getMinIndex();
  497.         return minIndex < 0 ? Double.NaN : getEntry(minIndex);
  498.     }

  499.     /**
  500.      * Get the index of the maximum entry.
  501.      *
  502.      * @return the index of the maximum entry or -1 if vector length is 0
  503.      * or all entries are {@code NaN}
  504.      */
  505.     public int getMaxIndex() {
  506.         int maxIndex    = -1;
  507.         double maxValue = Double.NEGATIVE_INFINITY;
  508.         Iterator<Entry> iterator = iterator();
  509.         while (iterator.hasNext()) {
  510.             final Entry entry = iterator.next();
  511.             if (entry.getValue() >= maxValue) {
  512.                 maxIndex = entry.getIndex();
  513.                 maxValue = entry.getValue();
  514.             }
  515.         }
  516.         return maxIndex;
  517.     }

  518.     /**
  519.      * Get the value of the maximum entry.
  520.      *
  521.      * @return the value of the maximum entry or {@code NaN} if all
  522.      * entries are {@code NaN}.
  523.      */
  524.     public double getMaxValue() {
  525.         final int maxIndex = getMaxIndex();
  526.         return maxIndex < 0 ? Double.NaN : getEntry(maxIndex);
  527.     }


  528.     /**
  529.      * Multiply each entry by the argument. Returns a new vector.
  530.      * Does not change instance data.
  531.      *
  532.      * @param d Multiplication factor.
  533.      * @return {@code this} * {@code d}.
  534.      */
  535.     public RealVector mapMultiply(double d) {
  536.         return copy().mapMultiplyToSelf(d);
  537.     }

  538.     /**
  539.      * Multiply each entry.
  540.      * The instance is changed in-place.
  541.      *
  542.      * @param d Multiplication factor.
  543.      * @return {@code this}.
  544.      */
  545.     public RealVector mapMultiplyToSelf(double d){
  546.         return mapToSelf(FunctionUtils.fix2ndArgument(new Multiply(), d));
  547.     }

  548.     /**
  549.      * Subtract a value from each entry. Returns a new vector.
  550.      * Does not change instance data.
  551.      *
  552.      * @param d Value to be subtracted.
  553.      * @return {@code this} - {@code d}.
  554.      */
  555.     public RealVector mapSubtract(double d) {
  556.         return copy().mapSubtractToSelf(d);
  557.     }

  558.     /**
  559.      * Subtract a value from each entry.
  560.      * The instance is changed in-place.
  561.      *
  562.      * @param d Value to be subtracted.
  563.      * @return {@code this}.
  564.      */
  565.     public RealVector mapSubtractToSelf(double d){
  566.         return mapAddToSelf(-d);
  567.     }

  568.     /**
  569.      * Divide each entry by the argument. Returns a new vector.
  570.      * Does not change instance data.
  571.      *
  572.      * @param d Value to divide by.
  573.      * @return {@code this} / {@code d}.
  574.      */
  575.     public RealVector mapDivide(double d) {
  576.         return copy().mapDivideToSelf(d);
  577.     }

  578.     /**
  579.      * Divide each entry by the argument.
  580.      * The instance is changed in-place.
  581.      *
  582.      * @param d Value to divide by.
  583.      * @return {@code this}.
  584.      */
  585.     public RealVector mapDivideToSelf(double d){
  586.         return mapToSelf(FunctionUtils.fix2ndArgument(new Divide(), d));
  587.     }

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

  610.     /**
  611.      * Find the orthogonal projection of this vector onto another vector.
  612.      *
  613.      * @param v vector onto which instance must be projected.
  614.      * @return projection of the instance onto {@code v}.
  615.      * @throws MathIllegalArgumentException if {@code v} is not the same size as
  616.      * {@code this} vector.
  617.      * @throws MathRuntimeException if {@code this} or {@code v} is the null
  618.      * vector
  619.      */
  620.     public RealVector projection(final RealVector v)
  621.         throws MathIllegalArgumentException, MathRuntimeException {
  622.         final double norm2 = v.dotProduct(v);
  623.         if (norm2 == 0.0) {
  624.             throw new MathRuntimeException(LocalizedCoreFormats.ZERO_NORM);
  625.         }
  626.         return v.mapMultiply(dotProduct(v) / norm2);
  627.     }

  628.     /**
  629.      * Set all elements to a single value.
  630.      *
  631.      * @param value Single value to set for all elements.
  632.      */
  633.     public void set(double value) {
  634.         Iterator<Entry> it = iterator();
  635.         while (it.hasNext()) {
  636.             final Entry e = it.next();
  637.             e.setValue(value);
  638.         }
  639.     }

  640.     /**
  641.      * Convert the vector to an array of {@code double}s.
  642.      * The array is independent from this vector data: the elements
  643.      * are copied.
  644.      *
  645.      * @return an array containing a copy of the vector elements.
  646.      */
  647.     public double[] toArray() {
  648.         int dim = getDimension();
  649.         double[] values = new double[dim];
  650.         for (int i = 0; i < dim; i++) {
  651.             values[i] = getEntry(i);
  652.         }
  653.         return values;
  654.     }

  655.     /**
  656.      * Creates a unit vector pointing in the direction of this vector.
  657.      * The instance is not changed by this method.
  658.      *
  659.      * @return a unit vector pointing in direction of this vector.
  660.      * @throws MathRuntimeException if the norm is zero.
  661.      */
  662.     public RealVector unitVector() throws MathRuntimeException {
  663.         final double norm = getNorm();
  664.         if (norm == 0) {
  665.             throw new MathRuntimeException(LocalizedCoreFormats.ZERO_NORM);
  666.         }
  667.         return mapDivide(norm);
  668.     }

  669.     /**
  670.      * Converts this vector into a unit vector.
  671.      * The instance itself is changed by this method.
  672.      *
  673.      * @throws MathRuntimeException if the norm is zero.
  674.      */
  675.     public void unitize() throws MathRuntimeException {
  676.         final double norm = getNorm();
  677.         if (norm == 0) {
  678.             throw new MathRuntimeException(LocalizedCoreFormats.ZERO_NORM);
  679.         }
  680.         mapDivideToSelf(getNorm());
  681.     }

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

  697.     /**
  698.      * Generic dense iterator. Iteration is in increasing order
  699.      * of the vector index.
  700.      *
  701.      * <p>Note: derived classes are required to return an {@link Iterator} that
  702.      * returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()}
  703.      * returns {@code true}.</p>
  704.      *
  705.      * @return a dense iterator.
  706.      */
  707.     public Iterator<Entry> iterator() {
  708.         final int dim = getDimension();
  709.         return new Iterator<Entry>() {

  710.             /** Current index. */
  711.             private int i;

  712.             /** Current entry. */
  713.             private Entry e = new Entry();

  714.             /** {@inheritDoc} */
  715.             @Override
  716.             public boolean hasNext() {
  717.                 return i < dim;
  718.             }

  719.             /** {@inheritDoc} */
  720.             @Override
  721.             public Entry next() {
  722.                 if (i < dim) {
  723.                     e.setIndex(i++);
  724.                     return e;
  725.                 } else {
  726.                     throw new NoSuchElementException();
  727.                 }
  728.             }

  729.             /**
  730.              * {@inheritDoc}
  731.              *
  732.              * @throws MathRuntimeException in all circumstances.
  733.              */
  734.             @Override
  735.             public void remove() throws MathRuntimeException {
  736.                 throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  737.             }
  738.         };
  739.     }

  740.     /**
  741.      * Acts as if implemented as:
  742.      * <pre>
  743.      *  return copy().mapToSelf(function);
  744.      * </pre>
  745.      * Returns a new vector. Does not change instance data.
  746.      *
  747.      * @param function Function to apply to each entry.
  748.      * @return a new vector.
  749.      */
  750.     public RealVector map(UnivariateFunction function) {
  751.         return copy().mapToSelf(function);
  752.     }

  753.     /**
  754.      * Acts as if it is implemented as:
  755.      * <pre>
  756.      *  Entry e = null;
  757.      *  for(Iterator&lt;Entry&gt; it = iterator(); it.hasNext(); e = it.next()) {
  758.      *      e.setValue(function.value(e.getValue()));
  759.      *  }
  760.      * </pre>
  761.      * Entries of this vector are modified in-place by this method.
  762.      *
  763.      * @param function Function to apply to each entry.
  764.      * @return a reference to this vector.
  765.      */
  766.     public RealVector mapToSelf(UnivariateFunction function) {
  767.         Iterator<Entry> it = iterator();
  768.         while (it.hasNext()) {
  769.             final Entry e = it.next();
  770.             e.setValue(function.value(e.getValue()));
  771.         }
  772.         return this;
  773.     }

  774.     /**
  775.      * Returns a new vector representing {@code a * this + b * y}, the linear
  776.      * combination of {@code this} and {@code y}.
  777.      * Returns a new vector. Does not change instance data.
  778.      *
  779.      * @param a Coefficient of {@code this}.
  780.      * @param b Coefficient of {@code y}.
  781.      * @param y Vector with which {@code this} is linearly combined.
  782.      * @return a vector containing {@code a * this[i] + b * y[i]} for all
  783.      * {@code i}.
  784.      * @throws MathIllegalArgumentException if {@code y} is not the same size as
  785.      * {@code this} vector.
  786.      */
  787.     public RealVector combine(double a, double b, RealVector y)
  788.         throws MathIllegalArgumentException {
  789.         return copy().combineToSelf(a, b, y);
  790.     }

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

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

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

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

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

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

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

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

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

  957.     /** An entry in the vector. */
  958.     public 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 overriden 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 MathRuntimeException if this method is not
  1018.      * overridden.
  1019.      */
  1020.     @Override
  1021.     public boolean equals(Object other)
  1022.         throws MathRuntimeException {
  1023.         throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  1024.     }

  1025.     /**
  1026.      * {@inheritDoc}. This method <em>must</em> be overriden by concrete
  1027.      * subclasses of {@link RealVector} (current implementation throws an
  1028.      * exception).
  1029.      *
  1030.      * @throws MathRuntimeException if this method is not
  1031.      * overridden.
  1032.      */
  1033.     @Override
  1034.     public int hashCode() throws MathRuntimeException {
  1035.         throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  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 MathRuntimeException in all circumstances.
  1102.          */
  1103.         @Override
  1104.         public void remove() throws MathRuntimeException {
  1105.             throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  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 MathRuntimeException}. 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 MathRuntimeException}. It should be noted
  1134.          * that {@link UnmodifiableVector} is <em>not</em> immutable.
  1135.          */
  1136.         return new RealVector() {
  1137.             /**
  1138.              * {@inheritDoc}
  1139.              *
  1140.              * @throws MathRuntimeException in all circumstances.
  1141.              */
  1142.             @Override
  1143.             public RealVector mapToSelf(UnivariateFunction function)
  1144.                 throws MathRuntimeException {
  1145.                 throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  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 MathRuntimeException in all
  1174.                      * circumstances.
  1175.                      */
  1176.                     @Override
  1177.                     public void remove() throws MathRuntimeException {
  1178.                         throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  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 MathRuntimeException in all
  1204.                      * circumstances.
  1205.                      */
  1206.                     @Override
  1207.                     public void remove()
  1208.                         throws MathRuntimeException {
  1209.                         throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  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 MathIllegalArgumentException {
  1222.                 return v.add(w);
  1223.             }

  1224.             /** {@inheritDoc} */
  1225.             @Override
  1226.             public RealVector subtract(RealVector w)
  1227.                 throws MathIllegalArgumentException {
  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 MathRuntimeException in all
  1239.              * circumstances.
  1240.              */
  1241.             @Override
  1242.             public RealVector mapAddToSelf(double d)
  1243.                 throws MathRuntimeException {
  1244.                 throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  1245.             }

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

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

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

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

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

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

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

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

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

  1312.             /** {@inheritDoc} */
  1313.             @Override
  1314.             public double cosine(RealVector w)
  1315.                 throws MathRuntimeException {
  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 MathIllegalArgumentException {
  1337.                 return v.getDistance(w);
  1338.             }

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

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

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

  1356.             /**
  1357.              * {@inheritDoc}
  1358.              *
  1359.              * @throws MathRuntimeException in all
  1360.              * circumstances.
  1361.              */
  1362.             @Override
  1363.             public void unitize() throws MathRuntimeException {
  1364.                 throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  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 MathIllegalArgumentException {
  1374.                 return v.getEntry(index);
  1375.             }

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

  1387.             /**
  1388.              * {@inheritDoc}
  1389.              *
  1390.              * @throws MathRuntimeException in all
  1391.              * circumstances.
  1392.              */
  1393.             @Override
  1394.             public void addToEntry(int index, double value)
  1395.                 throws MathRuntimeException {
  1396.                 throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  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 MathIllegalArgumentException {
  1417.                 return v.getSubVector(index, n);
  1418.             }

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

  1430.             /**
  1431.              * {@inheritDoc}
  1432.              *
  1433.              * @throws MathRuntimeException in all
  1434.              * circumstances.
  1435.              */
  1436.             @Override
  1437.             public void set(double value)
  1438.                 throws MathRuntimeException {
  1439.                 throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  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 MathIllegalArgumentException {
  1460.                 return v.combine(a, b, y);
  1461.             }

  1462.             /**
  1463.              * {@inheritDoc}
  1464.              *
  1465.              * @throws MathRuntimeException in all
  1466.              * circumstances.
  1467.              */
  1468.             @Override
  1469.             public RealVector combineToSelf(double a, double b, RealVector y)
  1470.                 throws MathRuntimeException {
  1471.                 throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  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 MathRuntimeException in all
  1484.                  * circumstances.
  1485.                  */
  1486.                 @Override
  1487.                 public void setValue(double value)
  1488.                     throws MathRuntimeException {
  1489.                     throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  1490.                 }
  1491.             }
  1492.         };
  1493.     }
  1494. }