Package org.ejml.data
Class DMatrixSparseTriplet
java.lang.Object
org.ejml.data.DMatrixSparseTriplet
- All Implemented Interfaces:
Serializable,DMatrix,DMatrixSparse,Matrix,MatrixSparse,ReshapeMatrix
A sparse matrix format that is designed to act as an intermediate step for other matrix types. Constructing
DMatrixSparseCSC 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, double)
is an O(1) operation but reading a specific element is O(N) operation, making it impractical for operations like
matrix multiplications.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.ejml.data.DMatrixSparse
DMatrixSparse.CoordinateRealValue -
Field Summary
FieldsModifier and TypeFieldDescriptionintNumber of columns in the matrixintNumber of rows in the matrixintNumber of non-zero elements in this matrixStorage for row and column coordinate for non-zero elementsStorage for value of a non-zero element -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddItem(int row, int col, double value) Adds a triplet of (row,vol,value) to the end of the list.voidaddItemCheck(int row, int col, double 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>
Tcopy()Creates an exact copy of the matrix<T extends Matrix>
Tcreate(int numRows, int numCols) Creates a new matrix of the same type with the specified shapeCreates an iterator which will go through each non-zero value in the sparse matrix.<T extends Matrix>
TCreates a new matrix with the same shape as this matrixdoubleget(int row, int col) Searches the list to see if the element at (row,col) has been assigned.doubleget(int row, int col, double fallBackValue) Searches the list to see if the element at (row,col) has been assigned.intintReturns the number of non-zero elements.intReturns the number of columns in this matrix.intReturns the number of rows in this matrix.getType()Returns the type of matrixbooleanisAssigned(int row, int col) Is the specified element explicitly assigned a valueintnz_index(int row, int col) voidprint()Prints the matrix to standard out using standard formatting.voidPrints the matrix to standard out with the specified formatting.voidPrints to standard out the non-zero elements only.voidremove(int row, int col) If the specified element is non-zero it is removed from the structurevoidreset()voidreshape(int numRows, int numCols) Changes the number of rows and columns in the matrix.voidreshape(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.voidset(int row, int col, double value) Sets the element's value at (row,col).voidSets this matrix to be identical to the 'original' matrix passed in.voidReduces the size of internal data structures to their minimal size.doubleunsafe_get(int row, int col) Same asDMatrix.get(int, int)but does not perform bounds check on input parameters.doubleunsafe_get(int row, int col, double fallBackValue) Same asDMatrixSparse.get(int, int, double)but does not perform bounds check on input parameters.voidunsafe_set(int row, int col, double value) Same asset(int, int, double)but does not check to see if row and column are within bounds.voidzero()Sets all values inside the matrix to zeroMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.ejml.data.DMatrix
getNumElements
-
Field Details
-
nz_rowcol
Storage for row and column coordinate for non-zero elements -
nz_value
Storage for value of a non-zero element -
nz_length
public int nz_lengthNumber of non-zero elements in this matrix -
numRows
public int numRowsNumber of rows in the matrix -
numCols
public int numColsNumber of columns in the matrix
-
-
Constructor Details
-
DMatrixSparseTriplet
public DMatrixSparseTriplet() -
DMatrixSparseTriplet
public DMatrixSparseTriplet(int numRows, int numCols, int arrayLength) - Parameters:
numRows- Number of rows in the matrixnumCols- Number of columns in the matrixarrayLength- Initial maximum length of data array.
-
DMatrixSparseTriplet
-
-
Method Details
-
reset
public void reset() -
reshape
public void reshape(int numRows, int numCols) Description copied from interface:MatrixSparseChanges the number of rows and columns in the matrix. The graph structure is flushed and the matrix will be empty/all zeros. Similar toMatrixSparse.reshape(int, int, int), but the storage for non-zero elements is not changed- Specified by:
reshapein interfaceMatrixSparse- Specified by:
reshapein interfaceReshapeMatrix- Parameters:
numRows- number of rowsnumCols- number of columns
-
reshape
public void reshape(int numRows, int numCols, int arrayLength) Description copied from interface:MatrixSparseReshapes the matrix so that it can store a matrix with the specified dimensions and the number of non-zero elements. The reshaped matrix will be empty.- Specified by:
reshapein interfaceMatrixSparse- Parameters:
numRows- number of rowsnumCols- number of columnsarrayLength- Array length for storing non-zero elements.
-
addItem
public void addItem(int row, int col, double 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, double)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.- Parameters:
row- Row the element belongs incol- Column the element belongs invalue- The value of the element
-
addItemCheck
public void addItemCheck(int row, int col, double 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.- Parameters:
row- Row the element belongs incol- Column the element belongs invalue- The value of the element- See Also:
-
set
public void set(int row, int col, double value) Sets the element's value at (row,col). It first checks to see if the element already has a value and if it does that value is changed. As a result this operation is O(N), where N is the number of elements in the matrix. -
unsafe_set
public void unsafe_set(int row, int col, double value) Same asset(int, int, double)but does not check to see if row and column are within bounds.- Specified by:
unsafe_setin interfaceDMatrix- Parameters:
row- Matrix element's row index.col- Matrix element's column index.value- value of element.
-
get
public double get(int row, int col) Searches the list to see if the element at (row,col) has been assigned. The worst case runtime for this operation is O(N), where N is the number of elements in the matrix. -
get
public double get(int row, int col, double fallBackValue) Searches the list to see if the element at (row,col) has been assigned. The worst case runtime for this operation is O(N), where N is the number of elements in the matrix.- Specified by:
getin interfaceDMatrixSparse- Parameters:
row- Matrix element's row index.col- Matrix element's column index.fallBackValue- Value to return, if the element is not assigned- Returns:
- Value at (row,col) or the fallBackValue, if the element is not assigned.
-
unsafe_get
public double unsafe_get(int row, int col) Description copied from interface:DMatrixSame asDMatrix.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.- Specified by:
unsafe_getin interfaceDMatrix- Parameters:
row- Matrix element's row index..col- Matrix element's column index.- Returns:
- The specified element's value.
-
unsafe_get
public double unsafe_get(int row, int col, double fallBackValue) Description copied from interface:DMatrixSparseSame asDMatrixSparse.get(int, int, double)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.- Specified by:
unsafe_getin interfaceDMatrixSparse- Parameters:
row- Matrix element's row index..col- Matrix element's column index.fallBackValue- Value to return, if the matrix element is not assigned- Returns:
- The specified element's value or the fallBackValue, if the element is not assigned.
-
nz_index
public int nz_index(int row, int col) -
getLength
public int getLength() -
getNumRows
public int getNumRows()Description copied from interface:MatrixReturns the number of rows in this matrix.- Specified by:
getNumRowsin interfaceMatrix- Returns:
- Number of rows.
-
getNumCols
public int getNumCols()Description copied from interface:MatrixReturns the number of columns in this matrix.- Specified by:
getNumColsin interfaceMatrix- Returns:
- Number of columns.
-
copy
Description copied from interface:MatrixCreates an exact copy of the matrix -
createLike
Description copied from interface:MatrixCreates a new matrix with the same shape as this matrix- Specified by:
createLikein interfaceMatrix
-
create
Description copied from interface:MatrixCreates a new matrix of the same type with the specified shape -
setTo
Description copied from interface:MatrixSets this matrix to be identical to the 'original' matrix passed in. -
shrinkArrays
public void shrinkArrays()Description copied from interface:MatrixSparseReduces the size of internal data structures to their minimal size. No information is lost but the arrays will change- Specified by:
shrinkArraysin interfaceMatrixSparse
-
remove
public void remove(int row, int col) Description copied from interface:MatrixSparseIf the specified element is non-zero it is removed from the structure- Specified by:
removein interfaceMatrixSparse- Parameters:
row- the rowcol- the column
-
isAssigned
public boolean isAssigned(int row, int col) Description copied from interface:MatrixSparseIs the specified element explicitly assigned a value- Specified by:
isAssignedin interfaceMatrixSparse- Parameters:
row- the rowcol- the column- Returns:
- true if it has been assigned a value or false if not
-
zero
public void zero()Description copied from interface:MatrixSets all values inside the matrix to zero- Specified by:
zeroin interfaceMatrix- Specified by:
zeroin interfaceMatrixSparse
-
getNonZeroLength
public int getNonZeroLength()Description copied from interface:MatrixSparseReturns the number of non-zero elements.- Specified by:
getNonZeroLengthin interfaceMatrixSparse
-
print
public void print()Description copied from interface:MatrixPrints the matrix to standard out using standard formatting. This is the same as calling print("%e") -
print
Description copied from interface:MatrixPrints the matrix to standard out with the specified formatting. -
printNonZero
public void printNonZero()Description copied from interface:MatrixSparsePrints to standard out the non-zero elements only.- Specified by:
printNonZeroin interfaceMatrixSparse
-
getType
Description copied from interface:MatrixReturns the type of matrix -
createCoordinateIterator
Description copied from interface:DMatrixSparseCreates an iterator which will go through each non-zero value in the sparse matrix. Order is not defined and is implementation specific- Specified by:
createCoordinateIteratorin interfaceDMatrixSparse
-