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
Modifier and TypeFieldDescriptionfinal int
The number of columns in this matrix.final int
The number of rows in this matrix.Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorDescriptionMatrix
(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> R
accept
(MatrixVisitor<R> visitor) Accepts a visitor that implements theMatrixVisitor
interface.add
(Complex128 b) Adds a complex-valued scalar to each entry of this matrix.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.Stacks matrices along rows.Augments a vector to this matrix.int
Gets the length of the data array which backs this matrix.det()
Computes the determinant of a square matrix.static Matrix
diag
(double... data) Constructs a diagonal matrix from an array specifying the diagonal elements of the matrix.static Matrix
Constructs a diagonal matrix from a vector specifying the diagonal elements of the matrix.div
(Complex128 b) Computes the scalar division of this matrix with a complex number.Computes the element-wise division between two tensors.Computes the element-wise product of two matrices.Computes the element-wise product of two matrices.Computes the element-wise product of two matrices.boolean
Checks 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 specifiedrow
andcol
.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.int
hashCode()
static Matrix
I
(int size) Constructs an identity matrix of the specified size.static Matrix
I
(int numRows, int numCols) Constructs an identity-like matrix of the specified shape.static Matrix
Constructs an identity-like matrix of the specified shape.boolean
Checks that this matrix is close to the identity matrix.boolean
Checks if a matrix is Hermitian.boolean
isI()
Checks if this matrix is the identity matrix.boolean
Checks if this matrix is orthogonal.boolean
Checks if a matrix is symmetric.boolean
isTriL()
Checks if this matrix is lower triangular.boolean
isTriU()
Checks if this matrix is upper triangular.makeLikeTensor
(Shape shape, double[] data) Constructs a tensor of the same type as this tensor with the given the shape and data.int
Computes the rank of this matrix (i.e. the number of linearly independent rows/columns in this matrix).mult
(Complex128 b) Computes the scalar multiplication of this matrix with a complex number.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.Multiplies this matrix with the transpose of theb
tensor as if bythis.mult(b.T())
.int
numCols()
Gets the number of columns in this matrix.int
numRows()
Gets the number of rows in this matrix.pow
(int n) Computes the matrix multiplication of this matrix with itselfn
times.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.sub
(Complex128 b) Subtracts a complex-valued scalar from each entry of this matrix.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.swapCols
(int colIndex1, int colIndex2) Swaps specified columns in the matrix.swapRows
(int rowIndex1, int rowIndex2) Swaps specified rows in the matrix.T()
Computes the transpose of a tensor by exchanging the first and last axes of this tensor.T
(int... axes) Computes the transpose of this tensor.T
(int axis1, int axis2) Computes the transpose of a tensor by exchangingaxis1
andaxis2
.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, tensorTr
Methods 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, sum
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
-
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 theshape
is 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 theshape
is 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:
makeLikeTensor
in interfaceTensorOverSemiring<Matrix,
Matrix, double[], Double> - Specified by:
makeLikeTensor
in 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:
dataLength
in interfaceMatrixMixin<Matrix,
Matrix, Vector, Double> - Returns:
- The length of the data array which backs this matrix.
-
flatten
Flattens this matrix to a row vector.- Specified by:
flatten
in classAbstractTensor<Matrix,
double[], Double> - Returns:
- The flattened matrix.
- See Also:
-
flatten
Flattens this matrix along the specified axis.- Specified by:
flatten
in 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 == 0
a matrix with the shape(this.numRows*this.numCols, 1)
is returned. - If
axis == 1
a 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 specifiedrow
andcol
. -
tr
Computes the trace of this matrix. That is, the sum of elements along the principle diagonal of this matrix.
Same as
MatrixMixin.trace()
.- Specified by:
tr
in interfaceMatrixMixin<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
tol
where: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:
mult
in 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 theb
tensor as if bythis.mult(b.T())
. For large matrices, this method may be significantly faster than directly computing the transpose followed by the multiplication asthis.mult(b.T())
.- Specified by:
multTranspose
in interfaceMatrixMixin<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:
fib
in 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:
stack
in 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 matrixb
have a different number of columns.- See Also:
-
augment
Stacks matrices along rows.- Specified by:
augment
in interfaceMatrixMixin<Matrix,
Matrix, Vector, Double> - Parameters:
b
- Matrix to stack to this matrix.- Returns:
- The result of stacking
b
to the right of this matrix. - Throws:
IllegalArgumentException
- If this matrix and matrixb
have a different number of rows.- See Also:
-
augment
-
swapRows
Swaps specified rows in the matrix. This is done in place.- Specified by:
swapRows
in 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:
swapCols
in 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:
isSymmetric
in interfaceMatrixMixin<Matrix,
Matrix, Vector, Double> - Returns:
true
if this matrix is symmetric;false
otherwise.
-
isHermitian
public boolean isHermitian()Checks if a matrix is Hermitian. That is, if the matrix is square and equal to its conjugate transpose.- Specified by:
isHermitian
in interfaceMatrixMixin<Matrix,
Matrix, Vector, Double> - Returns:
true
if this matrix is Hermitian;false
otherwise.
-
isOrthogonal
public boolean isOrthogonal()Checks if this matrix is orthogonal. That is, if the inverse of this matrix is equal to its transpose.- Specified by:
isOrthogonal
in interfaceMatrixMixin<Matrix,
Matrix, Vector, Double> - Returns:
true
if this matrix it is orthogonal;false
otherwise.
-
removeRow
-
removeRows
Removes a specified set of rows from this matrix.- Specified by:
removeRows
in 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:
removeCols
in 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:
setSliceCopy
in 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
. TherowStart
andcolStart
parameters 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
- IfrowStart
orcolStart
are not within the matrix.IllegalArgumentException
- If thevalues
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
. TherowStart
andcolStart
parameters 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
- IfrowStart
orcolStart
are not within the matrix.IllegalArgumentException
- If thevalues
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
. TherowStart
andcolStart
parameters 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
- IfrowStart
orcolStart
are not within the matrix.IllegalArgumentException
- If thevalues
slice, with upper left corner at the specified location, does not fit completely within this matrix.
-
getSlice
Gets a specified slice of this matrix.- Specified by:
getSlice
in interfaceMatrixMixin<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
- IfrowEnd
is not greater thanrowStart
or ifcolEnd
is 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 thevalues
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 thevalues
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 thevalues
array 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 thevalues
matrix has a different shape then this matrix.
-
setCol
Sets a column of this matrix at the given index to the specified values.- Specified by:
setCol
in 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 thevalues
vector 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 thevalues
array 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 thevalues
array 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:
setRow
in 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 thevalues
vector 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 thevalues
array 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 thevalues
array 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:
getTriU
in 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
- IfdiagOffset
is not in the range (-numRows, numCols).
-
getTriL
Extracts the lower-triangular portion of this matrix with a specified diagonal offset. All other data of the resulting matrix will be zero.- Specified by:
getTriL
in interfaceMatrixMixin<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
- IfdiagOffset
is not in the range (-numRows, numCols).
-
mult
Computes matrix-vector multiplication.- Specified by:
mult
in 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:
getRow
in 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
rowIdx
of this matrix between thecolStart
andcolEnd
indices. - Throws:
IndexOutOfBoundsException
- If eithercolEnd
arecolStart
out of bounds for the shape of this matrix.IllegalArgumentException
- IfcolEnd
is less thancolStart
.
-
getCol
Gets a specified column of this matrix betweenrowStart
(inclusive) androwEnd
(exclusive).- Specified by:
getCol
in 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
colIdx
of this matrix between therowStart
androwEnd
indices. - Throws:
IndexOutOfBoundsException
- If eithercolEnd
arecolStart
out of bounds for the shape of this matrix.IllegalArgumentException
- IfrowEnd
is less thanrowStart
.
-
getDiag
-
getDiag
Gets the elements of this matrix along the specified diagonal.- Specified by:
getDiag
in 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-diagonaldiagOffset
below the principle diagonal are collected. - If
diagOffset > 0
: Then the elements of the super-diagonaldiagOffset
above the principle diagonal are collected.
- If
- Returns:
- The elements of the specified diagonal as a vector.
-
T
-
T
Computes the transpose of a tensor by exchangingaxis1
andaxis2
.- Overrides:
T
in 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 eitheraxis1
oraxis2
are out of bounds for the rank of this tensor.- See Also:
-
T
Computes the transpose of this tensor. That is, permutes the axes of this tensor so that it matches the permutation specified byaxes
.- Overrides:
T
in classAbstractDenseDoubleTensor<Matrix>
- Parameters:
axes
- Permutation of tensor axis. If the tensor has rankN
, then this must be an array of lengthN
which is a permutation of{0, 1, 2, ..., N-1}
.- Returns:
- The transpose of this tensor with its axes permuted by the
axes
array. - Throws:
IndexOutOfBoundsException
- If any element ofaxes
is out of bounds for the rank of this tensor.IllegalArgumentException
- Ifaxes
is not a permutation of{1, 2, 3, ... N-1}
.- See Also:
-
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
n
times. This matrix must be square.For large
n
values, this method may be significantly more efficient than callingthis.mult(this)
a total ofn
times.- 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. -
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.- Specified by:
accept
in 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
-