Documentation API of the 'cern.colt.matrix.impl.RCDoubleMatrix2D' Java class

Class RCDoubleMatrix2D

  • All Implemented Interfaces:
    Serializable, Cloneable

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


    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 < 10% on rows with < 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 quickmatrix.assign(0);for (int row=0; row < rows; row++) {   for (int column=0; column < columns; column++) {      if (someCondition) matrix.setQuick(row,column,someValue);   }}// poormatrix.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 * xA.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*BB.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.
    See Also:
    Serialized Form

Warning: You cannot see the full API documentation of this class since the access to the DatMelt documentation for third-party Java classes is denied. Guests can only view jhplot Java API. To view the complete description of this class and its methods, please request the full DataMelt membership.

If you are already a full member, please login to the DataMelt member area before visiting this documentation.