Package org.ejml.data
Class FMatrixSparseTriplet
- java.lang.Object
-
- org.ejml.data.FMatrixSparseTriplet
-
- All Implemented Interfaces:
java.io.Serializable,FMatrix,FMatrixSparse,Matrix,MatrixSparse,ReshapeMatrix
public class FMatrixSparseTriplet extends java.lang.Object implements FMatrixSparse
A sparse matrix format that is designed to act as an intermediate step for other matrix types. ConstructingFMatrixSparseCSCfrom 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 withaddItem(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.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.ejml.data.FMatrixSparse
FMatrixSparse.CoordinateRealValue
-
-
Field Summary
Fields Modifier and Type Field Description intnumColsNumber of columns in the matrixintnumRowsNumber of rows in the matrixintnz_lengthNumber of non-zero elements in this matrixIGrowArraynz_rowcolStorage for row and column coordinate for non-zero elementsFGrowArraynz_valueStorage for value of a non-zero element
-
Constructor Summary
Constructors Constructor Description FMatrixSparseTriplet()FMatrixSparseTriplet(int numRows, int numCols, int initLength)FMatrixSparseTriplet(FMatrixSparseTriplet orig)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddItem(int row, int col, float value)Adds a triplet of (row,vol,value) to the end of the list.voidaddItemCheck(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>
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 shapejava.util.Iterator<FMatrixSparse.CoordinateRealValue>createCoordinateIterator()Creates an iterator which will go through each non-zero value in the sparse matrix.<T extends Matrix>
TcreateLike()Creates a new matrix with the same shape as this matrixfloatget(int row, int col)Searches the list to see if the element at (row,col) has been assigned.intgetLength()intgetNonZeroLength()Returns the number of non-zero elements.intgetNumCols()Returns the number of columns in this matrix.intgetNumElements()Returns the number of elements in this matrix, which is the number of rows times the number of columns.intgetNumRows()Returns the number of rows in this matrix.MatrixTypegetType()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.voidprint(java.lang.String format)Prints the matrix to standard out with the specified formatting.voidprintNonZero()Prints 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, float value)Sets the element's value at (row,col).voidset(Matrix original)Sets this matrix to be identical to the 'original' matrix passed in.voidshrinkArrays()Reduces the size of internal data structures to their minimal size.floatunsafe_get(int row, int col)Same asFMatrix.get(int, int)but does not perform bounds check on input parameters.voidunsafe_set(int row, int col, float value)Same asset(int, int, float)but does not check to see if row and column are within bounds.voidzero()Sets all values inside the matrix to zero
-
-
-
Field Detail
-
nz_rowcol
public IGrowArray nz_rowcol
Storage for row and column coordinate for non-zero elements
-
nz_value
public FGrowArray nz_value
Storage for value of a non-zero element
-
nz_length
public int nz_length
Number of non-zero elements in this matrix
-
numRows
public int numRows
Number of rows in the matrix
-
numCols
public int numCols
Number of columns in the matrix
-
-
Constructor Detail
-
FMatrixSparseTriplet
public FMatrixSparseTriplet()
-
FMatrixSparseTriplet
public FMatrixSparseTriplet(int numRows, int numCols, int initLength)- Parameters:
numRows- Number of rows in the matrixnumCols- Number of columns in the matrixinitLength- Initial maximum length of data array.
-
FMatrixSparseTriplet
public FMatrixSparseTriplet(FMatrixSparseTriplet orig)
-
-
Method Detail
-
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, 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.- 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, 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.- Parameters:
row- Row the element belongs incol- Column the element belongs invalue- The value of the element
-
set
public void set(int row, int col, float 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.- Specified by:
setin interfaceFMatrix- Parameters:
row- Matrix element's row index.col- Matrix element's column index.value- value of element.- See Also:
For a faster but less "safe" alternative
-
unsafe_set
public void unsafe_set(int row, int col, float value)Same asset(int, int, float)but does not check to see if row and column are within bounds.- Specified by:
unsafe_setin interfaceFMatrix- Parameters:
row- Matrix element's row index.col- Matrix element's column index.value- value of element.
-
getNumElements
public int getNumElements()
Description copied from interface:FMatrixReturns the number of elements in this matrix, which is the number of rows times the number of columns.- Specified by:
getNumElementsin interfaceFMatrix- Returns:
- Number of elements in this matrix.
-
get
public float 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.
-
unsafe_get
public float unsafe_get(int row, int col)Description copied from interface:FMatrixSame asFMatrix.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 interfaceFMatrix- Parameters:
row- Matrix element's row index..col- Matrix element's column index.- Returns:
- The specified element's value.
-
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
public <T extends Matrix> T copy()
Description copied from interface:MatrixCreates an exact copy of the matrix
-
createLike
public <T extends Matrix> T createLike()
Description copied from interface:MatrixCreates a new matrix with the same shape as this matrix- Specified by:
createLikein interfaceMatrix
-
create
public <T extends Matrix> T create(int numRows, int numCols)
Description copied from interface:MatrixCreates a new matrix of the same type with the specified shape
-
set
public void set(Matrix original)
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
public void print(java.lang.String format)
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
public MatrixType getType()
Description copied from interface:MatrixReturns the type of matrix
-
createCoordinateIterator
public java.util.Iterator<FMatrixSparse.CoordinateRealValue> createCoordinateIterator()
Description copied from interface:FMatrixSparseCreates 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 interfaceFMatrixSparse- Returns:
- Iterator
-
-