Class SparseMatrixUtils


  • public final class SparseMatrixUtils
    extends Object
    These are the utility functions for SparseMatrix.
    • Method Detail

      • toEntryList

        public static List<SparseMatrix.Entry> toEntryList​(int[] rowIndices,
                                                           int[] columnIndices,
                                                           double[] values)
      • toEntryArray

        public static SparseMatrix.Entry[] toEntryArray​(int[] rowIndices,
                                                        int[] columnIndices,
                                                        double[] values)
      • sortInRowColumnOrder

        public static int[] sortInRowColumnOrder​(List<SparseMatrix.Entry> entries,
                                                 int nRows,
                                                 int nCols,
                                                 boolean checkRange,
                                                 boolean checkDuplicate)
        Sorts a list of sparse matrix entries in row-column order in linear time. The returned array contains the start indices of all rows in the sorted entries. For example, result[0] contains the start index of the first row, result[1] contains the start index of the second row, etc. That means, the entries from entries[result[k-1]] to entries[result[k] - 1] are entries in row k.
        Parameters:
        entries - the sparse matrix entries to be sorted
        nRows - the number of rows in the matrix (or the maximum possible row index)
        nCols - the number of columns in the matrix (or the maximum possible column index)
        checkRange - true if entry coordinates should be checked against range
        checkDuplicate - true if duplicates are NOT allowed
        Returns:
        the start indices of rows in the sorted entries
      • sortInRowColumnOrder

        public static int[] sortInRowColumnOrder​(SparseMatrix.Entry[] entries,
                                                 int nRows,
                                                 int nCols,
                                                 boolean checkRange,
                                                 boolean checkDuplicate)
        Sorts an array of sparse matrix entries in row-column order in linear time. The returned array contains the start indices of all rows in the sorted entries. For example, result[0] contains the start index of the first row, result[1] contains the start index of the second row, etc. That means, the entries from entries[result[k-1]] to entries[result[k] - 1] are entries in row k.
        Parameters:
        entries - the sparse matrix entries to be sorted
        nRows - the number of rows of the matrix (or the maximum possible row index)
        nCols - the number of columns of the matrix (or the maximum possible column index)
        checkRange - true if entry coordinates should be checked against range
        checkDuplicate - true if duplicates are NOT allowed
        Returns:
        the start indices of rows in the sorted entries
      • sortInRowOrder

        public static int[] sortInRowOrder​(List<SparseMatrix.Entry> entries,
                                           int nRows)
        Sorts a list of sparse matrix entries in row order (column indices in the same row can be in arbitrary order) in linear time. The returned array contains the start indices of all rows in the sorted entries. For example, result[0] contains the start index of the first row, result[1] contains the start index of the second row, etc. That means, the entries from entries[result[k-1]] to entries[result[k] - 1] are entries in row k.
        Parameters:
        entries - the sparse matrix entries to be sorted
        nRows - the number of rows of the matrix (or the maximum row index)
        Returns:
        the start indices of rows in the sorted entries
      • sortInRowOrder

        public static int[] sortInRowOrder​(SparseMatrix.Entry[] entries,
                                           int nRows)
        Sorts an array of sparse matrix entries in row order (column indices in the same row can be in arbitrary order) in linear time. The returned array contains the start indices of all rows in the sorted entries. For example, result[0] contains the start index of the first row, result[1] contains the start index of the second row, etc. That means, the entries from entries[result[k-1]] to entries[result[k] - 1] are entries in row k.
        Parameters:
        entries - the sparse matrix entries to be sorted
        nRows - the number of rows of the matrix (or the maximum row index)
        Returns:
        the start indices of rows in the sorted entries
      • sortInColumnOrder

        public static int[] sortInColumnOrder​(List<SparseMatrix.Entry> entries,
                                              int nCols)
        Sorts a list of sparse matrix entries in column order (row indices in the same row can be in arbitrary order) in linear time. The returned array contains the start indices of all columns in the sorted entries. For example, result[0] contains the start index of the first column, result[1] contains the start index of the second column, etc. That means, the entries from entries[result[k-1]] to entries[result[k] - 1] are entries in column k.
        Parameters:
        entries - the sparse matrix entries to be sorted
        nCols - the number of columns of the matrix (or the maximum column index)
        Returns:
        the start indices of columns in the sorted entries
      • sortInColumnOrder

        public static int[] sortInColumnOrder​(SparseMatrix.Entry[] entries,
                                              int nCols)
        Sorts an array of sparse matrix entries in column order (row indices in the same row can be in arbitrary order) in linear time. The returned array contains the start indices of all columns in the sorted entries. For example, result[0] contains the start index of the first column, result[1] contains the start index of the second column, etc. That means, the entries from entries[result[k-1]] to entries[result[k] - 1] are entries in column k.
        Parameters:
        entries - the sparse matrix entries to be sorted
        nCols - the number of columns of the matrix (or the maximum column index)
        Returns:
        the start indices of columns in the sorted entries
      • countEntriesInEachRow

        public static int[] countEntriesInEachRow​(List<SparseMatrix.Entry> entries,
                                                  int nRows)
        Counts the number of entries in each row. Note: Row index starts from 1, therefore the zeroth element in the returned array is always 0.
        Parameters:
        entries - the sparse matrix entries
        nRows - the number of rows of the matrix (or the maximum row index)
        Returns:
        the counts (i.e., counts[1] stores the number of entries in the first row)
      • countEntriesInEachColumn

        public static int[] countEntriesInEachColumn​(List<SparseMatrix.Entry> entries,
                                                     int nCols)
        Counts the number of entries in each column. Note: Column index starts from 1, therefore the zeroth element in the returned array is always 0.
        Parameters:
        entries - the sparse matrix entries
        nCols - the number of columns of the matrix (or the maximum column index)
        Returns:
        the counts (i.e., counts[1] stores the number of entries in the first column)
      • toString

        public static String toString​(SparseMatrix A)
        Returns a string representation of a SparseMatrix.
        Parameters:
        A - the sparse matrix
        Returns:
        the String representation
      • equals

        public static boolean equals​(SparseMatrix A,
                                     SparseMatrix B)
        Checks if two SparseMatrixs are equal.
        Parameters:
        A - one sparse matrix
        B - another sparse matrix
        Returns:
        true if A and B are equal