Class SparseMatrixUtils
- java.lang.Object
-
- dev.nm.algebra.linear.matrix.doubles.matrixtype.sparse.SparseMatrixUtils
-
public final class SparseMatrixUtils extends Object
These are the utility functions forSparseMatrix
.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int[]
countEntriesInEachColumn(List<SparseMatrix.Entry> entries, int nCols)
Counts the number of entries in each column.static int[]
countEntriesInEachRow(List<SparseMatrix.Entry> entries, int nRows)
Counts the number of entries in each row.static boolean
equals(SparseMatrix A, SparseMatrix B)
Checks if two SparseMatrixs are equal.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.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.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.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.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.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.static SparseMatrix.Entry[]
toEntryArray(int[] rowIndices, int[] columnIndices, double[] values)
static List<SparseMatrix.Entry>
toEntryList(int[] rowIndices, int[] columnIndices, double[] values)
static String
toString(SparseMatrix A)
Returns a string representation of a 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 fromentries[result[k-1]]
toentries[result[k] - 1]
are entries in row k.- Parameters:
entries
- the sparse matrix entries to be sortednRows
- 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 rangecheckDuplicate
-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 fromentries[result[k-1]]
toentries[result[k] - 1]
are entries in row k.- Parameters:
entries
- the sparse matrix entries to be sortednRows
- 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 rangecheckDuplicate
-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 fromentries[result[k-1]]
toentries[result[k] - 1]
are entries in row k.- Parameters:
entries
- the sparse matrix entries to be sortednRows
- 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 fromentries[result[k-1]]
toentries[result[k] - 1]
are entries in row k.- Parameters:
entries
- the sparse matrix entries to be sortednRows
- 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 fromentries[result[k-1]]
toentries[result[k] - 1]
are entries in column k.- Parameters:
entries
- the sparse matrix entries to be sortednCols
- 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 fromentries[result[k-1]]
toentries[result[k] - 1]
are entries in column k.- Parameters:
entries
- the sparse matrix entries to be sortednCols
- 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 entriesnRows
- 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 entriesnCols
- 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 matrixB
- another sparse matrix- Returns:
true
ifA
andB
are equal
-
-