Interface RealVector

All Known Subinterfaces:
SparseRealVector
All Known Implementing Classes:
AbstractRealVector, ArrayRealVector, OpenMapRealVector

public interface RealVector
Interface defining a real-valued vector with basic algebraic operations.

vector element indexing is 0-based -- e.g., getEntry(0) returns the first element of the vector.

The various mapXxx and mapXxxToSelf methods operate on vectors element-wise, i.e. they perform the same operation (adding a scalar, applying a function ...) on each element in turn. The mapXxx versions create a new vector to hold the result and do not change the instance. The mapXxxToSelf versions use the instance itself to store the results, so the instance is changed by these methods. In both cases, the result vector is returned by the methods, this allows to use the fluent API style, like this:

   RealVector result = v.mapAddToSelf(3.0).mapTanToSelf().mapSquareToSelf();
 

Remark on the deprecated mapXxx and mapXxxToSelf methods: In Commons-Math v3.0, the same functionality will be achieved by directly using the map(UnivariateRealFunction) and mapToSelf(UnivariateRealFunction) together with new function objects (not available in v2.2).

Since:
2.0
Version:
$Revision: 1070725 $ $Date: 2011-02-15 02:31:12 +0100 (mar. 15 févr. 2011) $
  • Method Details

    • mapToSelf

      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()));
        }
       
      Parameters:
      function - Function to apply to each entry.
      Returns:
      this vector.
      Throws:
      FunctionEvaluationException - if the function throws it.
    • map

      Acts as if implemented as:
        return copy().map(function);
       
      Parameters:
      function - Function to apply to each entry.
      Returns:
      a new vector.
      Throws:
      FunctionEvaluationException - if the function throws it.
    • iterator

      Generic dense iterator. It iterates in increasing order of the vector index.
      Returns:
      a dense iterator
    • sparseIterator

      Iterator<RealVector.Entry> sparseIterator()
      Specialized implementations may choose to not iterate over all dimensions, either because those values are unset, or are equal to defaultValue(), or are small enough to be ignored for the purposes of iteration. No guarantees are made about order of iteration. In dense implementations, this method will often delegate to iterator().
      Returns:
      a sparse iterator
    • copy

      RealVector copy()
      Returns a (deep) copy of this vector.
      Returns:
      a vector copy.
    • add

      Compute the sum of this vector and v.
      Parameters:
      v - Vector to be added.
      Returns:
      this + v.
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • add

      RealVector add(double[] v)
      Compute the sum of this vector and v.
      Parameters:
      v - Vector to be added.
      Returns:
      this + v.
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • subtract

      RealVector subtract(RealVector v)
      Subtract v from this vector.
      Parameters:
      v - Vector to be subtracted.
      Returns:
      this - v.
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • subtract

      RealVector subtract(double[] v)
      Subtract v from this vector.
      Parameters:
      v - Vector to be subtracted.
      Returns:
      this - v.
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • mapAdd

      RealVector mapAdd(double d)
      Add a value to each entry.
      Parameters:
      d - Value to be added to each entry.
      Returns:
      this + d.
    • mapAddToSelf

      RealVector mapAddToSelf(double d)
      Add a value to each entry. The instance is changed in-place.
      Parameters:
      d - Value to be added to each entry.
      Returns:
      this.
    • mapSubtract

      RealVector mapSubtract(double d)
      Subtract a value from each entry.
      Parameters:
      d - Value to be subtracted.
      Returns:
      this - d.
    • mapSubtractToSelf

      RealVector mapSubtractToSelf(double d)
      Subtract a value from each entry. The instance is changed in-place.
      Parameters:
      d - Value to be subtracted.
      Returns:
      this.
    • mapMultiply

      RealVector mapMultiply(double d)
      Multiply each entry.
      Parameters:
      d - Multiplication factor.
      Returns:
      this * d.
    • mapMultiplyToSelf

      RealVector mapMultiplyToSelf(double d)
      Multiply each entry. The instance is changed in-place.
      Parameters:
      d - Multiplication factor.
      Returns:
      this.
    • mapDivide

      RealVector mapDivide(double d)
      Divide each entry.
      Parameters:
      d - Value to divide by.
      Returns:
      this / d.
    • mapDivideToSelf

      RealVector mapDivideToSelf(double d)
      Divide each entry. The instance is changed in-place.
      Parameters:
      d - Value to divide by.
      Returns:
      this.
    • mapPow

      @Deprecated RealVector mapPow(double d)
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map a power operation to each entry.
      Parameters:
      d - Operator value.
      Returns:
      a mapped copy of the vector.
    • mapPowToSelf

      @Deprecated RealVector mapPowToSelf(double d)
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map a power operation to each entry. The instance is changed in-place.
      Parameters:
      d - Operator value.
      Returns:
      the mapped vector.
    • mapExp

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.exp(double) function to each entry.
      Returns:
      a mapped copy of the vector.
    • mapExpToSelf

      @Deprecated RealVector mapExpToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map Math.exp(double) operation to each entry. The instance is changed in-place.
      Returns:
      the mapped vector.
    • mapExpm1

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.expm1(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapExpm1ToSelf

      @Deprecated RealVector mapExpm1ToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.expm1(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapLog

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.log(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapLogToSelf

      @Deprecated RealVector mapLogToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.log(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapLog10

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.log10(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapLog10ToSelf

      @Deprecated RealVector mapLog10ToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.log10(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapLog1p

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.log1p(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapLog1pToSelf

      @Deprecated RealVector mapLog1pToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.log1p(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapCosh

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.cosh(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapCoshToSelf

      @Deprecated RealVector mapCoshToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.cosh(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapSinh

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.sinh(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapSinhToSelf

      @Deprecated RealVector mapSinhToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.sinh(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapTanh

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.tanh(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapTanhToSelf

      @Deprecated RealVector mapTanhToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.tanh(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapCos

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.cos(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapCosToSelf

      @Deprecated RealVector mapCosToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.cos(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapSin

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.sin(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapSinToSelf

      @Deprecated RealVector mapSinToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.sin(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapTan

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.tan(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapTanToSelf

      @Deprecated RealVector mapTanToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.tan(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapAcos

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.acos(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapAcosToSelf

      @Deprecated RealVector mapAcosToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.acos(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapAsin

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.asin(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapAsinToSelf

      @Deprecated RealVector mapAsinToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.asin(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapAtan

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.atan(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapAtanToSelf

      @Deprecated RealVector mapAtanToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.atan(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapInv

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the 1/x function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapInvToSelf

      @Deprecated RealVector mapInvToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the 1/x function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapAbs

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.abs(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapAbsToSelf

      @Deprecated RealVector mapAbsToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.abs(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapSqrt

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.sqrt(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapSqrtToSelf

      @Deprecated RealVector mapSqrtToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.sqrt(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapCbrt

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.cbrt(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapCbrtToSelf

      @Deprecated RealVector mapCbrtToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.cbrt(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapCeil

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.ceil(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapCeilToSelf

      @Deprecated RealVector mapCeilToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.ceil(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapFloor

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.floor(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapFloorToSelf

      @Deprecated RealVector mapFloorToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.floor(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapRint

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.rint(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapRintToSelf

      @Deprecated RealVector mapRintToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.rint(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapSignum

      @Deprecated RealVector mapSignum()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.signum(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapSignumToSelf

      @Deprecated RealVector mapSignumToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.signum(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • mapUlp

      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.ulp(double) function to each entry.
      Returns:
      a vector containing the result of applying the function to each entry
    • mapUlpToSelf

      @Deprecated RealVector mapUlpToSelf()
      Deprecated.
      in 2.2 (to be removed in 3.0).
      Map the Math.ulp(double) function to each entry.

      The instance is changed by this method.

      Returns:
      for convenience, return this
    • ebeMultiply

      RealVector ebeMultiply(RealVector v)
      Element-by-element multiplication.
      Parameters:
      v - vector by which instance elements must be multiplied
      Returns:
      a vector containing this[i] * v[i] for all i
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • ebeMultiply

      RealVector ebeMultiply(double[] v)
      Element-by-element multiplication.
      Parameters:
      v - vector by which instance elements must be multiplied
      Returns:
      a vector containing this[i] * v[i] for all i
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • ebeDivide

      RealVector ebeDivide(RealVector v)
      Element-by-element division.
      Parameters:
      v - vector by which instance elements must be divided
      Returns:
      a vector containing this[i] / v[i] for all i
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • ebeDivide

      RealVector ebeDivide(double[] v)
      Element-by-element division.
      Parameters:
      v - vector by which instance elements must be divided
      Returns:
      a vector containing this[i] / v[i] for all i
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • getData

      double[] getData()
      Returns vector entries as a double array.
      Returns:
      double array of entries
    • dotProduct

      double dotProduct(RealVector v)
      Compute the dot product.
      Parameters:
      v - vector with which dot product should be computed
      Returns:
      the scalar dot product between instance and v
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • dotProduct

      double dotProduct(double[] v)
      Compute the dot product.
      Parameters:
      v - vector with which dot product should be computed
      Returns:
      the scalar dot product between instance and v
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • getNorm

      double getNorm()
      Returns the L2 norm of the vector.

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

      Returns:
      norm
      See Also:
    • getL1Norm

      double getL1Norm()
      Returns the L1 norm of the vector.

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

      Returns:
      norm
      See Also:
    • getLInfNorm

      double getLInfNorm()
      Returns the L norm of the vector.

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

      Returns:
      norm
      See Also:
    • getDistance

      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 elements differences, or euclidian distance.

      Parameters:
      v - vector to which distance is requested
      Returns:
      distance between two vectors.
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
      See Also:
    • getDistance

      double getDistance(double[] v)
      Distance between two vectors.

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

      Parameters:
      v - vector to which distance is requested
      Returns:
      distance between two vectors.
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
      See Also:
    • getL1Distance

      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 elements differences.

      Parameters:
      v - vector to which distance is requested
      Returns:
      distance between two vectors.
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
      See Also:
    • getL1Distance

      double getL1Distance(double[] v)
      Distance between two vectors.

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

      Parameters:
      v - vector to which distance is requested
      Returns:
      distance between two vectors.
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
      See Also:
    • getLInfDistance

      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 elements differences.

      Parameters:
      v - vector to which distance is requested
      Returns:
      distance between two vectors.
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
      See Also:
    • getLInfDistance

      double getLInfDistance(double[] v)
      Distance between two vectors.

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

      Parameters:
      v - vector to which distance is requested
      Returns:
      distance between two vectors.
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
      See Also:
    • unitVector

      RealVector unitVector()
      Creates a unit vector pointing in the direction of this vector.

      The instance is not changed by this method.

      Returns:
      a unit vector pointing in direction of this vector
      Throws:
      ArithmeticException - if the norm is null
    • unitize

      void unitize()
      Converts this vector into a unit vector.

      The instance itself is changed by this method.

      Throws:
      ArithmeticException - if the norm is zero.
    • projection

      RealVector projection(RealVector v)
      Find the orthogonal projection of this vector onto another vector.
      Parameters:
      v - vector onto which instance must be projected
      Returns:
      projection of the instance onto v
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • projection

      RealVector projection(double[] v)
      Find the orthogonal projection of this vector onto another vector.
      Parameters:
      v - vector onto which instance must be projected
      Returns:
      projection of the instance onto v
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • outerProduct

      RealMatrix outerProduct(RealVector v)
      Compute the outer product.
      Parameters:
      v - vector with which outer product should be computed
      Returns:
      the square matrix outer product between instance and v
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • outerProduct

      RealMatrix outerProduct(double[] v)
      Compute the outer product.
      Parameters:
      v - vector with which outer product should be computed
      Returns:
      the square matrix outer product between instance and v
      Throws:
      DimensionMismatchException - if v is not the same size as this vector.
    • getEntry

      double getEntry(int index)
      Returns the entry in the specified index.
      Parameters:
      index - Index location of entry to be fetched.
      Returns:
      the vector entry at index.
      Throws:
      OutOfRangeException - if the index is not valid.
      See Also:
    • setEntry

      void setEntry(int index, double value)
      Set a single element.
      Parameters:
      index - element index.
      value - new value for the element.
      Throws:
      OutOfRangeException - if the index is not valid.
      See Also:
    • getDimension

      int getDimension()
      Returns the size of the vector.
      Returns:
      size
    • append

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

      RealVector append(double d)
      Construct a vector by appending a double to this vector.
      Parameters:
      d - double to append.
      Returns:
      a new vector
    • append

      RealVector append(double[] a)
      Construct a vector by appending a double array to this vector.
      Parameters:
      a - double array to append.
      Returns:
      a new vector
    • getSubVector

      RealVector getSubVector(int index, int n)
      Get a subvector from consecutive elements.
      Parameters:
      index - index of first element.
      n - number of elements to be retrieved.
      Returns:
      a vector containing n elements.
      Throws:
      OutOfRangeException - if the index is not valid.
    • setSubVector

      void setSubVector(int index, RealVector 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 not valid.
      See Also:
    • setSubVector

      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 not valid.
      See Also:
    • set

      void set(double value)
      Set all elements to a single value.
      Parameters:
      value - single value to set for all elements
    • toArray

      double[] toArray()
      Convert the vector to a double array.

      The array is independent from vector data, it's elements are copied.

      Returns:
      array containing a copy of vector elements
    • isNaN

      boolean isNaN()
      Check whether any coordinate of this vector is NaN.
      Returns:
      true if any coordinate of this vector is NaN, false otherwise.
    • isInfinite

      boolean isInfinite()
      Check whether any coordinate of this vector is infinite and none are NaN.
      Returns:
      true if any coordinate of this vector is infinite and none are NaN, false otherwise.