Class CRSMatrix

All Implemented Interfaces:
Iterable<Double>

public class CRSMatrix extends RowMajorSparseMatrix
This is a CRS (Compressed Row Storage) matrix class.
  • Field Details

    • MATRIX_TAG

      private static final byte MATRIX_TAG
      See Also:
    • MINIMUM_SIZE

      private static final int MINIMUM_SIZE
      See Also:
    • values

      private double[] values
    • columnIndices

      private int[] columnIndices
    • rowPointers

      private int[] rowPointers
  • Constructor Details

    • CRSMatrix

      public CRSMatrix()
    • CRSMatrix

      public CRSMatrix(int rows, int columns)
    • CRSMatrix

      public CRSMatrix(int rows, int columns, int capacity)
    • CRSMatrix

      public CRSMatrix(int rows, int columns, int cardinality, double[] values, int[] columnIndices, int[] rowPointers)
  • Method Details

    • zero

      public static CRSMatrix zero(int rows, int columns)
      Creates a zero CRSMatrix of the given shape: rows x columns.
    • zero

      public static CRSMatrix zero(int rows, int columns, int capacity)
      Creates a zero CRSMatrix of the given shape: rows x columns with the given capacity.
    • diagonal

      public static CRSMatrix diagonal(int size, double diagonal)
      Creates a diagonal CRSMatrix of the given size whose diagonal elements are equal to diagonal.
    • identity

      public static CRSMatrix identity(int size)
      Creates an identity CRSMatrix of the given size.
    • random

      public static CRSMatrix random(int rows, int columns, double density, Random random)
      Creates a random CRSMatrix of the given shape: rows x columns.
    • randomSymmetric

      public static CRSMatrix randomSymmetric(int size, double density, Random random)
      Creates a random symmetric CRSMatrix of the given size.
    • from1DArray

      public static CRSMatrix from1DArray(int rows, int columns, double[] array)
      Creates a new CRSMatrix from the given 1D array with compressing (copying) the underlying array.
    • from2DArray

      public static CRSMatrix from2DArray(double[][] array)
      Creates a new CRSMatrix from the given 2D array with compressing (copying) the underlying array.
    • block

      public static CRSMatrix block(Matrix a, Matrix b, Matrix c, Matrix d)
      Creates a block CRSMatrix of the given blocks a, b, c and d.
    • fromBinary

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

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

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

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

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

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

      public Vector getRow(int i)
      Description copied from class: Matrix
      Copies the specified row of this matrix into the vector.
      Overrides:
      getRow in class SparseMatrix
      Parameters:
      i - the row index
      Returns:
      the row represented as vector
    • getColumn

      public Vector getColumn(int j)
      Description copied from class: Matrix
      Copies the specified column of this matrix into the vector.
      Overrides:
      getColumn in class SparseMatrix
      Parameters:
      j - the column index
      Returns:
      the column represented as vector
    • copyOfShape

      public Matrix copyOfShape(int rows, int columns)
      Description copied from class: Matrix
      Copies this matrix into the new matrix with specified dimensions: rows and columns.
      Specified by:
      copyOfShape in class Matrix
      Parameters:
      rows - the number of rows in new matrix
      columns - the number of columns in new matrix
      Returns:
      the copy of this matrix with new size
    • eachNonZero

      public void eachNonZero(MatrixProcedure procedure)
      Description copied from class: SparseMatrix
      Applies given procedure to each non-zero element of this matrix.
      Overrides:
      eachNonZero in class SparseMatrix
      Parameters:
      procedure - the matrix procedure
    • each

      public void each(MatrixProcedure procedure)
      Description copied from class: Matrix
      Applies given procedure to each element of this matrix.
      Overrides:
      each in class Matrix
      Parameters:
      procedure - the matrix procedure
    • eachInRow

      public void eachInRow(int i, VectorProcedure procedure)
      Description copied from class: Matrix
      Applies given procedure to each element of specified row of this matrix.
      Overrides:
      eachInRow in class Matrix
      Parameters:
      i - the row index
      procedure - the vector procedure
    • eachNonZeroInRow

      public void eachNonZeroInRow(int i, VectorProcedure procedure)
      Description copied from class: SparseMatrix
      Applies the given procedure to each non-zero element of the specified row of this matrix.
      Overrides:
      eachNonZeroInRow in class SparseMatrix
      Parameters:
      i - the row index.
      procedure - the VectorProcedure.
    • updateAt

      public void updateAt(int i, int j, MatrixFunction function)
      Description copied from class: Matrix
      Updates the specified element of this matrix by applying given function.
      Overrides:
      updateAt in class Matrix
      Parameters:
      i - the row index
      j - the column index
      function - the matrix function
    • nonZeroAt

      public boolean nonZeroAt(int i, int j)
      Description copied from class: SparseMatrix
      Whether or not the specified element is not zero.
      Specified by:
      nonZeroAt in class SparseMatrix
      Parameters:
      i - element's row index
      j - element's column index
      Returns:
      true if specified element is not zero, false otherwise
    • searchForColumnIndex

      private int searchForColumnIndex(int j, int left, int right)
    • insert

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

      private void remove(int k, int i)
    • growUp

      private void growUp()
    • align

      private int align(int cardinality)
    • max

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

      public double min()
      Description copied from class: Matrix
      Searches for the minimum value of the elements of this matrix.
      Overrides:
      min in class Matrix
      Returns:
      minimum value of this matrix
    • maxInRow

      public double maxInRow(int i)
      Description copied from class: Matrix
      Searches for the maximum value of specified row in this matrix.
      Overrides:
      maxInRow in class Matrix
      Parameters:
      i - the row index
      Returns:
      maximum value of specified row in this matrix
    • minInRow

      public double minInRow(int i)
      Description copied from class: Matrix
      Searches for the minimum value of specified row in this matrix.
      Overrides:
      minInRow in class Matrix
      Parameters:
      i - the row index
      Returns:
      minimum value of specified row in this matrix
    • select

      public Matrix select(int[] rowIndices, int[] columnIndices)
      Returns a CRSMatrix with the selected rows and columns.
      Overrides:
      select in class Matrix
      Parameters:
      rowIndices - the array of row indices
      columnIndices - the array of column indices
      Returns:
      the new matrix with the selected rows and columns
    • to

      public <T extends Matrix> T to(MatrixFactory<T> factory)
      Description copied from class: Matrix
      Converts this matrix using the given factory.
      Overrides:
      to in class Matrix
      Type Parameters:
      T - type of the result matrix
      Parameters:
      factory - the factory that creates an output matrix
      Returns:
      converted matrix
    • blankOfShape

      public Matrix blankOfShape(int rows, int columns)
      Description copied from class: Matrix
      Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape: rows x columns.
      Specified by:
      blankOfShape in class Matrix
      Returns:
      blank matrix
    • iteratorOfNonZeroRows

      public Iterator<Integer> iteratorOfNonZeroRows()
      Specified by:
      iteratorOfNonZeroRows in class RowMajorSparseMatrix
    • rowMajorIterator

      public RowMajorMatrixIterator rowMajorIterator()
      Description copied from class: Matrix
      Returns a row-major matrix iterator.
      Overrides:
      rowMajorIterator in class Matrix
      Returns:
      a row-major matrix iterator.
    • nonZeroRowMajorIterator

      public RowMajorMatrixIterator nonZeroRowMajorIterator()
      Description copied from class: SparseMatrix
      Returns a non-zero row-major matrix iterator.
      Overrides:
      nonZeroRowMajorIterator in class SparseMatrix
      Returns:
      a non-zero row-major matrix iterator.
    • nonZeroIteratorOfRow

      public VectorIterator nonZeroIteratorOfRow(int i)
      Description copied from class: SparseMatrix
      Returns a non-zero vector iterator of the given row i.
      Overrides:
      nonZeroIteratorOfRow in class SparseMatrix
      Returns:
      a non-zero vector iterator
    • iteratorOfRow

      public VectorIterator iteratorOfRow(int i)
      Description copied from class: Matrix
      Returns a vector iterator of the given row {code i}.
      Overrides:
      iteratorOfRow in class Matrix
      Returns:
      a vector iterator
    • toBinary

      public byte[] toBinary()
      Description copied from class: Matrix
      Encodes this matrix into a byte array.
      Specified by:
      toBinary in class Matrix
      Returns:
      a byte array representing this matrix