Package org.tribuo.math.la
Class DenseSparseMatrix
java.lang.Object
org.tribuo.math.la.DenseSparseMatrix
- All Implemented Interfaces:
Serializable
,Iterable<MatrixTuple>
,Matrix
,Tensor
A matrix which is dense in the first dimension and sparse in the second.
Backed by an array of SparseVector
.
- See Also:
-
Constructor Summary
ConstructorDescriptionDenseSparseMatrix
(int dim1, int dim2) Creates a DenseSparseMatrix with no values or indices.DenseSparseMatrix
(List<SparseVector> values) Constructs a DenseSparseMatrix out of the supplied sparse vector list.Creates a new DenseSparseMatrix by deep copying the supplied DenseSparseMatrix. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int i, int j, double value) Adds the argument value to the value at the supplied index.copy()
Copies the matrix.static DenseSparseMatrix
createFromSparseVectors
(SparseVector[] values) Defensively copies the values.boolean
void
Applies aDoubleUnaryOperator
elementwise to thisTensor
.double
get
(int i, int j) Gets an element from thisMatrix
.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) Only implemented forDenseMatrix
.int
hashCode()
void
intersectAndAddInPlace
(Tensor other, DoubleUnaryOperator f) Only implemented forDenseMatrix
.iterator()
leftMultiply
(SGDVector input) Multiplies this Matrix by aSGDVector
returning a vector of the appropriate size.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.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) rightMultiply is very inefficient on DenseSparseMatrix due to the storage format.void
rowScaleInPlace
(DenseVector scalingCoefficients) Scales each row by the appropriate value in theDenseVector
.rowSum()
Generates aDenseVector
representing the sum of each row.void
set
(int i, int j, double value) Sets an element at the supplied location.toString()
double
twoNorm()
Calculates the euclidean norm for this vector.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
-
Constructor Details
-
DenseSparseMatrix
Constructs a DenseSparseMatrix out of the supplied sparse vector list.Throws IllegalArgumentException if the list forms a ragged matrix.
Does not copy the vectors, it directly wraps them.
- Parameters:
values
- The SparseVectors.
-
DenseSparseMatrix
Creates a new DenseSparseMatrix by deep copying the supplied DenseSparseMatrix.- Parameters:
other
- The matrix to copy.
-
DenseSparseMatrix
public DenseSparseMatrix(int dim1, int dim2) Creates a DenseSparseMatrix with no values or indices.Used as a placeholder in the gradient system.
- Parameters:
dim1
- The first dimension.dim2
- The second dimension.
-
-
Method Details
-
createFromSparseVectors
Defensively copies the values.- Parameters:
values
- The sparse vectors to use.- Returns:
- A DenseSparseMatrix containing the supplied vectors.
-
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
Description copied from interface:Matrix
Copies the matrix. -
get
public double get(int i, int j) Description copied from interface:Matrix
Gets an element from thisMatrix
. -
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
rightMultiply is very inefficient on DenseSparseMatrix due to the storage format.- Specified by:
rightMultiply
in interfaceMatrix
- Parameters:
input
- The input vector.- Returns:
- A*input.
-
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. -
intersectAndAddInPlace
Only implemented forDenseMatrix
.- Specified by:
intersectAndAddInPlace
in interfaceTensor
- Parameters:
other
- The otherTensor
.f
- A function to apply.
-
hadamardProductInPlace
Only implemented forDenseMatrix
.- 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.
-
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.
-
equals
-
hashCode
public int hashCode() -
twoNorm
public double twoNorm()Description copied from interface:Tensor
Calculates the euclidean norm for this vector. -
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()
.
-
toString
-
iterator
- Specified by:
iterator
in interfaceIterable<MatrixTuple>
-