Class CooMatrix
- All Implemented Interfaces:
Serializable,TensorOverField<CooMatrix,,CooMatrix, double[], Double> MatrixMixin<CooMatrix,,Matrix, CooVector, Double> TensorOverRing<CooMatrix,,CooMatrix, double[], Double> TensorOverSemiring<CooMatrix,CooMatrix, double[], Double>
A real sparse matrix stored in coordinate list (COO) format. The AbstractTensor.data of this COO tensor are
primitive doubles.
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.
COO matrices are well-suited for incremental matrix construction and modification but may not have ideal efficiency for matrix
ops like matrix multiplication. For heavy computations, it may be better to construct a matrix as a CooMatrix then
convert to a CsrMatrix (using toCsr()) as CSR (compressed sparse row) matrices are generally better suited for
efficient
matrix ops.
COO Representation:
A sparse COO matrix is stored as:- The full
shapeof the matrix. - The non-zero
AbstractTensor.dataof the matrix. All other data in the matrix are assumed to be zero. Zero values can also explicitly be stored inAbstractTensor.data. - The
row indicesof the non-zero values in the sparse matrix. - The
column indicesof the non-zero values in the sparse matrix.
Some ops on sparse tensors behave differently than on dense tensors. For instance, add(double) will not
add the scalar to all data of the tensor since this would cause catastrophic loss of sparsity. Instead, such non-zero preserving
element-wise ops only act on the non-zero data of the sparse tensor as to not affect the sparsity.
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. 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 intNumber of non-zero data in this COO matrix.final intThe number of columns in this matrix.final intThe number of rows in this matrix.final int[]Row indices for non-zero value of this sparse COO matrix.Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape -
Constructor Summary
ConstructorsConstructorDescriptionCooMatrix(int size) Constructs a square zero matrix with the specified size.CooMatrix(int size, double[] data, int[] rowIndices, int[] colIndices) Creates a square sparse COO matrix with the specified size, non-zero data, and non-zero indices.CooMatrix(int rows, int cols) Constructs a zero matrix with the specified shape.CooMatrix(int numRows, int numCols, double[] data, int[] rowIndices, int[] colIndices) Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.Constructs a zero matrix with the specified shape.Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.Constructs a copy of a real sparse COO matrix. -
Method Summary
Modifier and TypeMethodDescription<R> Raccept(MatrixVisitor<R> visitor) Accepts a visitor that implements theMatrixVisitorinterface.add(double b) Adds a scalar value to each non-zero element of this tensor.Computes the element-wise sum between two tensors of the same shape.add(Complex128 b) Adds a scalar value to each non-zero element of this tensor.int[]argmax()Finds the indices of the maximum value in this tensor.int[]Finds the indices of the maximum absolute value in this tensor.int[]argmin()Finds the indices of the minimum value in this tensor.int[]Finds the indices of the minimum absolute value in this tensor.Stacks matrices along rows.Augments a vector to this matrix.coalesce()Coalesces this sparse COO matrix.coalesce(BinaryOperator<Double> aggregator) Coalesces this sparse COO matrix.intGets the length of the data array which backs this matrix.det()Computes the determinant of a square matrix.Computes the element-wise quotient between two tensors.Drops any explicit zeros in this sparse COO matrix.Computes the element-wise product between two matrices.Computes the element-wise product between two matrices.Computes the element-wise product between two matrices.Computes the element-wise multiplication of two tensors of the same shape.booleanChecks if an object is equal to this matrix.Computes the Frobenius inner product of two matrices.flatten()Flattens tensor to single dimension while preserving order of data.flatten(int axis) Flattens a tensor along the specified axis.get(int... indices) Gets the element of this tensor at the specified indices.get(int row, int col) Gets the element of this matrix at this specifiedrowandcol.getCol(int colIdx) Get the column of this matrix at the specified index.getCol(int colIdx, int rowStart, int rowEnd) Gets a specified column of this matrix betweenrowStart(inclusive) androwEnd(exclusive).getDiag()Extracts the diagonal elements of this matrix and returns them as a vector.getDiag(int diagOffset) Gets the elements of this matrix along the specified diagonal.getRow(int rowIdx) Get the row of this matrix at the specified index.getRow(int rowIdx, int colStart, int colEnd) Gets a specified row of this matrix betweencolStart(inclusive) andcolEnd(exclusive).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.H()Computes the conjugate transpose of a tensor by exchanging the first and last axes of this tensor and conjugating the exchanged values.inthashCode()booleanChecks if a matrix is anti-symmetric.booleanChecks that this matrix is close to the identity matrix.booleanChecks if a matrix is Hermitian.booleanisI()Checks if this matrix is the identity matrix.booleanChecks if this matrix is orthogonal.booleanChecks if a matrix is symmetric.booleanisTriL()Checks if this matrix is lower triangular.booleanisTriU()Checks if this matrix is upper triangular.makeLikeTensor(Shape shape, double[] data) Constructs a sparse COO matrix of the same type as this tensor with the given the shape and data and indices copied from this matrix.mult(CooCMatrix b) Computes the matrix multiplication between two matrices.Computes the matrix multiplication between two matrices.Computes matrix-vector multiplication.Multiplies this matrix with the transpose of thebtensor as if bythis.mult(b.T()).intnumCols()Gets the number of columns in this matrix.intnumRows()Gets the number of rows in this matrix.prod()Computes the product of all non-zero values in this tensor.recip()Computes the element-wise reciprocals of non-zero values of this tensor.removeCol(int colIndex) Removes a specified column from this matrix.removeCols(int... colIndices) Removes a specified set of columns from this matrix.removeRow(int rowIndex) Removes a specified row from this matrix.removeRows(int... rowIndices) 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 values.setRow(double[] row, int rowIdx) Sets a specified row of this matrix to an array.Sets a row of this matrix at the given index to the specified values.setSliceCopy(CooMatrix values, int rowStart, int colStart) Creates a copy of this matrix and sets a slice of the copy to the specified values.Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index.doublesparsity()The sparsity of this sparse tensor.Stacks matrices along columns.sub(double b) Subtracts a scalar value from each non-zero element of this tensor.Computes the element-wise difference between two tensors of the same shape.sub(Complex128 b) Subtracts a scalar value from each non-zero element of this tensor.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 exchangingaxis1andaxis2.Computes the tensor contraction of this tensor with a specified tensor over the specified set of axes.tensorTr(int axis1, int axis2) Computes the generalized trace of this tensor along the specified axes.Converts this real sparse COO matrix to an equivalent complex sparse COO matrix.toCsr()Converts this COO matrix to an equivalent CSR matrix.toDense()Converts this sparse tensor to an equivalent dense tensor.toString()Formats this sparse matrix as a human-readable string.toTensor()Converts this sparse COO matrix to an equivalentsparse COO tensor.toVector()Converts this matrix to an equivalent vector.tr()Computes the trace of this matrix.static CooMatrixunsafeMake(Shape shape, double[] data, int[] rowIndices, int[] colIndices) Factory to construct a COO matrix which bypasses any validation checks on the data and indices.Methods inherited from class org.flag4j.arrays.backend.primitive_arrays.AbstractDoubleTensor
abs, add, addEq, addEq, conj, copy, div, div, divEq, divEq, H, H, isFinite, isInfinite, isNaN, isNeg, isOnes, isPos, isZeros, max, maxAbs, min, minAbs, mult, mult, multEq, multEq, round, round, roundToZero, roundToZero, sqrt, sub, subEq, subEq, sumMethods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, reshape, sameShape, totalEntriesMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Field Details
-
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.
-
-
Constructor Details
-
CooMatrix
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.
-
CooMatrix
public CooMatrix(int numRows, int numCols, double[] data, int[] rowIndices, int[] colIndices) Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.- Parameters:
numRows- Number of rows in the matrix.numCols- Number of columns in the matrix.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.
-
CooMatrix
public CooMatrix(Shape shape, List<Double> data, List<Integer> rowIndices, List<Integer> colIndices) 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.
-
CooMatrix
public CooMatrix(int size) Constructs a square zero matrix with the specified size.- Parameters:
size- Size of the square zero matrix to construct.
-
CooMatrix
public CooMatrix(int rows, int cols) Constructs a zero matrix with the specified shape.- Parameters:
rows- The number of rows in the zero matrix.cols- The number of columns in the zero matrix.
-
CooMatrix
Constructs a zero matrix with the specified shape.- Parameters:
shape-
-
CooMatrix
public CooMatrix(int size, double[] data, int[] rowIndices, int[] colIndices) Creates a square sparse COO matrix with the specified size, non-zero data, and non-zero indices.- Parameters:
size- Size of the square matrix.data- Non-zero data of the sparse matrix.rowIndices- Row indices of the non-zero data in the matrix.colIndices- Column indices of the non-zero data in the matrix.
-
CooMatrix
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.
-
CooMatrix
-
-
Method Details
-
unsafeMake
Factory to construct a COO matrix which bypasses any validation checks on the data and indices.
Warning: This method should be used with extreme caution. It primarily exists for internal use. Only use this factory if you are 100% certain the parameters are valid as some methods may throw exceptions or exhibit undefined behavior.
- Parameters:
shape- The full size of the COO matrix.data- The non-zero data of the COO matrix.rowIndices- The non-zero row indices of the COO matrix.colIndices- The non-zero column indices of the COO matrix.- Returns:
- A COO matrix constructed from the provided parameters.
-
dataLength
public int dataLength()Gets the length of the data array which backs this matrix.- Specified by:
dataLengthin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Overrides:
dataLengthin classAbstractDoubleTensor<CooMatrix>- Returns:
- The length of the data array which backs this matrix.
-
tensorDot
Computes the tensor contraction of this tensor with a specified tensor over the specified set of axes. That is, computes the sum of products between the two tensors along the specified set of axes.- Specified by:
tensorDotin interfaceTensorOverSemiring<CooMatrix,CooMatrix, double[], Double> - Parameters:
src2- Tensor to contract with this tensor.aAxes- Axes along which to compute products for this tensor.bAxes- Axes along which to compute products forsrc2tensor.- Returns:
- The tensor dot product over the specified axes.
- Throws:
IllegalArgumentException- If the two tensors shapes do not match along the specified axes pairwise inaAxesandbAxes.IllegalArgumentException- IfaAxesandbAxesdo not match in length, or if any of the axes are out of bounds for the corresponding tensor.
-
makeLikeTensor
Constructs a sparse COO matrix of the same type as this tensor with the given the shape and data and indices copied from this matrix.- Specified by:
makeLikeTensorin interfaceTensorOverSemiring<CooMatrix,CooMatrix, double[], Double> - Specified by:
makeLikeTensorin classAbstractTensor<CooMatrix,double[], Double> - Parameters:
shape- Shape of the matrix to construct.data- Entries of the matrix to construct.- Returns:
- A matrix of the same type as this matrix with the given the shape and data.
-
T
Computes the transpose of a tensor by exchangingaxis1andaxis2.- Specified by:
Tin classAbstractTensor<CooMatrix,double[], Double> - 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 eitheraxis1oraxis2are 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:
Tin classAbstractTensor<CooMatrix,double[], Double> - Parameters:
axes- Permutation of tensor axis. If the tensor has rankN, then this must be an array of lengthNwhich is a permutation of{0, 1, 2, ..., N-1}.- Returns:
- The transpose of this tensor with its axes permuted by the
axesarray. - Throws:
IndexOutOfBoundsException- If any element ofaxesis out of bounds for the rank of this tensor.IllegalArgumentException- Ifaxesis not a permutation of{1, 2, 3, ... N-1}.- See Also:
-
sparsity
public double sparsity()The sparsity of this sparse tensor. That is, the percentage of elements in this tensor which are zero as a decimal.- Returns:
- The density of this sparse tensor.
-
toDense
Converts this sparse tensor to an equivalent dense tensor.- Returns:
- A dense tensor equivalent to this sparse tensor.
- Throws:
ArithmeticException- If the total number of data in this sparse matrix does not fit into an int.
-
toCsr
Converts this COO matrix to an equivalent CSR matrix.- Returns:
- A CSR matrix equivalent to this matrix.
-
sortIndices
Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index.- Returns:
- A reference to this matrix.
-
get
Gets the element of this tensor at the specified indices.- Specified by:
getin classAbstractTensor<CooMatrix,double[], Double> - Parameters:
indices- Indices of the element to get.- Returns:
- The element of this tensor at the specified indices.
- Throws:
ArrayIndexOutOfBoundsException- If any indices are not within this matrix.
-
set
Sets the element of this tensor at the specified indices.- Specified by:
setin classAbstractTensor<CooMatrix,double[], Double> - Parameters:
value- New value to set the specified index of this tensor to.indices- Indices of the element to set.- Returns:
- A copy of this tensor with the updated value is returned.
- Throws:
IndexOutOfBoundsException- Ifindicesis not within the bounds of this tensor.
-
flatten
Flattens tensor to single dimension while preserving order of data.- Specified by:
flattenin classAbstractTensor<CooMatrix,double[], Double> - Returns:
- The flattened tensor.
- See Also:
-
flatten
Flattens a tensor along the specified axis.- Specified by:
flattenin classAbstractTensor<CooMatrix,double[], Double> - 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:
reshapein classAbstractTensor<CooMatrix,double[], Double> - Parameters:
newShape- New shape for the tensor.- Returns:
- A copy of this tensor with the new shape.
- Throws:
TensorShapeException- IfnewShapeis not broadcastable tothis.shape.
-
add
Computes the element-wise sum between two tensors of the same shape.- Specified by:
addin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Specified by:
addin interfaceTensorOverSemiring<CooMatrix,CooMatrix, double[], Double> - Parameters:
b- Second tensor in the element-wise sum.- Returns:
- The sum of this tensor with
b. - Throws:
IllegalArgumentException- If this tensor andbdo not have the same shape.
-
sub
Computes the element-wise difference between two tensors of the same shape.- Specified by:
subin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Specified by:
subin interfaceTensorOverRing<CooMatrix,CooMatrix, double[], Double> - Parameters:
b- Second tensor in the element-wise difference.- Returns:
- The difference of this tensor with
b. - Throws:
IllegalArgumentException- If this tensor andbdo not have the same shape.
-
H
Computes the conjugate transpose of a tensor by exchanging the first and last axes of this tensor and conjugating the exchanged values. -
argmin
public int[] argmin()Finds the indices of the minimum value in this tensor.- Specified by:
argminin interfaceTensorOverRing<CooMatrix,CooMatrix, double[], Double> - 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:
argmaxin interfaceTensorOverRing<CooMatrix,CooMatrix, double[], Double> - 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.
-
argminAbs
public int[] argminAbs()Finds the indices of the minimum absolute value in this tensor.- Specified by:
argminAbsin interfaceTensorOverRing<CooMatrix,CooMatrix, double[], Double> - Returns:
- The indices of the minimum absolute value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
argmaxAbs
public int[] argmaxAbs()Finds the indices of the maximum absolute value in this tensor.- Specified by:
argmaxAbsin interfaceTensorOverRing<CooMatrix,CooMatrix, double[], Double> - Returns:
- The indices of the maximum absolute value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
elemMult
Computes the element-wise multiplication of two tensors of the same shape.- Specified by:
elemMultin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Specified by:
elemMultin interfaceTensorOverSemiring<CooMatrix,CooMatrix, double[], Double> - Parameters:
b- Second tensor in the element-wise product.- Returns:
- The element-wise product between this tensor and
b. - Throws:
IllegalArgumentException- If this tensor andbdo not have the same shape.
-
tensorTr
Computes the generalized trace of this tensor along the specified axes.
Note: for a matrix, the
tr()method is preferred.The generalized tensor trace is the sum along the diagonal values of the 2D sub-arrays of this tensor specified by
axis1andaxis2. The shape of the resulting tensor is equal to this tensor with theaxis1andaxis2removed.- Specified by:
tensorTrin interfaceTensorOverSemiring<CooMatrix,CooMatrix, double[], Double> - Parameters:
axis1- First axis for 2D sub-array.axis2- Second axis for 2D sub-array.- Returns:
- The generalized trace of this tensor along
axis1andaxis2. This will be a tensor of rankthis.getRank() - 2with the same shape as this tensor but withaxis1andaxis2removed. - Throws:
IndexOutOfBoundsException- If the two axes are not both larger than zero and less than this tensors rank.IllegalArgumentException- Ifaxis1 == axis2orthis.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.)
-
prod
Computes the product of all non-zero values in this tensor.- Specified by:
prodin interfaceTensorOverSemiring<CooMatrix,CooMatrix, double[], Double> - Overrides:
prodin classAbstractDoubleTensor<CooMatrix>- Returns:
- The product of all non-zero values in this tensor.
-
T
-
recip
Computes the element-wise reciprocals of non-zero values of this tensor.- Specified by:
recipin interfaceTensorOverField<CooMatrix,CooMatrix, double[], Double> - Overrides:
recipin classAbstractDoubleTensor<CooMatrix>- Returns:
- A tensor containing the reciprocals of the non-zero values of this tensor.
-
add
Adds a scalar value to each non-zero element of this tensor.- Specified by:
addin interfaceTensorOverField<CooMatrix,CooMatrix, double[], Double> - Overrides:
addin classAbstractDoubleTensor<CooMatrix>- Parameters:
b- Value to add to each non-zero entry of this tensor.- Returns:
- The result of adding the specified scalar value to each non-zero entry of this tensor.
-
add
Adds a scalar value to each non-zero element of this tensor.- Parameters:
b- Value to add to each non-zero entry of this tensor.- Returns:
- The result of adding the specified scalar value to each non-zero entry of this tensor.
-
sub
Subtracts a scalar value from each non-zero element of this tensor.- Specified by:
subin interfaceTensorOverField<CooMatrix,CooMatrix, double[], Double> - Overrides:
subin classAbstractDoubleTensor<CooMatrix>- Parameters:
b- Value to subtract from each non-zero entry of this tensor.- Returns:
- The result of subtracting the specified scalar value from each non-zero entry of this tensor.
-
div
Computes the element-wise quotient between two tensors.
Warning: This method is not supported for sparse matrices. If called on a sparse matrix, an
UnsupportedOperationExceptionwill be thrown. Element-wise division is undefined for sparse matrices as it would almost certainly result in a division by zero. -
sub
Subtracts a scalar value from each non-zero element of this tensor.- Parameters:
b- Value to subtract from each non-zero entry of this tensor.- Returns:
- The result of subtracting the specified scalar value from each non-zero entry of this tensor.
-
numRows
-
numCols
-
get
Gets the element of this matrix at this specifiedrowandcol. -
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:
trin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Returns:
- The trace of this matrix.
- Throws:
IllegalArgumentException- If this matrix is not square.
-
isTriU
-
isTriL
-
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. -
isCloseToI
public boolean isCloseToI()Checks that this matrix is close to the identity matrix.- Returns:
- True if this matrix is approximately the identity matrix.
- See Also:
-
det
Computes the determinant of a square matrix.- Returns:
- The determinant of this matrix.
- Throws:
LinearAlgebraException- If this matrix is not square.
-
mult
Computes the matrix multiplication between two matrices.- Specified by:
multin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
b- Second matrix in the matrix multiplication.- Returns:
- The result of matrix multiplying this matrix with matrix
b. - Throws:
LinearAlgebraException- If the number of columns in this matrix do not equal the number of rows in matrixb.
-
mult
Computes the matrix multiplication between two matrices.- Parameters:
b- Second matrix in the matrix multiplication.- Returns:
- The result of matrix multiplying this matrix with matrix
b. - Throws:
LinearAlgebraException- If the number of columns in this matrix do not equal the number of rows in matrixb.
-
multTranspose
Multiplies this matrix with the transpose of thebtensor 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:
multTransposein interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - 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.
-
fib
Computes the Frobenius inner product of two matrices.- Specified by:
fibin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
b- Second matrix in the Frobenius inner product- Returns:
- The Frobenius inner product of this matrix and matrix b.
- Throws:
IllegalArgumentException- If this matrix and b have different shapes.
-
stack
Stacks matrices along columns.- Specified by:
stackin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - 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 matrixbhave a different number of columns.- See Also:
-
augment
Stacks matrices along rows.- Specified by:
augmentin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
b- Matrix to stack to this matrix.- Returns:
- The result of stacking
bto the right of this matrix. - Throws:
IllegalArgumentException- If this matrix and matrixbhave a different number of rows.- See Also:
-
augment
-
swapRows
Swaps specified rows in the matrix. This is done in place.- Specified by:
swapRowsin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - 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:
swapColsin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - 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:
isSymmetricin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Returns:
trueif this matrix is symmetric;falseotherwise.- See Also:
-
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:
isHermitianin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Returns:
trueif this matrix is Hermitian;falseotherwise.
-
isAntiSymmetric
public boolean isAntiSymmetric()Checks if a matrix is anti-symmetric. That is, if the matrix is equal to the negative of its transpose.- Returns:
trueif this matrix is anti-symmetric;falseotherwise.- See Also:
-
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:
isOrthogonalin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Returns:
trueif this matrix it is orthogonal;falseotherwise.
-
removeRow
-
removeRows
Removes a specified set of rows from this matrix.- Specified by:
removeRowsin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
rowIndices- 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. -
removeCols
Removes a specified set of columns from this matrix.- Specified by:
removeColsin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
colIndices- 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:
setSliceCopyin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - 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:
getSlicein interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - 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- IfrowEndis not greater thanrowStartor ifcolEndis not greater thancolStart.
-
set
Sets an index of this matrix to the specified value. -
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:
getTriUin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - 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- IfdiagOffsetis 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:
getTriLin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - 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- IfdiagOffsetis not in the range (-numRows, numCols).
-
mult
Computes matrix-vector multiplication.- Specified by:
multin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
b- Vector in the matrix-vector multiplication.- Returns:
- The result of matrix multiplying this matrix with vector
b. - Throws:
IllegalArgumentException- If the number of columns in this matrix do not equal the number of data in the vectorb.
-
toVector
Converts this matrix to an equivalent vector. If this matrix is not shaped as a row/column vector, it will first be flattened then converted to a vector. -
toTensor
Converts this sparse COO matrix to an equivalentsparse COO tensor.- Returns:
- A
sparse COO tensorequivalent to this sparse COO matrix.
-
getRow
Get the row of this matrix at the specified index.- Specified by:
getRowin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
rowIdx- Index of row to get.- Returns:
- The specified row of this matrix.
- Throws:
ArrayIndexOutOfBoundsException- IfrowIdxis less than zero or greater than/equal to the number of rows in this matrix.
-
getRow
Gets a specified row of this matrix betweencolStart(inclusive) andcolEnd(exclusive).- Specified by:
getRowin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
rowIdx- Index of the row of this matrix to get.colStart- Starting column of the row (inclusive).colEnd- Ending column of the row (exclusive).- Returns:
- The row at index
rowIdxof this matrix between thecolStartandcolEndindices. - Throws:
IndexOutOfBoundsException- If eithercolEndarecolStartout of bounds for the shape of this matrix.IllegalArgumentException- IfcolEndis less thancolStart.
-
getCol
Get the column of this matrix at the specified index.- Specified by:
getColin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
colIdx- Index of column to get.- Returns:
- The specified column of this matrix.
- Throws:
ArrayIndexOutOfBoundsException- IfcolIdxis less than zero or greater than/equal to the number of columns in this matrix.
-
getCol
Gets a specified column of this matrix betweenrowStart(inclusive) androwEnd(exclusive).- Specified by:
getColin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
colIdx- Index of the column of this matrix to get.rowStart- Starting row of the column (inclusive).rowEnd- Ending row of the column (exclusive).- Returns:
- The column at index
colIdxof this matrix between therowStartandrowEndindices. - Throws:
IllegalArgumentException- IfrowEndis less thanrowStart.
-
getDiag
-
getDiag
Gets the elements of this matrix along the specified diagonal.- Specified by:
getDiagin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - 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-diagonaldiagOffsetbelow the principle diagonal are collected. - If
diagOffset > 0: Then the elements of the super-diagonaldiagOffsetabove the principle diagonal are collected.
- If
- Returns:
- The elements of the specified diagonal as a vector.
-
setCol
Sets a column of this matrix at the given index to the specified values.- Specified by:
setColin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
values- New values for the column.colIndex- The index of the column which is to be set.- Returns:
- A copy of this matrix with the specified column set to
values. - Throws:
IllegalArgumentException- If the values vector has a different length than the number of rows of this matrix.IndexOutOfBoundsException- IfcolIndex < 0 || colIndex >= this.numCols.
-
setRow
Sets a row of this matrix at the given index to the specified values.- Specified by:
setRowin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Parameters:
values- New values for the row.rowIndex- The index of the row which is to be set.- Returns:
- A copy of this matrix with the specified row set to
values. - Throws:
IllegalArgumentException- If the values vector has a different length than the number of rows of this matrix.
-
setRow
Sets a specified row of this matrix to an array.- Parameters:
row- Array containing values 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.
-
toComplex
Converts this real sparse COO matrix to an equivalent complex sparse COO matrix.- Returns:
- A complex sparse COO matrix equivalent to this matrix.
-
elemMult
Computes the element-wise product between two matrices.- Parameters:
b- Second matrix in the element-wise product.- Returns:
- The element-wise product of this matrix and
b.
-
elemMult
-
elemMult
Computes the element-wise product between two matrices.- Parameters:
b- Second matrix in the element-wise product.- Returns:
- The element-wise product of this matrix and
b.
-
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.
-
accept
Accepts a visitor that implements theMatrixVisitorinterface. This method is part of the "Visitor Pattern" and allows operations to be performed on the matrix without modifying the matrix's class directly.- Specified by:
acceptin interfaceMatrixMixin<CooMatrix,Matrix, CooVector, Double> - Type Parameters:
R- The return type of the visitor's operation.- Parameters:
visitor- The visitor implementing the operation to be performed.- Returns:
- The result of the visitor's operation, typically another matrix or a scalar value.
- Throws:
NullPointerException- if the visitor isnull.
-
equals
Checks if an object is equal to this matrix. -
hashCode
-
toString
-