Class SparseMatrix

All Implemented Interfaces:
Matrix, Serializable

public class SparseMatrix extends AbstractMatrix implements Serializable
Sparse matrix implementation in CSC format. This implementation rely on a native library which is a wrapper around KLU module of SuiteSparse project.
Author:
Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
See Also:
  • Constructor Details

    • SparseMatrix

      public SparseMatrix(int rowCount, int columnCount, int[] columnStart, int[] rowIndices, double[] values)
      Create a sparse matrix from its internal structure vectors. This constructor is only called on C++ side.
      Parameters:
      rowCount - row count
      columnCount - column count
      columnStart - column start vector
      rowIndices - row indices vector
      values - value vector
  • Method Details

    • getRgrowthThreshold

      public double getRgrowthThreshold()
    • setRgrowthThreshold

      public void setRgrowthThreshold(double rgrowthThreshold)
    • getColumnStart

      public int[] getColumnStart()
      Get columm start index vector.
      Returns:
      columm start index vector
    • getRowIndices

      public int[] getRowIndices()
      Get row index vector.
      Returns:
      row index vector
    • getValues

      public double[] getValues()
      Get non zero value vector.
      Returns:
      non zero value vector
    • getRowCount

      public int getRowCount()
      Description copied from interface: Matrix
      Get row count.
      Specified by:
      getRowCount in interface Matrix
      Returns:
      row count
    • getColumnCount

      public int getColumnCount()
      Description copied from interface: Matrix
      Get column count.
      Specified by:
      getColumnCount in interface Matrix
      Returns:
      column count
    • set

      public void set(int i, int j, double value)
      Set value at row i and column j.

      As sparse matrix is stored in CSC format. Columns must be filled in ascending order but values inside a column may be filled in any order.

      Specified by:
      set in interface Matrix
      Parameters:
      i - row index
      j - column index
      value - the value to set at row i and column j
      Throws:
      MatrixException - if values are filled in wrong order.
    • add

      public void add(int i, int j, double value)
      Add value at row i and column j.

      As sparse matrix is stored in CSC format. Columns must be filled in ascending order but values inside a column may be filled in any order.

      Specified by:
      add in interface Matrix
      Parameters:
      i - row index
      j - column index
      value - the value to add at row i and column j
      Throws:
      MatrixException - if values are filled in wrong order.
    • addAndGetElement

      public Matrix.Element addAndGetElement(int i, int j, double value)
      Add value at row i and column j and get an #Element to later update the element.

      As sparse matrix is stored in CSC format. Columns must be filled in ascending order but values inside a column may be filled in any order.

      Specified by:
      addAndGetElement in interface Matrix
      Parameters:
      i - row index
      j - column index
      value - the value to add at row i and column j
      Returns:
      an element at row i and column j
      Throws:
      MatrixException - if values are filled in wrong order.
    • addAndGetIndex

      public int addAndGetIndex(int i, int j, double value)
      Description copied from interface: Matrix
      Add value at row i and column j and get an element index to later update the element.
      Specified by:
      addAndGetIndex in interface Matrix
      Parameters:
      i - row index
      j - column index
      value - the value to add at row i and column j
      Returns:
      an element index corresponding to row i and column j
    • setAtIndex

      public void setAtIndex(int index, double value)
      Description copied from interface: Matrix
      Set value at element index index.
      Specified by:
      setAtIndex in interface Matrix
      Parameters:
      index - element index
      value - the value to set at element index index
    • setQuickAtIndex

      public void setQuickAtIndex(int index, double value)
      Description copied from interface: Matrix
      Set value at element index index without doing any bound checking.
      Specified by:
      setQuickAtIndex in interface Matrix
      Parameters:
      index - element index
      value - the value to set at element index index
    • addAtIndex

      public void addAtIndex(int index, double value)
      Description copied from interface: Matrix
      Add value at element index index.
      Specified by:
      addAtIndex in interface Matrix
      Parameters:
      index - element index
      value - the value to add at element index index
    • addQuickAtIndex

      public void addQuickAtIndex(int index, double value)
      Description copied from interface: Matrix
      Add value at element index index without doing any bound checking.
      Specified by:
      addQuickAtIndex in interface Matrix
      Parameters:
      index - element index
      value - the value to add at element index index
    • reset

      public void reset()
      Description copied from interface: Matrix
      Fill matrix with zeros.
      Specified by:
      reset in interface Matrix
    • decomposeLU

      public LUDecomposition decomposeLU()
      Description copied from interface: Matrix
      Get LU decomposition utility class for this matrix.
      Specified by:
      decomposeLU in interface Matrix
      Returns:
      LU decomposition utility class for this matrix
    • times

      public SparseMatrix times(SparseMatrix other)
    • times

      public SparseMatrix times(SparseMatrix other, double scalar)
    • times

      public Matrix times(Matrix other, double scalar)
      Description copied from interface: Matrix
      Multiply the matrix by another one and by a scalar (this*other*scalar). The resulting matrix has the same implementation as this matrix.
      Specified by:
      times in interface Matrix
      Parameters:
      other - the other matrix
      scalar - a scalar to multiply the result matrix
      Returns:
      the result of the multiplication of this matrix by the other one
    • add

      public SparseMatrix add(SparseMatrix other, double alpha, double beta)
    • add

      public Matrix add(Matrix other, double alpha, double beta)
      Description copied from interface: Matrix
      Addition the matrix with another one (alpha * this + beta * other). The resulting matrix has the same implementation as this matrix.
      Specified by:
      add in interface Matrix
      Parameters:
      other - the other matrix
      alpha - a scalar to multiply this matrix
      beta - a scalar to multiply other matrix
      Returns:
      the result of the addition of this matrix and the other one
    • iterateNonZeroValue

      public void iterateNonZeroValue(Matrix.ElementHandler handler)
      Description copied from interface: Matrix
      Iterate over non zero values of the matrix. At each non zero value Matrix.ElementHandler.onElement(int, int, double) is called.
      Specified by:
      iterateNonZeroValue in interface Matrix
      Parameters:
      handler - the element handler
    • iterateNonZeroValueOfColumn

      public void iterateNonZeroValueOfColumn(int j, Matrix.ElementHandler handler)
      Description copied from interface: Matrix
      Iterate over non zero values of the j column of the matrix. At each non zero value Matrix.ElementHandler.onElement(int, int, double) is called.
      Specified by:
      iterateNonZeroValueOfColumn in interface Matrix
      Parameters:
      j - column index
      handler - the element handler
    • toDense

      public DenseMatrix toDense()
      Description copied from interface: Matrix
      Copy this matrix using a dense implementation. If already a dense matrix, this method is allowed to return this.
      Specified by:
      toDense in interface Matrix
      Returns:
      a copy of the matrix with a dense implementation.
    • toSparse

      public SparseMatrix toSparse()
      Description copied from interface: Matrix
      Copy this matrix using a sparse implementation. If already a sparse matrix, this method is allowed to return this.
      Specified by:
      toSparse in interface Matrix
      Returns:
      a copy of the matrix with a sparse implementation.
    • to

      public Matrix to(MatrixFactory factory)
      Description copied from interface: Matrix
      Copy this matrix using another implementation. If already with the right implementation, this method is allowed to return this.
      Specified by:
      to in interface Matrix
      Parameters:
      factory - a matrix factory to create the copy.
      Returns:
      a copy of the matrix
    • getValueCount

      public int getValueCount()
      Description copied from class: AbstractMatrix
      Get value count.
      Specified by:
      getValueCount in class AbstractMatrix
      Returns:
      the value count
    • transpose

      public SparseMatrix transpose()
      Description copied from interface: Matrix
      Calculate the transposed matrix.
      Specified by:
      transpose in interface Matrix
      Returns:
      the transposed matrix
    • print

      public void print(PrintStream out)
      Description copied from interface: Matrix
      Print the matrix to a stream.
      Specified by:
      print in interface Matrix
      Parameters:
      out - the stream
    • print

      public void print(PrintStream out, List<String> rowNames, List<String> columnNames)
      Description copied from interface: Matrix
      Print the matrix to a stream. Row and column names are also printed to facilitate debugging.
      Specified by:
      print in interface Matrix
      Parameters:
      out - the stream
      rowNames - row names
      columnNames - column names
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

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

      public void write(OutputStream outputStream)
    • read

      public static SparseMatrix read(InputStream inputStream)
    • write

      public void write(Path file)
    • read

      public static SparseMatrix read(Path file)