java.lang.Object
dev.nm.algebra.linear.matrix.doubles.matrixtype.sparse.LILSparseMatrix
All Implemented Interfaces:
Matrix, MatrixAccess, MatrixRing, MatrixTable, Densifiable, SparseMatrix, SparseStructure, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Table, DeepCopyable

public class LILSparseMatrix extends Object implements SparseMatrix
The list of lists (LIL) format for sparse matrix stores one list per row, where each entry stores a column index and value. Typically, these entries are kept sorted by column index for faster lookup. This is another format which is good for incremental matrix construction.
See Also:
  • Constructor Details

    • LILSparseMatrix

      public LILSparseMatrix(int nRows, int nCols)
      Construct a sparse matrix in LIL format.
      Parameters:
      nRows - the number of rows
      nCols - the number of columns
    • LILSparseMatrix

      public LILSparseMatrix(int nRows, int nCols, int[] rowIndices, int[] columnIndices, double[] value)
      Construct a sparse matrix in LIL format.
      Parameters:
      nRows - the number of rows
      nCols - the number of columns
      rowIndices - the row indices of the non-zeros values
      columnIndices - the column indices of the non-zeros values
      value - the non-zero values
    • LILSparseMatrix

      public LILSparseMatrix(int nRows, int nCols, List<SparseMatrix.Entry> entries)
      Construct a sparse matrix in LIL format by a list of non-zero entries.
      Parameters:
      nRows - the number of rows
      nCols - the number of columns
      entries - the list of entries
    • LILSparseMatrix

      public LILSparseMatrix(LILSparseMatrix that)
      Copy constructor.
      Parameters:
      that - the matrix to be copied
  • Method Details

    • nRows

      public int nRows()
      Description copied from interface: Table
      Gets the number of rows. Rows count from 1.
      Specified by:
      nRows in interface Table
      Returns:
      the number of rows
    • nCols

      public int nCols()
      Description copied from interface: Table
      Gets the number of columns. Columns count from 1.
      Specified by:
      nCols in interface Table
      Returns:
      the number of columns
    • getEntryList

      public List<SparseMatrix.Entry> getEntryList()
      Description copied from interface: SparseMatrix
      Exports the non-zero values in the matrix as a list of SparseMatrix.Entrys. This is useful for converting between the different formats of SparseMatrix.Entry. For example,
      
       // construct matrix using DOK
       DOKSparseMatrix dok = new DOKSparseMatrix(5, 5);
       // ... insert some values to DOK matrix
       // convert to CSR matrix for efficient matrix operations
       CSRSparseMatrix csr = new CSRSparseMatrix(5, 5, dok.getEntryList());
       
      Specified by:
      getEntryList in interface SparseMatrix
      Returns:
      the sparse entries
    • getValueArray

      public SparseMatrix.ValueArray getValueArray()
      Description copied from interface: SparseMatrix
      Exports the non-zero values in the matrix as arrays of row/column indices and values.
      Specified by:
      getValueArray in interface SparseMatrix
      Returns:
      the arrays of indices and values
      See Also:
    • subMatrix

      public LILSparseMatrix subMatrix(int rowFrom, int rowTo, int colFrom, int colTo)
      Description copied from interface: SparseMatrix
      Extracts a sub-matrix given the bounds of row and column indices (inclusive).
      Specified by:
      subMatrix in interface SparseMatrix
      Parameters:
      rowFrom - the beginning row index
      rowTo - the ending row index
      colFrom - the beginning column index
      colTo - the ending column index
      Returns:
      A[rowFrom:rowTo, colFrom:colTo]
    • get

      public double get(int row, int col)
      Description copied from interface: MatrixAccess
      Get the matrix entry at [i,j].
      Specified by:
      get in interface MatrixAccess
      Parameters:
      row - the row index
      col - the column index
      Returns:
      A[i,j]
    • set

      public void set(int row, int col, double value)
      Description copied from interface: MatrixAccess
      Set the matrix entry at [i,j] to a value. This is the only method that may change a matrix.
      Specified by:
      set in interface MatrixAccess
      Parameters:
      row - the row index
      col - the column index
      value - the value to set A[i,j] to
    • getRow

      public SparseVector getRow(int i)
      Description copied from interface: Matrix
      Get the specified row in the matrix as a vector.
      Specified by:
      getRow in interface Matrix
      Parameters:
      i - the row index
      Returns:
      the vector A[i, ]
    • getColumn

      public SparseVector getColumn(int j)
      Description copied from interface: Matrix
      Get the specified column in the matrix as a vector.
      Specified by:
      getColumn in interface Matrix
      Parameters:
      j - the column index
      Returns:
      a vector A[, j]
    • add

      public Matrix add(Matrix that)
      Description copied from interface: MatrixRing
      this + that
      Specified by:
      add in interface AbelianGroup<Matrix>
      Specified by:
      add in interface MatrixRing
      Parameters:
      that - a matrix
      Returns:
      the sum of this and that
    • minus

      public Matrix minus(Matrix that)
      Description copied from interface: MatrixRing
      this - that
      Specified by:
      minus in interface AbelianGroup<Matrix>
      Specified by:
      minus in interface MatrixRing
      Parameters:
      that - a matrix
      Returns:
      the difference between this and that
    • multiply

      public Matrix multiply(Matrix that)
      Description copied from interface: MatrixRing
      this * that
      Specified by:
      multiply in interface MatrixRing
      Specified by:
      multiply in interface Monoid<Matrix>
      Parameters:
      that - a matrix
      Returns:
      the product ofthis and that
    • multiply

      public Vector multiply(Vector v)
      Description copied from interface: Matrix
      Right multiply this matrix, A, by a vector.
      Specified by:
      multiply in interface Matrix
      Parameters:
      v - a vector
      Returns:
      Av, a vector
    • scaled

      public LILSparseMatrix scaled(double c)
      Description copied from interface: Matrix
      Scale this matrix, A, by a constant.
      Specified by:
      scaled in interface Matrix
      Parameters:
      c - a double
      Returns:
      cA
    • opposite

      public LILSparseMatrix opposite()
      Description copied from interface: MatrixRing
      Get the opposite of this matrix.
      Specified by:
      opposite in interface AbelianGroup<Matrix>
      Specified by:
      opposite in interface MatrixRing
      Returns:
      -this
      See Also:
    • t

      public LILSparseMatrix t()
      Description copied from interface: MatrixRing
      Get the transpose of this matrix. This is the involution on the matrix ring.
      Specified by:
      t in interface MatrixRing
      Returns:
      the transpose of this matrix
    • ZERO

      public LILSparseMatrix ZERO()
      Description copied from interface: MatrixRing
      Get a zero matrix that has the same dimension as this matrix.
      Specified by:
      ZERO in interface AbelianGroup<Matrix>
      Specified by:
      ZERO in interface MatrixRing
      Returns:
      the 0 matrix
    • ONE

      public LILSparseMatrix ONE()
      Description copied from interface: MatrixRing
      Get an identity matrix that has the same dimension as this matrix. For a non-square matrix, it zeros out the rows (columns) with index > nCols (nRows).
      Specified by:
      ONE in interface MatrixRing
      Specified by:
      ONE in interface Monoid<Matrix>
      Returns:
      an identity matrix
    • deepCopy

      public LILSparseMatrix deepCopy()
      Description copied from interface: DeepCopyable
      The implementation returns an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
      Specified by:
      deepCopy in interface DeepCopyable
      Specified by:
      deepCopy in interface Matrix
      Returns:
      an independent (deep) copy of the instance
    • toDense

      public DenseMatrix toDense()
      Description copied from interface: Densifiable
      Densify a matrix, i.e., convert a matrix implementation to the standard dense matrix, DenseMatrix.
      Specified by:
      toDense in interface Densifiable
      Returns:
      a matrix representation in DenseMatrix
    • nNonZeros

      public int nNonZeros()
      Description copied from interface: SparseStructure
      Get the number of non-zero entries in the structure.
      Specified by:
      nNonZeros in interface SparseStructure
      Returns:
      the number of non-zero entries in the structure
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object