org.apache.commons.math3.linear

## Class ArrayRealVector

- java.lang.Object
- - org.apache.commons.math3.linear.RealVector
  - - org.apache.commons.math3.linear.ArrayRealVector

- All Implemented Interfaces:
    Serializable

      public class ArrayRealVector
      extends RealVector
      implements Serializable

  This class implements the `RealVector` interface with a double array.
  Since:
    2.0
  Version:
    $Id: ArrayRealVector.java 1296539 2012-03-03 00:47:39Z sebb $
  See Also:
    Serialized Form

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.apache.commons.math3.linear.RealVector

      `RealVector.Entry, RealVector.SparseEntryIterator`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ArrayRealVector()` Build a 0-length vector.
    `ArrayRealVector(ArrayRealVector v)` Construct a vector from another vector, using a deep copy.
    `ArrayRealVector(ArrayRealVector v1, ArrayRealVector v2)` Construct a vector by appending one vector to another vector.
    `ArrayRealVector(ArrayRealVector v, boolean deep)` Construct a vector from another vector.
    `ArrayRealVector(ArrayRealVector v1, double[] v2)` Construct a vector by appending one vector to another vector.
    `ArrayRealVector(ArrayRealVector v1, RealVector v2)` Construct a vector by appending one vector to another vector.
    `ArrayRealVector(double[] d)` Construct a vector from an array, copying the input array.
    `ArrayRealVector(Double[] d)` Construct a vector from an array.
    `ArrayRealVector(double[] v1, ArrayRealVector v2)` Construct a vector by appending one vector to another vector.
    `ArrayRealVector(double[] d, boolean copyArray)` Create a new ArrayRealVector using the input array as the underlying data array.
    `ArrayRealVector(double[] v1, double[] v2)` Construct a vector by appending one vector to another vector.
    `ArrayRealVector(double[] d, int pos, int size)` Construct a vector from part of a array.
    `ArrayRealVector(Double[] d, int pos, int size)` Construct a vector from part of an array.
    `ArrayRealVector(int size)` Construct a vector of zeroes.
    `ArrayRealVector(int size, double preset)` Construct a vector with preset values.
    `ArrayRealVector(RealVector v)` Construct a vector from another vector, using a deep copy.
    `ArrayRealVector(RealVector v1, ArrayRealVector v2)` Construct a vector by appending one vector to another vector.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `ArrayRealVector` | `add(RealVector v)` Compute the sum of this vector and `v`.
    `void` | `addToEntry(int index, double increment)` Change an entry at the specified index.
    `ArrayRealVector` | `append(ArrayRealVector v)` Construct a vector by appending a vector to this vector.
    `RealVector` | `append(double in)` Construct a new vector by appending a double to this vector.
    `RealVector` | `append(RealVector v)` Construct a new vector by appending a vector to this vector.
    `protected void` | `checkVectorDimensions(int n)` Check if instance dimension is equal to some expected value.
    `protected void` | `checkVectorDimensions(RealVector v)` Check if instance and specified vectors have the same dimension.
    `ArrayRealVector` | `combine(double a, double b, RealVector y)` Returns a new vector representing `a * this + b * y`, the linear combination of `this` and `y`.
    `ArrayRealVector` | `combineToSelf(double a, double b, RealVector y)` Updates `this` with the linear combination of `this` and `y`.
    `ArrayRealVector` | `copy()` Returns a (deep) copy of this vector.
    `double` | `dotProduct(RealVector v)` Compute the dot product of this vector with `v`.
    `ArrayRealVector` | `ebeDivide(RealVector v)` Element-by-element division.
    `ArrayRealVector` | `ebeMultiply(RealVector v)` Element-by-element multiplication.
    `boolean` | `equals(Object other)` Test for the equality of two real vectors.
    `double[]` | `getDataRef()` Get a reference to the underlying data array.
    `int` | `getDimension()` Returns the size of the vector.
    `double` | `getDistance(RealVector v)` Distance between two vectors.
    `double` | `getEntry(int index)` Return the entry at the specified index.
    `double` | `getL1Distance(RealVector v)` Distance between two vectors.
    `double` | `getL1Norm()` Returns the L1 norm of the vector.
    `double` | `getLInfDistance(RealVector v)` Distance between two vectors.
    `double` | `getLInfNorm()` Returns the L∞ norm of the vector.
    `double` | `getNorm()` Returns the L2 norm of the vector.
    `RealVector` | `getSubVector(int index, int n)` Get a subvector from consecutive elements.
    `int` | `hashCode()` Get a hashCode for the real vector.
    `boolean` | `isInfinite()` Check whether any coordinate of this vector is infinite and none are `NaN`.
    `boolean` | `isNaN()` Check if any coordinate of this vector is `NaN`.
    `ArrayRealVector` | `map(UnivariateFunction function)` Acts as if implemented as:
    `RealVector` | `mapAddToSelf(double d)` Add a value to each entry.
    `RealVector` | `mapDivideToSelf(double d)` Divide each entry by the argument.
    `RealVector` | `mapMultiplyToSelf(double d)` Multiply each entry.
    `RealVector` | `mapSubtractToSelf(double d)` Subtract a value from each entry.
    `ArrayRealVector` | `mapToSelf(UnivariateFunction function)` Acts as if it is implemented as:
    `RealMatrix` | `outerProduct(RealVector v)` Compute the outer product.
    `RealVector` | `projection(RealVector v)` Find the orthogonal projection of this vector onto another vector.
    `void` | `set(double value)` Set all elements to a single value.
    `void` | `setEntry(int index, double value)` Set a single element.
    `void` | `setSubVector(int index, double[] v)` Set a set of consecutive elements.
    `void` | `setSubVector(int index, RealVector v)` Set a sequence of consecutive elements.
    `ArrayRealVector` | `subtract(RealVector v)` Subtract `v` from this vector.
    `double[]` | `toArray()` Convert the vector to an array of `double`s.
    `String` | `toString()`
    `void` | `unitize()` Converts this vector into a unit vector.
    `RealVector` | `unitVector()` Creates a unit vector pointing in the direction of this vector.

    - ### Methods inherited from class org.apache.commons.math3.linear.RealVector

      `checkIndex, cosine, getMaxIndex, getMaxValue, getMinIndex, getMinValue, iterator, mapAdd, mapDivide, mapMultiply, mapSubtract, sparseIterator, unmodifiableRealVector`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### ArrayRealVector

          public ArrayRealVector()

      Build a 0-length vector. Zero-length vectors may be used to initialized construction of vectors by data gathering. We start with zero-length and use either the `ArrayRealVector(ArrayRealVector, ArrayRealVector)` constructor or one of the `append` method (`append(double)`, `append(ArrayRealVector)`) to gather data into this vector.

    - #### ArrayRealVector

          public ArrayRealVector(int size)

      Construct a vector of zeroes.
      Parameters:
        `size` - Size of the vector.

    - #### ArrayRealVector

          public ArrayRealVector(int size,
                         double preset)

      Construct a vector with preset values.
      Parameters:
        `size` - Size of the vector
        `preset` - All entries will be set with this value.

    - #### ArrayRealVector

          public ArrayRealVector(double[] d)

      Construct a vector from an array, copying the input array.
      Parameters:
        `d` - Array.
      Throws:
        `NullArgumentException` - if `d` is `null`.

    - #### ArrayRealVector

          public ArrayRealVector(double[] d,
                         boolean copyArray)

      Create a new ArrayRealVector using the input array as the underlying data array. If an array is built specially in order to be embedded in a ArrayRealVector and not used directly, the `copyArray` may be set to `false`. This will prevent the copying and improve performance as no new array will be built and no data will be copied.
      Parameters:
        `d` - Data for the new vector.
        `copyArray` - if `true`, the input array will be copied, otherwise it will be referenced.
      Throws:
        `NullArgumentException` - if `d` is `null`.
      See Also:
        `ArrayRealVector(double[])`

    - #### ArrayRealVector

          public ArrayRealVector(double[] d,
                         int pos,
                         int size)

      Construct a vector from part of a array.
      Parameters:
        `d` - Array.
        `pos` - Position of first entry.
        `size` - Number of entries to copy.
      Throws:
        `NullArgumentException` - if `d` is `null`.
        `NumberIsTooLargeException` - if the size of `d` is less than `pos + size`.

    - #### ArrayRealVector

          public ArrayRealVector(Double[] d)

      Construct a vector from an array.
      Parameters:
        `d` - Array of `Double`s.

    - #### ArrayRealVector

          public ArrayRealVector(Double[] d,
                         int pos,
                         int size)

      Construct a vector from part of an array.
      Parameters:
        `d` - Array.
        `pos` - Position of first entry.
        `size` - Number of entries to copy.
      Throws:
        `NullArgumentException` - if `d` is `null`.
        `NumberIsTooLargeException` - if the size of `d` is less than `pos + size`.

    - #### ArrayRealVector

          public ArrayRealVector(RealVector v)

      Construct a vector from another vector, using a deep copy.
      Parameters:
        `v` - vector to copy.
      Throws:
        `NullArgumentException` - if `v` is `null`.

    - #### ArrayRealVector

          public ArrayRealVector(ArrayRealVector v)

      Construct a vector from another vector, using a deep copy.
      Parameters:
        `v` - Vector to copy.
      Throws:
        `NullArgumentException` - if `v` is `null`.

    - #### ArrayRealVector

          public ArrayRealVector(ArrayRealVector v,
                         boolean deep)

      Construct a vector from another vector.
      Parameters:
        `v` - Vector to copy.
        `deep` - If `true` perform a deep copy, otherwise perform a shallow copy.

    - #### ArrayRealVector

          public ArrayRealVector(ArrayRealVector v1,
                         ArrayRealVector v2)

      Construct a vector by appending one vector to another vector.
      Parameters:
        `v1` - First vector (will be put in front of the new vector).
        `v2` - Second vector (will be put at back of the new vector).

    - #### ArrayRealVector

          public ArrayRealVector(ArrayRealVector v1,
                         RealVector v2)

      Construct a vector by appending one vector to another vector.
      Parameters:
        `v1` - First vector (will be put in front of the new vector).
        `v2` - Second vector (will be put at back of the new vector).

    - #### ArrayRealVector

          public ArrayRealVector(RealVector v1,
                         ArrayRealVector v2)

      Construct a vector by appending one vector to another vector.
      Parameters:
        `v1` - First vector (will be put in front of the new vector).
        `v2` - Second vector (will be put at back of the new vector).

    - #### ArrayRealVector

          public ArrayRealVector(ArrayRealVector v1,
                         double[] v2)

      Construct a vector by appending one vector to another vector.
      Parameters:
        `v1` - First vector (will be put in front of the new vector).
        `v2` - Second vector (will be put at back of the new vector).

    - #### ArrayRealVector

          public ArrayRealVector(double[] v1,
                         ArrayRealVector v2)

      Construct a vector by appending one vector to another vector.
      Parameters:
        `v1` - First vector (will be put in front of the new vector).
        `v2` - Second vector (will be put at back of the new vector).

    - #### ArrayRealVector

          public ArrayRealVector(double[] v1,
                         double[] v2)

      Construct a vector by appending one vector to another vector.
      Parameters:
        `v1` - first vector (will be put in front of the new vector)
        `v2` - second vector (will be put at back of the new vector)

  - ### Method Detail

    - #### copy

          public ArrayRealVector copy()

      Returns a (deep) copy of this vector.
      Specified by:
        `copy` in class `RealVector`
      Returns:
        a vector copy.

    - #### add

          public ArrayRealVector add(RealVector v)

      Compute the sum of this vector and `v`. Returns a new vector. Does not change instance data.
      Overrides:
        `add` in class `RealVector`
      Parameters:
        `v` - Vector to be added.
      Returns:
        `this` + `v`.

    - #### subtract

          public ArrayRealVector subtract(RealVector v)

      Subtract `v` from this vector. Returns a new vector. Does not change instance data.
      Overrides:
        `subtract` in class `RealVector`
      Parameters:
        `v` - Vector to be subtracted.
      Returns:
        `this` - `v`.

    - #### map

          public ArrayRealVector map(UnivariateFunction function)

      Acts as if implemented as:

            return copy().mapToSelf(function);

      Returns a new vector. Does not change instance data.
      Overrides:
        `map` in class `RealVector`
      Parameters:
        `function` - Function to apply to each entry.
      Returns:
        a new vector.

    - #### mapToSelf

          public ArrayRealVector mapToSelf(UnivariateFunction function)

      Acts as if it is implemented as:

            Entry e = null;
            for(Iterator it = iterator(); it.hasNext(); e = it.next()) {
                e.setValue(function.value(e.getValue()));
            }

      Entries of this vector are modified in-place by this method.
      Overrides:
        `mapToSelf` in class `RealVector`
      Parameters:
        `function` - Function to apply to each entry.
      Returns:
        a reference to this vector.

    - #### mapAddToSelf

          public RealVector mapAddToSelf(double d)

      Add a value to each entry. The instance is changed in-place.
      Overrides:
        `mapAddToSelf` in class `RealVector`
      Parameters:
        `d` - Value to be added to each entry.
      Returns:
        `this`.

    - #### mapSubtractToSelf

          public RealVector mapSubtractToSelf(double d)

      Subtract a value from each entry. The instance is changed in-place.
      Overrides:
        `mapSubtractToSelf` in class `RealVector`
      Parameters:
        `d` - Value to be subtracted.
      Returns:
        `this`.

    - #### mapMultiplyToSelf

          public RealVector mapMultiplyToSelf(double d)

      Multiply each entry. The instance is changed in-place.
      Overrides:
        `mapMultiplyToSelf` in class `RealVector`
      Parameters:
        `d` - Multiplication factor.
      Returns:
        `this`.

    - #### mapDivideToSelf

          public RealVector mapDivideToSelf(double d)

      Divide each entry by the argument. The instance is changed in-place.
      Overrides:
        `mapDivideToSelf` in class `RealVector`
      Parameters:
        `d` - Value to divide by.
      Returns:
        `this`.

    - #### ebeMultiply

          public ArrayRealVector ebeMultiply(RealVector v)

      Element-by-element multiplication.
      Specified by:
        `ebeMultiply` in class `RealVector`
      Parameters:
        `v` - Vector by which instance elements must be multiplied
      Returns:
        a vector containing this[i] * v[i] for all i.

    - #### ebeDivide

          public ArrayRealVector ebeDivide(RealVector v)

      Element-by-element division.
      Specified by:
        `ebeDivide` in class `RealVector`
      Parameters:
        `v` - Vector by which instance elements must be divided.
      Returns:
        a vector containing this[i] / v[i] for all i.

    - #### getDataRef

          public double[] getDataRef()

      Get a reference to the underlying data array. This method does not make a fresh copy of the underlying data.
      Returns:
        the array of entries.

    - #### dotProduct

          public double dotProduct(RealVector v)

      Compute the dot product of this vector with `v`.
      Overrides:
        `dotProduct` in class `RealVector`
      Parameters:
        `v` - Vector with which dot product should be computed
      Returns:
        the scalar dot product between this instance and `v`.

    - #### getNorm

          public double getNorm()

      Returns the L2 norm of the vector.

      The L2 norm is the root of the sum of the squared elements.

      Overrides:
        `getNorm` in class `RealVector`
      Returns:
        the norm.
      See Also:
        `RealVector.getL1Norm()`, `RealVector.getLInfNorm()`, `RealVector.getDistance(RealVector)`

    - #### getL1Norm

          public double getL1Norm()

      Returns the L1 norm of the vector.

      The L1 norm is the sum of the absolute values of the elements.

      Overrides:
        `getL1Norm` in class `RealVector`
      Returns:
        the norm.
      See Also:
        `RealVector.getNorm()`, `RealVector.getLInfNorm()`, `RealVector.getL1Distance(RealVector)`

    - #### getLInfNorm

          public double getLInfNorm()

      Returns the L∞ norm of the vector.

      The L∞ norm is the max of the absolute values of the elements.

      Overrides:
        `getLInfNorm` in class `RealVector`
      Returns:
        the norm.
      See Also:
        `RealVector.getNorm()`, `RealVector.getL1Norm()`, `RealVector.getLInfDistance(RealVector)`

    - #### getDistance

          public double getDistance(RealVector v)

      Distance between two vectors.

      This method computes the distance consistent with the L2 norm, i.e. the square root of the sum of element differences, or Euclidian distance.

      Overrides:
        `getDistance` in class `RealVector`
      Parameters:
        `v` - Vector to which distance is requested.
      Returns:
        the distance between two vectors.
      See Also:
        `RealVector.getL1Distance(RealVector)`, `RealVector.getLInfDistance(RealVector)`, `RealVector.getNorm()`

    - #### getL1Distance

          public double getL1Distance(RealVector v)

      Distance between two vectors.

      This method computes the distance consistent with L1 norm, i.e. the sum of the absolute values of the elements differences.

      Overrides:
        `getL1Distance` in class `RealVector`
      Parameters:
        `v` - Vector to which distance is requested.
      Returns:
        the distance between two vectors.

    - #### getLInfDistance

          public double getLInfDistance(RealVector v)

      Distance between two vectors.

      This method computes the distance consistent with L∞ norm, i.e. the max of the absolute values of element differences.

      Overrides:
        `getLInfDistance` in class `RealVector`
      Parameters:
        `v` - Vector to which distance is requested.
      Returns:
        the distance between two vectors.
      See Also:
        `RealVector.getDistance(RealVector)`, `RealVector.getL1Distance(RealVector)`, `RealVector.getLInfNorm()`

    - #### unitVector

          public RealVector unitVector()

      Creates a unit vector pointing in the direction of this vector. The instance is not changed by this method.
      Overrides:
        `unitVector` in class `RealVector`
      Returns:
        a unit vector pointing in direction of this vector.

    - #### unitize

          public void unitize()

      Converts this vector into a unit vector. The instance itself is changed by this method.
      Overrides:
        `unitize` in class `RealVector`

    - #### projection

          public RealVector projection(RealVector v)

      Find the orthogonal projection of this vector onto another vector.
      Specified by:
        `projection` in class `RealVector`
      Parameters:
        `v` - vector onto which instance must be projected.
      Returns:
        projection of the instance onto `v`.

    - #### outerProduct

          public RealMatrix outerProduct(RealVector v)

      Compute the outer product.
      Overrides:
        `outerProduct` in class `RealVector`
      Parameters:
        `v` - Vector with which outer product should be computed.
      Returns:
        the matrix outer product between this instance and `v`.

    - #### getEntry

          public double getEntry(int index)

      Return the entry at the specified index.
      Specified by:
        `getEntry` in class `RealVector`
      Parameters:
        `index` - Index location of entry to be fetched.
      Returns:
        the vector entry at `index`.
      See Also:
        `RealVector.setEntry(int, double)`

    - #### getDimension

          public int getDimension()

      Returns the size of the vector.
      Specified by:
        `getDimension` in class `RealVector`
      Returns:
        the size of this vector.

    - #### append

          public RealVector append(RealVector v)

      Construct a new vector by appending a vector to this vector.
      Specified by:
        `append` in class `RealVector`
      Parameters:
        `v` - vector to append to this one.
      Returns:
        a new vector.

    - #### append

          public ArrayRealVector append(ArrayRealVector v)

      Construct a vector by appending a vector to this vector.
      Parameters:
        `v` - Vector to append to this one.
      Returns:
        a new vector.

    - #### append

          public RealVector append(double in)

      Construct a new vector by appending a double to this vector.
      Specified by:
        `append` in class `RealVector`
      Parameters:
        `in` - double to append.
      Returns:
        a new vector.

    - #### getSubVector

          public RealVector getSubVector(int index,
                                int n)

      Get a subvector from consecutive elements.
      Specified by:
        `getSubVector` in class `RealVector`
      Parameters:
        `index` - index of first element.
        `n` - number of elements to be retrieved.
      Returns:
        a vector containing n elements.

    - #### setEntry

          public void setEntry(int index,
                      double value)

      Set a single element.
      Specified by:
        `setEntry` in class `RealVector`
      Parameters:
        `index` - element index.
        `value` - new value for the element.
      See Also:
        `RealVector.getEntry(int)`

    - #### addToEntry

          public void addToEntry(int index,
                        double increment)

      Change an entry at the specified index.
      Overrides:
        `addToEntry` in class `RealVector`
      Parameters:
        `index` - Index location of entry to be set.
        `increment` - Value to add to the vector entry.

    - #### setSubVector

          public void setSubVector(int index,
                          RealVector v)

      Set a sequence of consecutive elements.
      Specified by:
        `setSubVector` in class `RealVector`
      Parameters:
        `index` - index of first element to be set.
        `v` - vector containing the values to set.

    - #### setSubVector

          public void setSubVector(int index,
                          double[] v)

      Set a set of consecutive elements.
      Parameters:
        `index` - Index of first element to be set.
        `v` - Vector containing the values to set.
      Throws:
        `OutOfRangeException` - if the index is inconsistent with the vector size.

    - #### set

          public void set(double value)

      Set all elements to a single value.
      Overrides:
        `set` in class `RealVector`
      Parameters:
        `value` - Single value to set for all elements.

    - #### toArray

          public double[] toArray()

      Convert the vector to an array of `double`s. The array is independent from this vector data: the elements are copied.
      Overrides:
        `toArray` in class `RealVector`
      Returns:
        an array containing a copy of the vector elements.

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### checkVectorDimensions

          protected void checkVectorDimensions(RealVector v)

      Check if instance and specified vectors have the same dimension.
      Overrides:
        `checkVectorDimensions` in class `RealVector`
      Parameters:
        `v` - Vector to compare instance with.
      Throws:
        `DimensionMismatchException` - if the vectors do not have the same dimension.

    - #### checkVectorDimensions

          protected void checkVectorDimensions(int n)

      Check if instance dimension is equal to some expected value.
      Overrides:
        `checkVectorDimensions` in class `RealVector`
      Parameters:
        `n` - Expected dimension.
      Throws:
        `DimensionMismatchException` - if the dimension is inconsistent with vector size.

    - #### isNaN

          public boolean isNaN()

      Check if any coordinate of this vector is `NaN`.
      Specified by:
        `isNaN` in class `RealVector`
      Returns:
        `true` if any coordinate of this vector is `NaN`, `false` otherwise.

    - #### isInfinite

          public boolean isInfinite()

      Check whether any coordinate of this vector is infinite and none are `NaN`.
      Specified by:
        `isInfinite` in class `RealVector`
      Returns:
        `true` if any coordinate of this vector is infinite and none are `NaN`, `false` otherwise.

    - #### equals

          public boolean equals(Object other)

      Test for the equality of two real vectors. If all coordinates of two real vectors are exactly the same, and none are `NaN`, the two real vectors are considered to be equal. `NaN` coordinates are considered to affect globally the vector and be equals to each other - i.e, if either (or all) coordinates of the real vector are equal to `NaN`, the real vector is equal to a vector with all `NaN` coordinates.
      Overrides:
        `equals` in class `Object`
      Parameters:
        `other` - Object to test for equality.
      Returns:
        `true` if two vector objects are equal, `false` if `other` is null, not an instance of `RealVector`, or not equal to this `RealVector` instance.

    - #### hashCode

          public int hashCode()

      Get a hashCode for the real vector. All `NaN` values have the same hash code.
      Overrides:
        `hashCode` in class `Object`
      Returns:
        a hash code.

    - #### combine

          public ArrayRealVector combine(double a,
                                double b,
                                RealVector y)

      Returns a new vector representing `a * this + b * y`, the linear combination of `this` and `y`. Returns a new vector. Does not change instance data.
      Overrides:
        `combine` in class `RealVector`
      Parameters:
        `a` - Coefficient of `this`.
        `b` - Coefficient of `y`.
        `y` - Vector with which `this` is linearly combined.
      Returns:
        a vector containing `a * this[i] + b * y[i]` for all `i`.

    - #### combineToSelf

          public ArrayRealVector combineToSelf(double a,
                                      double b,
                                      RealVector y)

      Updates `this` with the linear combination of `this` and `y`.
      Overrides:
        `combineToSelf` in class `RealVector`
      Parameters:
        `a` - Weight of `this`.
        `b` - Weight of `y`.
        `y` - Vector with which `this` is linearly combined.
      Returns:
        `this`, with components equal to `a * this[i] + b * y[i]` for all `i`.

Copyright © 2003-2012 The Apache Software Foundation. All Rights Reserved.