Class RingMatrix<T extends Ring<T>>
- Type Parameters:
T
- Type of thering
element for the matrix.
- All Implemented Interfaces:
Serializable
,MatrixMixin<RingMatrix<T>,
,RingMatrix<T>, RingVector<T>, T> RingTensorMixin<RingMatrix<T>,
,RingMatrix<T>, T> TensorOverRing<RingMatrix<T>,
,RingMatrix<T>, T[], T> SemiringTensorMixin<RingMatrix<T>,
,RingMatrix<T>, T> TensorOverSemiring<RingMatrix<T>,
RingMatrix<T>, T[], T>
Instances of this class represents a dense matrix backed by a Ring
array. The RingMatrix
class
provides functionality for matrix operations whose elements are members of a ring, supporting mutable data with a fixed shape.
A RingMatrix
is essentially equivalent to a rank-2 tensor but includes extended functionality
and may offer improved performance for certain operations compared to general rank-n tensors.
Key Features:
- Support for standard matrix operations like addition, subtraction, multiplication, and exponentiation.
- Conversion methods to other matrix representations, such as COO (Coordinate) and CSR (Compressed Sparse Row) formats.
- Utility methods for checking properties like being unitary, real, or complex.
Example Usage:
Using 32-bit real integers
:
// Constructing an integer matrix from a 2D array.
RealInt32[][] data = {
{ new RealInt32(5), new RealInt32(-3) },
{ new RealInt32(-7), new RealInt32(8) }
};
RingMatrix<RealInt32> matrix = new FieldMatrix(data);
// Performing matrix multiplication.
RingMatrix<RealInt32> result = matrix.mult(matrix);
// Performing matrix transpose.
RingMatrix<RealInt32> transpose = matrix.T();
Using 128-bit complex number
:
// Constructing a complex matrix from a 2D array of complex numbers
Complex128[][] data = {
{ new Complex128(1, 2), new Complex128(3, 4) },
{ new Complex128(5, 6), new Complex128(7, 8) }
};
RingMatrix<Complex128> matrix = new FieldMatrix(data);
// Performing matrix multiplication.
RingMatrix<Complex128> result = matrix.mult(matrix);
// Performing matrix transpose.
RingMatrix<Complex128> transpose = matrix.T();
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringMatrix
numCols, numRows
Fields inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringTensor
zeroElement
Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorsConstructorDescriptionRingMatrix
(int rows, int cols, T fillValue) Creates a dense ring matrix with the specified data and filled withfilledValue
.RingMatrix
(int rows, int cos, T[] entries) Creates a dense ring matrix with the specified data and shape.RingMatrix
(int rows, int cols, T[][] entries) Creates a dense ring matrix with the specified data and shape.RingMatrix
(Shape shape, T fillValue) Creates a dense ring matrix with the specified data and filled withfilledValue
.RingMatrix
(Shape shape, T[] entries) Creates a tensor with the specified data and shape.RingMatrix
(T[][] entries) Creates a dense ring matrix with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescriptionabs()
Computes the element-wise absolute value of this tensor.<R> R
accept
(MatrixVisitor<R> visitor) Accepts a visitor that implements theMatrixVisitor
interface.div
(RingMatrix<T> b) Computes the element-wise quotient of two matrices.boolean
Checks if an object is equal to this matrix object.int
hashCode()
static <T extends Ring<T>>
RingMatrix<T> I
(int numRows, int numCols, T fieldValue) Constructs an identity-like matrix of the specified shape.static <T extends Ring<T>>
RingMatrix<T> I
(int size, T fieldValue) Constructs an identity matrix of the specified size.static <T extends Ring<T>>
RingMatrix<T> Constructs an identity-like matrix of the specified shape.protected CooRingMatrix
<T> makeLikeCooMatrix
(Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Constructs a sparse COO matrix which is of a similar type as this dense matrix.protected CooRingTensor
<T> makeLikeCooTensor
(Shape shape, T[] data, int[][] indices) Constructs a sparse COO tensor which is of a similar type as this dense tensor.makeLikeCsrMatrix
(Shape shape, T[] entries, int[] rowPointers, int[] colIndices) Constructs a sparse CSR matrix which is of a similar type as this dense matrix.makeLikeTensor
(Shape shape, T[] entries) Constructs a tensor of the same type as this tensor with the given theshape
anddata
.protected RingVector
<T> makeLikeVector
(Shape shape, T[] entries) Constructs a vector of a similar type as this matrix.protected RingVector
<T> makeLikeVector
(T[] entries) Constructs a vector of a similar type as this matrix.pow
(int n) Computes the matrix multiplication of this matrix with itselfn
times.sub
(RingMatrix<T> b) Computes the element-wise difference of two matrices.toCoo()
Converts this dense tensor to an equivalent sparse COO tensor.toCoo
(double estimatedSparsity) Converts this matrix to an equivalent sparse COO matrix.toCsr()
Converts this matrix to an equivalent sparse CSR matrix.toCsr
(double estimatedSparsity) Converts this matrix to an equivalent sparse CSR matrix.toString()
Generates a human-readable string representing this matrix.toTensor()
Converts this matrix to an equivalent tensor.Converts this matrix to an equivalent tensor with the specifiednewShape
.Methods inherited from class org.flag4j.arrays.backend.ring_arrays.AbstractDenseRingMatrix
allClose, allClose, H, H, isCloseToIdentity, isHermitian, norm, subEq
Methods inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringMatrix
augment, augment, dataLength, flatten, flatten, get, getCol, getDiag, getRow, getSlice, getTriL, getTriU, H, isI, isOrthogonal, isSymmetric, isTriL, isTriU, mult, mult, multTranspose, numCols, numRows, removeCol, removeCols, removeRow, removeRows, set, setCol, setCol, setRow, setRow, setSlice, setSliceCopy, setValues, stack, swapCols, swapRows, T, toVector, tr
Methods inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringTensor
add, addEq, argmax, argmin, copy, elemMult, get, getZeroElement, max, min, reshape, set, setZeroElement, T, T, tensorDot, tensorTr
Methods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, reshape, sameShape, totalEntries
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.flag4j.arrays.backend.MatrixMixin
add, augment, augment, copy, dataLength, elemMult, fib, get, getCol, getCol, getDiag, getDiag, getRow, getRow, getShape, getSlice, getTriL, getTriL, getTriU, getTriU, H, isDiag, isI, isOrthogonal, isSquare, isSymmetric, isTri, isTriL, isTriU, isVector, mult, mult, multTranspose, numCols, numRows, removeCol, removeCols, removeRow, removeRows, set, setCol, setRow, setSliceCopy, stack, stack, swapCols, swapRows, T, toVector, tr, trace, vectorType
Methods inherited from interface org.flag4j.arrays.backend.ring_arrays.RingTensorMixin
add, addEq, argmax, argmaxAbs, argmin, argminAbs, conj, isOnes, isZeros, makeEmptyDataArray, max, maxAbs, min, minAbs, mult, multEq, norm, norm, prod, sub, subEq, sum
Methods inherited from interface org.flag4j.arrays.backend.ring_arrays.TensorOverRing
H
-
Constructor Details
-
RingMatrix
Creates a tensor with the specified data and shape.- Parameters:
shape
- Shape of this tensor.entries
- Entries of this tensor. If this tensor is dense, this specifies all data within the tensor. If this tensor is sparse, this specifies only the non-zero data of the tensor.
-
RingMatrix
Creates a dense ring matrix with the specified data and shape.- Parameters:
rows
- Number of rows in the matrix.entries
- Entries of this matrix.cols
- Number of columns in the matrix.
-
RingMatrix
Creates a dense ring matrix with the specified data and shape.- Parameters:
entries
- Entries of this matrix.shape
- Shape of this matrix.
-
RingMatrix
-
RingMatrix
Creates a dense ring matrix with the specified data and shape.- Parameters:
rows
- Number of rows in the matrix.cols
- Number of columns in the matrix.entries
- Entries of this matrix.shape
- Shape of this matrix.
-
RingMatrix
Creates a dense ring matrix with the specified data and filled withfilledValue
.- Parameters:
rows
- Number of rows in the matrix.cols
- Number of columns in the matrix.fillValue
- Entries of this matrix.
-
-
Method Details
-
makeLikeVector
Constructs a vector of a similar type as this matrix.- Specified by:
makeLikeVector
in classAbstractDenseSemiringMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Parameters:
shape
- Shape of the vector to construct. Must be rank 1.entries
- Entries of the vector.- Returns:
- A vector of a similar type as this matrix.
-
makeLikeVector
Constructs a vector of a similar type as this matrix.- Specified by:
makeLikeVector
in classAbstractDenseSemiringMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Parameters:
entries
- Entries of the vector.- Returns:
- A vector of a similar type as this matrix.
-
makeLikeCooMatrix
protected CooRingMatrix<T> makeLikeCooMatrix(Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Constructs a sparse COO matrix which is of a similar type as this dense matrix.- Specified by:
makeLikeCooMatrix
in classAbstractDenseSemiringMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Parameters:
shape
- Shape of the COO matrix.entries
- Non-zero data of the COO matrix.rowIndices
- Non-zero row indices of the COO matrix.colIndices
- Non-zero column indices of the COO matrix.- Returns:
- A sparse COO matrix which is of a similar type as this dense matrix.
-
makeLikeCsrMatrix
public CsrRingMatrix<T> makeLikeCsrMatrix(Shape shape, T[] entries, int[] rowPointers, int[] colIndices) Constructs a sparse CSR matrix which is of a similar type as this dense matrix.- Specified by:
makeLikeCsrMatrix
in classAbstractDenseSemiringMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Parameters:
shape
- Shape of the CSR matrix.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 sparse CSR matrix which is of a similar type as this dense matrix.
-
makeLikeCooTensor
Constructs a sparse COO tensor which is of a similar type as this dense tensor.- Specified by:
makeLikeCooTensor
in classAbstractDenseSemiringTensor<RingMatrix<T extends Ring<T>>,
T extends Ring<T>> - Parameters:
shape
- Shape of the COO tensor.data
- Non-zero data of the COO tensor.indices
- Non-zero indices of the COO tensor.- Returns:
- A sparse COO tensor which is of a similar type as this dense tensor.
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the given theshape
anddata
. The resulting tensor will also have the same non-zero indices as this tensor.- Specified by:
makeLikeTensor
in interfaceTensorOverSemiring<RingMatrix<T extends Ring<T>>,
RingMatrix<T extends Ring<T>>, T extends Ring<T>[], T extends Ring<T>> - Specified by:
makeLikeTensor
in classAbstractTensor<RingMatrix<T extends Ring<T>>,
T extends Ring<T>[], T extends Ring<T>> - Parameters:
shape
- Shape of the tensor to construct.entries
- Entries of the tensor to construct.- Returns:
- A tensor of the same type and with the same non-zero indices as this tensor with the given the
shape
anddata
.
-
toTensor
Converts this matrix to an equivalent tensor.- Specified by:
toTensor
in classAbstractDenseSemiringMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Returns:
- A tensor with the same shape and data as this matrix.
-
toTensor
Converts this matrix to an equivalent tensor with the specifiednewShape
.- Specified by:
toTensor
in classAbstractDenseSemiringMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Parameters:
newShape
- Shape of the tensor. Can be any rank but must be broadcastable to the shape of this matrix.- Returns:
- A tensor with the specified
newShape
and the same data as this matrix.
-
I
Constructs an identity matrix of the specified size.- Parameters:
size
- Size of the identity matrix.fieldValue
- Value of field to create identity matrix for.- Returns:
- An identity matrix of specified size.
- Throws:
IllegalArgumentException
- If the specified size is less than 1.- See Also:
-
I
Constructs an identity-like matrix of the specified shape. That is, a matrix of zeros with ones along the principle diagonal.- Parameters:
numRows
- Number of rows in the identity-like matrix.numCols
- Number of columns in the identity-like matrix.fieldValue
- Value of field to create identity matrix for.- Returns:
- An identity matrix of specified shape.
- Throws:
IllegalArgumentException
- If the specified number of rows or columns is less than 1.- See Also:
-
I
Constructs an identity-like matrix of the specified shape. That is, a matrix of zeros with ones along the principle diagonal.- Parameters:
shape
- The shape of the identity-like matrix to construct.fieldValue
- Value of field to create identity matrix for.- Returns:
- An identity matrix of specified shape.
- Throws:
IllegalArgumentException
- If the specified number of rows or columns is less than 1.- See Also:
-
toCoo
Converts this matrix to an equivalent sparse COO matrix.- Overrides:
toCoo
in classAbstractDenseSemiringMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Parameters:
estimatedSparsity
- Estimated sparsity of the matrix. Must be between 0 and 1 inclusive. If this is an accurate estimation it may provide a slight speedup and can reduce unneeded memory consumption. If memory is a concern, it is better to over-estimate the sparsity. If speed is the concern it is better to under-estimate the sparsity.- Returns:
- A sparse COO matrix that is equivalent to this dense matrix.
- See Also:
-
toCoo
Converts this dense tensor to an equivalent sparse COO tensor.- Overrides:
toCoo
in classAbstractDenseSemiringMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Returns:
- A sparse COO tensor equivalent to this dense tensor.
- See Also:
-
toCsr
Converts this matrix to an equivalent sparse CSR matrix.- Overrides:
toCsr
in classAbstractDenseSemiringMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Returns:
- A sparse CSR matrix that is equivalent to this dense matrix.
- See Also:
-
toCsr
Converts this matrix to an equivalent sparse CSR matrix.- Overrides:
toCsr
in classAbstractDenseSemiringMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Parameters:
estimatedSparsity
- Estimated sparsity of the matrix. Must be between 0 and 1 inclusive. If this is an accurate estimation it may provide a slight speedup and can reduce unneeded memory consumption. If memory is a concern, it is better to over-estimate the sparsity. If speed is the concern it is better to under-estimate the sparsity.- Returns:
- A sparse CSR matrix that is equivalent to this dense matrix.
- See Also:
-
pow
Computes the matrix multiplication of this matrix with itself
n
times. This matrix must be square.For large
n
values, this method may significantly more efficient than calling#mult(Matrix) this.mult(this)
n
times.- Parameters:
n
- Number of times to multiply this matrix with itself. Must be non-negative.- Returns:
- If
n=0
, then the identity
-
accept
Accepts a visitor that implements theMatrixVisitor
interface. This method is part of the "Visitor Pattern" and allows operations to be performed on the matrix without modifying the matrix's class directly.- 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
.
-
sub
Computes the element-wise difference of two matrices.
This method will throw an
UnsupportedOperationException
as subtraction is not defined for a general ring.- Specified by:
sub
in interfaceMatrixMixin<RingMatrix<T extends Ring<T>>,
RingMatrix<T extends Ring<T>>, RingVector<T extends Ring<T>>, T extends Ring<T>> - Specified by:
sub
in interfaceTensorOverRing<RingMatrix<T extends Ring<T>>,
RingMatrix<T extends Ring<T>>, T extends Ring<T>[], T extends Ring<T>> - Overrides:
sub
in classAbstractDenseRingMatrix<RingMatrix<T extends Ring<T>>,
RingVector<T extends Ring<T>>, T extends Ring<T>> - Parameters:
b
- Second matrix in the element-wise difference.- Returns:
- The element-wise difference of this matrix and
b
.
-
abs
Computes the element-wise absolute value of this tensor.- Returns:
- The element-wise absolute value of this tensor.
-
div
Computes the element-wise quotient of two matrices.
This method will throw an
UnsupportedOperationException
as division is not defined for a general ring.- Parameters:
b
- Second matrix in the element-wise quotient.- Returns:
- The element-wise quotient of this matrix and
b
.
-
equals
Checks if an object is equal to this matrix object.- Overrides:
equals
in classObject
- Parameters:
object
- Object to check equality with this matrix.- Returns:
true
if the two matrices have the same shape, are numerically equivalent, and are of typeRingMatrix
;false
otherwise.
-
hashCode
-
toString
-