Class DenseMatrix
- All Implemented Interfaces:
Serializable
,Iterable<MatrixTuple>
,Matrix
,Tensor
,ProtoSerializable<org.tribuo.math.protos.TensorProto>
- Direct Known Subclasses:
ShrinkingMatrix
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
The output of a successful Cholesky factorization.static final class
The output of a successful eigen decomposition.static final class
The output of a successful LU factorization.Nested classes/interfaces inherited from interface org.tribuo.math.la.Matrix
Matrix.Factorization
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Protobuf serialization version.protected final int
The number of rows.protected final int
The number of columns.static final double
Tolerance for non-zero diagonal values in the factorizations.protected final double[][]
The value array.Fields inherited from interface org.tribuo.protos.ProtoSerializable
DESERIALIZATION_METHOD_NAME, PROVENANCE_SERIALIZER
-
Constructor Summary
ConstructorDescriptionDenseMatrix
(int dim1, int dim2) Creates a dense matrix full of zeros.DenseMatrix
(DenseMatrix other) Copies the supplied matrix.DenseMatrix
(Matrix other) Copies the supplied matrix, densifying it if it's sparse. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int i, int j, double value) Adds the argument value to the value at the supplied index.void
addAcrossDim1
(int[] indices, double value) Adds the specified value to the specified elements across dimension 1.void
addAcrossDim2
(int[] indices, double value) Adds the specified value to the specified elements across dimension 2.void
broadcastIntersectAndAddInPlace
(SGDVector input, boolean broadcastOverDim1) Broadcasts the input vector and adds it to each row/column of the matrix.Computes the Cholesky factorization of a positive definite matrix.Returns a dense vector containing each column sum.double
columnSum
(int columnIndex) Calculates the sum of the specified column.copy()
Copies the matrix.static DenseMatrix
createDenseMatrix
(double[][] values) Defensively copies the values before construction.static DenseMatrix
createDenseMatrix
(SGDVector[] vectors) Constructs a new DenseMatrix copying the values from the supplied vectors.static DenseMatrix
deserializeFromProto
(int version, String className, com.google.protobuf.Any message) Deserialization factory.Eigen decomposition of a symmetric matrix.boolean
void
Applies aDoubleUnaryOperator
elementwise to thisTensor
.gatherAcrossDim1
(int[] elements) Constructs a dense vector by gathering values across dimension 1.gatherAcrossDim2
(int[] elements) Constructs a dense vector by gathering values across dimension 2.double
get
(int i, int j) Gets an element from thisMatrix
.getColumn
(int index) Returns a copy of the specified column.int
The size of the first dimension.int
The size of the second dimension.getRow
(int i) AnSGDVector
view of the row.int[]
getShape()
Returns an int array specifying the shape of thisTensor
.void
hadamardProductInPlace
(Tensor other, DoubleUnaryOperator f) Updates thisTensor
with the Hadamard product (i.e., a term by term multiply) of this andother
.int
hashCode()
void
intersectAndAddInPlace
(Tensor other, DoubleUnaryOperator f) Updates thisTensor
by adding all the values from the intersection withother
.boolean
isSquare()
Is this a square matrix?boolean
Returns true if this matrix is square and symmetric.iterator()
leftMultiply
(SGDVector input) Multiplies this Matrix by aSGDVector
returning a vector of the appropriate size.Computes the LU factorization of a square matrix.matrixMultiply
(Matrix other) Multiplies this Matrix by anotherMatrix
returning a matrix of the appropriate size.matrixMultiply
(Matrix other, boolean transposeThis, boolean transposeOther) Multiplies this Matrix by anotherMatrix
returning a matrix of the appropriate size.void
normalizeRows
(VectorNormalizer normalizer) Normalizes each row using the supplied normalizer in place.int
numActiveElements
(int row) The number of non-zero elements in that row.reshape
(int[] newShape) Reshapes the Tensor to the supplied shape.rightMultiply
(SGDVector input) Multiplies this Matrix by aSGDVector
returning a vector of the appropriate size.void
rowScaleInPlace
(DenseVector scalingCoefficients) Scales each row by the appropriate value in theDenseVector
.rowSum()
Generates aDenseVector
representing the sum of each row.double
rowSum
(int rowIndex) Calculates the sum of the specified row.selectColumns
(int[] columnIndices) Returns a new DenseMatrix containing a copy of the selected columns.selectColumns
(List<Integer> columnIndices) Returns a new DenseMatrix containing a copy of the selected columns.org.tribuo.math.protos.TensorProto
Serializes this object to a protobuf.void
set
(int i, int j, double value) Sets an element at the supplied location.void
Sets the column to the supplied vector value.double[][]
toArray()
Returns a copy of this matrix as a 2d array.toString()
Returns a transposed copy of this matrix.double
twoNorm()
Calculates the euclidean norm for this vector.protected static DenseMatrix
unpackProto
(org.tribuo.math.protos.DenseTensorProto proto) Unpacks aDenseTensorProto
into aDenseMatrix
.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface org.tribuo.math.la.Tensor
hadamardProductInPlace, intersectAndAddInPlace, scalarAddInPlace, scaleInPlace
-
Field Details
-
CURRENT_VERSION
public static final int CURRENT_VERSIONProtobuf serialization version.- See Also:
-
FACTORIZATION_TOLERANCE
public static final double FACTORIZATION_TOLERANCETolerance for non-zero diagonal values in the factorizations.- See Also:
-
values
protected final double[][] valuesThe value array. -
dim1
protected final int dim1The number of rows. -
dim2
protected final int dim2The number of columns.
-
-
Constructor Details
-
DenseMatrix
public DenseMatrix(int dim1, int dim2) Creates a dense matrix full of zeros.- Parameters:
dim1
- The first dimension.dim2
- The second dimension.
-
DenseMatrix
Copies the supplied matrix.- Parameters:
other
- The matrix to copy.
-
DenseMatrix
Copies the supplied matrix, densifying it if it's sparse.- Parameters:
other
- The matrix to copy.
-
-
Method Details
-
createDenseMatrix
Defensively copies the values before construction.Throws IllegalArgumentException if the supplied values are a ragged array.
- Parameters:
values
- The values of this dense matrix.- Returns:
- A new dense matrix.
-
createDenseMatrix
Constructs a new DenseMatrix copying the values from the supplied vectors.Throws
IllegalArgumentException
if the supplied vectors are ragged (i.e., are not all the same size).- Parameters:
vectors
- The vectors to coalesce.- Returns:
- A new dense matrix.
-
deserializeFromProto
public static DenseMatrix deserializeFromProto(int version, String className, com.google.protobuf.Any message) throws com.google.protobuf.InvalidProtocolBufferException Deserialization factory.- Parameters:
version
- The serialized object version.className
- The class name.message
- The serialized data.- Returns:
- The deserialized object.
- Throws:
com.google.protobuf.InvalidProtocolBufferException
- If the protobuf could not be parsed from themessage
.
-
unpackProto
Unpacks aDenseTensorProto
into aDenseMatrix
.- Parameters:
proto
- The proto to unpack.- Returns:
- The dense matrix.
-
serialize
public org.tribuo.math.protos.TensorProto serialize()Description copied from interface:ProtoSerializable
Serializes this object to a protobuf.- Specified by:
serialize
in interfaceProtoSerializable<org.tribuo.math.protos.TensorProto>
- Returns:
- The protobuf.
-
getShape
public int[] getShape()Description copied from interface:Tensor
Returns an int array specifying the shape of thisTensor
. -
reshape
Description copied from interface:Tensor
Reshapes the Tensor to the supplied shape. ThrowsIllegalArgumentException
if the shape isn't compatible. -
copy
Copies the matrix. -
get
public double get(int i, int j) Description copied from interface:Matrix
Gets an element from thisMatrix
. -
gatherAcrossDim1
Constructs a dense vector by gathering values across dimension 1.- Parameters:
elements
- The indices to gather.- Returns:
- A dense vector.
-
gatherAcrossDim2
Constructs a dense vector by gathering values across dimension 2.- Parameters:
elements
- The indices to gather.- Returns:
- A dense vector.
-
transpose
Returns a transposed copy of this matrix.- Returns:
- A transposed copy.
-
equals
-
hashCode
public int hashCode() -
set
public void set(int i, int j, double value) Description copied from interface:Matrix
Sets an element at the supplied location. -
getDimension1Size
public int getDimension1Size()Description copied from interface:Matrix
The size of the first dimension.- Specified by:
getDimension1Size
in interfaceMatrix
- Returns:
- The size of the first dimension.
-
getDimension2Size
public int getDimension2Size()Description copied from interface:Matrix
The size of the second dimension.- Specified by:
getDimension2Size
in interfaceMatrix
- Returns:
- The size of the second dimension.
-
leftMultiply
Description copied from interface:Matrix
Multiplies this Matrix by aSGDVector
returning a vector of the appropriate size.The input must have dimension equal to
Matrix.getDimension2Size()
.- Specified by:
leftMultiply
in interfaceMatrix
- Parameters:
input
- The input vector.- Returns:
- A new
SGDVector
of sizeMatrix.getDimension1Size()
.
-
rightMultiply
Description copied from interface:Matrix
Multiplies this Matrix by aSGDVector
returning a vector of the appropriate size.The input must have dimension equal to
Matrix.getDimension1Size()
.- Specified by:
rightMultiply
in interfaceMatrix
- Parameters:
input
- The input vector.- Returns:
- A new
SGDVector
of sizeMatrix.getDimension2Size()
.
-
matrixMultiply
Description copied from interface:Matrix
Multiplies this Matrix by anotherMatrix
returning a matrix of the appropriate size.The input must have dimension 1 equal to
Matrix.getDimension2Size()
.- Specified by:
matrixMultiply
in interfaceMatrix
- Parameters:
other
- The input matrix.- Returns:
- A new
Matrix
of sizeMatrix.getDimension1Size()
,input.getDimension2Size()
.
-
matrixMultiply
Description copied from interface:Matrix
Multiplies this Matrix by anotherMatrix
returning a matrix of the appropriate size.Must obey the rules of matrix multiplication after the transposes are applied.
- Specified by:
matrixMultiply
in interfaceMatrix
- Parameters:
other
- The input matrix.transposeThis
- Implicitly transposes this matrix just for the multiplication.transposeOther
- Implicitly transposes other just for the multiplication.- Returns:
- A new
Matrix
.
-
rowSum
Description copied from interface:Matrix
Generates aDenseVector
representing the sum of each row.- Specified by:
rowSum
in interfaceMatrix
- Returns:
- A new
DenseVector
of sizeMatrix.getDimension1Size()
.
-
rowScaleInPlace
Description copied from interface:Matrix
Scales each row by the appropriate value in theDenseVector
.- Specified by:
rowScaleInPlace
in interfaceMatrix
- Parameters:
scalingCoefficients
- ADenseVector
with sizeMatrix.getDimension1Size()
.
-
add
public void add(int i, int j, double value) Description copied from interface:Matrix
Adds the argument value to the value at the supplied index. -
addAcrossDim1
public void addAcrossDim1(int[] indices, double value) Adds the specified value to the specified elements across dimension 1.- Parameters:
indices
- The indices to update.value
- The value to add.
-
addAcrossDim2
public void addAcrossDim2(int[] indices, double value) Adds the specified value to the specified elements across dimension 2.- Parameters:
indices
- The indices to update.value
- The value to add.
-
intersectAndAddInPlace
Description copied from interface:Tensor
Updates thisTensor
by adding all the values from the intersection withother
.The function
f
is applied to all values fromother
before the addition.Each value is updated as value += f(otherValue).
- Specified by:
intersectAndAddInPlace
in interfaceTensor
- Parameters:
other
- The otherTensor
.f
- A function to apply.
-
hadamardProductInPlace
Description copied from interface:Tensor
Updates thisTensor
with the Hadamard product (i.e., a term by term multiply) of this andother
.The function
f
is applied to all values fromother
before the addition.Each value is updated as value *= f(otherValue).
- Specified by:
hadamardProductInPlace
in interfaceTensor
- Parameters:
other
- The otherTensor
.f
- A function to apply.
-
foreachInPlace
Description copied from interface:Tensor
Applies aDoubleUnaryOperator
elementwise to thisTensor
.- Specified by:
foreachInPlace
in interfaceTensor
- Parameters:
f
- The function to apply.
-
broadcastIntersectAndAddInPlace
Broadcasts the input vector and adds it to each row/column of the matrix.- Parameters:
input
- The input vector.broadcastOverDim1
- If true broadcasts over the first dimension, else broadcasts over the second.
-
numActiveElements
public int numActiveElements(int row) Description copied from interface:Matrix
The number of non-zero elements in that row.An element could be active and zero, if it was active on construction.
- Specified by:
numActiveElements
in interfaceMatrix
- Parameters:
row
- The index of the row.- Returns:
- The number of non-zero elements.
-
getRow
Description copied from interface:Matrix
AnSGDVector
view of the row.This refers to the same values as the matrix, so updating this vector will update the matrix.
-
getColumn
Returns a copy of the specified column. -
setColumn
Sets the column to the supplied vector value.- Parameters:
index
- The column to set.vector
- The vector to write.
-
rowSum
public double rowSum(int rowIndex) Calculates the sum of the specified row.- Parameters:
rowIndex
- The index of the row to sum.- Returns:
- The row sum.
-
columnSum
public double columnSum(int columnIndex) Calculates the sum of the specified column.- Parameters:
columnIndex
- The index of the column to sum.- Returns:
- The column sum.
-
twoNorm
public double twoNorm()Description copied from interface:Tensor
Calculates the euclidean norm for this vector. -
toArray
public double[][] toArray()Returns a copy of this matrix as a 2d array.- Returns:
- A copy of this matrix.
-
isSquare
public boolean isSquare()Is this a square matrix?- Returns:
- True if the matrix is square.
-
isSymmetric
public boolean isSymmetric()Returns true if this matrix is square and symmetric.- Returns:
- True if the matrix is symmetric.
-
choleskyFactorization
Computes the Cholesky factorization of a positive definite matrix.If the matrix is not symmetric or positive definite then it returns an empty optional.
- Returns:
- The Cholesky factorization or an empty optional.
-
luFactorization
Computes the LU factorization of a square matrix.If the matrix is singular or not square it returns an empty optional.
- Returns:
- The LU factorization or an empty optional.
-
eigenDecomposition
Eigen decomposition of a symmetric matrix.Non-symmetric matrices return an empty Optional as they may have complex eigenvalues, and any matrix which exceeds the default number of QL iterations in the decomposition also returns an empty Optional.
- Returns:
- The eigen decomposition of a symmetric matrix, or an empty optional if it's not symmetric.
-
toString
-
iterator
- Specified by:
iterator
in interfaceIterable<MatrixTuple>
-
normalizeRows
Normalizes each row using the supplied normalizer in place.- Parameters:
normalizer
- The vector normalizer to use.
-
columnSum
Returns a dense vector containing each column sum.- Returns:
- The column sums.
-
selectColumns
Returns a new DenseMatrix containing a copy of the selected columns.Throws
IllegalArgumentException
if any column index is invalid or the array is null/empty.- Parameters:
columnIndices
- The column indices- Returns:
- The submatrix comprising the selected columns.
-
selectColumns
Returns a new DenseMatrix containing a copy of the selected columns.Throws
IllegalArgumentException
if any column index is invalid or the list is null/empty.- Parameters:
columnIndices
- The column indices- Returns:
- The submatrix comprising the selected columns.
-