Module flag4j
Package org.flag4j.arrays.backend
Class FieldTensorBase<T extends FieldTensorBase<T,U,V>,U extends FieldTensorBase<U,U,V>,V extends Field<V>>
java.lang.Object
org.flag4j.arrays.backend.AbstractTensor<T,Field<V>[],V>
org.flag4j.arrays.backend.TensorOverSemiRing<T,U,Field<V>[],V>
org.flag4j.arrays.backend.TensorOverRing<T,U,Field<V>[],V>
org.flag4j.arrays.backend.TensorOverField<T,U,Field<V>[],V>
org.flag4j.arrays.backend.FieldTensorBase<T,U,V>
- Type Parameters:
T
- The type of this tensor.U
- Type of dense tensor equivalent toT
. IfT
is dense, then this should be the same type asT
. This parameter is required because some operations (e.g.TensorOverSemiRing.tensorDot(TensorOverSemiRing, int)
) between two sparse tensors result in a dense tensor.V
- The type of theField
for this tensor's entries.
- All Implemented Interfaces:
Serializable
,TensorBinaryOpsMixin<T,
,T> TensorMixin<T,
Field<V>[], V>
- Direct Known Subclasses:
CooFieldMatrixBase
,CooFieldTensorBase
,CooFieldVectorBase
,CsrFieldMatrixBase
,DenseFieldMatrixBase
,DenseFieldTensorBase
,DenseFieldVectorBase
public abstract class FieldTensorBase<T extends FieldTensorBase<T,U,V>,U extends FieldTensorBase<U,U,V>,V extends Field<V>>
extends TensorOverField<T,U,Field<V>[],V>
The base class for all tensors whose entries are elements of a
Field
.- See Also:
-
Field Summary
Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
entries, rank, shape
-
Constructor Summary
ModifierConstructorDescriptionprotected
FieldTensorBase
(Shape shape, Field<V>[] entries) Creates a tensor with the specified entries and shape. -
Method Summary
Modifier and TypeMethodDescriptionadd
(double b) Adds a real value to each entry of this tensor.Computes the element-wise sum between two tensors of the same shape.Adds a scalar field value to each entry of this tensor.void
Adds a scalar value to each entry of this tensor and stores the result in this tensor.conj()
Computes the element-wise conjugation of this tensor.copy()
Creates a copy of this tensor.div
(double b) Divides each entry of this tensor by a real value.Divides each entry of this tensor by a scalar field element.void
Divides each entry of this tensor by s scalar field element and stores the result in this tensor.Computes the element-wise multiplication of two tensors of the same shape.flatten()
Flattens tensor to single dimension while preserving order of entries.flatten
(int axis) Flattens a tensor along the specified axis.get
(int... indices) Gets the element of this tensor at the specified indices.Gets the zero element for the field of this tensor.H()
Computes the Hermitian transpose of a tensor by exchanging and conjugating the first and last axes of this tensor.boolean
isOnes()
Checks if this tensor only contains ones.boolean
isZeros()
Checks if this tensor only contains zeros.mult
(double b) Multiplies a real value to each entry of this tensor.Multiplies a scalar field value to each entry of this tensor.void
Multiplies a scalar value to each entry of this tensor and stores the result in this tensor.prod()
Computes the product of all values in this tensor.recip()
Computes the element-wise reciprocals of this tensor.Copies and reshapes this tensor.protected void
setZeroElement
(V zeroElement) Sets the zero element for the field of this tensor.sqrt()
Computes the element-wise square root of a tensor.sub
(double b) Subtracts a real value from each entry of this tensor.Computes the element-wise difference between two tensors of the same shape.Subtracts a scalar field value from each entry of this tensor.void
Subtracts a scalar value from each entry of this tensor and stores the result in this tensor.sum()
Computes the sum of all values in this tensor.T()
Computes the transpose of a tensor by exchanging the first and last axes of this tensor.tensorTr
(int axis1, int axis2) Computes the generalized trace of this tensor along the specified axes.Methods inherited from class org.flag4j.arrays.backend.TensorOverRing
abs, H, H
Methods inherited from class org.flag4j.arrays.backend.TensorOverSemiRing
tensorDot, tensorDot, tensorDot, tensorDot, tensorTr
Methods inherited from class org.flag4j.arrays.backend.AbstractTensor
getEntries, getRank, getShape, makeLikeTensor, reshape, sameShape, set, totalEntries
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.flag4j.arrays.backend.TensorMixin
T, T
-
Field Details
-
zeroElement
-
-
Constructor Details
-
FieldTensorBase
Creates a tensor with the specified entries and shape.- Parameters:
shape
- Shape of this tensor.entries
- Entries of this tensor. If this tensor is dense, this specifies all entries within the tensor. If this tensor is sparse, this specifies only the non-zero entries of the tensor.
-
-
Method Details
-
setZeroElement
Sets the zero element for the field of this tensor. This is useful in some operations for cases where the total number of entries or total number of non-zero entries is zero. In such cases, the zero element cannot be determined for a generic field sonull
is used. When- Parameters:
zeroElement
- Zero element for the field of this tensor.
-
getZeroElement
Gets the zero element for the field of this tensor.- Returns:
- The zero element for the field of this tensor. If it could not be determined during construction of this object
and has not been set explicitly by
setZeroElement(Field)
thennull
will be returned.
-
get
Gets the element of this tensor at the specified indices.- Specified by:
get
in interfaceTensorMixin<T extends FieldTensorBase<T,
U, V>, U extends FieldTensorBase<U, U, V>, V extends Field<V>> - Specified by:
get
in classAbstractTensor<T extends FieldTensorBase<T,
U, V>, Field<V extends Field<V>>[], V extends Field<V>> - Parameters:
indices
- Indices of the element to get.- Returns:
- The element of this tensor at the specified indices.
- Throws:
ArrayIndexOutOfBoundsException
- If any indices are not within this matrix.
-
flatten
-
flatten
Flattens a tensor along the specified axis.- Specified by:
flatten
in classAbstractTensor<T extends FieldTensorBase<T,
U, V>, Field<V extends Field<V>>[], V extends Field<V>> - Parameters:
axis
- Axis along which to flatten tensor.- Throws:
ArrayIndexOutOfBoundsException
- If the axis is not positive or larger thanthis.
.AbstractTensor.getRank()
-1- See Also:
-
reshape
Copies and reshapes this tensor.- Specified by:
reshape
in classAbstractTensor<T extends FieldTensorBase<T,
U, V>, Field<V extends Field<V>>[], V extends Field<V>> - Parameters:
newShape
- New shape for the tensor.- Returns:
- A copy of this tensor with the new shape.
- Throws:
TensorShapeException
- IfnewShape
is not broadcastable tothis.shape
.
-
sum
-
prod
-
add
Computes the element-wise sum between two tensors of the same shape.- Specified by:
add
in classTensorOverSemiRing<T extends FieldTensorBase<T,
U, V>, U extends FieldTensorBase<U, U, V>, Field<V extends Field<V>>[], V extends Field<V>> - Parameters:
b
- Second tensor in the element-wise sum.- Returns:
- The sum of this tensor with
b
. - Throws:
IllegalArgumentException
- If this tensor andb
do not have the same shape.
-
sub
Computes the element-wise difference between two tensors of the same shape.- Specified by:
sub
in classTensorOverRing<T extends FieldTensorBase<T,
U, V>, U extends FieldTensorBase<U, U, V>, Field<V extends Field<V>>[], V extends Field<V>> - Parameters:
b
- Second tensor in the element-wise difference.- Returns:
- The difference of this tensor with the scalar
b
. - Throws:
IllegalArgumentException
- If this tensor andb
do not have the same shape.
-
add
Adds a real value to each entry of this tensor. -
sub
Subtracts a real value from each entry of this tensor. -
mult
Multiplies a real value to each entry of this tensor. -
div
Divides each entry of this tensor by a real value. -
conj
-
elemMult
Computes the element-wise multiplication of two tensors of the same shape.- Specified by:
elemMult
in classTensorOverSemiRing<T extends FieldTensorBase<T,
U, V>, U extends FieldTensorBase<U, U, V>, Field<V extends Field<V>>[], V extends Field<V>> - Parameters:
b
- Second tensor in the element-wise product.- Returns:
- The element-wise product between this tensor and
b
. - Throws:
IllegalArgumentException
- If this tensor andb
do not have the same shape.
-
tensorTr
Computes the generalized trace of this tensor along the specified axes.
The generalized tensor trace is the sum along the diagonal values of the 2D sub-arrays_old of this tensor specified by
axis1
andaxis2
. The shape of the resulting tensor is equal to this tensor with theaxis1
andaxis2
removed.- Specified by:
tensorTr
in classTensorOverSemiRing<T extends FieldTensorBase<T,
U, V>, U extends FieldTensorBase<U, U, V>, Field<V extends Field<V>>[], V extends Field<V>> - Parameters:
axis1
- First axis for 2D sub-array.axis2
- Second axis for 2D sub-array.- Returns:
- The generalized trace of this tensor along
axis1
andaxis2
. - Throws:
IndexOutOfBoundsException
- If the two axes are not both larger than zero and less than this tensors rank.IllegalArgumentException
- Ifaxis1 == @code axis2
orthis.shape.get(axis1) != this.shape.get(axis1)
(i.e. the axes are equal or the tensor does not have the same length along the two axes.)
-
isZeros
public boolean isZeros()Checks if this tensor only contains zeros. -
isOnes
public boolean isOnes()Checks if this tensor only contains ones. If this tensor is sparse, only the non-zero entries are considered. -
add
Adds a scalar field value to each entry of this tensor. -
addEq
Adds a scalar value to each entry of this tensor and stores the result in this tensor. -
sub
Subtracts a scalar field value from each entry of this tensor. -
subEq
Subtracts a scalar value from each entry of this tensor and stores the result in this tensor. -
mult
Multiplies a scalar field value to each entry of this tensor. -
multEq
Multiplies a scalar value to each entry of this tensor and stores the result in this tensor. -
div
Divides each entry of this tensor by a scalar field element. -
divEq
Divides each entry of this tensor by s scalar field element and stores the result in this tensor. -
sqrt
Computes the element-wise square root of a tensor.- Specified by:
sqrt
in classTensorOverField<T extends FieldTensorBase<T,
U, V>, U extends FieldTensorBase<U, U, V>, Field<V extends Field<V>>[], V extends Field<V>> - Returns:
- The result of applying an element-wise square root to this tensor. Note, this method will compute the principle square root i.e. the square root with positive real part.
-
T
Computes the transpose of a tensor by exchanging the first and last axes of this tensor.- Returns:
- The transpose of this tensor.
- See Also:
-
H
Computes the Hermitian transpose of a tensor by exchanging and conjugating the first and last axes of this tensor. -
recip
-
copy
-