Class Vector

All Implemented Interfaces:
Serializable, Matrix, Tensor

public abstract class Vector extends DenseMatrix
Mathematical vector interface. Vectors are a specialized case of matrices. Specifically, a vector is a matrix with either one row (a row-vector) or one column (a column-vector). Column-vectors are the more common representation and are often simply referred to as vectors.
See Also:
  • Method Details

    • size

      public abstract int size()
      Returns the number of elements.
      Returns:
      the number of elements.
    • get

      public abstract double get(int i)
      Returns A[i].
      Parameters:
      i - the row index.
      Returns:
      the cell value.
    • apply

      public double apply(int i)
      Returns A[i] for Scala users.
      Parameters:
      i - the index.
      Returns:
      the cell value.
    • set

      public abstract void set(int i, double x)
      Sets A[i] = x.
      Parameters:
      i - the index.
      x - the cell value.
    • update

      public void update(int i, double x)
      Sets A[i] = x for Scala users.
      Parameters:
      i - the index.
      x - the cell value.
    • add

      public abstract void add(int i, double x)
      Sets A[i] += x.
      Parameters:
      i - the index.
      x - the operand.
    • sub

      public abstract void sub(int i, double x)
      Sets A[i] -= x.
      Parameters:
      i - the index.
      x - the operand.
    • mul

      public abstract void mul(int i, double x)
      Sets A[i] *= x.
      Parameters:
      i - the index.
      x - the operand.
    • div

      public abstract void div(int i, double x)
      Sets A[i] /= x.
      Parameters:
      i - the index.
      x - the operand.
    • slice

      public abstract Vector slice(int from, int to)
      Returns a slice of vector, which shares the data storage.
      Parameters:
      from - the initial index of the range to be copied, inclusive.
      to - the final index of the range to be copied, exclusive.
      Returns:
      a slice of this vector.
    • column

      public Vector column(int j)
      Description copied from class: DenseMatrix
      Returns the j-th column. Negative index -j means the j-th row from the end.
      Specified by:
      column in class DenseMatrix
      Parameters:
      j - the column index.
      Returns:
      the column.
    • copy

      public Vector copy()
      Description copied from interface: Matrix
      Returns a deep copy of matrix.
      Specified by:
      copy in interface Matrix
      Specified by:
      copy in class DenseMatrix
      Returns:
      a deep copy of matrix.
    • copy

      public abstract Vector copy(int from, int to)
      Copies the specified range of the vector into a new vector. The final index of the range may be greater than vector length, in which case 0 is placed in all extra elements.
      Parameters:
      from - the initial index of the range to be copied, inclusive.
      to - the final index of the range to be copied, exclusive. This index may lie outside the array.
      Returns:
      a copy of this vector.
    • copy

      public static void copy(Vector src, int pos, Vector dest, int destPos, int length)
      Copies the specified range of the vector into another vector.
      Parameters:
      src - the destination vector.
      pos - starting position in this vector.
      dest - the destination vector.
      destPos - starting position in the destination vector.
      length - the number of vector elements to be copied.
    • toArray

      public abstract double[] toArray(double[] a)
      Returns an array containing all the elements in this vector.
      Parameters:
      a - the array into which the elements of the vector are to be stored if it is big enough; otherwise, a new array is allocated.
      Returns:
      an array containing the elements of the vector.
    • toArray

      public abstract float[] toArray(float[] a)
      Returns an array containing all the elements in this vector.
      Parameters:
      a - the array into which the elements of the vector are to be stored, if it is big enough; otherwise, a new array is allocated.
      Returns:
      an array containing the elements of the vector.
    • fill

      public abstract void fill(int from, int to, double value)
      Assigns the specified value to each element of the specified range of the specified vector.
      Parameters:
      from - the index of the first element (inclusive) to be filled with the specified value.
      to - the index of the last element (exclusive) to be filled with the specified value.
      value - the value to be stored in specified range of the vector.
    • diagflat

      public DenseMatrix diagflat()
      Returns the matrix with the elements of this vector as the diagonal.
      Returns:
      the diagonal matrix.
    • column

      public static Vector column(double[] array)
      Creates a column vector.
      Parameters:
      array - the primitive array backing the vector.
      Returns:
      a column vector.
    • column

      public static Vector column(float[] array)
      Creates a column vector.
      Parameters:
      array - the primitive array backing the vector.
      Returns:
      a column vector.
    • row

      public static Vector row(double[] array)
      Creates a row vector.
      Parameters:
      array - the primitive array backing the vector.
      Returns:
      a row vector.
    • row

      public static Vector row(float[] array)
      Creates a row vector.
      Parameters:
      array - the primitive array backing the vector.
      Returns:
      a row vector.
    • zeros

      public static Vector zeros(ScalarType scalarType, int length)
      Returns a zero column vector.
      Parameters:
      scalarType - the scalar type.
      length - the length of vector.
      Returns:
      a zero column vector.
    • zeros

      public Vector zeros(int length)
      Returns a zero column vector of the same scalar type as this vector.
      Parameters:
      length - the length of vector.
      Returns:
      a zero column vector.
    • ones

      public static Vector ones(ScalarType scalarType, int length)
      Returns a one column vector.
      Parameters:
      scalarType - the scalar type.
      length - the length of vector.
      Returns:
      a one column vector.
    • ones

      public Vector ones(int length)
      Returns a one column vector of the same scalar type as this vector.
      Parameters:
      length - the length of vector.
      Returns:
      a one column vector.
    • min

      public double min()
      Returns the minimal elements of the vector.
      Returns:
      the minimal elements of the vector.
    • max

      public double max()
      Returns the maximal elements of the vector.
      Returns:
      the maximal elements of the vector.
    • sum

      public double sum()
      Sums the elements of the vector.
      Returns:
      Sum of the elements of the vector.
    • mean

      public double mean()
      Returns the mean of the elements of the vector.
      Returns:
      the mean of the elements of the vector.
    • softmax

      public abstract int softmax()
      The softmax function without overflow. The function normalizes the vector into a probability distribution proportional to the exponentials of the input numbers. That is, prior to applying softmax, some vector components could be negative, or greater than one; and might not sum to 1; but after applying softmax, each component will be in the interval (0,1), and the components will add up to 1, so that they can be interpreted as probabilities. Furthermore, the larger input components will correspond to larger probabilities.
      Returns:
      the index of largest posteriori probability.
    • asum

      public double asum()
      Sums the absolute values of the elements of the vector.
      Returns:
      Sum of the absolute values of the elements of the vector.
    • axpy

      public void axpy(double alpha, Vector x)
      Computes a constant alpha times a vector x plus this vector y. The result overwrites the initial values of this vector y.
      Parameters:
      alpha - If alpha = 0 this routine returns without any computation.
      x - Input vector.
    • dot

      public double dot(Vector x)
      Computes the dot product of two vectors.
      Parameters:
      x - Input vector contains the second vector operand.
      Returns:
      dot product.
    • norm2

      public double norm2()
      Computes the Euclidean (L2) norm of a vector.
      Returns:
      Euclidean norm.
    • norm1

      public double norm1()
      Computes the L1 norm of a vector.
      Returns:
      L1 norm.
    • normInf

      public double normInf()
      Computes the L-Infinity norm of a vector.
      Returns:
      L-Infinity norm.
    • scale

      public Vector scale(double alpha)
      Description copied from interface: Matrix
      A *= alpha
      Specified by:
      scale in interface Matrix
      Overrides:
      scale in class DenseMatrix
      Parameters:
      alpha - the scaling factor.
      Returns:
      this matrix.
    • swap

      public void swap(Vector x)
      Swaps two vectors.
      Parameters:
      x - Vector to be swapped.
    • iamax

      public int iamax()
      Searches a vector for the first occurrence of the maximum absolute value.
      Returns:
      The first index of the maximum absolute value of vector x.