Class DenseMatrix
java.lang.Object
org.tribuo.math.la.DenseMatrix
- All Implemented Interfaces:
Serializable,Iterable<MatrixTuple>,Matrix,Tensor
- Direct Known Subclasses:
ShrinkingMatrix
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDenseMatrix(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 TypeMethodDescriptionvoidadd(int i, int j, double value) Adds the argument value to the value at the supplied index.voidaddAcrossDim1(int[] indices, double value) Adds the specified value to the specified elements across dimension 1.voidaddAcrossDim2(int[] indices, double value) Adds the specified value to the specified elements across dimension 2.voidbroadcastIntersectAndAddInPlace(SGDVector input, boolean broadcastOverDim1) Broadcasts the input vector and adds it to each row/column of the matrix.Returns the dense vector containing each column sum.doublecolumnSum(int columnIndex) Calculates the sum of the specified column.copy()Copies the matrix.static DenseMatrixcreateDenseMatrix(double[][] values) Defensively copies the values before construction.booleanvoidApplies aDoubleUnaryOperatorelementwise 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.doubleget(int i, int j) Gets an element from thisMatrix.getColumn(int index) Returns a copy of the specified column.intThe size of the first dimension.intThe size of the second dimension.getRow(int i) AnSGDVectorview of the row.int[]getShape()Returns an int array specifying the shape of thisTensor.voidhadamardProductInPlace(Tensor other, DoubleUnaryOperator f) Updates thisTensorwith the Hadamard product (i.e., a term by term multiply) of this andother.inthashCode()voidintersectAndAddInPlace(Tensor other, DoubleUnaryOperator f) Updates thisTensorby adding all the values from the intersection withother.iterator()leftMultiply(SGDVector input) Multiplies this Matrix by aSGDVectorreturning a vector of the appropriate size.matrixMultiply(Matrix other) Multiplies this Matrix by anotherMatrixreturning a matrix of the appropriate size.matrixMultiply(Matrix other, boolean transposeThis, boolean transposeOther) Multiplies this Matrix by anotherMatrixreturning a matrix of the appropriate size.voidnormalizeRows(VectorNormalizer normalizer) Normalizes each row using the supplied normalizer in place.intnumActiveElements(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 aSGDVectorreturning a vector of the appropriate size.voidrowScaleInPlace(DenseVector scalingCoefficients) Scales each row by the appropriate value in theDenseVector.rowSum()Generates aDenseVectorrepresenting the sum of each row.doublerowSum(int rowIndex) Calculates the sum of the specified row.voidset(int i, int j, double value) Sets an element at the supplied location.toString()Returns a transposed copy of this matrix.doubletwoNorm()Calculates the euclidean norm for this vector.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface org.tribuo.math.la.Tensor
hadamardProductInPlace, intersectAndAddInPlace, scalarAddInPlace, scaleInPlace
-
Field Details
-
values
protected final double[][] values -
dim1
protected final int dim1 -
dim2
protected final int dim2
-
-
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.
-
getShape
-
reshape
Description copied from interface:TensorReshapes the Tensor to the supplied shape. ThrowsIllegalArgumentExceptionif the shape isn't compatible. -
copy
Copies the matrix. -
get
-
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
-
set
public void set(int i, int j, double value) Description copied from interface:MatrixSets an element at the supplied location. -
getDimension1Size
public int getDimension1Size()Description copied from interface:MatrixThe size of the first dimension.- Specified by:
getDimension1Sizein interfaceMatrix- Returns:
- The size of the first dimension.
-
getDimension2Size
public int getDimension2Size()Description copied from interface:MatrixThe size of the second dimension.- Specified by:
getDimension2Sizein interfaceMatrix- Returns:
- The size of the second dimension.
-
leftMultiply
Description copied from interface:MatrixMultiplies this Matrix by aSGDVectorreturning a vector of the appropriate size.The input must have dimension equal to
Matrix.getDimension2Size().- Specified by:
leftMultiplyin interfaceMatrix- Parameters:
input- The input vector.- Returns:
- A new
SGDVectorof sizeMatrix.getDimension1Size().
-
rightMultiply
Description copied from interface:MatrixMultiplies this Matrix by aSGDVectorreturning a vector of the appropriate size.The input must have dimension equal to
Matrix.getDimension1Size().- Specified by:
rightMultiplyin interfaceMatrix- Parameters:
input- The input vector.- Returns:
- A new
SGDVectorof sizeMatrix.getDimension2Size().
-
matrixMultiply
Description copied from interface:MatrixMultiplies this Matrix by anotherMatrixreturning a matrix of the appropriate size.The input must have dimension 1 equal to
Matrix.getDimension2Size().- Specified by:
matrixMultiplyin interfaceMatrix- Parameters:
other- The input matrix.- Returns:
- A new
Matrixof sizeMatrix.getDimension1Size(),input.getDimension2Size().
-
matrixMultiply
Description copied from interface:MatrixMultiplies this Matrix by anotherMatrixreturning a matrix of the appropriate size.Must obey the rules of matrix multiplication after the transposes are applied.
- Specified by:
matrixMultiplyin 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:MatrixGenerates aDenseVectorrepresenting the sum of each row.- Specified by:
rowSumin interfaceMatrix- Returns:
- A new
DenseVectorof sizeMatrix.getDimension1Size().
-
rowScaleInPlace
Description copied from interface:MatrixScales each row by the appropriate value in theDenseVector.- Specified by:
rowScaleInPlacein interfaceMatrix- Parameters:
scalingCoefficients- ADenseVectorwith sizeMatrix.getDimension1Size().
-
add
public void add(int i, int j, double value) Description copied from interface:MatrixAdds 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:TensorUpdates thisTensorby adding all the values from the intersection withother.The function
fis applied to all values fromotherbefore the addition.Each value is updated as value += f(otherValue).
- Specified by:
intersectAndAddInPlacein interfaceTensor- Parameters:
other- The otherTensor.f- A function to apply.
-
hadamardProductInPlace
Description copied from interface:TensorUpdates thisTensorwith the Hadamard product (i.e., a term by term multiply) of this andother.The function
fis applied to all values fromotherbefore the addition.Each value is updated as value *= f(otherValue).
- Specified by:
hadamardProductInPlacein interfaceTensor- Parameters:
other- The otherTensor.f- A function to apply.
-
foreachInPlace
Description copied from interface:TensorApplies aDoubleUnaryOperatorelementwise to thisTensor.- Specified by:
foreachInPlacein 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:MatrixThe number of non-zero elements in that row.An element could be active and zero, if it was active on construction.
- Specified by:
numActiveElementsin interfaceMatrix- Parameters:
row- The index of the row.- Returns:
- The number of non-zero elements.
-
getRow
-
getColumn
Returns a copy of the specified column.- Parameters:
index- The column index.- Returns:
- A copy of the column.
-
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
-
toString
-
iterator
- Specified by:
iteratorin interfaceIterable<MatrixTuple>
-
normalizeRows
Normalizes each row using the supplied normalizer in place.- Parameters:
normalizer- The vector normalizer to use.
-
columnSum
Returns the dense vector containing each column sum.- Returns:
- The column sums.
-