Class SparseMatrix

java.lang.Object
org.la4j.Matrix
org.la4j.matrix.SparseMatrix
All Implemented Interfaces:
Iterable<Double>
Direct Known Subclasses:
ColumnMajorSparseMatrix, RowMajorSparseMatrix

public abstract class SparseMatrix extends Matrix
  • Field Details

    • cardinality

      protected int cardinality
  • Constructor Details

    • SparseMatrix

      public SparseMatrix(int rows, int columns)
    • SparseMatrix

      public SparseMatrix(int rows, int columns, int cardinality)
  • Method Details

    • zero

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

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

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

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

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

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

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

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

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

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

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

      public double get(int i, int j)
      Description copied from class: Matrix
      Gets the specified element of this matrix.
      Specified by:
      get in class Matrix
      Parameters:
      i - element's row index
      j - element's column index
      Returns:
      the element of this matrix
    • getOrElse

      public abstract double getOrElse(int i, int j, double defaultValue)
      Gets the specified element, or a defaultValue if there is no actual element at (i, j) in this sparse matrix.
      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
    • isRowMajor

      public abstract boolean isRowMajor()
      Checks whether or not this sparse matrix row-major.
    • isColumnMajor

      public boolean isColumnMajor()
    • cardinality

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

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

      protected long capacity()
      Returns:
      a capacity of this sparse matrix
    • getRow

      public Vector getRow(int i)
      Description copied from class: Matrix
      Copies the specified row of this matrix into the vector.
      Specified by:
      getRow in class Matrix
      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.
      Specified by:
      getColumn in class Matrix
      Parameters:
      j - the column index
      Returns:
      the column represented as vector
    • multiply

      public Matrix multiply(double value)
      Description copied from class: Matrix
      Scales this matrix by given value (v).
      Overrides:
      multiply in class Matrix
      Parameters:
      value - the scale factor
      Returns:
      A * v
    • add

      public Matrix add(double value)
      Description copied from class: Matrix
      Adds given value (v) to every element of this matrix (A).
      Overrides:
      add in class Matrix
      Parameters:
      value - the right hand value for addition
      Returns:
      A + v
    • isZeroAt

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

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

      public void eachNonZero(MatrixProcedure procedure)
      Applies given procedure to each non-zero element of this matrix.
      Parameters:
      procedure - the matrix procedure
    • eachNonZeroInRow

      public void eachNonZeroInRow(int i, VectorProcedure procedure)
      Applies the given procedure to each non-zero element of the specified row of this matrix.
      Parameters:
      i - the row index.
      procedure - the VectorProcedure.
    • eachNonZeroInColumn

      public void eachNonZeroInColumn(int j, VectorProcedure procedure)
      Applies the given procedure to each non-zero element of the specified column of this matrix.
      Parameters:
      j - the column index.
      procedure - the VectorProcedure.
    • foldNonZero

      public double foldNonZero(MatrixAccumulator accumulator)
      Folds non-zero elements of this matrix with given accumulator.
      Parameters:
      accumulator - the matrix accumulator
      Returns:
      the accumulated value
    • foldNonZeroInRow

      public double foldNonZeroInRow(int i, VectorAccumulator accumulator)
      Folds non-zero elements of the specified row in this matrix with the given accumulator.
      Parameters:
      i - the row index.
      accumulator - the VectorAccumulator.
      Returns:
      the accumulated value.
    • foldNonZeroInColumn

      public double foldNonZeroInColumn(int j, VectorAccumulator accumulator)
      Folds non-zero elements of the specified column in this matrix with the given accumulator.
      Parameters:
      j - the column index.
      accumulator - the VectorAccumulator.
      Returns:
      the accumulated value.
    • foldNonZeroInColumns

      public double[] foldNonZeroInColumns(VectorAccumulator accumulator)
      Folds non-zero elements (in a column-by-column manner) of this matrix with given accumulator.
      Parameters:
      accumulator - the matrix accumulator
      Returns:
      the accumulated vector
    • foldNonZeroInRows

      public double[] foldNonZeroInRows(VectorAccumulator accumulator)
      Folds non-zero elements (in a row-by-row manner) of this matrix with given accumulator.
      Parameters:
      accumulator - the matrix accumulator
      Returns:
      the accumulated vector
    • nonZeroIterator

      public MatrixIterator nonZeroIterator()
      Returns a non-zero matrix iterator.
      Returns:
      a non-zero matrix iterator
    • nonZeroRowMajorIterator

      public RowMajorMatrixIterator nonZeroRowMajorIterator()
      Returns a non-zero row-major matrix iterator.
      Returns:
      a non-zero row-major matrix iterator.
    • nonZeroColumnMajorIterator

      public ColumnMajorMatrixIterator nonZeroColumnMajorIterator()
      Returns a non-zero column-major matrix iterator.
      Returns:
      a non-zero column major matrix iterator.
    • nonZeroIteratorOfRow

      public VectorIterator nonZeroIteratorOfRow(int i)
      Returns a non-zero vector iterator of the given row i.
      Returns:
      a non-zero vector iterator
    • nonZeroIteratorOfColumn

      public VectorIterator nonZeroIteratorOfColumn(int j)
      Returns a non-zero vector iterator of the given column j.
      Returns:
      a non-zero vector iterator
    • toMatrixMarket

      public String toMatrixMarket(NumberFormat formatter)
      Description copied from class: Matrix
      Converts this matrix into the Matrix Market string using the given number formatter.
      Specified by:
      toMatrixMarket in class Matrix
      Returns:
      a string in Matrix Market format representing this matrix;
    • ensureCardinalityIsCorrect

      protected void ensureCardinalityIsCorrect(long rows, long columns, long cardinality)