Class SemiringTensor<T extends Semiring<T>>
java.lang.Object
org.flag4j.arrays.backend.AbstractTensor<SemiringTensor<T>,T[],T>
org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringTensor<SemiringTensor<T>,T>
org.flag4j.arrays.dense.SemiringTensor<T>
- Type Parameters:
T- Type of thesemiringelement for the tensor.
- All Implemented Interfaces:
Serializable,SemiringTensorMixin<SemiringTensor<T>,,SemiringTensor<T>, T> TensorOverSemiring<SemiringTensor<T>,SemiringTensor<T>, T[], T>
public class SemiringTensor<T extends Semiring<T>>
extends AbstractDenseSemiringTensor<SemiringTensor<T>,T>
Instances of this class represent a dense tensor backed by a Semiring array. The SemiringTensor class
provides functionality for tensor operations whose elements are members of a Semiring, supporting mutable data with a fixed shape.
A SemiringTensor is a generalization of the SemiringMatrix, allowing for higher-dimensional data and operations
while maintaining the benefits of Semiring-based arithmetic and dense storage.
Key Features:
- Support for standard tensor operations like addition, element-wise multiplication, and reshaping.
- Conversion methods to other representations, including
SemiringMatrix,SemiringVector, and COO format. - Utility methods for computing properties like rank and shape
Example Usage:
- Constructing a tensor from a
Shape shapeand flat data array. This is generally the preferred and most efficient method of constructing a tensor.// Constructing a complex tensor from a shape and flat data array. BoolSemiring[] data = { new BoolSemiring(true), new BoolSemiring(false), new BoolSemiring(true), new BoolSemiring(true), new BoolSemiring(false), new BoolSemiring(false), new BoolSemiring(true), new BoolSemiring(false) }; SemiringTensor<Complex128> tensor = new SemiringTensor<>(data); - Constructing a tensor from an nD array. This is provided for convenience but is generally much less efficient than
SemiringTensor(Shape, T[]).// Constructing a complex tensor from a 3D array of complex numbers BoolSemiring[][][] data = { {{ new BoolSemiring(true), new BoolSemiring(false) }, { new BoolSemiring(true), new BoolSemiring(true) }}, {{ new BoolSemiring(false), new BoolSemiring(false) }, { new BoolSemiring(true), new BoolSemiring(false) }} }; SemiringTensor<BoolSemiring> tensor = new SemiringTensor<>(data); -
Operations with/on tensors.
// Performing element-wise addition SemiringTensor<BoolSemiring> result = tensor.add(tensor); // Reshape tensor SemiringTensor<BoolSemiring> reshape = tensor.reshape(new Shape(4, 1, 2)); // Converting the tensor to a matrix SemiringMatrix<BoolSemiring> matrix = tensor.toMatrix(new Shape(4, 2)); // Computing the tensor dot product. SemiringTensor<BoolSemiring> dot = tensor.tensorDot(tensor, new int[]{0, 1}, new int[]{2, 0} );
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringTensor
zeroElementFields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape -
Constructor Summary
ConstructorsConstructorDescriptionSemiringTensor(Object nDArray) Creates a tensor from an nD array.SemiringTensor(Shape shape, T fillValue) Creates a dense semiring tensor with the specified data and filled withfilledValue.SemiringTensor(Shape shape, T[] data) Creates a tensor with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks if an object is equal to this tensor object.inthashCode()protected CooSemiringTensor<T> makeLikeCooTensor(Shape shape, T[] data, int[][] indices) Constructs a sparse COO tensor which is of a similar type as this dense tensor.makeLikeTensor(Shape shape, T[] entries) Constructs a tensor of the same type as this tensor with the given theshapeanddata.Converts this tensor to a matrix with the specified shape.toString()Formats this tensor as a human-readable string.toVector()Converts this tensor to an equivalent vector.Methods inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringTensor
add, addEq, argmax, argmin, copy, dataLength, elemMult, flatten, flatten, get, getZeroElement, max, min, reshape, set, setZeroElement, T, T, tensorDot, tensorTr, toCoo, toCooMethods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, reshape, sameShape, T, totalEntriesMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.flag4j.arrays.backend.semiring_arrays.SemiringTensorMixin
add, addEq, isOnes, isZeros, makeEmptyDataArray, mult, multEq, prod, sum
-
Constructor Details
-
SemiringTensor
Creates a tensor with the specified data and shape.- Parameters:
shape- Shape of this tensor.data- 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.
-
SemiringTensor
Creates a tensor from an nD array. The tensors shape will be inferred from.- Parameters:
nDArray- Array to construct tensor from. Must be a rectangular array.- Throws:
IllegalArgumentException- IfnDArrayis not an array or not rectangular.
-
SemiringTensor
-
-
Method Details
-
makeLikeCooTensor
Constructs a sparse COO tensor which is of a similar type as this dense tensor.- Specified by:
makeLikeCooTensorin classAbstractDenseSemiringTensor<SemiringTensor<T extends Semiring<T>>,T extends Semiring<T>> - Parameters:
shape- Shape of the COO tensor.data- Non-zero data of the COO tensor.indices-- Returns:
- A sparse COO tensor which is of a similar type as this dense tensor.
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the given theshapeanddata. The resulting tensor will also have the same non-zero indices as this tensor.- Specified by:
makeLikeTensorin interfaceTensorOverSemiring<SemiringTensor<T extends Semiring<T>>,SemiringTensor<T extends Semiring<T>>, T extends Semiring<T>[], T extends Semiring<T>> - Specified by:
makeLikeTensorin classAbstractTensor<SemiringTensor<T extends Semiring<T>>,T extends Semiring<T>[], T extends Semiring<T>> - Parameters:
shape- Shape of the tensor to construct.entries- Entries of the tensor to construct.- Returns:
- A tensor of the same type and with the same non-zero indices as this tensor with the given the
shapeanddata.
-
toVector
Converts this tensor to an equivalent vector. If this tensor is not rank 1, then it will be flattened.- Returns:
- A vector equivalent of this tensor.
-
toMatrix
Converts this tensor to a matrix with the specified shape.- Parameters:
matShape- Shape of the resulting matrix. Must bebroadcastablewith the shape of this tensor.- Returns:
- A matrix of shape
matShapewith the values of this tensor. - Throws:
LinearAlgebraException- IfmatShapeis not of rank 2.
-
equals
Checks if an object is equal to this tensor object.- Overrides:
equalsin classObject- Parameters:
object- Object to check equality with this tensor.- Returns:
trueif the two tensors have the same shape, are numerically equivalent, and are of typeSemiringTensor.falseotherwise.
-
hashCode
-
toString
-