Class SparseVector

java.lang.Object
org.la4j.Vector
org.la4j.vector.SparseVector
All Implemented Interfaces:
Iterable<Double>
Direct Known Subclasses:
CompressedVector

public abstract class SparseVector extends Vector
A sparse vector. A vector represents an array of elements. It can be re-sized. A sparse data structure does not store blank elements, and instead just stores elements with values. A sparse data structure can be initialized with a large length but take up no storage until the space is filled with non-zero elements. However, there is a performance cost. Fetch/store operations take O(log n) time instead of the O(1) time of a dense data structure.
  • Field Details

    • cardinality

      protected int cardinality
  • Constructor Details

    • SparseVector

      public SparseVector(int length)
    • SparseVector

      public SparseVector(int length, int cardinality)
  • Method Details

    • zero

      public static SparseVector zero(int length)
      Creates a zero SparseVector of the given length.
    • zero

      public static SparseVector zero(int length, int capacity)
      Creates a zero SparseVector of the given length with the given capacity.
    • random

      public static SparseVector random(int length, double density, Random random)
      Creates a constant SparseVector of the given length with the given value.
    • fromArray

      public static SparseVector fromArray(double[] array)
      Creates a new SparseVector from the given array with compressing (copying) the underlying array.
    • fromCSV

      public static SparseVector fromCSV(String csv)
      Parses SparseVector from the given CSV string.
      Parameters:
      csv - the CSV string representing a vector
      Returns:
      a parsed vector
    • fromMatrixMarket

      public static SparseVector fromMatrixMarket(String mm)
      Parses SparseVector from the given Matrix Market string.
      Parameters:
      mm - the string in Matrix Market format
      Returns:
      a parsed vector
    • fromCollection

      public static SparseVector fromCollection(Collection<? extends Number> list)
      Creates new SparseVector from collection
      Parameters:
      list - value list
      Returns:
      created vector
    • fromMap

      public static SparseVector fromMap(Map<Integer,? extends Number> map, int length)
      Creates new SparseVector from given index-value map
      Parameters:
      map -
      Returns:
    • cardinality

      public int cardinality()
      Returns the cardinality (the number of non-zero elements) of this sparse vector.
      Returns:
      the cardinality of this vector
    • density

      public double density()
      Returns the density (non-zero elements divided by total elements) of this sparse vector.
      Returns:
      the density of this vector
    • get

      public double get(int i)
      Description copied from class: Vector
      Gets the specified element of this vector.
      Specified by:
      get in class Vector
      Parameters:
      i - element's index
      Returns:
      the element of this vector
    • getOrElse

      public abstract double getOrElse(int i, double defaultValue)
      Gets the specified element, or a defaultValue if there is no actual element at index i in this sparse vector.
      Parameters:
      i - the element's index
      defaultValue - the default value
      Returns:
      the element of this vector or a default value
    • isZeroAt

      public boolean isZeroAt(int i)
      Whether or not the specified element is zero.
      Parameters:
      i - element's index
      Returns:
      true if specified element is zero, false otherwise
    • nonZeroAt

      public abstract boolean nonZeroAt(int i)
      * Whether or not the specified element is not zero.
      Parameters:
      i - element's index
      Returns:
      true if specified element is zero, false otherwise
    • foldNonZero

      public double foldNonZero(VectorAccumulator accumulator)
      Folds non-zero elements of this vector with given accumulator.
      Parameters:
      accumulator - the vector accumulator
      Returns:
      the accumulated value
    • eachNonZero

      public void eachNonZero(VectorProcedure procedure)
      Applies given procedure to each non-zero element of this vector.
      Parameters:
      procedure - the vector procedure
    • add

      public Vector add(double value)
      Description copied from class: Vector
      Adds given value (v) to this vector (X).
      Overrides:
      add in class Vector
      Parameters:
      value - the right hand value for addition
      Returns:
      X + v
    • multiply

      public Vector multiply(double value)
      Description copied from class: Vector
      Multiplies this vector (X) by given value (v).
      Overrides:
      multiply in class Vector
      Parameters:
      value - the right hand value for multiplication
      Returns:
      X * v
    • max

      public double max()
      Description copied from class: Vector
      Searches for the maximum value of the elements of this vector.
      Overrides:
      max in class Vector
      Returns:
      the maximum value of this vector
    • min

      public double min()
      Description copied from class: Vector
      Searches for the minimum value of the elements of this vector.
      Overrides:
      min in class Vector
      Returns:
      the minimum value of this vector
    • euclideanNorm

      public double euclideanNorm()
      Description copied from class: Vector
      Calculates an Euclidean norm of this vector.
      Overrides:
      euclideanNorm in class Vector
      Returns:
      an Euclidean norm
    • manhattanNorm

      public double manhattanNorm()
      Description copied from class: Vector
      Calculates a Manhattan norm of this vector.
      Overrides:
      manhattanNorm in class Vector
      Returns:
      a Manhattan norm
    • infinityNorm

      public double infinityNorm()
      Description copied from class: Vector
      Calculates an Infinity norm of this vector.
      Overrides:
      infinityNorm in class Vector
      Returns:
      an Infinity norm
    • nonZeroIterator

      public abstract VectorIterator nonZeroIterator()
      Returns a non-zero vector iterator.
      Returns:
      a non-zero vector iterator
    • to

      public <T extends Vector> T to(VectorFactory<T> factory)
      Description copied from class: Vector
      Converts this vector using the given factory.
      Overrides:
      to in class Vector
      Type Parameters:
      T - type of the result vector
      Parameters:
      factory - the factory that creates an output vector
      Returns:
      a converted vector
    • hashCode

      public int hashCode()
      Description copied from class: Vector
      Calculates the hash-code of this vector.
      Overrides:
      hashCode in class Vector
    • apply

      public <T> T apply(VectorOperation<T> operation)
      Description copied from class: Vector
      Pipes this vector to a given operation.
      Specified by:
      apply in class Vector
      Type Parameters:
      T - the result type
      Parameters:
      operation - the vector operation (an operation that take vector and returns T)
      Returns:
      the result of an operation applied to this vector
    • apply

      public <T> T apply(VectorVectorOperation<T> operation, Vector that)
      Description copied from class: Vector
      Pipes this vector to a given operation.
      Specified by:
      apply in class Vector
      Type Parameters:
      T - the result type
      Parameters:
      operation - the vector-vector operation (an operation that takes two vectors and returns T)
      that - the right hand vector for the given operation
      Returns:
      the result of an operation applied to this and that vector
    • apply

      public <T> T apply(VectorMatrixOperation<T> operation, Matrix that)
      Description copied from class: Vector
      Pipes this vector to a given operation.
      Specified by:
      apply in class Vector
      Type Parameters:
      T - the result type
      Parameters:
      operation - the vector-matrix operation (an operation that takes vector and matrix and returns T)
      that - the right hand matrix for the given operation
      Returns:
      the result of an operation applied to this vector and that matrix
    • toRowMatrix

      public Matrix toRowMatrix()
      Description copied from class: Vector
      Converts this vector to matrix with only one row.
      Specified by:
      toRowMatrix in class Vector
      Returns:
      the row matrix
    • toColumnMatrix

      public Matrix toColumnMatrix()
      Description copied from class: Vector
      Converts this vector to matrix with only one column.
      Specified by:
      toColumnMatrix in class Vector
      Returns:
      the column matrix
    • toDiagonalMatrix

      public Matrix toDiagonalMatrix()
      Description copied from class: Vector
      Converts this vector to a diagonal matrix.
      Specified by:
      toDiagonalMatrix in class Vector
      Returns:
      a diagonal matrix
    • toMatrixMarket

      public String toMatrixMarket(NumberFormat formatter)
      Description copied from class: Vector
      Converts this vector into the string in Matrix Market format using the given formatter;
      Specified by:
      toMatrixMarket in class Vector
      Parameters:
      formatter - the number formater
      Returns:
      a Matrix Market string representing this vector
    • ensureIndexIsInBounds

      protected void ensureIndexIsInBounds(int i)
      Ensures the provided index is in the bounds of this SparseVector.
      Parameters:
      i - The index to check.