Class AbstractCooSemiringMatrix<T extends AbstractCooSemiringMatrix<T,U,V,W>,U extends AbstractDenseSemiringMatrix<U,?,W>,V extends AbstractCooSemiringVector<V,?,T,U,W>,W extends Semiring<W>>
- Type Parameters:
T
- Type of this sparse COO matrix.U
- Type of dense matrix which is similar toT
.V
- Type of sparse COO vector which is similar toT
.W
- Type of the semiring element in this matrix.
- All Implemented Interfaces:
Serializable
,MatrixMixin<T,
,U, V, W> SemiringTensorMixin<T,
,U, W> TensorOverSemiring<T,
U, W[], W>
- Direct Known Subclasses:
AbstractCooRingMatrix
,CooSemiringMatrix
A sparse matrix stored in coordinate list (COO) format. The AbstractTensor.data
of this COO vector are
elements of a Semiring
.
The non-zero data
and non-zero indices of a COO matrix are mutable but the AbstractTensor.shape
and total number of non-zero data is fixed.
Sparse matrices allow for the efficient storage of and ops on matrices that contain many zero values.
COO matrices are optimized for hyper-sparse matrices (i.e. matrices which contain almost all zeros relative to the size of the matrix).
COO Representation:
A sparse COO matrix is stored as:- The full
shape
of the matrix. - The non-zero
AbstractTensor.data
of the matrix. All other data in the matrix are assumed to be zero. Zero values can also explicitly be stored inAbstractTensor.data
. - The
row indices
of the non-zero values in the sparse matrix. - The
column indices
of the non-zero values in the sparse matrix.
Note: many ops assume that the data of the COO matrix are sorted lexicographically by the row and column indices. (i.e.) by row indices first then column indices. However, this is not explicitly verified but any ops implemented in this class will preserve the lexicographical sorting.
If indices need to be sorted, call sortIndices()
.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal int[]
column indices for non-zero value of this sparse COO matrix.final int
Number of non-zero data in this COO matrix.final int
The number of columns in this matrix.final int
The number of rows in this matrix.final int[]
Row indices for non-zero value of this sparse COO matrix.final double
The sparsity of this matrix.protected W
The zero element for the semiring that this tensor's elements belong to.Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractCooSemiringMatrix
(Shape shape, W[] data, int[] rowIndices, int[] colIndices) Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.protected
AbstractCooSemiringMatrix
(Shape shape, W[] data, int[] rowIndices, int[] colIndices, Object dummy) Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape. -
Method Summary
Modifier and TypeMethodDescriptionComputes the element-wise sum between two tensors of the same shape.int[]
argmax()
Finds the indices of the maximum value in this tensor.int[]
argmin()
Finds the indices of the minimum value in this tensor.Stacks matrices along rows.Augments a vector to this matrix.coalesce()
Coalesces this sparse COO matrix.coalesce
(BinaryOperator<W> aggregator) Coalesces this sparse COO matrix.copy()
Creates a deep copy of this tensor.int
Gets the length of the data array which backs this matrix.double
density()
Gets the density of this matrix as a decimal percentage.Drops any explicit zeros in this sparse COO matrix.Computes the element-wise multiplication of two tensors of the same shape.flatten()
Flattens this matrix to a single row.flatten
(int axis) Flattens a tensor along the specified axis.get
(int... index) Gets the element of this tensor at the specified index.get
(int row, int col) Gets the element of this matrix at this specifiedrow
andcol
.getCol
(int colIdx, int start, int stop) Gets a range of a column of this matrix.getDiag
(int diagOffset) Gets the elements of this matrix along the specified diagonal.getRow
(int rowIdx, int start, int stop) Gets a range of a row of this matrix.getSlice
(int rowStart, int rowEnd, int colStart, int colEnd) Gets a specified slice of this matrix.getTriL
(int diagOffset) Extracts the lower-triangular portion of this matrix with a specified diagonal offset.getTriU
(int diagOffset) Extracts the upper-triangular portion of this matrix with a specified diagonal offset.Gets the zero element for the semiring of this tensor.H()
Computes the Hermitian transpose of this matrix.boolean
Checks if a matrix is Hermitian.boolean
isI()
Checks if this matrix is the identity matrix.boolean
Checks if this matrix is orthogonal.boolean
Checks if a matrix is symmetric.boolean
isTriL()
Checks if this matrix is lower triangular.boolean
isTriU()
Checks if this matrix is upper triangular.abstract AbstractCsrSemiringMatrix
<?, U, V, W> makeLikeCsrMatrix
(Shape shape, W[] entries, int[] rowPointers, int[] colIndices) Constructs a sparse CSR matrix of a similar type to this sparse COO matrix.abstract U
makeLikeDenseTensor
(Shape shape, W[] entries) Constructs a dense tensor with the specifiedshape
anddata
which is a similar type to this sparse tensor.abstract T
Constructs a COO matrix with the specified shape, non-zero data, and non-zero indices.abstract T
makeLikeTensor
(Shape shape, W[] entries, int[] rowIndices, int[] colIndices) Constructs a sparse COO tensor of the same type as this tensor with the specified non-zero data and indices.abstract V
makeLikeVector
(Shape shape, W[] entries, int[] indices) Constructs a sparse COO vector of a similar type to this COO matrix.max()
Finds the maximum value in this tensor.min()
Finds the minimum value in this tensor.Computes the matrix multiplication between two matrices.multTranspose
(T b) Multiplies this matrix with the transpose of theb
tensor as if bythis.mult(b.T())
.int
numCols()
Gets the number of columns in this matrix.int
numRows()
Gets the number of rows in this matrix.removeCol
(int colIndex) Removes a specified column from this matrix.removeCols
(int... colIdxs) Removes a specified set of columns from this matrix.removeRow
(int rowIndex) Removes a specified row from this matrix.removeRows
(int... rowIdxs) Removes a specified set of rows from this matrix.Copies and reshapes this tensor.Sets the element of this tensor at the specified indices.Sets an index of this matrix to the specified value.Sets a column of this matrix at the given index to the specified vector.Sets a specified row of this matrix to a vector.setSliceCopy
(T values, int rowStart, int colStart) Creates a copy of this matrix and sets a slice of the copy to the specified values.void
setZeroElement
(W zeroElement) Sets the zero element for the semiring of this tensor.Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index.double
sparsity()
Gets the sparsity of this matrix as a decimal percentage.Stacks matrices along columns.swapCols
(int colIndex1, int colIndex2) Swaps specified columns in the matrix.swapRows
(int rowIndex1, int rowIndex2) Swaps specified rows in the matrix.T()
Computes the transpose of a tensor by exchanging the first and last axes of this tensor.T
(int... axes) Computes the transpose of this tensor.T
(int axis1, int axis2) Computes the transpose of a tensor by exchangingaxis1
andaxis2
.tensorTr
(int axis1, int axis2) Computes the generalized trace of this tensor along the specified axes.toCsr()
Converts this sparse COO matrix to an equivalent sparse CSR matrix.toDense()
Converts this sparse COO matrix to an equivalent dense matrix.abstract AbstractCooSemiringTensor
<?, ?, W> toTensor()
Converts this matrix to an equivalent rank 2 tensor.abstract AbstractCooSemiringTensor
<?, ?, W> Converts this matrix to an equivalent tensor with the specified shape.toVector()
Converts this sparse CSR matrix to an equivalent vector.tr()
Computes the trace of this matrix.Methods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, makeLikeTensor, reshape, sameShape, totalEntries
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.flag4j.arrays.backend.MatrixMixin
accept, div, fib, getCol, getDiag, getRow, getShape, getTriL, getTriU, isDiag, isSquare, isTri, isVector, mult, stack, sub, trace, vectorType
Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.SemiringTensorMixin
add, addEq, isOnes, isZeros, makeEmptyDataArray, mult, multEq, prod, sum
Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.TensorOverSemiring
getData, getRank, getShape, makeLikeTensor, tensorDot, tensorDot, tensorDot, tensorDot, tensorTr
-
Field Details
-
zeroElement
-
rowIndices
public final int[] rowIndicesRow indices for non-zero value of this sparse COO matrix. -
colIndices
public final int[] colIndicescolumn indices for non-zero value of this sparse COO matrix. -
nnz
public final int nnzNumber of non-zero data in this COO matrix. -
numRows
public final int numRowsThe number of rows in this matrix. -
numCols
public final int numColsThe number of columns in this matrix. -
sparsity
public final double sparsityThe sparsity of this matrix.
-
-
Constructor Details
-
AbstractCooSemiringMatrix
Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.- Parameters:
shape
- Shape of this tensor.data
- Non-zero data of this sparse matrix.rowIndices
- Non-zero row indices of this sparse matrix.colIndices
- Non-zero column indies of this sparse matrix.
-
AbstractCooSemiringMatrix
protected AbstractCooSemiringMatrix(Shape shape, W[] data, int[] rowIndices, int[] colIndices, Object dummy) Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.
This constructor will not perform data validation on any of the parameters.
- Parameters:
shape
- Shape of this tensor.data
- Non-zero data of this sparse matrix.rowIndices
- Non-zero row indices of this sparse matrix.colIndices
- Non-zero column indies of this sparse matrix.dummy
- Dummy object to distinguish this constructor from the safe variant. The value of this object is ignored.
-
-
Method Details
-
makeLikeTensor
Constructs a sparse COO tensor of the same type as this tensor with the specified non-zero data and indices.- Parameters:
shape
- Shape of the matrix.entries
- Non-zero data of the matrix.rowIndices
- Non-zero row indices of the matrix.colIndices
- Non-zero column indices of the matrix.- Returns:
- A sparse COO tensor of the same type as this tensor with the specified non-zero data and indices.
-
makeLikeTensor
public abstract T makeLikeTensor(Shape shape, List<W> entries, List<Integer> rowIndices, List<Integer> colIndices) Constructs a COO matrix with the specified shape, non-zero data, and non-zero indices.- Parameters:
shape
- Shape of the matrix.entries
- Non-zero values of the matrix.rowIndices
- Non-zero row indices of the matrix.colIndices
- Non-zero column indices of the matrix.- Returns:
- A COO matrix with the specified shape, non-zero data, and non-zero indices.
-
makeLikeVector
Constructs a sparse COO vector of a similar type to this COO matrix.- Parameters:
shape
- Shape of the vector. Must be rank 1.entries
- Non-zero data of the COO vector.indices
- Non-zero indices of the COO vector.- Returns:
- A sparse COO vector of a similar type to this COO matrix.
-
makeLikeDenseTensor
Constructs a dense tensor with the specifiedshape
anddata
which is a similar type to this sparse tensor.- Parameters:
shape
- Shape of the dense tensor.entries
- Entries of the dense tensor.- Returns:
- A dense tensor with the specified
shape
anddata
which is a similar type to this sparse tensor.
-
makeLikeCsrMatrix
public abstract AbstractCsrSemiringMatrix<?,U, makeLikeCsrMatrixV, W> (Shape shape, W[] entries, int[] rowPointers, int[] colIndices) Constructs a sparse CSR matrix of a similar type to this sparse COO matrix.- Parameters:
shape
- Shape of the CSR matrix to construct.entries
- Non-zero data of the CSR matrix.rowPointers
- Non-zero row pointers of the CSR matrix.colIndices
- Non-zero column indices of the CSR matrix.- Returns:
- A CSR matrix of a similar type to this sparse COO matrix.
-
getZeroElement
Gets the zero element for the semiring of this tensor.- Returns:
- The zero element for the semiring of this tensor. If it could not be determined during construction of this object
and has not been set explicitly by
setZeroElement(Semiring)
thennull
will be returned. - See Also:
-
sparsity
public double sparsity()Gets the sparsity of this matrix as a decimal percentage. That is, the percentage of data in this matrix that are zero.- Returns:
- The sparsity of this matrix as a decimal percentage.
- See Also:
-
density
public double density()Gets the density of this matrix as a decimal percentage. That is, the percentage of data in this matrix that are non-zero.- Returns:
- The density of this matrix as a decimal percentage.
- See Also:
-
dataLength
public int dataLength()Gets the length of the data array which backs this matrix.- Specified by:
dataLength
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Specified by:
dataLength
in classAbstractTensor<T extends AbstractCooSemiringMatrix<T,
U, V, W>, W extends Semiring<W>[], W extends Semiring<W>> - Returns:
- The length of the data array which backs this matrix.
-
setZeroElement
Sets the zero element for the semiring of this tensor.- Parameters:
zeroElement
- The zero element of this tensor.- Throws:
IllegalArgumentException
- IfzeroElement
is not an additive identity for the semiring.- See Also:
-
get
Gets the element of this tensor at the specified index.- Specified by:
get
in classAbstractTensor<T extends AbstractCooSemiringMatrix<T,
U, V, W>, W extends Semiring<W>[], W extends Semiring<W>> - Parameters:
index
- Indices of the element to get.- Returns:
- The element of this tensor at the specified index. If there is a non-zero value with the specified index, that value
will be returned. If there is no non-zero value at the specified index than the zero element will attempt to be
returned (i.e. the additive identity of the semiring). However, if the zero element could not be determined during
construction or if it was not set with
setZeroElement(Semiring)
thennull
will be returned. - Throws:
ArrayIndexOutOfBoundsException
- If any index are not within this tensor.
-
set
Sets the element of this tensor at the specified indices.- Specified by:
set
in classAbstractTensor<T extends AbstractCooSemiringMatrix<T,
U, V, W>, W extends Semiring<W>[], W extends Semiring<W>> - Parameters:
value
- New value to set the specified index of this tensor to.indices
- Indices of the element to set.- Returns:
- If this tensor is dense, a reference to this tensor is returned. If this tensor is sparse, a copy of this tensor with the updated value is returned.
- Throws:
IndexOutOfBoundsException
- Ifindices
is not within the bounds of this tensor.
-
set
Sets an index of this matrix to the specified value.- Specified by:
set
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
value
- Value to set.row
- Row index to set.col
- Column index to set.- Returns:
- A reference to this matrix.
-
setRow
Sets a specified row of this matrix to a vector.- Specified by:
setRow
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
row
- Vector to replace specified row in this matrix.rowIdx
- Index of the row to set.- Returns:
- If this matrix is dense, the row set operation is done in place and a reference to this matrix is returned. If this matrix is sparse a copy will be created with the new row and returned.
-
setCol
Sets a column of this matrix at the given index to the specified vector.- Specified by:
setCol
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
col
- Vector containing new column data.colIndex
- The index of the column which is to be set.- Returns:
- A copy of this matrix with the specified column set to
col
. - Throws:
IllegalArgumentException
- If thecol
vector has a different length than the number of rows of this matrix.IndexOutOfBoundsException
- IfcolIndex < 0 || colIndex >= this.numCols
.
-
flatten
-
flatten
Flattens a tensor along the specified axis.- Specified by:
flatten
in classAbstractTensor<T extends AbstractCooSemiringMatrix<T,
U, V, W>, W extends Semiring<W>[], W extends Semiring<W>> - Parameters:
axis
- Axis along which to flatten tensor.- Throws:
ArrayIndexOutOfBoundsException
- If the axis is not positive or larger thanthis.{@link #getRank()}-1
.- See Also:
-
reshape
Copies and reshapes this tensor.- Specified by:
reshape
in classAbstractTensor<T extends AbstractCooSemiringMatrix<T,
U, V, W>, W extends Semiring<W>[], W extends Semiring<W>> - Parameters:
newShape
- New shape for the tensor.- Returns:
- A copy of this tensor with the new shape.
- Throws:
TensorShapeException
- IfnewShape
is not broadcastable tothis.shape
.
-
T
Computes the transpose of a tensor by exchanging the first and last axes of this tensor.- Specified by:
T
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Overrides:
T
in classAbstractTensor<T extends AbstractCooSemiringMatrix<T,
U, V, W>, W extends Semiring<W>[], W extends Semiring<W>> - Returns:
- The transpose of this tensor.
- See Also:
-
T
Computes the transpose of a tensor by exchangingaxis1
andaxis2
.- Specified by:
T
in classAbstractTensor<T extends AbstractCooSemiringMatrix<T,
U, V, W>, W extends Semiring<W>[], W extends Semiring<W>> - Parameters:
axis1
- First axis to exchange.axis2
- Second axis to exchange.- Returns:
- The transpose of this tensor according to the specified axes.
- Throws:
IndexOutOfBoundsException
- If eitheraxis1
oraxis2
are out of bounds for the rank of this tensor.- See Also:
-
T
Computes the transpose of this tensor. That is, permutes the axes of this tensor so that it matches the permutation specified byaxes
.- Specified by:
T
in classAbstractTensor<T extends AbstractCooSemiringMatrix<T,
U, V, W>, W extends Semiring<W>[], W extends Semiring<W>> - Parameters:
axes
- Permutation of tensor axis. If the tensor has rankN
, then this must be an array of lengthN
which is a permutation of{0, 1, 2, ..., N-1}
.- Returns:
- The transpose of this tensor with its axes permuted by the
axes
array. - Throws:
IndexOutOfBoundsException
- If any element ofaxes
is out of bounds for the rank of this tensor.IllegalArgumentException
- Ifaxes
is not a permutation of{1, 2, 3, ... N-1}
.- See Also:
-
numRows
public int numRows()Gets the number of rows in this matrix. -
numCols
public int numCols()Gets the number of columns in this matrix. -
get
Gets the element of this matrix at this specifiedrow
andcol
.- Specified by:
get
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
row
- Row index of the item to get from this matrix.col
- Column index of the item to get from this matrix.- Returns:
- The element of this matrix at the specified index.
-
tr
Computes the trace of this matrix. That is, the sum of elements along the principle diagonal of this matrix.
Same as
MatrixMixin.trace()
.- Specified by:
tr
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Returns:
- The trace of this matrix.
- Throws:
IllegalArgumentException
- If this matrix is not square.
-
isTriU
public boolean isTriU()Checks if this matrix is upper triangular. -
isTriL
public boolean isTriL()Checks if this matrix is lower triangular. -
isI
public boolean isI()Checks if this matrix is the identity matrix. That is, checks if this matrix is square and contains only ones along the principle diagonal and zeros everywhere else. -
mult
Computes the matrix multiplication between two matrices.- Specified by:
mult
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
b
- Second matrix in the matrix multiplication.- Returns:
- The result of matrix multiplying this matrix with matrix
b
. - Throws:
LinearAlgebraException
- Ifthis.numCols() != b.numRows()
.
-
multTranspose
Multiplies this matrix with the transpose of theb
tensor as if bythis.mult(b.T())
. For large matrices, this method may be significantly faster than directly computing the transpose followed by the multiplication asthis.mult(b.T())
.- Specified by:
multTranspose
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
b
- The second matrix in the multiplication and the matrix to transpose.- Returns:
- The result of multiplying this matrix with the transpose of
b
.
-
stack
Stacks matrices along columns.- Specified by:
stack
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
b
- Matrix to stack to this matrix.- Returns:
- The result of stacking this matrix on top of the matrix
b
. - Throws:
IllegalArgumentException
- If this matrix and matrixb
have a different number of columns.- See Also:
-
augment
Stacks matrices along rows.- Specified by:
augment
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
b
- Matrix to stack to this matrix.- Returns:
- The result of stacking
b
to the right of this matrix. - Throws:
IllegalArgumentException
- If this matrix and matrixb
have a different number of rows.- See Also:
-
augment
Augments a vector to this matrix.- Specified by:
augment
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
b
- The vector to augment to this matrix.- Returns:
- The result of augmenting
b
to this matrix.
-
swapRows
Swaps specified rows in the matrix. This is done in place.- Specified by:
swapRows
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
rowIndex1
- Index of the first row to swap.rowIndex2
- Index of the second row to swap.- Returns:
- A reference to this matrix.
- Throws:
ArrayIndexOutOfBoundsException
- If either index is outside the matrix bounds.
-
swapCols
Swaps specified columns in the matrix. This is done in place.- Specified by:
swapCols
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
colIndex1
- Index of the first column to swap.colIndex2
- Index of the second column to swap.- Returns:
- A reference to this matrix.
- Throws:
ArrayIndexOutOfBoundsException
- If either index is outside the matrix bounds.
-
isSymmetric
public boolean isSymmetric()Checks if a matrix is symmetric. That is, if the matrix is square and equal to its transpose.- Specified by:
isSymmetric
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Returns:
true
if this matrix is symmetric;false
otherwise.
-
isHermitian
public boolean isHermitian()Checks if a matrix is Hermitian. That is, if the matrix is square and equal to its conjugate transpose.- Specified by:
isHermitian
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Returns:
true
if this matrix is Hermitian;false
otherwise.
-
isOrthogonal
public boolean isOrthogonal()Checks if this matrix is orthogonal. That is, if the inverse of this matrix is equal to its transpose.- Specified by:
isOrthogonal
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Returns:
true
if this matrix it is orthogonal;false
otherwise.
-
getRow
Gets a range of a row of this matrix.- Specified by:
getRow
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
rowIdx
- The index of the row to get.start
- The staring column of the row range to get (inclusive).stop
- The ending column of the row range to get (exclusive).- Returns:
- A vector containing the elements of the specified row over the range [start, stop).
- Throws:
IllegalArgumentException
- IfrowIdx < 0 || rowIdx >= this.numRows()
orstart < 0 || start >= numCols
orstop < start || stop > numCols
.
-
getCol
Gets a range of a column of this matrix.- Specified by:
getCol
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
colIdx
- The index of the column to get.start
- The staring row of the column range to get (inclusive).stop
- The ending row of the column range to get (exclusive).- Returns:
- A vector containing the elements of the specified column over the range [start, stop).
- Throws:
IllegalArgumentException
- IfcolIdx < 0 || colIdx >= this.numCols()
orstart < 0 || start >= numRows
orstop < start || stop > numRows
.
-
getDiag
Gets the elements of this matrix along the specified diagonal.- Specified by:
getDiag
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
diagOffset
- The diagonal to get within this matrix.- If
diagOffset == 0
: Then the elements of the principle diagonal are collected. - If
diagOffset < 0
: Then the elements of the sub-diagonaldiagOffset
below the principle diagonal are collected. - If
diagOffset > 0
: Then the elements of the super-diagonaldiagOffset
above the principle diagonal are collected.
- If
- Returns:
- The elements of the specified diagonal as a vector.
-
removeRow
Removes a specified row from this matrix.- Specified by:
removeRow
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
rowIndex
- Index of the row to remove from this matrix.- Returns:
- A copy of this matrix with the specified row removed.
-
removeRows
Removes a specified set of rows from this matrix.- Specified by:
removeRows
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
rowIdxs
- The indices of the rows to remove from this matrix. Assumed to contain unique values.- Returns:
- A copy of this matrix with the specified column removed.
-
removeCol
Removes a specified column from this matrix.- Specified by:
removeCol
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
colIndex
- Index of the column to remove from this matrix.- Returns:
- A copy of this matrix with the specified column removed.
-
removeCols
Removes a specified set of columns from this matrix.- Specified by:
removeCols
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
colIdxs
- Indices of the columns to remove from this matrix. Assumed to contain unique values.- Returns:
- A copy of this matrix with the specified column removed.
-
setSliceCopy
Creates a copy of this matrix and sets a slice of the copy to the specified values. The rowStart and colStart parameters specify the upper left index location of the slice to set.- Specified by:
setSliceCopy
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
values
- New values for the specified slice.rowStart
- Starting row index for the slice (inclusive).colStart
- Starting column index for the slice (inclusive).- Returns:
- A copy of this matrix with the given slice set to the specified values.
- Throws:
IndexOutOfBoundsException
- If rowStart or colStart are not within the matrix.IllegalArgumentException
- If the values slice, with upper left corner at the specified location, does not fit completely within this matrix.
-
getSlice
Gets a specified slice of this matrix.- Specified by:
getSlice
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
rowStart
- Starting row index of slice (inclusive).rowEnd
- Ending row index of slice (exclusive).colStart
- Starting column index of slice (inclusive).colEnd
- Ending row index of slice (exclusive).- Returns:
- The specified slice of this matrix. This is a completely new matrix and NOT a view into the matrix.
- Throws:
ArrayIndexOutOfBoundsException
- If any of the indices are out of bounds of this matrix.IllegalArgumentException
- IfrowEnd
is not greater thanrowStart
or ifcolEnd
is not greater thancolStart
.
-
getTriU
Extracts the upper-triangular portion of this matrix with a specified diagonal offset. All other data of the resulting matrix will be zero.- Specified by:
getTriU
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
diagOffset
- Diagonal offset for upper-triangular portion to extract:- If zero, then all data at and above the principle diagonal of this matrix are extracted.
- If positive, then all data at and above the equivalent super-diagonal are extracted.
- If negative, then all data at and above the equivalent sub-diagonal are extracted.
- Returns:
- The upper-triangular portion of this matrix with a specified diagonal offset. All other data of the returned matrix will be zero.
- Throws:
IllegalArgumentException
- IfdiagOffset
is not in the range (-numRows, numCols).
-
getTriL
Extracts the lower-triangular portion of this matrix with a specified diagonal offset. All other data of the resulting matrix will be zero.- Specified by:
getTriL
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
diagOffset
- Diagonal offset for lower-triangular portion to extract:- If zero, then all data at and above the principle diagonal of this matrix are extracted.
- If positive, then all data at and above the equivalent super-diagonal are extracted.
- If negative, then all data at and above the equivalent sub-diagonal are extracted.
- Returns:
- The lower-triangular portion of this matrix with a specified diagonal offset. All other data of the returned matrix will be zero.
- Throws:
IllegalArgumentException
- IfdiagOffset
is not in the range (-numRows, numCols).
-
copy
Creates a deep copy of this tensor.- Specified by:
copy
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Specified by:
copy
in classAbstractTensor<T extends AbstractCooSemiringMatrix<T,
U, V, W>, W extends Semiring<W>[], W extends Semiring<W>> - Returns:
- A deep copy of this tensor.
-
H
Computes the Hermitian transpose of this matrix. -
min
Finds the minimum value in this tensor. If this tensor is complex, then this method finds the smallest value in magnitude.- Specified by:
min
in interfaceSemiringTensorMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>> - Returns:
- The minimum value (smallest in magnitude for a complex valued tensor) in this tensor.
-
max
Finds the maximum value in this tensor. If this tensor is complex, then this method finds the largest value in magnitude.- Specified by:
max
in interfaceSemiringTensorMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>> - Returns:
- The maximum value (largest in magnitude for a complex valued tensor) in this tensor.
-
argmin
public int[] argmin()Finds the indices of the minimum value in this tensor.- Specified by:
argmin
in interfaceSemiringTensorMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>> - Returns:
- The indices of the minimum value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
argmax
public int[] argmax()Finds the indices of the maximum value in this tensor.- Specified by:
argmax
in interfaceSemiringTensorMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>> - Returns:
- The indices of the maximum value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
add
Computes the element-wise sum between two tensors of the same shape.- Specified by:
add
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Specified by:
add
in interfaceTensorOverSemiring<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
b
- Second tensor in the element-wise sum.- Returns:
- The sum of this tensor with
b
. - Throws:
TensorShapeException
- If this tensor andb
do not have the same shape.
-
elemMult
Computes the element-wise multiplication of two tensors of the same shape.- Specified by:
elemMult
in interfaceMatrixMixin<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Specified by:
elemMult
in interfaceTensorOverSemiring<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
b
- Second tensor in the element-wise product.- Returns:
- The element-wise product between this tensor and
b
. - Throws:
IllegalArgumentException
- If this tensor andb
do not have the same shape.
-
tensorTr
Computes the generalized trace of this tensor along the specified axes.
The generalized tensor trace is the sum along the diagonal values of the 2D sub-arrays of this tensor specified by
axis1
andaxis2
. The shape of the resulting tensor is equal to this tensor with theaxis1
andaxis2
removed.- Specified by:
tensorTr
in interfaceTensorOverSemiring<T extends AbstractCooSemiringMatrix<T,
U, V, W>, U extends AbstractDenseSemiringMatrix<U, ?, W>, V extends AbstractCooSemiringVector<V, ?, T, U, W>, W extends Semiring<W>> - Parameters:
axis1
- First axis for 2D sub-array.axis2
- Second axis for 2D sub-array.- Returns:
- The generalized trace of this tensor along
axis1
andaxis2
. - Throws:
IndexOutOfBoundsException
- If the two axes are not both larger than zero and less than this tensors rank.IllegalArgumentException
- Ifaxis1 == axis2
orthis.shape.get(axis1) != this.shape.get(axis1)
(i.e. the axes are equal or the tensor does not have the same length along the two axes.)
-
sortIndices
Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index. -
toDense
Converts this sparse COO matrix to an equivalent dense matrix.- Returns:
- A dense matrix equivalent to this sparse COO matrix.
-
toCsr
Converts this sparse COO matrix to an equivalent sparse CSR matrix.- Returns:
- A sparse CSR matrix equivalent to this sparse COO matrix.
-
toTensor
Converts this matrix to an equivalent rank 2 tensor.- Returns:
- A tensor which is equivalent to this matrix.
-
toTensor
Converts this matrix to an equivalent tensor with the specified shape.- Parameters:
newShape
- New shape for the tensor. Can be any rank but must be broadcastable tothis.shape
.- Returns:
- A tensor equivalent to this matrix which has been reshaped to
newShape
-
toVector
Converts this sparse CSR matrix to an equivalent vector. If this matrix is not a row or column vector it will be flattened before conversion. -
coalesce
Coalesces this sparse COO matrix. An uncoalesced matrix is a sparse matrix with multiple data for a single index. This method will ensure that each index only has one non-zero value by summing duplicated data. If another form of aggregation other than summing is desired, usecoalesce(BinaryOperator)
.- Returns:
- A new coalesced sparse COO matrix which is equivalent to this COO matrix.
- See Also:
-
coalesce
Coalesces this sparse COO matrix. An uncoalesced matrix is a sparse matrix with multiple data for a single index. This method will ensure that each index only has one non-zero value by aggregating duplicated data usingaggregator
.- Parameters:
aggregator
- Custom aggregation function to combine multiple.- Returns:
- A new coalesced sparse COO matrix which is equivalent to this COO matrix.
- See Also:
-
dropZeros
Drops any explicit zeros in this sparse COO matrix.- Returns:
- A copy of this COO matrix with any explicitly stored zeros removed.
-