Class DenseSparseMatrix
java.lang.Object
org.tribuo.math.la.DenseSparseMatrix
- All Implemented Interfaces:
Serializable,Iterable<MatrixTuple>,Matrix,Tensor,ProtoSerializable<org.tribuo.math.protos.TensorProto>
A matrix which is dense in the first dimension and sparse in the second.
Backed by an array of SparseVector.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.tribuo.math.la.Matrix
Matrix.Factorization -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intProtobuf serialization version.Fields inherited from interface org.tribuo.protos.ProtoSerializable
DESERIALIZATION_METHOD_NAME, PROVENANCE_SERIALIZER -
Constructor Summary
ConstructorsConstructorDescriptionDenseSparseMatrix(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 TypeMethodDescriptionvoidadd(int i, int j, double value) Adds the argument value to the value at the supplied index.copy()Copies the matrix.static DenseSparseMatrixcreateDiagonal(SGDVector diagonal) Creates a diagonal matrix using the supplied values.static DenseSparseMatrixcreateFromSparseVectors(SparseVector[] values) Defensively copies the values.static DenseSparseMatrixcreateIdentity(int dimension) Creates an identity matrix of the specified size.static DenseSparseMatrixdeserializeFromProto(int version, String className, com.google.protobuf.Any message) Deserialization factory.booleanvoidApplies aDoubleUnaryOperatorelementwise to thisTensor.doubleget(int i, int j) Gets an element from thisMatrix.getColumn(int i) Gets a copy of the 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) Only implemented forDenseMatrix.inthashCode()voidintersectAndAddInPlace(Tensor other, DoubleUnaryOperator f) Only implemented forDenseMatrix.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.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) rightMultiply is very inefficient on DenseSparseMatrix due to the storage format.voidrowScaleInPlace(DenseVector scalingCoefficients) Scales each row by the appropriate value in theDenseVector.rowSum()Generates aDenseVectorrepresenting the sum of each row.org.tribuo.math.protos.TensorProtoSerializes this object to a protobuf.voidset(int i, int j, double value) Sets an element at the supplied location.toString()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
-
CURRENT_VERSION
public static final int CURRENT_VERSIONProtobuf serialization version.- See Also:
-
-
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.
-
createIdentity
Creates an identity matrix of the specified size.- Parameters:
dimension- The matrix dimension.- Returns:
- The identity matrix.
-
createDiagonal
Creates a diagonal matrix using the supplied values.- Parameters:
diagonal- The values along the diagonal.- Returns:
- A diagonal matrix.
-
deserializeFromProto
public static DenseSparseMatrix 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.
-
serialize
public org.tribuo.math.protos.TensorProto serialize()Description copied from interface:ProtoSerializableSerializes this object to a protobuf.- Specified by:
serializein interfaceProtoSerializable<org.tribuo.math.protos.TensorProto>- Returns:
- The protobuf.
-
getShape
-
reshape
Description copied from interface:TensorReshapes the Tensor to the supplied shape. ThrowsIllegalArgumentExceptionif the shape isn't compatible. -
copy
-
get
-
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
rightMultiply is very inefficient on DenseSparseMatrix due to the storage format.- Specified by:
rightMultiplyin interfaceMatrix- Parameters:
input- The input vector.- Returns:
- A*input.
-
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. -
intersectAndAddInPlace
Only implemented forDenseMatrix.- Specified by:
intersectAndAddInPlacein interfaceTensor- Parameters:
other- The otherTensor.f- A function to apply.
-
hadamardProductInPlace
Only implemented forDenseMatrix.- 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.
-
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
Gets a copy of the column.This function is O(dim1 * log(dim2)) as it requires searching each vector for the column index.
-
equals
-
hashCode
-
twoNorm
-
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().
-
toString
-
iterator
- Specified by:
iteratorin interfaceIterable<MatrixTuple>
-