Class ArrayRealVector

  • All Implemented Interfaces:
    java.io.Serializable, RealVector

    public class ArrayRealVector
    extends AbstractRealVector
    implements java.io.Serializable
    This class implements the RealVector interface with a double array.
    Since:
    2.0
    Version:
    $Revision: 1003993 $ $Date: 2010-10-03 18:39:16 +0200 (dim. 03 oct. 2010) $
    See Also:
    Serialized Form
    • Field Detail

      • data

        protected double[] data
        Entries of the vector.
    • Constructor Detail

      • ArrayRealVector

        public ArrayRealVector​(int size)
        Construct a (size)-length vector of zeros.
        Parameters:
        size - size of the vector
      • ArrayRealVector

        public ArrayRealVector​(int size,
                               double preset)
        Construct an (size)-length vector with preset values.
        Parameters:
        size - size of the vector
        preset - fill the vector with this scalar value
      • ArrayRealVector

        public ArrayRealVector​(double[] d)
        Construct a vector from an array, copying the input array.
        Parameters:
        d - array of doubles.
      • 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

        Parameters:
        d - data for new vector
        copyArray - if true, the input array will be copied, otherwise it will be referenced
        See Also:
        ArrayRealVector(double[])
      • ArrayRealVector

        public ArrayRealVector​(double[] d,
                               int pos,
                               int size)
        Construct a vector from part of a array.
        Parameters:
        d - array of doubles.
        pos - position of first entry
        size - number of entries to copy
      • ArrayRealVector

        public ArrayRealVector​(java.lang.Double[] d)
        Construct a vector from an array.
        Parameters:
        d - array of Doubles.
      • ArrayRealVector

        public ArrayRealVector​(java.lang.Double[] d,
                               int pos,
                               int size)
        Construct a vector from part of a Double array
        Parameters:
        d - array of Doubles.
        pos - position of first entry
        size - number of entries to copy
      • ArrayRealVector

        public ArrayRealVector​(RealVector v)
        Construct a vector from another vector, using a deep copy.
        Parameters:
        v - vector to copy
      • ArrayRealVector

        public ArrayRealVector​(ArrayRealVector v)
        Construct a vector from another vector, using a deep copy.
        Parameters:
        v - vector to copy
      • 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

      • add

        public RealVector add​(double[] v)
                       throws java.lang.IllegalArgumentException
        Compute the sum of this vector and v.
        Specified by:
        add in interface RealVector
        Overrides:
        add in class AbstractRealVector
        Parameters:
        v - Vector to be added.
        Returns:
        this + v.
        Throws:
        DimensionMismatchException - if v is not the same size as this vector.
        java.lang.IllegalArgumentException
      • add

        public ArrayRealVector add​(ArrayRealVector v)
                            throws java.lang.IllegalArgumentException
        Compute the sum of this and v.
        Parameters:
        v - vector to be added
        Returns:
        this + v
        Throws:
        java.lang.IllegalArgumentException - if v is not the same size as this
      • subtract

        public ArrayRealVector subtract​(ArrayRealVector v)
                                 throws java.lang.IllegalArgumentException
        Compute this minus v.
        Parameters:
        v - vector to be subtracted
        Returns:
        this + v
        Throws:
        java.lang.IllegalArgumentException - if v is not the same size as this
      • ebeMultiply

        public RealVector ebeMultiply​(RealVector v)
                               throws java.lang.IllegalArgumentException
        Element-by-element multiplication.
        Specified by:
        ebeMultiply in interface RealVector
        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.
        java.lang.IllegalArgumentException
      • ebeMultiply

        public RealVector ebeMultiply​(double[] v)
                               throws java.lang.IllegalArgumentException
        Element-by-element multiplication.
        Specified by:
        ebeMultiply in interface RealVector
        Overrides:
        ebeMultiply in class AbstractRealVector
        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.
        java.lang.IllegalArgumentException
      • ebeMultiply

        public ArrayRealVector ebeMultiply​(ArrayRealVector v)
                                    throws java.lang.IllegalArgumentException
        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:
        java.lang.IllegalArgumentException - if v is not the same size as this
      • ebeDivide

        public RealVector ebeDivide​(RealVector v)
                             throws java.lang.IllegalArgumentException
        Element-by-element division.
        Specified by:
        ebeDivide in interface RealVector
        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.
        java.lang.IllegalArgumentException
      • ebeDivide

        public RealVector ebeDivide​(double[] v)
                             throws java.lang.IllegalArgumentException
        Element-by-element division.
        Specified by:
        ebeDivide in interface RealVector
        Overrides:
        ebeDivide in class AbstractRealVector
        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.
        java.lang.IllegalArgumentException
      • ebeDivide

        public ArrayRealVector ebeDivide​(ArrayRealVector v)
                                  throws java.lang.IllegalArgumentException
        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:
        java.lang.IllegalArgumentException - if v is not the same size as this
      • getDataRef

        public double[] getDataRef()
        Returns a reference to the underlying data array.

        Does not make a fresh copy of the underlying data.

        Returns:
        array of entries
      • dotProduct

        public double dotProduct​(RealVector v)
                          throws java.lang.IllegalArgumentException
        Compute the dot product.
        Specified by:
        dotProduct in interface RealVector
        Overrides:
        dotProduct in class AbstractRealVector
        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.
        java.lang.IllegalArgumentException
      • dotProduct

        public double dotProduct​(double[] v)
                          throws java.lang.IllegalArgumentException
        Compute the dot product.
        Specified by:
        dotProduct in interface RealVector
        Overrides:
        dotProduct in class AbstractRealVector
        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.
        java.lang.IllegalArgumentException
      • dotProduct

        public double dotProduct​(ArrayRealVector v)
                          throws java.lang.IllegalArgumentException
        Compute the dot product.
        Parameters:
        v - vector with which dot product should be computed
        Returns:
        the scalar dot product between instance and v
        Throws:
        java.lang.IllegalArgumentException - if v is not the same size as this
      • getDistance

        public double getDistance​(ArrayRealVector v)
                           throws java.lang.IllegalArgumentException
        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:
        java.lang.IllegalArgumentException - if v is not the same size as this
        See Also:
        getDistance(RealVector), getL1Distance(ArrayRealVector), getLInfDistance(ArrayRealVector), getNorm()
      • getLInfDistance

        public double getLInfDistance​(ArrayRealVector v)
                               throws java.lang.IllegalArgumentException
        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:
        java.lang.IllegalArgumentException - if v is not the same size as this
        See Also:
        getDistance(RealVector), getL1Distance(ArrayRealVector), getLInfDistance(ArrayRealVector), getNorm()
      • unitVector

        public RealVector unitVector()
                              throws java.lang.ArithmeticException
        Creates a unit vector pointing in the direction of this vector.

        The instance is not changed by this method.

        Specified by:
        unitVector in interface RealVector
        Overrides:
        unitVector in class AbstractRealVector
        Returns:
        a unit vector pointing in direction of this vector
        Throws:
        java.lang.ArithmeticException - if the norm is null
      • unitize

        public void unitize()
                     throws java.lang.ArithmeticException
        Converts this vector into a unit vector.

        The instance itself is changed by this method.

        Specified by:
        unitize in interface RealVector
        Overrides:
        unitize in class AbstractRealVector
        Throws:
        java.lang.ArithmeticException - if the norm is zero.
      • projection

        public RealVector projection​(RealVector v)
        Find the orthogonal projection of this vector onto another vector.
        Specified by:
        projection in interface RealVector
        Parameters:
        v - vector onto which instance must be projected
        Returns:
        projection of the instance onto v
      • projection

        public RealVector projection​(double[] v)
        Find the orthogonal projection of this vector onto another vector.
        Specified by:
        projection in interface RealVector
        Overrides:
        projection in class AbstractRealVector
        Parameters:
        v - vector onto which instance must be projected
        Returns:
        projection of the instance onto v
      • projection

        public ArrayRealVector projection​(ArrayRealVector 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:
        java.lang.IllegalArgumentException - if v is not the same size as this
      • outerProduct

        public RealMatrix outerProduct​(ArrayRealVector v)
                                throws java.lang.IllegalArgumentException
        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:
        java.lang.IllegalArgumentException - if v is not the same size as this
      • outerProduct

        public RealMatrix outerProduct​(double[] v)
                                throws java.lang.IllegalArgumentException
        Compute the outer product.
        Specified by:
        outerProduct in interface RealVector
        Overrides:
        outerProduct in class AbstractRealVector
        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.
        java.lang.IllegalArgumentException
      • getDimension

        public int getDimension()
        Returns the size of the vector.
        Specified by:
        getDimension in interface RealVector
        Returns:
        size
      • append

        public RealVector append​(RealVector v)
        Construct a vector by appending a vector to this vector.
        Specified by:
        append in interface 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 vector by appending a double to this vector.
        Specified by:
        append in interface RealVector
        Parameters:
        in - double to append.
        Returns:
        a new vector
      • append

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

        public RealVector getSubVector​(int index,
                                       int n)
        Get a subvector from consecutive elements.
        Specified by:
        getSubVector in interface 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 interface RealVector
        Parameters:
        index - element index.
        value - new value for the element.
        See Also:
        RealVector.getEntry(int)
      • set

        public void set​(int index,
                        ArrayRealVector v)
                 throws MatrixIndexException
        Set a set of consecutive elements.
        Parameters:
        index - index of first element to be set.
        v - vector containing the values to set.
        Throws:
        MatrixIndexException - if the index is inconsistent with vector size
      • set

        public void set​(double value)
        Set all elements to a single value.
        Specified by:
        set in interface RealVector
        Overrides:
        set in class AbstractRealVector
        Parameters:
        value - single value to set for all elements
      • toArray

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

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

        Specified by:
        toArray in interface RealVector
        Overrides:
        toArray in class AbstractRealVector
        Returns:
        array containing a copy of vector elements
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • checkVectorDimensions

        protected void checkVectorDimensions​(RealVector v)
                                      throws java.lang.IllegalArgumentException
        Check if instance and specified vectors have the same dimension.
        Overrides:
        checkVectorDimensions in class AbstractRealVector
        Parameters:
        v - vector to compare instance with
        Throws:
        java.lang.IllegalArgumentException - if the vectors do not have the same dimension
      • checkVectorDimensions

        protected void checkVectorDimensions​(int n)
                                      throws java.lang.IllegalArgumentException
        Check if instance dimension is equal to some expected value.
        Overrides:
        checkVectorDimensions in class AbstractRealVector
        Parameters:
        n - expected dimension.
        Throws:
        java.lang.IllegalArgumentException - if the dimension is inconsistent with vector size
      • isNaN

        public boolean isNaN()
        Returns true if any coordinate of this vector is NaN; false otherwise
        Specified by:
        isNaN in interface RealVector
        Returns:
        true if any coordinate of this vector is NaN; false otherwise
      • isInfinite

        public boolean isInfinite()
        Returns true if any coordinate of this vector is infinite and none are NaN; false otherwise
        Specified by:
        isInfinite in interface RealVector
        Returns:
        true if any coordinate of this vector is infinite and none are NaN; false otherwise
      • equals

        public boolean equals​(java.lang.Object other)
        Test for the equality of two real vectors.

        If all coordinates of two real vectors are exactly the same, and none are Double.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 Double.NaN, the real vector is equal to a vector with all Double.NaN coordinates.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        other - Object to test for equality to this
        Returns:
        true if two vector objects are equal, false if object 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 java.lang.Object
        Returns:
        a hash code value for this object