Class SmartMatrix
The SmartMatrix
class provides a unified interface for performing operations on matrices of various types
without requiring users to know the specific underlying implementation. It wraps a base matrix type, such as
Matrix
, CMatrix
, FieldMatrix
, or SemiringMatrix
, and delegates operations to the
appropriate concrete implementation. The concrete implementation must implement MatrixMixin
.
Features:
The SmartMatrix
class supports most basic matrix operations but may be more limited than the base concrete matrix types.
- Support for element-wise operations such as addition, subtraction, multiplication, and division.
- Matrix-specific operations, including transpose and conjugate transpose.
- Compatibility with heterogeneous matrix types, allowing flexible operation dispatch.
- Support for methods which return a scalar (e.g.
tr(Class)
). - Human-readable string representations and standard equality checks.
Usage:
- Creating and adding a real dense and complex dense matrix:
double[][] realData = {{1.0, 2.0}, {3.0, 4.0}}; SmartMatrix realMatrix = new SmartMatrix(realData); Complex128[][] complexData = { {new Complex128(1.0, 1.0), new Complex128(2.0, 2.0)}, {new Complex128(3.0, 3.0), new Complex128(4.0, 4.0)} }; SmartMatrix complexMatrix = new SmartMatrix(complexData); // Compute real-complex element-wise sum. SmartMatrix result = realMatrix.add(complexMatrix);
- Creating and multiplying a real dense and complex sparse CSR matrix:
// Create real dense matrix. SmartMatrix realDenseMatrix = new SmartMatrix(new Matrix(...)); // Create complex sparse CSR matrix. SmartMatrix complexCsrMatrix = new SmartMatrix(new CsrCMatrix(...)); // Compute real-dense complex-csr matrix multiplication. SmartMatrix result = realDenseMatrix.mult(complexCsrMatrix);
- Example of attempting to compute an unsupported operation:
// Create real dense matrix. SmartMatrix realDenseMatrix = new SmartMatrix(new Matrix(...)); // Create real sparse COO matrix. SmartMatrix complexCsrMatrix = new SmartMatrix(new CooMatrix(...)); // Will throw UnsupportedOperationException as element-wise division // is not supported between dense and sparse matrices. SmartMatrix result = realDenseMatrix.div(complexCsrMatrix);
- Specifying class for methods which return a scalar:
// Create real dense matrix. SmartMatrix realMatrix = new SmartMatrix(new Matrix(...)); // Class of the expected return type must be provided. // If class is incorrect, a ClassCastException will be thrown. Double traceReal = realMatrix.tr(Double.class); // Create complex dense matrix. SmartMatrix complexMatrix = new SmartMatrix(new CMatrix(...)); Complex128 traceComplex = complexMatrix.tr(Complex128.class);
Notes:
- Operations between incompatible base matrix types will result in an
UnsupportedOperationException
. - For example: attempting to divide a dense matrix by a sparse matrix or adding a
real matrix
to a generalfield matrix
. - Equality checks (by
equals(Object)
) are strict and include type comparison as well as shape and numerical equivalence. This means even if aMatrix
andCMatrix
are numerically equal, theequals(Object)
method will returnfalse
because they are not the same type.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionSmartMatrix
(double[][] data) Constructs aSmartMatrix
from a 2D primitive double array.SmartMatrix
(Complex128[][] matrix) Constructs aSmartMatrix
from a 2DComplex128
array.SmartMatrix
(MatrixMixin<?, ?, ?, ?> matrix) Constructs aSmartMatrix
which is backed by the specified matrix.SmartMatrix
(T[][] matrix) Constructs aSmartMatrix
from a 2DField
array.SmartMatrix
(T[][] matrix) Constructs aSmartMatrix
from a 2DSemiringMatrix
array. -
Method Summary
Modifier and TypeMethodDescriptionadd
(SmartMatrix b) Computes the element-wise sum of two matrices.div
(SmartMatrix b) Computes the element-wise quotient of two matrices.Computes the element-wise product of two matrices.boolean
Checks if an object is equal to this matrix object.<T> T
Gets the element from this matrix at the specified indices.MatrixMixin
<?, ?, ?, ?> Gets reference to the matrix which backs thisSmartMatrix
.getShape()
Gets the shape of this matrix.H()
Computes the conjugate transpose of this matrix.int
hashCode()
mult
(SmartMatrix b) Computes the matrix multiplication of two matrices.sub
(SmartMatrix b) Computes the element-wise difference of two matrices.T()
Computes the transpose of this matrix.toString()
Converts this matrix to a human-readable string.<T> T
Computes the trace of this matrix.
-
Constructor Details
-
SmartMatrix
Constructs aSmartMatrix
which is backed by the specified matrix.- Parameters:
matrix
- The backing matrix for thisSmartMatrix
.
-
SmartMatrix
public SmartMatrix(double[][] data) Constructs aSmartMatrix
from a 2D primitive double array. The matrix which backs thisSmartMatrix
will be an instance ofMatrix
.- Parameters:
data
- Array specifying shape and data of thisSmartMatrix
.
-
SmartMatrix
Constructs aSmartMatrix
from a 2DComplex128
array. The matrix which backs thisSmartMatrix
will be an instance ofCMatrix
.- Parameters:
data
- Array specifying shape and data of thisSmartMatrix
.
-
SmartMatrix
public SmartMatrix(T[][] matrix) Constructs aSmartMatrix
from a 2DField
array. The matrix which backs thisSmartMatrix
will be an instance ofFieldMatrix
.- Parameters:
data
- Array specifying shape and data of thisSmartMatrix
.
-
SmartMatrix
public SmartMatrix(T[][] matrix) Constructs aSmartMatrix
from a 2DSemiringMatrix
array. The matrix which backs thisSmartMatrix
will be an instance ofSemiringMatrix
.- Parameters:
data
- Array specifying shape and data of thisSmartMatrix
.
-
-
Method Details
-
getMatrix
Gets reference to the matrix which backs thisSmartMatrix
.- Returns:
- The matrix which backs this
SmartMatrix
.
-
add
Computes the element-wise sum of two matrices.- Parameters:
b
- The second matrix in the element-wise sum.- Returns:
- The element-wise sum of this matrix and
b
.
-
sub
Computes the element-wise difference of two matrices.- Parameters:
b
- The second matrix in the element-wise difference.- Returns:
- The element-wise difference of this matrix and
b
.
-
elemMult
Computes the element-wise product of two matrices.- Parameters:
b
- The second matrix in the element-wise product.- Returns:
- The element-wise product of this matrix and
b
.
-
div
Computes the element-wise quotient of two matrices.- Parameters:
b
- The second matrix in the element-wise quotient.- Returns:
- The element-wise quotient of this matrix and
b
.
-
mult
Computes the matrix multiplication of two matrices.- Parameters:
b
- The second matrix in the matrix multiplication problem.- Returns:
- The result of the matrix multiplication of this matrix and
b
.
-
T
Computes the transpose of this matrix.- Returns:
- The transpose of this matrix.
-
H
Computes the conjugate transpose of this matrix. This may not be supported for all matrix types.- Returns:
- The conjugate transpose of this matrix.
-
tr
Computes the trace of this matrix. The result is a scalar value whose type is dependent on the underlying backing matrix of this
SmartMatrix
instance (e.g.,Double
,Complex128
, etc.).This method uses a
Class
object to specify the expected type of the resulting scalar. It ensures type safety by attempting to cast the result to the specified type. If the cast is not valid, aClassCastException
will be thrown at runtime.Usage Examples:
- Real Matrix:
SmartMatrix realMatrix = new SmartMatrix(new double[][] { {1.0, 2.0}, {3.0, 4.0}}); Double128 realTrace = realMatrix.tr(Double.class); System.out.println("Trace: " + realMatrix);
- Complex Matrix:
SmartMatrix complexMatrix = new SmartMatrix(new Complex128[][] { {new Complex128(1.0, 1.0), new Complex128(2.0, 2.0)}, {new Complex128(3.0, 3.0), new Complex128(4.0, 4.0)}}); Complex128 complexTrace = complexMatrix.tr(Complex128.class); System.out.println("Trace: " + complexTrace);
- Type Parameters:
T
- The type of the scalar expected as the result.- Parameters:
type
- TheClass
object representing the expected type of the resulting scalar.- Returns:
- The trace of this matrix as an instance of the specified type
T
. - Throws:
ClassCastException
- If the resulting trace cannot be cast to the specified typeT
.UnsupportedOperationException
- If the trace computation is not supported by the backing matrix of thisSmartMatrix
instance.
- Real Matrix:
-
get
Gets the element from this matrix at the specified indices. The result is a scalar value whose type is dependent on the underlying backing matrix of thisSmartMatrix
instance (e.g.,Double
,Complex128
, etc.).This method uses a
Class
object to specify the expected type of the resulting scalar. It ensures type safety by attempting to cast the result to the specified type. If the cast is not valid, aClassCastException
will be thrown at runtime.- Parameters:
rowIdx
- Row index of the element to get.colIdx
- Column index of the element to get.type
- TheClass
object representing the expected type of the resulting scalar.- Returns:
- The element of this matrix at the specified row and column index.
- Throws:
ClassCastException
- If the resulting trace cannot be cast to the specified typeT
.UnsupportedOperationException
- If the trace computation is not supported by the backing matrix of this invalid input: '{@code @param <T> The type of the scalar expected as the result.'
-
getShape
-
equals
Checks if an object is equal to this matrix object. -
hashCode
-
toString
-