Package org.flag4j.core
Class TensorBase<T,U,W,Z,Y,D extends Serializable,X extends Number>
java.lang.Object
org.flag4j.core.TensorBase<T,U,W,Z,Y,D,X>
- Type Parameters:
T
- Type of this tensor.U
- Dense Tensor type.W
- Complex Tensor type.Z
- Dense complex tensor type.Y
- Real Tensor type.D
- Type of the storage data structure for the tensor. This common use case will be an array or list-like data structure.X
- The type of individual entry within theD
data structure
- All Implemented Interfaces:
Serializable
,TensorComparisonsMixin
,TensorManipulationsMixin<T>
,TensorOperationsMixin<T,
,U, W, Z, Y, X> TensorPropertiesMixin
- Direct Known Subclasses:
DenseTensorBase
,SparseTensorBase
public abstract class TensorBase<T,U,W,Z,Y,D extends Serializable,X extends Number>
extends Object
implements Serializable, TensorComparisonsMixin, TensorPropertiesMixin, TensorManipulationsMixin<T>, TensorOperationsMixin<T,U,W,Z,Y,X>
The base class for all tensors. A tensor is an algebraic object equivalent to a multidimensional array with a single
data type.
- See Also:
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
TensorBase
(Shape shape, D entries) Creates a tensor with specified entries and shape. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks if all entries of this tensor are close to the entries of the argumenttensor
.abstract boolean
Checks if all entries of this tensor are close to the entries of the argumenttensor
.Gets the entries of this tensor as a 1D array.int
getRank()
Gets the rank of this tensor.protected abstract T
getSelf()
Simply returns a reference of this tensor.getShape()
Gets the shape of this tensor.static boolean
sameLength
(TensorBase<?, ?, ?, ?, ?, ?, ?> A, TensorBase<?, ?, ?, ?, ?, ?, ?> B, int axis) Checks if two matrices have the same length along a specified axis.boolean
sameShape
(TensorBase<?, ?, ?, ?, ?, ?, ?> B) Checks if a tensor has the same shape as this tensor.boolean
tensorEquals
(TensorBase<?, ?, ?, ?, ?, ?, ?> src2) Checks if two tensors are equal.Gets the total number of entries in this tensor.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.flag4j.core.TensorComparisonsMixin
equals, isOnes, isZeros
Methods inherited from interface org.flag4j.core.TensorManipulationsMixin
flatten, flatten, reshape, reshape, round, round, roundToZero, roundToZero, set
-
Field Details
-
DEFAULT_ROUND_TO_ZERO_THRESHOLD
protected static final double DEFAULT_ROUND_TO_ZERO_THRESHOLDDefault value for rounding to zero.- See Also:
-
entries
Entry data for this tensor. -
shape
The shape of this tensor
-
-
Constructor Details
-
TensorBase
Creates a tensor with 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
-
getShape
-
getRank
public int getRank()Gets the rank of this tensor. That is, number of indices needed to uniquely select an element of the tensor.
Note, this method is distinct from the
MatrixPropertiesMixin.matrixRank()
method. This returns the number of dimensions (i.e. order or degree) of the tensor and indicates the number of indices needed to uniquely select an element of the tensor.- Returns:
- The rank of this tensor.
-
getEntries
Gets the entries of this tensor as a 1D array.- Returns:
- The entries of this tensor.
-
totalEntries
Gets the total number of entries in this tensor.- Returns:
- The total number of entries in this tensor.
-
sameShape
Checks if a tensor has the same shape as this tensor.- Parameters:
B
- Second tensor.- Returns:
- True if this tensor and B have the same shape. False otherwise.
-
sameLength
public static boolean sameLength(TensorBase<?, ?, ?, ?, ?, ?, ?> A, TensorBase<?, ?, ?, ?, ?, ?, ?> B, int axis) Checks if two matrices have the same length along a specified axis.- Parameters:
A
- First tensor to compare.B
- Second tensor to compare.axis
- The axis along which to compare the lengths of the two tensors.- Returns:
- True if tensor A and tensor B have the same length along the specified axis. Otherwise, returns false.
- Throws:
IllegalArgumentException
- If axis is negative or unspecified for either tensor.
-
getSelf
Simply returns a reference of this tensor.- Returns:
- A reference to this tensor.
-
allClose
Checks if all entries of this tensor are close to the entries of the argumenttensor
.- Parameters:
tensor
- Tensor to compare this tensor to.- Returns:
- True if the argument
tensor
is the same shape as this tensor and all entries are 'close', i.e. elementsa
andb
at the same positions in the two tensors respectively satisfy|a-b| <= (1E-05 + 1E-08*|b|)
. Otherwise, returns false. - See Also:
-
allClose
Checks if all entries of this tensor are close to the entries of the argumenttensor
.- Parameters:
tensor
- Tensor to compare this tensor to.relTol
- Relative tolerance.absTol
- Absolute tolerance.- Returns:
- True if the argument
tensor
is the same shape as this tensor and all entries are 'close', i.e. elementsa
andb
at the same positions in the two tensors respectively satisfy|a-b| <= (atol + rtol*|b|)
. Otherwise, returns false. - See Also:
-
tensorEquals
Description copied from interface:TensorComparisonsMixin
Checks if two tensors are equal. Note, this method is much more permissive than
TensorComparisonsMixin.equals(Object)
as it allows for comparisons with any tensor, matrix, or vector. If the shapes are equal and entries are element-wise equivalent then this method returns true regardless of the types of the two tensors.xFor Example, a
Dense Tensor
of rank 2 with the same values as aSparse CSR Matrix
will be considered equal.- Specified by:
tensorEquals
in interfaceTensorComparisonsMixin
- Parameters:
src2
- Tensor, matrix, or vector to compare to this tensor.- Returns:
- True if both tensors (or matrix/vector) have the same shape and all entries are numerically equivalent by index. This accounts for possible zero values in sparse objects. Returns false if the tensors do not have the same shape or if the tensors differ in value at any index.
-