public class FMatrixSparseTriplet extends java.lang.Object implements FMatrixSparse
FMatrixSparseCSC from scratch is difficult, but if a triplet is first defined then it is much easier.
Inside this class elements are stored in an unsorted list. Adding an element to the list with addItem(int, int, float)
is an O(1) operation but reading a specific element is O(N) operation, making it impractical for operations like
matrix multiplications.| Modifier and Type | Field and Description |
|---|---|
int |
numCols
Number of columns in the matrix
|
int |
numRows
Number of rows in the matrix
|
int |
nz_length
Number of non-zero elements in this matrix
|
IGrowArray |
nz_rowcol
Storage for row and column coordinate for non-zero elements
|
FGrowArray |
nz_value
Storage for value of a non-zero element
|
| Constructor and Description |
|---|
FMatrixSparseTriplet() |
FMatrixSparseTriplet(FMatrixSparseTriplet orig) |
FMatrixSparseTriplet(int numRows,
int numCols,
int initLength) |
| Modifier and Type | Method and Description |
|---|---|
void |
addItem(int row,
int col,
float value)
Adds a triplet of (row,vol,value) to the end of the list.
|
void |
addItemCheck(int row,
int col,
float value)
Adds a triplet of (row,vol,value) to the end of the list and performs a bounds check to make
sure it is a legal value.
|
<T extends Matrix> |
copy()
Creates an exact copy of the matrix
|
<T extends Matrix> |
createLike()
Creates a new matrix with the same shape as this matrix
|
float |
get(int row,
int col)
Searches the list to see if the element at (row,col) has been assigned.
|
int |
getLength() |
int |
getNonZeroLength()
Returns the number of non-zero elements.
|
int |
getNumCols()
Returns the number of columns in this matrix.
|
int |
getNumElements()
Returns the number of elements in this matrix, which is the number of rows
times the number of columns.
|
int |
getNumRows()
Returns the number of rows in this matrix.
|
MatrixType |
getType()
Returns the type of matrix
|
boolean |
isAssigned(int row,
int col)
Is the specified element explicitly assigned a value
|
int |
nz_index(int row,
int col) |
void |
print()
Prints the matrix to standard out using standard formatting.
|
void |
print(java.lang.String format)
Prints the matrix to standard out with the specified formatting.
|
void |
printNonZero()
Prints to standard out the non-zero elements only.
|
void |
remove(int row,
int col)
If the specified element is non-zero it is removed from the structure
|
void |
reset() |
void |
reshape(int numRows,
int numCols)
Changes the number of rows and columns in the matrix.
|
void |
reshape(int numRows,
int numCols,
int arrayLength)
Reshapes the matrix so that it can store a matrix with the specified dimensions and the number of
non-zero elements.
|
void |
set(int row,
int col,
float value)
Sets the element's value at (row,col).
|
void |
set(Matrix original)
Sets this matrix to be identical to the 'original' matrix passed in.
|
void |
shrinkArrays()
Reduces the size of internal data structures to their minimal size.
|
float |
unsafe_get(int row,
int col)
Same as
FMatrix.get(int, int) but does not perform bounds check on input parameters. |
void |
unsafe_set(int row,
int col,
float value)
Same as
set(int, int, float) but does not check to see if row and column are within bounds. |
void |
zero()
Sets all values inside the matrix to zero
|
public IGrowArray nz_rowcol
public FGrowArray nz_value
public int nz_length
public int numRows
public int numCols
public FMatrixSparseTriplet()
public FMatrixSparseTriplet(int numRows,
int numCols,
int initLength)
numRows - Number of rows in the matrixnumCols - Number of columns in the matrixinitLength - Initial maximum length of data array.public FMatrixSparseTriplet(FMatrixSparseTriplet orig)
public void reset()
public void reshape(int numRows,
int numCols)
MatrixSparseMatrixSparse.reshape(int, int, int), but the storage for non-zero elements is
not changedreshape in interface MatrixSparsereshape in interface ReshapeMatrixnumRows - number of rowsnumCols - number of columnspublic void reshape(int numRows,
int numCols,
int arrayLength)
MatrixSparsereshape in interface MatrixSparsenumRows - number of rowsnumCols - number of columnsarrayLength - Array length for storing non-zero elements.public void addItem(int row,
int col,
float value)
Adds a triplet of (row,vol,value) to the end of the list. This is the preferred way to add elements into this array type as it has a runtime complexity of O(1).
One potential problem with using this function instead ofset(int, int, float) is that it does
not check to see if a (row,col) has already been assigned a value. If a (row,col) is defined multiple times
how this is handled is not defined.row - Row the element belongs incol - Column the element belongs invalue - The value of the elementpublic void addItemCheck(int row,
int col,
float value)
row - Row the element belongs incol - Column the element belongs invalue - The value of the elementpublic void set(int row,
int col,
float value)
set in interface FMatrixrow - Matrix element's row index.col - Matrix element's column index.value - value of element.For a faster but less "safe" alternativepublic void unsafe_set(int row,
int col,
float value)
set(int, int, float) but does not check to see if row and column are within bounds.unsafe_set in interface FMatrixrow - Matrix element's row index.col - Matrix element's column index.value - value of element.public int getNumElements()
FMatrixgetNumElements in interface FMatrixpublic float get(int row,
int col)
public float unsafe_get(int row,
int col)
FMatrixFMatrix.get(int, int) but does not perform bounds check on input parameters. This results in about a 25%
speed increase but potentially sacrifices stability and makes it more difficult to track down simple errors.
It is not recommended that this function be used, except in highly optimized code where the bounds are
implicitly being checked.unsafe_get in interface FMatrixrow - Matrix element's row index..col - Matrix element's column index.public int nz_index(int row,
int col)
public int getLength()
public int getNumRows()
MatrixgetNumRows in interface Matrixpublic int getNumCols()
MatrixgetNumCols in interface Matrixpublic <T extends Matrix> T copy()
Matrixpublic <T extends Matrix> T createLike()
MatrixcreateLike in interface Matrixpublic void set(Matrix original)
Matrixpublic void shrinkArrays()
MatrixSparseshrinkArrays in interface MatrixSparsepublic void remove(int row,
int col)
MatrixSparseremove in interface MatrixSparserow - the rowcol - the columnpublic boolean isAssigned(int row,
int col)
MatrixSparseisAssigned in interface MatrixSparserow - the rowcol - the columnpublic void zero()
Matrixzero in interface Matrixzero in interface MatrixSparsepublic int getNonZeroLength()
MatrixSparsegetNonZeroLength in interface MatrixSparsepublic void print()
Matrixpublic void print(java.lang.String format)
Matrixpublic void printNonZero()
MatrixSparseprintNonZero in interface MatrixSparsepublic MatrixType getType()
Matrix