Class Matrix
- All Implemented Interfaces:
Serializable,TensorOverField<Matrix,,Matrix, double[], Double> MatrixMixin<Matrix,,Matrix, Vector, Double> TensorOverRing<Matrix,,Matrix, double[], Double> TensorOverSemiring<Matrix,Matrix, double[], Double>
Instances of this class represents a complex dense matrix backed by a double[] array. The Matrix class
provides functionality for real dense matrix operations, supporting mutable data with a fixed shape. This class extends
AbstractDenseDoubleTensor and offers additional methods optimized for complex
arithmetic and matrix computations.
A Matrix 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) andCSR(Compressed Sparse Row) formats. - Utility methods for checking properties like being orthogonal, symmetric, etc.
Example Usage:
// Constructing a complex matrix from a 2D array of complex numbers
double[][] data = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9,}};
Matrix matrix = new Matrix(data);
// Performing matrix multiplication.
Matrix result = matrix.mult(matrix);
// Performing matrix transpose.
Matrix transpose = matrix.T();
// Performing matrix transpose.
Matrix conjugateTranspose = matrix.T();
// Checking if the matrix is orthogonal.
boolean isUnitary = matrix.isOrthogonal();
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal intThe number of columns in this matrix.final intThe number of rows in this matrix.Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape -
Constructor Summary
ConstructorsConstructorDescriptionMatrix(double[][] data) Creates a real dense matrix whose data are specified by a double array.Matrix(int size) Constructs a square real dense matrix of a specified size.Matrix(int[][] data) Creates a real dense matrix whose data are specified by a double array.Matrix(int size, double value) Creates a square real dense matrix with a specified fill value.Matrix(int rows, int cols) Creates a real dense matrix of a specified shape filled with zeros.Matrix(int rows, int cols, double value) Creates a real dense matrix with a specified shape and fills the matrix with the specified value.Matrix(int numRows, int numCols, double... data) Constructs a matrix with specified shape and data.Creates a real dense matrix whose data are specified by a double array.Creates a real dense matrix whose data are specified by a double array.Creates a real dense matrix which is a copy of a specified matrix.Creates a real dense matrix with specified shape filled with zeros.Creates a real dense matrix with specified shape filled with a specific value.Creates a tensor with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescription<R> Raccept(MatrixVisitor<R> visitor) Accepts a visitor that implements theMatrixVisitorinterface.Sums this matrix with a complex dense matrix.add(CooCMatrix b) Sums this matrix with a real sparse COO matrix.Sums this matrix with a real sparse COO matrix.add(CsrCMatrix b) Sums this matrix with a real sparse CSR matrix.Sums this matrix with a real sparse CSR matrix.add(Complex128 b) Adds a complex-valued scalar to each entry of this matrix.Stacks matrices along rows.Augments a vector to this matrix.intGets the length of the data array which backs this matrix.det()Computes the determinant of a square matrix.static Matrixdiag(double... data) Constructs a diagonal matrix from an array specifying the diagonal elements of the matrix.static MatrixConstructs a diagonal matrix from a vector specifying the diagonal elements of the matrix.Computes the element-wise division between two tensors.div(Complex128 b) Computes the scalar division of this matrix with a complex number.Computes the element-wise product of two matrices.Computes the element-wise product of two matrices.Computes the element-wise product of two matrices.booleanChecks if an object is equal to this matrix object.Computes the Frobenius inner product of two matrices.flatten()Flattens this matrix to a row vector.flatten(int axis) Flattens this matrix along the specified axis.get(int row, int col) Gets the element of this matrix at this specifiedrowandcol.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, 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()static MatrixI(int size) Constructs an identity matrix of the specified size.static MatrixI(int numRows, int numCols) Constructs an identity-like matrix of the specified shape.static MatrixConstructs an identity-like matrix of the specified shape.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 tensor of the same type as this tensor with the given the shape and data.intComputes the rank of this matrix (i.e. the number of linearly independent rows/columns in this matrix).Computes the matrix multiplication between this matrix and a complex dense matrix.Computes the matrix-vector product of this matrix and a dense complex vector.Computes the matrix multiplication between two matrices.Computes matrix-vector multiplication.mult(CooCMatrix b) Computes the matrix multiplication between this matrix and a complex sparse COO matrix.mult(CooCVector b) Computes the matrix-vector product of this matrix and a complex sparse vector.Computes the matrix multiplication between this matrix and a real sparse COO matrix.Computes the matrix-vector product of this matrix and a real sparse vector.mult(CsrCMatrix b) Computes the matrix multiplication between this matrix and a complex sparse CSR matrix.Computes the matrix multiplication between this matrix and a real sparse CSR matrix.mult(Complex128 b) Computes the scalar multiplication of this matrix with a complex number.Multiplies this matrix with the transpose of thebtensor as if bythis.mult(b.T()).doublenorm()Computes the Frobenius (or \( L_{2, 2} \)) norm of this matrix.doublenorm(double p) Computes the matrix operator norm of this matrix "induced" by the vector p-norm.doublenorm(double p, double q) Computes the \( L_{p,q} \) norm of this matrix.intnumCols()Gets the number of columns in this matrix.intnumRows()Gets the number of rows in this matrix.pow(int n) Computes the matrix multiplication of this matrix with itselfntimes.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.Sets an index of this matrix to the specified value.setCol(double[] values, int colIndex) Sets a column of this matrix at the given index to the specified values.Sets a column of this matrix at the given index to the specified values.Sets a column of this matrix at the given index to the specified values.setRow(double[] values, int rowIndex) Sets a row of this matrix at the given index to the specified values.Sets a row of this matrix at the given index to the specified values.Sets a row of this matrix at the given index to the specified values.setSlice(double[][] values, int rowStart, int colStart) Sets a slice of this matrix to the specifiedvalues.Sets a slice of this matrix to the specifiedvalues.Sets a slice of this matrix to the specifiedvalues.setSliceCopy(Matrix values, int rowStart, int colStart) Creates a copy of this matrix and sets a slice of the copy to the specified values.setValues(double[][] values) Sets the value of this matrix using a 2D array.setValues(int[][] values) Sets the value of this matrix using a 2D array.Sets the value of this matrix using a 2D array.Sets the value of this matrix using a 2D array.Sets the value of this matrix using another matrix.Computes the element-wise square root of a tensor.Stacks matrices along columns.Computes the difference of this matrix with a complex dense matrix.sub(CooCMatrix b) Computes the difference of this matrix with a real sparse COO matrix.Computes the difference of this matrix with a real sparse COO matrix.sub(CsrCMatrix b) Computes the difference of this matrix with a real sparse CSR matrix.Computes the difference of this matrix with a real sparse CSR matrix.sub(Complex128 b) Subtracts a complex-valued scalar from each entry of this matrix.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.Converts this matrix to an equivalent complex matrix.toCoo()Converts this dense tensor to an equivalent sparse COO tensor.toCsr()Converts this dense matrix to sparse CSR matrix.toString()Generates a human-readable string representing this matrix.toTensor()Converts this matrix to an equivalentTensor.toVector()Converts this matrix to an equivalent vector.tr()Computes the trace of this matrix.Methods inherited from class org.flag4j.arrays.backend.primitive_arrays.AbstractDenseDoubleTensor
add, addEq, allClose, allClose, argmax, argmaxAbs, argmin, argminAbs, div, divEq, elemMult, elemMultEq, get, reshape, round, round, roundToZero, set, sub, subEq, tensorDot, tensorTrMethods inherited from class org.flag4j.arrays.backend.primitive_arrays.AbstractDoubleTensor
abs, add, 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, prod, recip, roundToZero, sqrt, sub, 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
-
numRows
public final int numRowsThe number of rows in this matrix. -
numCols
public final int numColsThe number of columns in this matrix.
-
-
Constructor Details
-
Matrix
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.
-
Matrix
public Matrix(int size) Constructs a square real dense matrix of a specified size. The data of the matrix will default to zero.- Parameters:
size- Size of the square matrix.- Throws:
IllegalArgumentException- if size negative.
-
Matrix
public Matrix(int size, double value) Creates a square real dense matrix with a specified fill value.- Parameters:
size- Size of the square matrix.value- Value to fill this matrix with.- Throws:
IllegalArgumentException- if size negative.
-
Matrix
public Matrix(int rows, int cols) Creates a real dense matrix of a specified shape filled with zeros.- Parameters:
rows- The number of rows in the matrix.cols- The number of columns in the matrix.- Throws:
IllegalArgumentException- if either m or n is negative.
-
Matrix
public Matrix(int rows, int cols, double value) Creates a real dense matrix with a specified shape and fills the matrix with the specified value.- Parameters:
rows- Number of rows in the matrix.cols- Number of columns in the matrix.value- Value to fill this matrix with.- Throws:
IllegalArgumentException- if either m or n is negative.
-
Matrix
Creates a real dense matrix whose data are specified by a double array.- Parameters:
data- Entries of the real dense matrix.
-
Matrix
Creates a real dense matrix whose data are specified by a double array.- Parameters:
data- Entries of the real dense matrix.
-
Matrix
public Matrix(double[][] data) Creates a real dense matrix whose data are specified by a double array.- Parameters:
data- Entries of the real dense matrix.
-
Matrix
public Matrix(int[][] data) Creates a real dense matrix whose data are specified by a double array.- Parameters:
data- Entries of the real dense matrix.
-
Matrix
Creates a real dense matrix which is a copy of a specified matrix.- Parameters:
A- The matrix defining the data for this matrix.
-
Matrix
Creates a real dense matrix with specified shape filled with zeros.- Parameters:
shape- Shape of matrix.- Throws:
IllegalArgumentException- If theshapeis not of rank 2.
-
Matrix
Creates a real dense matrix with specified shape filled with a specific value.- Parameters:
shape- Shape of matrix.value- Value to fill matrix with.- Throws:
IllegalArgumentException- If theshapeis not of rank 2.
-
Matrix
public Matrix(int numRows, int numCols, double... data) Constructs a matrix with specified shape and data.- Parameters:
numRows- Number of rows in this matrix.numCols- Number of columns in this matrix.data- Entries of the matrix.
-
-
Method Details
-
diag
Constructs a diagonal matrix from an array specifying the diagonal elements of the matrix.- Parameters:
data- Diagonal elements of the matrix. All other values will be zero.- Returns:
- A diagonal matrix whose diagonal elements are equal to
data. - See Also:
-
diag
Constructs a diagonal matrix from a vector specifying the diagonal elements of the matrix.- Parameters:
vec- Diagonal elements of the matrix. All other values will be zero.- Returns:
- A diagonal matrix whose diagonal elements are equal to the entries of
vec. - See Also:
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the given the shape and data.- Specified by:
makeLikeTensorin interfaceTensorOverSemiring<Matrix,Matrix, double[], Double> - Specified by:
makeLikeTensorin classAbstractTensor<Matrix,double[], Double> - Parameters:
shape- Shape of the tensor to construct.data- Entries of the tensor to construct.- Returns:
- A tensor of the same type as this tensor with the given the shape and data.
-
dataLength
public int dataLength()Gets the length of the data array which backs this matrix.- Specified by:
dataLengthin interfaceMatrixMixin<Matrix,Matrix, Vector, Double> - Overrides:
dataLengthin classAbstractDoubleTensor<Matrix>- Returns:
- The length of the data array which backs this matrix.
-
flatten
Flattens this matrix to a row vector.- Specified by:
flattenin classAbstractTensor<Matrix,double[], Double> - Returns:
- The flattened matrix.
- See Also:
-
flatten
Flattens this matrix along the specified axis.- Specified by:
flattenin classAbstractTensor<Matrix,double[], Double> - Parameters:
axis- Axis along which to flatten tensor.- Returns:
- A new matrix containing the same data as this matrix but flattened along the specified axis.
- If
axis == 0a matrix with the shape(this.numRows*this.numCols, 1)is returned. - If
axis == 1a matrix with the shape(1, this.numRows*this.numCols)is returned.
- If
- Throws:
ArrayIndexOutOfBoundsException- If the axis is negative or larger thanthis.{@link #getRank()}-1.- See Also:
-
I
Constructs an identity matrix of the specified size.- Parameters:
size- Size of the identity matrix.- 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.- 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- Shape of the identity-like matrix.- Returns:
- An identity matrix of specified size.
- Throws:
IllegalArgumentException- If the specified shape is not rank 2.- See Also:
-
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<Matrix,Matrix, Vector, 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.
-
matrixRank
public int matrixRank()Computes the rank of this matrix (i.e. the number of linearly independent rows/columns in this matrix).
This is computed as the number of singular values greater than
tolwhere:double tol = 2.0*Math.max(rows, cols)*Flag4jConstants.EPS_F64*Math.min(this.numRows, this.numCols);Note the "matrix rank" is NOT related to the "
tensor rank" which is number of indices needed to uniquely specify an entry in the tensor.- Returns:
- The matrix rank of this matrix.
-
mult
Computes the matrix multiplication between two matrices.- Specified by:
multin interfaceMatrixMixin<Matrix,Matrix, Vector, 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 matrix B.
-
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<Matrix,Matrix, Vector, 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<Matrix,Matrix, Vector, 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<Matrix,Matrix, Vector, 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<Matrix,Matrix, Vector, 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<Matrix,Matrix, Vector, 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<Matrix,Matrix, Vector, 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<Matrix,Matrix, Vector, Double> - Returns:
trueif this matrix is symmetric;falseotherwise.
-
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<Matrix,Matrix, Vector, Double> - Returns:
trueif this matrix is Hermitian;falseotherwise.
-
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<Matrix,Matrix, Vector, Double> - Returns:
trueif this matrix it is orthogonal;falseotherwise.
-
removeRow
-
removeRows
Removes a specified set of rows from this matrix.- Specified by:
removeRowsin interfaceMatrixMixin<Matrix,Matrix, Vector, 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
-
removeCols
Removes a specified set of columns from this matrix.- Specified by:
removeColsin interfaceMatrixMixin<Matrix,Matrix, Vector, 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<Matrix,Matrix, Vector, 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.
-
setSlice
Sets a slice of this matrix to the specifiedvalues. TherowStartandcolStartparameters specify the upper left index location of the slice to set within this matrix.- 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 reference to this matrix.
- Throws:
IllegalArgumentException- IfrowStartorcolStartare not within the matrix.IllegalArgumentException- If thevaluesslice, with upper left corner at the specified location, does not fit completely within this matrix.
-
setSlice
Sets a slice of this matrix to the specifiedvalues. TherowStartandcolStartparameters specify the upper left index location of the slice to set within this matrix.- 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 reference to this matrix.
- Throws:
IllegalArgumentException- IfrowStartorcolStartare not within the matrix.IllegalArgumentException- If thevaluesslice, with upper left corner at the specified location, does not fit completely within this matrix.
-
setSlice
Sets a slice of this matrix to the specifiedvalues. TherowStartandcolStartparameters specify the upper left index location of the slice to set within this matrix.- 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 reference to this matrix.
- Throws:
IllegalArgumentException- IfrowStartorcolStartare not within the matrix.IllegalArgumentException- If thevaluesslice, 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<Matrix,Matrix, Vector, 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
-
setValues
Sets the value of this matrix using a 2D array.- Parameters:
values- New values of the matrix.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If the values array has a different shape then this matrix.
-
setValues
Sets the value of this matrix using a 2D array.- Parameters:
values- New values of the matrix.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If thevaluesarray has a different shape then this matrix.
-
setValues
Sets the value of this matrix using a 2D array.- Parameters:
values- New values of the matrix.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If thevaluesarray has a different shape then this matrix.
-
setValues
Sets the value of this matrix using a 2D array.- Parameters:
values- New values of the matrix.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If thevaluesarray has a different shape then this matrix.
-
setValues
Sets the value of this matrix using another matrix.- Parameters:
values- New values of the matrix.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If thevaluesmatrix has a different shape then this matrix.
-
setCol
Sets a column of this matrix at the given index to the specified values.- Specified by:
setColin interfaceMatrixMixin<Matrix,Matrix, Vector, Double> - Parameters:
values- New values for the column.colIndex- The index of the column which is to be set.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If thevaluesvector has a different length than the number of rows of this matrix.
-
setCol
Sets a column of this matrix at the given index to the specified values.- Parameters:
values- New values for the column.colIndex- The index of the column which is to be set.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If thevaluesarray has a different length than the number of rows of this matrix.
-
setCol
Sets a column of this matrix at the given index to the specified values.- Parameters:
values- New values for the column.colIndex- The index of the column which is to be set.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If thevaluesarray has a different length than the number of rows of this matrix.
-
setRow
Sets a row of this matrix at the given index to the specified values.- Specified by:
setRowin interfaceMatrixMixin<Matrix,Matrix, Vector, Double> - Parameters:
values- New values for the row.rowIndex- The index of the row which is to be set.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If thevaluesvector has a different length than the number of rows of this matrix.
-
setRow
Sets a row of this matrix at the given index to the specified values.- Parameters:
values- New values for the row.rowIndex- The index of the row which is to be set.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If thevaluesarray has a different length than the number of rows of this matrix.
-
setRow
Sets a row of this matrix at the given index to the specified values.- Parameters:
values- New values for the row.rowIndex- The index of the row which is to be set.- Returns:
- A reference to this matrix.
- Throws:
IllegalArgumentException- If thevaluesarray has a different length than the number of rows of this matrix.
-
sqrtComplex
Computes the element-wise square root of a tensor.- Returns:
- The result of applying an element-wise square root to this tensor. Note, this method will compute the principle square root i.e. the square root with positive real part.
-
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<Matrix,Matrix, Vector, 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<Matrix,Matrix, Vector, 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<Matrix,Matrix, Vector, 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
-
getRow
Gets a specified row of this matrix betweencolStart(inclusive) andcolEnd(exclusive).- Specified by:
getRowin interfaceMatrixMixin<Matrix,Matrix, Vector, 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
Gets a specified column of this matrix betweenrowStart(inclusive) androwEnd(exclusive).- Specified by:
getColin interfaceMatrixMixin<Matrix,Matrix, Vector, 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:
IndexOutOfBoundsException- If eithercolEndarecolStartout of bounds for the shape of this matrix.IllegalArgumentException- IfrowEndis less thanrowStart.
-
getDiag
-
getDiag
Gets the elements of this matrix along the specified diagonal.- Specified by:
getDiagin interfaceMatrixMixin<Matrix,Matrix, Vector, 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.
-
T
-
T
Computes the transpose of a tensor by exchangingaxis1andaxis2.- Overrides:
Tin classAbstractDenseDoubleTensor<Matrix>- 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.- Overrides:
Tin classAbstractDenseDoubleTensor<Matrix>- 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:
-
toCoo
Converts this dense tensor to an equivalent sparse COO tensor.- Returns:
- A sparse COO tensor equivalent to this dense tensor.
- See Also:
-
toCsr
Converts this dense matrix to sparse CSR matrix.- Returns:
- A sparse CSR matrix equivalent to this dense matrix.
- See Also:
-
toComplex
Converts this matrix to an equivalent complex matrix.- Returns:
- A complex matrix with real components equal to the data of this matrix and imaginary components set to zero.
-
add
-
add
-
add
-
add
Sums this matrix with a real sparse CSR matrix.- Parameters:
b- real sparse CSR matrix in the sum.- Returns:
- The element-wise sum of this matrix and
b
-
add
Sums this matrix with a real sparse COO matrix.- Parameters:
b- real sparse CSR matrix in the sum.- Returns:
- The element-wise sum of this matrix and
b
-
add
Adds a complex-valued scalar to each entry of this matrix.- Parameters:
b- Scalar to add to this matrix.- Returns:
- A matrix containing the sum of each entry in this matrix with
b.
-
sub
-
sub
-
sub
-
sub
Computes the difference of this matrix with a real sparse CSR matrix.- Parameters:
b- real sparse CSR matrix in the difference.- Returns:
- The element-wise difference of this matrix and
b
-
sub
Computes the difference of this matrix with a real sparse COO matrix.- Parameters:
b- real sparse CSR matrix in the difference.- Returns:
- The element-wise difference of this matrix and
b
-
sub
Subtracts a complex-valued scalar from each entry of this matrix.- Parameters:
b- Scalar to subtract from each entry of this matrix.- Returns:
- A matrix containing the difference of each entry in this matrix with
b.
-
mult
Computes the matrix multiplication between this matrix and a complex dense matrix.- Parameters:
b- The complex dense matrix in the matrix multiplication.- Returns:
- The matrix product between this matrix and
b. - Throws:
TensorShapeException- Ifthis.numCols != b.numRows.
-
mult
Computes the matrix multiplication between this matrix and a real sparse CSR matrix.- Parameters:
b- The real sparse matrix in the matrix multiplication.- Returns:
- The matrix product between this matrix and
b. - Throws:
TensorShapeException- Ifthis.numCols != b.numRows.
-
mult
Computes the matrix multiplication between this matrix and a complex sparse CSR matrix.- Parameters:
b- The complex sparse matrix in the matrix multiplication.- Returns:
- The matrix product between this matrix and
b. - Throws:
TensorShapeException- Ifthis.numCols != b.numRows.
-
mult
Computes the matrix multiplication between this matrix and a real sparse COO matrix.- Parameters:
b- The real sparse matrix in the matrix multiplication.- Returns:
- The matrix product between this matrix and
b. - Throws:
TensorShapeException- Ifthis.numCols != b.numRows.
-
mult
Computes the matrix multiplication between this matrix and a complex sparse COO matrix.- Parameters:
b- The complex sparse matrix in the matrix multiplication.- Returns:
- The matrix product between this matrix and
b. - Throws:
TensorShapeException- Ifthis.numCols != b.numRows.
-
mult
-
mult
-
mult
Computes the matrix-vector product of this matrix and a complex sparse vector.- Parameters:
b- Vector in the matrix-vector product.- Returns:
- The matrix-vector product of this matrix and the vector
b.
-
mult
Computes the scalar multiplication of this matrix with a complex number.- Parameters:
b- Complex valued scalar to multiply with this matrix.- Returns:
- The matrix-scalar product of this matrix and
b.
-
div
Computes the element-wise division between two tensors.- Parameters:
b- The denominator tensor in the element-wise quotient.- Returns:
- The element-wise quotient of this tensor and
b. - Throws:
TensorShapeException- If this tensor andb's shape are not equal.
-
div
Computes the scalar division of this matrix with a complex number.- Parameters:
b- Complex valued scalar to divide this matrix by.- Returns:
- The matrix-scalar quotient of this matrix and
b.
-
pow
Computes the matrix multiplication of this matrix with itself
ntimes. This matrix must be square.For large
nvalues, this method may be significantly more efficient than callingthis.mult(this)a total ofntimes.- Parameters:
n- Number of times to multiply this matrix with itself. Must be non-negative.- Returns:
- If
n=0, then the identity
-
elemMult
-
elemMult
-
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.
-
H
Computes the conjugate transpose of a tensor by exchanging the first and last axes of this tensor and conjugating the exchanged values. -
norm
public double norm()Computes the Frobenius (or \( L_{2, 2} \)) norm of this matrix.
The Frobenius norm is defined as the square root of the sum of absolute squares of all entries in the matrix.
- Returns:
- the Frobenius of this tensor.
-
norm
public double norm(double p) Computes the matrix operator norm of this matrix "induced" by the vector p-norm. Specifically, this method computes the operator norm of the matrix as: \[ ||A||_p = \sup_{x \ne 0} \cfrac{||Ax||_p}{||x||_p} \]
This method supports a limited set of
pvalues which yield simple formulas. Whenp < 1, the result this method returns is not a true mathematical norm. However, these values may still be useful for numerical purposes.p=1: The maximum absolute column sum.p=-1: The minimum absolute column sum.p=2: The spectral norm. Equivalent to the largest singular value of the matrix.p=-2: The smallest singular value of the matrix.p=Double.POSITIVE_INFINITY: The maximum absolute row sum.p=Double.NEGATIVE_INFINITY: The minimum absolute row sum.
- Parameters:
p- The p value in the "induced" p-norm. Must be one of the following:1,-1,2,-2,Double.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITY.- Returns:
- Norm of the matrix.
- Throws:
IllegalArgumentException- Ifpis not one of the following:1,-1,2,-2,Double.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITY.
-
norm
public double norm(double p, double q) Computes the \( L_{p,q} \) norm of this matrix.
Some common special cases are:
p=2,q=1: The sum of Euclidean norms of the column vectors of the matrix.p=2,q=2: The Frobenius norm. Equivalent to the Euclidean norm of the vector of singular values of the matrix.
The \( L_{p,q} \) norm is computed as if by:
double norm = 0; for(int j=0; j<src.numCols; j++) { double sum = 0; for(int i=0; i<src.numRows; i++) sum += Math.pow(Math.abs(src.get(i, j)), p); norm += Math.pow(sum, q / p); } return Math.pow(norm, 1.0 / q);- Parameters:
p- p value in the \( L_{p,q} \) norm.q- q value in the \( L_{p,q} \) norm.- Returns:
- The \( L_{p,q} \) norm of
src.
-
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<Matrix,Matrix, Vector, 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 object. -
hashCode
-
toString
-