Class RCDoubleMatrix2D

All Implemented Interfaces:
Serializable, Cloneable

public class RCDoubleMatrix2D extends WrapperDoubleMatrix2D
Sparse row-compressed 2-d matrix holding double elements. First see the package summary and javadoc tree view to get the broad picture.

Implementation:

Internally uses the standard sparse row-compressed format, with two important differences that broaden the applicability of this storage format:

  • We use a IntArrayList and DoubleArrayList to hold the column indexes and nonzero values, respectively. This improves set(...) performance, because the standard way of using non-resizable primitive arrays causes excessive memory allocation, garbage collection and array copying. The small downside of this is that set(...,0) does not free memory (The capacity of an arraylist does not shrink upon element removal).
  • Column indexes are kept sorted within a row. This both improves get and set performance on rows with many non-zeros, because we can use a binary search. (Experiments show that this hurts invalid input: '<' 10% on rows with invalid input: '<' 4 nonZeros.)

Note that this implementation is not synchronized.

Memory requirements:

Cells that

  • are never set to non-zero values do not use any memory.
  • switch from zero to non-zero state do use memory.
  • switch back from non-zero to zero state also do use memory. Their memory is not automatically reclaimed (because of the lists vs. arrays). Reclamation can be triggered via trimToSize().

memory [bytes] = 4*rows + 12 * nonZeros.
Where nonZeros = cardinality() is the number of non-zero cells. Thus, a 1000 x 1000 matrix with 1000000 non-zero cells consumes 11.5 MB. The same 1000 x 1000 matrix with 1000 non-zero cells consumes 15 KB.

Time complexity:

Getting a cell value takes time O(log nzr) where nzr is the number of non-zeros of the touched row. This is usually quick, because typically there are only few nonzeros per row. So, in practice, get has expected constant time. Setting a cell value takes worst-case time O(nz) where nzr is the total number of non-zeros in the matrix. This can be extremely slow, but if you traverse coordinates properly (i.e. upwards), each write is done much quicker:

// rather quick
matrix.assign(0);
for (int row=0; row invalid input: '<' rows; row++) {
   for (int column=0; column invalid input: '<' columns; column++) {
      if (someCondition) matrix.setQuick(row,column,someValue);
   }
}

// poor
matrix.assign(0);
for (int row=rows; --row >= 0; ) {
   for (int column=columns; --column >= 0; ) {
      if (someCondition) matrix.setQuick(row,column,someValue);
   }
}
If for whatever reasons you can't iterate properly, consider to create an empty dense matrix, store your non-zeros in it, then call sparse.assign(dense). Under the circumstances, this is still rather quick.

Fast iteration over non-zeros can be done via forEachNonZero(cern.colt.function.IntIntDoubleFunction), which supplies your function with row, column and value of each nonzero. Although the internally implemented version is a bit more sophisticated, here is how a quite efficient user-level matrix-vector multiplication could look like:

// Linear algebraic y = A * x
A.forEachNonZero(
   new cern.colt.function.IntIntDoubleFunction() {
      public double apply(int row, int column, double value) {
         y.setQuick(row,y.getQuick(row) + value * x.getQuick(column));
         return value;
      }
   }
);

Here is how a a quite efficient user-level combined scaling operation could look like:

// Elementwise A = A + alpha*B
B.forEachNonZero(
   new cern.colt.function.IntIntDoubleFunction() {
      public double apply(int row, int column, double value) {
         A.setQuick(row,column,A.getQuick(row,column) + alpha*value);
         return value;
      }
   }
);
Method assign(DoubleMatrix2D,cern.colt.function.DoubleDoubleFunction) does just that if you supply Functions.plusMult(double) as argument.
Version:
0.9, 04/14/2000
See Also:
  • Field Details

  • Constructor Details

    • RCDoubleMatrix2D

      public RCDoubleMatrix2D(double[][] values)
      Constructs a matrix with a copy of the given values. values is required to have the form values[row][column] and have exactly the same number of columns in every row.

      The values are copied. So subsequent changes in values are not reflected in the matrix, and vice-versa.

      Parameters:
      values - The values to be filled into the new matrix.
      Throws:
      IllegalArgumentException - if for any 1 <= row < values.length: values[row].length != values[row-1].length.
    • RCDoubleMatrix2D

      public RCDoubleMatrix2D(int rows, int columns)
      Constructs a matrix with a given number of rows and columns. All entries are initially 0.
      Parameters:
      rows - the number of rows the matrix shall have.
      columns - the number of columns the matrix shall have.
      Throws:
      IllegalArgumentException - if rowsinvalid input: '<'0 || columnsinvalid input: '<'0 || (double)columns*rows > Integer.MAX_VALUE.
  • Method Details

    • assign

      public DoubleMatrix2D assign(double value)
      Sets all cells to the state specified by value.
      Overrides:
      assign in class DoubleMatrix2D
      Parameters:
      value - the value to be filled into the cells.
      Returns:
      this (for convenience only).
    • assign

      public DoubleMatrix2D assign(DoubleFunction function)
      Description copied from class: DoubleMatrix2D
      Assigns the result of a function to each cell; x[row,col] = function(x[row,col]).

      Example:

      matrix = 2 x 2 matrix 
      0.5 1.5      
      2.5 3.5
      
      // change each cell to its sine
      matrix.assign(cern.jet.math.Functions.sin);
      -->
      2 x 2 matrix
      0.479426  0.997495 
      0.598472 -0.350783
      
      For further examples, see the package doc.
      Overrides:
      assign in class DoubleMatrix2D
      Parameters:
      function - a function object taking as argument the current cell's value.
      Returns:
      this (for convenience only).
      See Also:
    • assign

      public DoubleMatrix2D assign(DoubleMatrix2D source)
      Replaces all cell values of the receiver with the values of another matrix. Both matrices must have the same number of rows and columns. If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces as if using an intermediate auxiliary deep copy of other.
      Overrides:
      assign in class DoubleMatrix2D
      Parameters:
      source - the source matrix to copy from (may be identical to the receiver).
      Returns:
      this (for convenience only).
      Throws:
      IllegalArgumentException - if columns() != source.columns() || rows() != source.rows()
    • assign

      public DoubleMatrix2D assign(DoubleMatrix2D y, DoubleDoubleFunction function)
      Description copied from class: DoubleMatrix2D
      Assigns the result of a function to each cell; x[row,col] = function(x[row,col],y[row,col]).

      Example:

      // assign x[row,col] = x[row,col]y[row,col]
      m1 = 2 x 2 matrix 
      0 1 
      2 3
      
      m2 = 2 x 2 matrix 
      0 2 
      4 6
      
      m1.assign(m2, cern.jet.math.Functions.pow);
      -->
      m1 == 2 x 2 matrix
       1   1 
      16 729
      
      For further examples, see the package doc.
      Overrides:
      assign in class DoubleMatrix2D
      Parameters:
      y - the secondary matrix to operate on.
      function - a function object taking as first argument the current cell's value of this, and as second argument the current cell's value of y,
      Returns:
      this (for convenience only).
      See Also:
    • forEachNonZero

      public DoubleMatrix2D forEachNonZero(IntIntDoubleFunction function)
      Description copied from class: DoubleMatrix2D
      Assigns the result of a function to each non-zero cell; x[row,col] = function(x[row,col]). Use this method for fast special-purpose iteration. If you want to modify another matrix instead of this (i.e. work in read-only mode), simply return the input value unchanged. Parameters to function are as follows: first==row, second==column, third==nonZeroValue.
      Overrides:
      forEachNonZero in class DoubleMatrix2D
      Parameters:
      function - a function object taking as argument the current non-zero cell's row, column and value.
      Returns:
      this (for convenience only).
    • getContent

      protected DoubleMatrix2D getContent()
      Returns the content of this matrix if it is a wrapper; or this otherwise. Override this method in wrappers.
      Overrides:
      getContent in class WrapperDoubleMatrix2D
    • getQuick

      public double getQuick(int row, int column)
      Returns the matrix cell value at coordinate [row,column].

      Provided with invalid parameters this method may return invalid objects without throwing any exception. You should only use this method when you are absolutely sure that the coordinate is within bounds. Precondition (unchecked): 0 <= column < columns() invalid input: '&'invalid input: '&' 0 <= row < rows().

      Overrides:
      getQuick in class WrapperDoubleMatrix2D
      Parameters:
      row - the index of the row-coordinate.
      column - the index of the column-coordinate.
      Returns:
      the value at the specified coordinate.
    • insert

      protected void insert(int row, int column, int index, double value)
    • like

      public DoubleMatrix2D like(int rows, int columns)
      Construct and returns a new empty matrix of the same dynamic type as the receiver, having the specified number of rows and columns. For example, if the receiver is an instance of type DenseDoubleMatrix2D the new matrix must also be of type DenseDoubleMatrix2D, if the receiver is an instance of type SparseDoubleMatrix2D the new matrix must also be of type SparseDoubleMatrix2D, etc. In general, the new matrix should have internal parametrization as similar as possible.
      Overrides:
      like in class WrapperDoubleMatrix2D
      Parameters:
      rows - the number of rows the matrix shall have.
      columns - the number of columns the matrix shall have.
      Returns:
      a new empty matrix of the same dynamic type.
    • like1D

      public DoubleMatrix1D like1D(int size)
      Construct and returns a new 1-d matrix of the corresponding dynamic type, entirelly independent of the receiver. For example, if the receiver is an instance of type DenseDoubleMatrix2D the new matrix must be of type DenseDoubleMatrix1D, if the receiver is an instance of type SparseDoubleMatrix2D the new matrix must be of type SparseDoubleMatrix1D, etc.
      Overrides:
      like1D in class WrapperDoubleMatrix2D
      Parameters:
      size - the number of cells the matrix shall have.
      Returns:
      a new matrix of the corresponding dynamic type.
    • remove

      protected void remove(int row, int index)
    • setQuick

      public void setQuick(int row, int column, double value)
      Sets the matrix cell at coordinate [row,column] to the specified value.

      Provided with invalid parameters this method may access illegal indexes without throwing any exception. You should only use this method when you are absolutely sure that the coordinate is within bounds. Precondition (unchecked): 0 <= column < columns() invalid input: '&'invalid input: '&' 0 <= row < rows().

      Overrides:
      setQuick in class WrapperDoubleMatrix2D
      Parameters:
      row - the index of the row-coordinate.
      column - the index of the column-coordinate.
      value - the value to be filled into the specified cell.
    • trimToSize

      public void trimToSize()
      Description copied from class: AbstractMatrix
      Releases any superfluous internal memory. An application can use this operation to minimize the storage of the receiver.

      This default implementation does nothing. Override this method if necessary.

      Overrides:
      trimToSize in class AbstractMatrix
    • zMult

      public DoubleMatrix1D zMult(DoubleMatrix1D y, DoubleMatrix1D z, double alpha, double beta, boolean transposeA)
      Description copied from class: DoubleMatrix2D
      Linear algebraic matrix-vector multiplication; z = alpha * A * y + beta*z. z[i] = alpha*Sum(A[i,j] * y[j]) + beta*z[i], i=0..A.rows()-1, j=0..y.size()-1. Where A == this.
      Note: Matrix shape conformance is checked after potential transpositions.
      Overrides:
      zMult in class DoubleMatrix2D
      Parameters:
      y - the source vector.
      z - the vector where results are to be stored. Set this parameter to null to indicate that a new result vector shall be constructed.
      Returns:
      z (for convenience only).
    • zMult

      public DoubleMatrix2D zMult(DoubleMatrix2D B, DoubleMatrix2D C, double alpha, double beta, boolean transposeA, boolean transposeB)
      Description copied from class: DoubleMatrix2D
      Linear algebraic matrix-matrix multiplication; C = alpha * A x B + beta*C. C[i,j] = alpha*Sum(A[i,k] * B[k,j]) + beta*C[i,j], k=0..n-1.
      Matrix shapes: A(m x n), B(n x p), C(m x p).
      Note: Matrix shape conformance is checked after potential transpositions.
      Overrides:
      zMult in class DoubleMatrix2D
      Parameters:
      B - the second source matrix.
      C - the matrix where results are to be stored. Set this parameter to null to indicate that a new result matrix shall be constructed.
      Returns:
      C (for convenience only).