Documentation of 'jsat.linear.Vec' Java class
Vec
jsat.linear

Class Vec

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<IndexValue>
    Direct Known Subclasses:
    ConcatenatedVec, ConstantVector, DenseVector, Poly2Vec, RandomVector, ScaledVector, ShiftedVec, SparseVector, SubVector, VecPaired, VecWithNorm


    public abstract class Vec
    extends java.lang.Object
    implements java.lang.Cloneable, java.lang.Iterable<IndexValue>, java.io.Serializable
    Vec is a object representing the math concept of a vector. A vector could be either sparse or dense, where sparse vectors have a high number of zero values that are not explicitly stored.

    This abstract class provides a large number of pre-implemented methods. Some of which are implemented only for a dense vector, or may not be completely efficient for the underlying implementation. Methods that should be considered for overloading by an implementation will be indicated in the documentation.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor and Description
      Vec() 
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method and Description
      Vec add(double c)
      Returns a new vector that is the result of this + c
      Vec add(Vec b)
      Returns a new vector that is the result of this + b
      void applyFunction(Function1D f)
      Applies the given function to each and every value in the vector.
      void applyIndexFunction(IndexFunction f)
      Applies the given function to each and every value in the vector.
      double[] arrayCopy()
      Creates a new array that contains all the values of this vector in the appropriate indices
      boolean canBeMutated()
      Indicates whether or not this vector can be mutated.
      abstract Vec clone() 
      void copyTo(Vec destination)
      Copies the values of this Vector into another vector
      void copyToCol(Matrix A, int col)
      Copies the values of this vector into a column of another Matrix.
      void copyToRow(Matrix A, int row)
      Copies the values of this vector into a row of another Matrix
      int countNaNs() 
      Vec divide(double c)
      Returns a new vector that is the result of this / c
      double dot(Vec v)
      Computes the dot product between two vectors, which is equivalent to
      Σ thisi*vi

      This method should be overloaded for a serious implementation.
      boolean equals(java.lang.Object obj) 
      boolean equals(java.lang.Object obj, double range) 
      abstract double get(int index)
      Gets the value stored at a specific index in the vector
      java.util.Iterator<IndexValue> getNonZeroIterator()
      Returns an iterator that will go over the non zero values in the given vector.
      java.util.Iterator<IndexValue> getNonZeroIterator(int start)
      Returns an iterator that will go over the non zero values starting from the specified index in the given vector.
      int hashCode()
      Provides a hashcode for Vectors.
      void increment(int index, double val)
      Increments the value stored at a specified index in the vector
      abstract boolean isSparse()
      Indicates whether or not this vector is optimized for sparce computation, meaning that most values in the vector are zero - and considered implicit.
      java.util.Iterator<IndexValue> iterator() 
      double kurtosis()
      Computes the kurtosis of this vector, which is the 4th moment.
      abstract int length()
      Returns the length of this vector
      double max()
      Returns the maximum value stored in this vector
      double mean()
      Computes the mean value of all values stored in this vector
      double median()
      Returns the median value in this vector
      double min()
      Returns the minimum value stored in this vector
      Vec multiply(double c)
      Returns a new vector that is the result of this * c
      void multiply(double c, Matrix A, Vec b)
      If this is vector a, this this computes b = b + c aT*A
      Vec multiply(Matrix A)
      Returns a new vector that is the result of the vector matrix product thisTA
      void multiply(Matrix A, Vec b)
      If this is vector a, this this computes b = b + aT*A
      void mutableAdd(double c)
      Alters this vector such that this = this + c

      This method should be overloaded for a serious implementation.
      void mutableAdd(double c, Vec b)
      Alters this vector such that this = this + c * b

      This method should be overloaded for a serious implementation.
      void mutableAdd(Vec b)
      Alters this vector such that this = this + b
      void mutableDivide(double c)
      Mutates this /= c

      This method should be overloaded for a serious implementation.
      void mutableMultiply(double c)
      Mutates this *= c

      This method should be overloaded for a serious implementation.
      void mutablePairwiseDivide(Vec b)
      Mutates this by dividing each value by the value in b that has the same index

      This method should be overloaded for a serious implementation.
      void mutablePairwiseMultiply(Vec b)
      Mutates this by multiplying each value by the value in b that has the same index.
      void mutableSubtract(double c)
      Alters this vector such that this = this - c
      void mutableSubtract(double c, Vec b)
      Alters this vector such that this = this - c * b
      void mutableSubtract(Vec b)
      Alters this vector such that this = this - b
      int nnz()
      Computes the number of non zero values in this vector
      void normalize()
      Mutates this vector to be normalized by the L2 norm
      Vec normalized()
      Returns a new vector that is the result of normalizing this vector by the L2 norm
      Vec pairwiseDivide(Vec b)
      Returns a new vector that is the result of dividing each value in this by the value in the same index in b
      Vec pairwiseMultiply(Vec b)
      Returns a new vector that is the result of multiplying each value in this by its corresponding value in b
      double pNorm(double p)
      Returns the p-norm of this vector.
      double pNormDist(double p, Vec y)
      Returns the p-norm distance between this and another vector y.
      static Vec random(int length)
      Creates a dense vector full of random values in the range [0, 1]
      static Vec random(int length, java.util.Random rand)
      Creates a dense vector full of random values in the range [0, 1]
      abstract void set(int index, double val)
      Sets the value stored at a specified index in the vector
      double skewness()
      Computes the skewness of this vector, which is the 3rd moment.
      Vec sortedCopy()
      Returns a copy of this array with the values moved around so that they are in sorted order
      double standardDeviation()
      Computes the standard deviation of the values in this vector
      Vec subtract(double c)
      Returns a new vector that is the result of this - c
      Vec subtract(Vec b)
      Returns a new vector that is the result of this - b
      double sum()
      Computes the sum of the values in this vector
      java.lang.String toString() 
      double variance()
      Computes the variance of the values in this vector, which is standardDeviation()2
      void zeroOut()
      Zeroes out all values in this vector

      This method should be overloaded for a serious implementation.
      static Vec zeros(int length)
      Creates a dense vector full of zeros.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • Vec

        public Vec()
    • Method Detail

      • length

        public abstract int length()
        Returns the length of this vector
        Returns:
        the length of this vector
      • countNaNs

        public int countNaNs()
        Returns:
        the number of NaNs present in this vector
      • canBeMutated

        public boolean canBeMutated()
        Indicates whether or not this vector can be mutated. If false, any method that contains "mutate" will not work.

        By default, this returns true
        Returns:
        true if the vector supports being altered, false other wise.
      • nnz

        public int nnz()
        Computes the number of non zero values in this vector
        Returns:
        the number of non zero values stored
      • get

        public abstract double get(int index)
        Gets the value stored at a specific index in the vector
        Parameters:
        index - the index to access
        Returns:
        the double value in the vector
        Throws:
        java.lang.IndexOutOfBoundsException - if the index given is greater than or equal to its length()
      • set

        public abstract void set(int index,
                                 double val)
        Sets the value stored at a specified index in the vector
        Parameters:
        index - the index to access
        val - the value to store in the index
        Throws:
        java.lang.IndexOutOfBoundsException - if the index given is greater than or equal to its length()
      • increment

        public void increment(int index,
                              double val)
        Increments the value stored at a specified index in the vector
        Parameters:
        index - the index to access
        val - the value to store in the index
        Throws:
        java.lang.IndexOutOfBoundsException - if the index given is greater than or equal to its length()
      • add

        public Vec add(double c)
        Returns a new vector that is the result of this + c
        Parameters:
        c - the constant to add
        Returns:
        the result of adding c to this
      • add

        public Vec add(Vec b)
        Returns a new vector that is the result of this + b
        Parameters:
        b - the vector to add
        Returns:
        the result of b + this
      • subtract

        public Vec subtract(double c)
        Returns a new vector that is the result of this - c
        Parameters:
        c - the constant to subtract
        Returns:
        the result of this - c
      • subtract

        public Vec subtract(Vec b)
        Returns a new vector that is the result of this - b
        Parameters:
        b - the vector to subtract from this
        Returns:
        the result of this - b
      • pairwiseMultiply

        public Vec pairwiseMultiply(Vec b)
        Returns a new vector that is the result of multiplying each value in this by its corresponding value in b
        Parameters:
        b - the vector to pairwise multiply by
        Returns:
        the result of the pairwise multiplication of b onto the values of this
      • multiply

        public Vec multiply(double c)
        Returns a new vector that is the result of this * c
        Parameters:
        c - the constant to multiply by
        Returns:
        the result of this * c
      • multiply

        public Vec multiply(Matrix A)
        Returns a new vector that is the result of the vector matrix product thisTA
        Parameters:
        A - the matrix to multiply with
        Returns:
        the vector matrix product
      • multiply

        public void multiply(Matrix A,
                             Vec b)
        If this is vector a, this this computes b = b + aT*A
        Parameters:
        A - the matrix to multiple by
        b - the vector to mutate by adding the result to
      • multiply

        public void multiply(double c,
                             Matrix A,
                             Vec b)
        If this is vector a, this this computes b = b + c aT*A
        Parameters:
        c - the constant factor to multiply by
        A - the matrix to multiple by
        b - the vector to mutate by adding the result to
      • pairwiseDivide

        public Vec pairwiseDivide(Vec b)
        Returns a new vector that is the result of dividing each value in this by the value in the same index in b
        Parameters:
        b - the vector to pairwise divide by
        Returns:
        the result of pairwise division of this by b
      • divide

        public Vec divide(double c)
        Returns a new vector that is the result of this / c
        Parameters:
        c - the constant to divide by
        Returns:
        the result of this / c
      • mutableAdd

        public void mutableAdd(double c)
        Alters this vector such that this = this + c

        This method should be overloaded for a serious implementation.
        Parameters:
        c - a scalar constant to add to each value in this vector
      • mutableAdd

        public void mutableAdd(double c,
                               Vec b)
        Alters this vector such that this = this + c * b

        This method should be overloaded for a serious implementation.
        Parameters:
        c - a scalar constant
        b - the vector to add to this
      • mutableAdd

        public void mutableAdd(Vec b)
        Alters this vector such that this = this + b
        Parameters:
        b - the vector to add to this
        Throws:
        java.lang.ArithmeticException - if the vectors do not have the same length
      • mutableSubtract

        public void mutableSubtract(double c)
        Alters this vector such that this = this - c
        Parameters:
        c - the scalar constant to subtract from all values in this vector
      • mutableSubtract

        public void mutableSubtract(double c,
                                    Vec b)
        Alters this vector such that this = this - c * b
        Parameters:
        c - a scalar constant
        b - the vector to subtract from this
        Throws:
        java.lang.ArithmeticException - if the vectors do not have the same length
      • mutableSubtract

        public void mutableSubtract(Vec b)
        Alters this vector such that this = this - b
        Parameters:
        b - the vector to subtract from this
        Throws:
        java.lang.ArithmeticException - if the vectors are not the same length
      • mutablePairwiseMultiply

        public void mutablePairwiseMultiply(Vec b)
        Mutates this by multiplying each value by the value in b that has the same index.

        This method should be overloaded for a serious implementation.
        Parameters:
        b - the vector to pairwise multiply by
      • mutableMultiply

        public void mutableMultiply(double c)
        Mutates this *= c

        This method should be overloaded for a serious implementation.
        Parameters:
        c - the constant to multiply by
      • mutablePairwiseDivide

        public void mutablePairwiseDivide(Vec b)
        Mutates this by dividing each value by the value in b that has the same index

        This method should be overloaded for a serious implementation.
        Parameters:
        b - the vector to pairwise divide by
      • mutableDivide

        public void mutableDivide(double c)
        Mutates this /= c

        This method should be overloaded for a serious implementation.
        Parameters:
        c - the constant to divide by
      • sortedCopy

        public Vec sortedCopy()
        Returns a copy of this array with the values moved around so that they are in sorted order
        Returns:
        a new array in sorted order
      • min

        public double min()
        Returns the minimum value stored in this vector
        Returns:
        the minimum value in this vector
      • max

        public double max()
        Returns the maximum value stored in this vector
        Returns:
        the maximum value in this vector
      • sum

        public double sum()
        Computes the sum of the values in this vector
        Returns:
        the sum of this vector's values
      • mean

        public double mean()
        Computes the mean value of all values stored in this vector
        Returns:
        the mean value
      • standardDeviation

        public double standardDeviation()
        Computes the standard deviation of the values in this vector
        Returns:
        the standard deviation
      • variance

        public double variance()
        Computes the variance of the values in this vector, which is standardDeviation()2
        Returns:
        the variance
      • median

        public double median()
        Returns the median value in this vector
        Returns:
        the median
      • skewness

        public double skewness()
        Computes the skewness of this vector, which is the 3rd moment.
        Returns:
        the skewness
      • kurtosis

        public double kurtosis()
        Computes the kurtosis of this vector, which is the 4th moment.
        Returns:
        the kurtosis
      • isSparse

        public abstract boolean isSparse()
        Indicates whether or not this vector is optimized for sparce computation, meaning that most values in the vector are zero - and considered implicit. Only non-zero values are stored.
        Returns:
        true if the vector is sparce, false otherwise.
      • copyTo

        public void copyTo(Vec destination)
        Copies the values of this Vector into another vector
        Parameters:
        destination - the vector to store the values in.
        Throws:
        java.lang.ArithmeticException - if the vectors are not of the same length
      • copyToRow

        public void copyToRow(Matrix A,
                              int row)
        Copies the values of this vector into a row of another Matrix
        Parameters:
        A - the matrix to store the contents of this vector in
        row - the row of the matrix to store the values to
        Throws:
        java.lang.ArithmeticException - if the columns of the matrix is not the same as the length of this vector.
      • copyToCol

        public void copyToCol(Matrix A,
                              int col)
        Copies the values of this vector into a column of another Matrix.
        Parameters:
        A - the matrix to store the contents of this vector in
        col - the column of the matrix to store the values to
      • clone

        public abstract Vec clone()
        Overrides:
        clone in class java.lang.Object
      • normalized

        public Vec normalized()
        Returns a new vector that is the result of normalizing this vector by the L2 norm
        Returns:
        a normalized version of this vector
      • normalize

        public void normalize()
        Mutates this vector to be normalized by the L2 norm
      • applyFunction

        public void applyFunction(Function1D f)
        Applies the given function to each and every value in the vector.

        This method should be overloaded for a serious implementation.
        Parameters:
        f - the single variable function to apply
      • applyIndexFunction

        public void applyIndexFunction(IndexFunction f)
        Applies the given function to each and every value in the vector. The function takes 2 arguments, an arbitrary value, and then an index. The index passed to the function is the index in the array that the value came from.

        NOTE: Because negative values are invalid indexes. The given function should return 0.0 when given a negative index, if and only if, f(0,index) = 0 for any valid index. If f(0, index) != 0 for even one value of index, it should return any non zero value when given a negative index.

        IE: f(value_i, i) = x

        This method should be overloaded for a serious implementation.
        Parameters:
        f - the 2 dimensional index function to apply
      • pNormDist

        public double pNormDist(double p,
                                Vec y)
        Returns the p-norm distance between this and another vector y.
        Parameters:
        p - the distance type. 2 is the common value
        y - the other vector to compare against
        Returns:
        the p-norm distance
      • pNorm

        public double pNorm(double p)
        Returns the p-norm of this vector.
        Parameters:
        p - the norm type. 2 is a common value
        Returns:
        the p-norm of this vector
      • dot

        public double dot(Vec v)
        Computes the dot product between two vectors, which is equivalent to
        Σ thisi*vi

        This method should be overloaded for a serious implementation.
        Parameters:
        v - the other vector
        Returns:
        the dot product of this vector and another
      • toString

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

        public boolean equals(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • equals

        public boolean equals(java.lang.Object obj,
                              double range)
      • arrayCopy

        public double[] arrayCopy()
        Creates a new array that contains all the values of this vector in the appropriate indices
        Returns:
        a new array that is a copy of this vector
      • iterator

        public java.util.Iterator<IndexValue> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<IndexValue>
      • getNonZeroIterator

        public java.util.Iterator<IndexValue> getNonZeroIterator()
        Returns an iterator that will go over the non zero values in the given vector. The iterator does not support the Iterator.remove() method.
        Returns:
        an iterator for the non zero index value pairs.
      • getNonZeroIterator

        public java.util.Iterator<IndexValue> getNonZeroIterator(int start)
        Returns an iterator that will go over the non zero values starting from the specified index in the given vector. The iterator does not support the Iterator.remove() method.

        This method should be overloaded for a serious implementation.
        Parameters:
        start - the first index (inclusive) to start returning non-zero values from
        Returns:
        an iterator for the non zero index value pairs
      • zeroOut

        public void zeroOut()
        Zeroes out all values in this vector

        This method should be overloaded for a serious implementation.
      • hashCode

        public int hashCode()
        Provides a hashcode for Vectors. All vector implementations should return the same result for cases when equals(java.lang.Object) returns true. Below is the code used for this class

        int result = 1;

        for (int i = 0; i < length(); i++)
        {
        double val = get(i);
        if(val != 0)
        {
        long bits = Double.doubleToLongBits(val);
        result = 31 * result + (int)(bits ^ (bits >>> 32));
        result = 31 * result + i;
        }
        }

        return 31* result + length();

        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code for a vector
      • random

        public static Vec random(int length)
        Creates a dense vector full of random values in the range [0, 1]
        Parameters:
        length - the length of the random vector to create
        Returns:
        a random vector of the specified length
      • random

        public static Vec random(int length,
                                 java.util.Random rand)
        Creates a dense vector full of random values in the range [0, 1]
        Parameters:
        length - the length of the random vector to create
        rand - the source of randomness
        Returns:
        a random vector of the specified length
      • zeros

        public static Vec zeros(int length)
        Creates a dense vector full of zeros.
        Parameters:
        length - the length of the vector to create
        Returns:
        a vector of zeros

DataMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.