Class FieldTensor<T extends Field<T>>
java.lang.Object
org.flag4j.arrays.backend.AbstractTensor<FieldTensor<T>,T[],T>
org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringTensor<FieldTensor<T>,T>
org.flag4j.arrays.backend.ring_arrays.AbstractDenseRingTensor<FieldTensor<T>,T>
org.flag4j.arrays.backend.field_arrays.AbstractDenseFieldTensor<FieldTensor<T>,T>
org.flag4j.arrays.dense.FieldTensor<T>
- Type Parameters:
T
- Type of thefield
element for the tensor.
- All Implemented Interfaces:
Serializable
,FieldTensorMixin<FieldTensor<T>,
,FieldTensor<T>, T> TensorOverField<FieldTensor<T>,
,FieldTensor<T>, T[], T> RingTensorMixin<FieldTensor<T>,
,FieldTensor<T>, T> TensorOverRing<FieldTensor<T>,
,FieldTensor<T>, T[], T> SemiringTensorMixin<FieldTensor<T>,
,FieldTensor<T>, T> TensorOverSemiring<FieldTensor<T>,
FieldTensor<T>, T[], T>
Instances of this class represent a dense tensor backed by a Field
array. The FieldTensor
class
provides functionality for tensor operations whose elements are members of a field, supporting mutable data with a fixed shape.
A FieldTensor
is a generalization of the FieldMatrix
, allowing for higher-dimensional data and operations
while maintaining the benefits of field-based arithmetic and dense storage.
Key Features:
- Support for standard tensor operations like addition, subtraction, element-wise multiplication, and reshaping.
- Conversion methods to other representations, including
FieldMatrix
,FieldVector
, and COO format. - Utility methods for computing properties like rank and shape
Example Usage:
-
Constructing a tensor from a
Shape shape
and 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. Complex128[] complexData = { new Complex128(1, 2), new Complex128(3, 4), new Complex128(5, 6), new Complex128(7, 8), new Complex128(9, 10), new Complex128(11, 12), new Complex128(13, 14), new Complex128(15, 16) }; FieldTensor<Complex128> tensor = new FieldTensor<>(complexData);
-
Constructing a tensor from an nD array. This is provided for convenience but is generally much less efficient than
FieldTensor(Shape, T[])
.// Constructing a complex tensor from a 3D array of complex numbers Complex128[][][] complexData = { {{ new Complex128(1, 2), new Complex128(3, 4) }, { new Complex128(5, 6), new Complex128(7, 8) }}, {{ new Complex128(9, 10), new Complex128(11, 12) }, { new Complex128(13, 14), new Complex128(15, 16) }} }; FieldTensor<Complex128> tensor = new FieldTensor<>(complexData);
-
Operations with/on tensors.
// Performing element-wise addition FieldTensor<Complex128> result = tensor.add(tensor); // Reshape tensor FieldTensor<Complex128> reshape = tensor.reshape(new Shape(4, 1, 2)); // Converting the tensor to a matrix FieldMatrix<Complex128> matrix = tensor.toMatrix(new Shape(4, 2)); // Computing the tensor dot product. FieldTensor<Complex128> 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
zeroElement
Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorsConstructorDescriptionFieldTensor
(Object nDArray) Creates a tensor from an nD array.FieldTensor
(Shape shape, T fillValue) Creates a tensor with the specified shape filled withfillValue
.FieldTensor
(Shape shape, T[] entries) Creates a tensor with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescriptionabs()
Computes the element-wise absolute value of this tensor.boolean
Checks if an object is equal to this tensor object.int
hashCode()
protected CooFieldTensor
<T> makeLikeCooTensor
(Shape shape, T[] entries, 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 the shape and data.toMatrix()
Converts this tensor to an equivalent matrix.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.field_arrays.AbstractDenseFieldTensor
allClose, allClose, div, H, isFinite, isInfinite, isNaN, sqrt
Methods inherited from class org.flag4j.arrays.backend.ring_arrays.AbstractDenseRingTensor
argmaxAbs, argminAbs, H, maxAbs, minAbs, sub, subEq
Methods inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringTensor
add, addEq, argmax, argmin, copy, elemMult, flatten, flatten, get, getZeroElement, max, min, reshape, set, setZeroElement, T, T, tensorDot, tensorTr, toCoo, toCoo
Methods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, reshape, sameShape, T, totalEntries
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.flag4j.arrays.backend.field_arrays.FieldTensorMixin
add, add, addEq, addEq, argmax, argmaxAbs, argmin, argminAbs, conj, div, div, divEq, divEq, isOnes, isZeros, makeEmptyDataArray, max, maxAbs, min, minAbs, mult, mult, multEq, multEq, norm, norm, prod, recip, sub, sub, subEq, subEq, sum
Methods inherited from interface org.flag4j.arrays.backend.ring_arrays.TensorOverRing
H, H, sub
-
Constructor Details
-
FieldTensor
Creates a tensor with the specified data and shape.- Parameters:
shape
- Shape of this tensor.entries
- 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.
-
FieldTensor
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
- IfnDArray
is not an array or not rectangular.
-
FieldTensor
-
-
Method Details
-
makeLikeCooTensor
Constructs a sparse COO tensor which is of a similar type as this dense tensor.- Specified by:
makeLikeCooTensor
in classAbstractDenseSemiringTensor<FieldTensor<T extends Field<T>>,
T extends Field<T>> - Parameters:
shape
- Shape of the COO tensor.entries
- 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 the shape and data.- Specified by:
makeLikeTensor
in interfaceTensorOverSemiring<FieldTensor<T extends Field<T>>,
FieldTensor<T extends Field<T>>, T extends Field<T>[], T extends Field<T>> - Specified by:
makeLikeTensor
in classAbstractTensor<FieldTensor<T extends Field<T>>,
T extends Field<T>[], T extends Field<T>> - Parameters:
shape
- Shape of the tensor to construct.entries
- Entries of the tensor to construct.- Returns:
- A tensor of the same type as this tensor with the given the shape and data.
-
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 bebroadcastable
with the shape of this tensor.- Returns:
- A matrix of shape
matShape
with the values of this tensor. - Throws:
LinearAlgebraException
- IfmatShape
is not of rank 2.
-
toMatrix
Converts this tensor to an equivalent matrix.- Returns:
- If this tensor is rank 2, then the equivalent matrix will be returned. If the tensor is rank 1, then a matrix with a single row will be returned. If the rank of this tensor is larger than 2, it will be flattened to a single row.
-
abs
Computes the element-wise absolute value of this tensor.
Note: the absolute value may not be defined for all fields.
- Returns:
- The element-wise absolute value of this tensor.
-
equals
Checks if an object is equal to this tensor object.- Overrides:
equals
in classObject
- Parameters:
object
- Object to check equality with this tensor.- Returns:
- True if the two tensors have the same shape, are numerically equivalent, and are of type
FieldTensor
. False otherwise.
-
hashCode
-
toString
-