Class CompressedVector

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

public class CompressedVector extends SparseVector
A basic sparse vector implementation using underlying value and index arrays. 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. CompressedVector stores the underlying data in a two arrays: A values array and a indices array. The values array matches the indices array, and they're both sorted by the indices array. To get a value at an index, the index is found in the indices array with a binary search and the respective value is obtained from the values array.
  • Field Details

    • VECTOR_TAG

      private static final byte VECTOR_TAG
      See Also:
    • MINIMUM_SIZE

      private static final int MINIMUM_SIZE
      See Also:
    • values

      private double[] values
    • indices

      private int[] indices
  • Constructor Details

    • CompressedVector

      public CompressedVector()
    • CompressedVector

      public CompressedVector(int length)
    • CompressedVector

      public CompressedVector(int length, int capacity)
    • CompressedVector

      public CompressedVector(int length, int cardinality, double[] values, int[] indices)
  • Method Details

    • zero

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

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

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

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

      public static CompressedVector fromBinary(byte[] array)
      Decodes CompressedVector from the given byte array.
      Parameters:
      array - the byte array representing a vector
      Returns:
      a decoded vector
    • fromCSV

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

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

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

      public static CompressedVector fromMap(Map<Integer,? extends Number> map, int length)
      Creates new CompressedVector from index-value map
      Parameters:
      map - index-value map
      length - vector length
      Returns:
      created vector
    • getOrElse

      public double getOrElse(int i, double defaultValue)
      Description copied from class: SparseVector
      Gets the specified element, or a defaultValue if there is no actual element at index i in this sparse vector.
      Specified by:
      getOrElse in class SparseVector
      Parameters:
      i - the element's index
      defaultValue - the default value
      Returns:
      the element of this vector or a default value
    • set

      public void set(int i, double value)
      Description copied from class: Vector
      Sets the specified element of this matrix to given value.
      Specified by:
      set in class Vector
      Parameters:
      i - element's index
      value - element's new value
    • setAll

      public void setAll(double value)
      Description copied from class: Vector
      Sets all elements of this vector to given value.
      Overrides:
      setAll in class Vector
      Parameters:
      value - the element's new value
    • swapElements

      public void swapElements(int i, int j)
      Description copied from class: Vector
      Swaps the specified elements of this vector.
      Overrides:
      swapElements in class Vector
      Parameters:
      i - element's index
      j - element's index
    • copyOfLength

      public Vector copyOfLength(int length)
      Description copied from class: Vector
      Copies this vector into the new vector with specified length.
      Specified by:
      copyOfLength in class Vector
      Parameters:
      length - the length of new vector
      Returns:
      the copy of this vector with new length
    • each

      public void each(VectorProcedure procedure)
      Description copied from class: Vector
      Applies given procedure to each element of this vector.
      Overrides:
      each in class Vector
      Parameters:
      procedure - the vector procedure
    • eachNonZero

      public void eachNonZero(VectorProcedure procedure)
      Description copied from class: SparseVector
      Applies given procedure to each non-zero element of this vector.
      Overrides:
      eachNonZero in class SparseVector
      Parameters:
      procedure - the vector procedure
    • updateAt

      public void updateAt(int i, VectorFunction function)
      Description copied from class: Vector
      Updates the specified element of this vector by applying given function.
      Overrides:
      updateAt in class Vector
      Parameters:
      i - element's index
      function - the vector function
    • nonZeroAt

      public boolean nonZeroAt(int i)
      Description copied from class: SparseVector
      * Whether or not the specified element is not zero.
      Specified by:
      nonZeroAt in class SparseVector
      Parameters:
      i - element's index
      Returns:
      true if specified element is zero, false otherwise
    • 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 SparseVector
      Type Parameters:
      T - type of the result vector
      Parameters:
      factory - the factory that creates an output vector
      Returns:
      a converted vector
    • blankOfLength

      public Vector blankOfLength(int length)
      Description copied from class: Vector
      Creates a blank (an empty vector) copy of this vector with the given length.
      Specified by:
      blankOfLength in class Vector
      Parameters:
      length - the length of the blank vector
      Returns:
      blank vector
    • toBinary

      public byte[] toBinary()
      Description copied from class: Vector
      Encodes this vector into a byte array.
      Specified by:
      toBinary in class Vector
      Returns:
      a byte array representing this vector
    • searchForIndex

      private int searchForIndex(int i)
      Does the binary searching to find the position in the value array given it's index.
      Parameters:
      i - the index to search for
      Returns:
      the position in the value array
    • insert

      private void insert(int k, int i, double value)
    • remove

      private void remove(int k)
    • growUp

      private void growUp()
    • align

      private int align(int length, int capacity)
    • nonZeroIterator

      public VectorIterator nonZeroIterator()
      Description copied from class: SparseVector
      Returns a non-zero vector iterator.
      Specified by:
      nonZeroIterator in class SparseVector
      Returns:
      a non-zero vector iterator
    • iterator

      public VectorIterator iterator()
      Description copied from class: Vector
      Returns a vector iterator.
      Specified by:
      iterator in interface Iterable<Double>
      Overrides:
      iterator in class Vector
      Returns:
      a vector iterator.