Package org.flag4j.arrays.dense
Class Tensor
java.lang.Object
- All Implemented Interfaces:
Serializable
,DenseMixin<Double>
,RealTensorMixin<Tensor,
,CTensor> TensorComparisonsMixin
,TensorExclusiveMixin<Tensor,
,Tensor, CooTensor, CTensor> TensorManipulationsMixin<Tensor>
,TensorOperationsMixin<Tensor,
,Tensor, CTensor, CTensor, Tensor, Double> TensorPropertiesMixin
public class Tensor
extends RealDenseTensorBase<Tensor,CTensor>
implements TensorExclusiveMixin<Tensor,Tensor,CooTensor,CTensor>
Real Dense Tensor. May have any rank (that is, may have any number of unique axes/dimensions).
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.core.TensorBase
DEFAULT_ROUND_TO_ZERO_THRESHOLD, entries, shape
-
Constructor Summary
ConstructorDescriptionConstructs a tensor whose shape and entries are specified by a matrix.Constructs a tensor from another tensor.Constructs a tensor whose shape and entries are specified by a vector.Constructs a tensor with given shape filled with zeros.Constructs a tensor with given shape filled with specified values.Constructs a tensor with given shape filled with specified values.Constructs a tensor with given shape filled with specified values.Constructs a tensor with given shape filled with specified values.Constructs a tensor with given shape filled with specified values. -
Method Summary
Modifier and TypeMethodDescriptionComputes the element-wise addition between two tensors of the same rank.add
(CooCTensor B) Computes the element-wise addition between two tensors of the same rank.Computes the element-wise addition between two tensors of the same rank.void
Computes the element-wise addition of two tensors of the same rank and stores the result in this tensor.Computes the element-wise division between two tensors.Computes the element-wise multiplication between two tensors.Computes the element-wise multiplication between two tensors.Computes the element-wise multiplication between two tensors.boolean
Checks if an object is equal to this tensor object.flatten()
Flattens tensor to single dimension.flatten
(int axis) Flattens a tensor along the specified axis.protected Tensor
getSelf()
Simply returns this tensor.protected CTensor
makeComplexTensor
(Shape shape, double[] entries) Factory to create a complex tensor with the specified shape and size.protected CTensor
makeComplexTensor
(Shape shape, CNumber[] entries) Factory to create a complex tensor with the specified shape and size.protected Tensor
makeTensor
(Shape shape, double[] entries) Factory to create a tensor with the specified shape and size.Computes the element-wise subtraction between two tensors of the same rank.sub
(CooCTensor B) Computes the element-wise subtraction between two tensors of the same rank.Computes the element-wise subtraction between two tensors of the same rank.void
Computes the element-wise subtraction of two tensors of the same rank and stores the result in this tensor.T()
Computes the transpose of a tensor.T
(int... axes) Computes the transpose of this tensor.T
(int axis1, int axis2) Computes the transpose of a tensor.Computes the tensor dot product of this tensor with a second tensor.Computes the tensor contraction of this tensor with a specified tensor over the specified set of axes.tensorInv
(int numIndices) Computes the 'inverse' of this tensor.toCoo()
Converts this dense tensor to an equivalent sparse COO tensor.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.core.dense_base.RealDenseTensorBase
abs, add, add, add, addEq, addEq, allClose, argMax, argMin, copy, div, div, elemDiv, elemMult, get, hashCode, isNeg, isOnes, isPos, isZeros, max, maxAbs, min, minAbs, mult, mult, recip, reshape, reshape, round, round, roundToZero, roundToZero, set, sqrt, sub, sub, sub, subEq, subEq, sum, toComplex
Methods inherited from class org.flag4j.core.TensorBase
allClose, getEntries, getRank, getShape, sameLength, sameShape, tensorEquals, totalEntries
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.flag4j.core.TensorExclusiveMixin
add, elemDiv, elemMult, getRank, sub, tensorDot, tensorDot, tensorInv, transpose, transpose
Methods inherited from interface org.flag4j.core.TensorOperationsMixin
transpose
-
Constructor Details
-
Tensor
Constructs a tensor with given shape filled with zeros.- Parameters:
shape
- Shape of the tensor.
-
Tensor
Constructs a tensor with given shape filled with specified values.- Parameters:
shape
- Shape of the tensor.fillValue
- Value to fill tensor with.
-
Tensor
Constructs a tensor with given shape filled with specified values.- Parameters:
shape
- Shape of the tensor.entries
- Entries of the vector.- Throws:
IllegalArgumentException
- If the shape does not match the number of entries.
-
Tensor
Constructs a tensor with given shape filled with specified values.- Parameters:
shape
- Shape of the tensor.entries
- Entries of the vector.- Throws:
IllegalArgumentException
- If the shape does not match the number of entries.
-
Tensor
Constructs a tensor with given shape filled with specified values.- Parameters:
shape
- Shape of the tensor.entries
- Entries of the vector.- Throws:
IllegalArgumentException
- If the shape does not match the number of entries.
-
Tensor
Constructs a tensor with given shape filled with specified values.- Parameters:
shape
- Shape of the tensor.entries
- Entries of the vector.- Throws:
IllegalArgumentException
- If the shape does not match the number of entries.
-
Tensor
Constructs a tensor from another tensor. This effectively copies the tensor.- Parameters:
A
- tensor to copy.
-
Tensor
Constructs a tensor whose shape and entries are specified by a matrix.- Parameters:
A
- Matrix to copy to tensor.
-
Tensor
Constructs a tensor whose shape and entries are specified by a vector.- Parameters:
A
- Vector to copy to tensor.
-
-
Method Details
-
makeTensor
Factory to create a tensor with the specified shape and size.- Specified by:
makeTensor
in classDenseTensorBase<Tensor,
CTensor, Tensor, double[], Double> - Parameters:
shape
- Shape of the tensor to make.entries
- Entries of the tensor to make.- Returns:
- A new tensor with the specified shape and entries.
-
makeComplexTensor
Factory to create a complex tensor with the specified shape and size.- Specified by:
makeComplexTensor
in classRealDenseTensorBase<Tensor,
CTensor> - Parameters:
shape
- Shape of the tensor to make.entries
- Entries of the tensor to make.- Returns:
- A new tensor with the specified shape and entries.
-
makeComplexTensor
Factory to create a complex tensor with the specified shape and size.- Specified by:
makeComplexTensor
in classRealDenseTensorBase<Tensor,
CTensor> - Parameters:
shape
- Shape of the tensor to make.entries
- Entries of the tensor to make.- Returns:
- A new tensor with the specified shape and entries.
-
getSelf
-
flatten
Flattens a tensor along the specified axis.- Specified by:
flatten
in interfaceTensorManipulationsMixin<Tensor>
- Parameters:
axis
- Axis along which to flatten tensor.- Throws:
ArrayIndexOutOfBoundsException
- If the axis is not positive or larger thanthis.
.TensorBase.getRank()
-1
-
equals
Checks if an object is equal to this tensor object.- Specified by:
equals
in interfaceTensorComparisonsMixin
- 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
Tensor
. False otherwise.
-
toCoo
-
flatten
Flattens tensor to single dimension.- Specified by:
flatten
in interfaceTensorManipulationsMixin<Tensor>
- Returns:
- The flattened tensor.
-
addEq
Computes the element-wise addition of two tensors of the same rank and stores the result in this tensor.- Parameters:
B
- Second tensor in the addition.- Throws:
IllegalArgumentException
- If this tensor andB
have different shapes.
-
T
Computes the transpose of a tensor. Same asTensorOperationsMixin.transpose()
. In the context of a tensor, this exchanges the first and last axis of the tensor. Also seeTensorExclusiveMixin.transpose(int, int)
andT(int, int)
. -
tensorDot
Computes the tensor contraction of this tensor with a specified tensor over the specified set of axes. That is, computes the sum of products between the two tensors along the specified set of axes.- Specified by:
tensorDot
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
src2
- Tensor to contract with this tensor.aAxes
- Axes along which to compute products for this tensor.bAxes
- Axes along which to compute products forsrc2
tensor.- Returns:
- The tensor dot product over the specified axes.
- Throws:
IllegalArgumentException
- If the two tensors shapes do not match along the specified axes pairwise inaAxes
andbAxes
.IllegalArgumentException
- IfaAxes
andbAxes
do not match in length, or if any of the axes are out of bounds for the corresponding tensor.
-
tensorDot
Computes the tensor dot product of this tensor with a second tensor. That is, sums the product of two tensor elements over the last axis of this tensor and the second-to-last axis ofsrc2
. If both tensors are rank 2, this is equivalent to matrix multiplication.- Specified by:
tensorDot
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
src2
- Tensor to compute dot product with this tensor.- Returns:
- The tensor dot product over the last axis of this tensor and the second to last axis of
src2
. - Throws:
IllegalArgumentException
- If this tensors shape along the last axis does not matchsrc2
shape along the second-to-last axis.- See Also:
-
T
Computes the transpose of a tensor. Same asTensorExclusiveMixin.transpose(int, int)
. In the context of a tensor, this exchanges the specified axes. Also seeTensorOperationsMixin.transpose()
andT()
to exchange first and last axes. -
T
Computes the transpose of this tensor. That is, interchanges the axes of this tensor so that it matches the specified axes permutation. Same asTensorExclusiveMixin.transpose(int[])
.- Specified by:
T
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
axes
- Permutation of tensor axis. If the tensor has rankN
, then this must be an array of lengthN
which is a permutation of{0, 1, 2, ..., N-1}
.- Returns:
- The transpose of this tensor with its axes permuted by the
axes
array. - Throws:
IllegalArgumentException
- Ifaxes
is not a permutation of{1, 2, 3, ... N-1}
.
-
add
Computes the element-wise addition between two tensors of the same rank.- Specified by:
add
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
B
- Second tensor in the addition.- Returns:
- The result of adding the tensor B to this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
add
Computes the element-wise addition between two tensors of the same rank.- Specified by:
add
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
B
- Second tensor in the addition.- Returns:
- The result of adding the tensor B to this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
add
Computes the element-wise addition between two tensors of the same rank.- Specified by:
add
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
B
- Second tensor in the addition.- Returns:
- The result of adding the tensor B to this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
sub
Computes the element-wise subtraction between two tensors of the same rank.- Specified by:
sub
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
B
- Second tensor in element-wise subtraction.- Returns:
- The result of subtracting the tensor B from this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
sub
Computes the element-wise subtraction between two tensors of the same rank.- Specified by:
sub
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
B
- Second tensor in element-wise subtraction.- Returns:
- The result of subtracting the tensor B from this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
sub
Computes the element-wise subtraction between two tensors of the same rank.- Specified by:
sub
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
B
- Second tensor in element-wise subtraction.- Returns:
- The result of subtracting the tensor B from this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
subEq
Computes the element-wise subtraction of two tensors of the same rank and stores the result in this tensor.- Parameters:
B
- Second tensor in the subtraction.- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
elemMult
Computes the element-wise multiplication between two tensors.- Specified by:
elemMult
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
B
- Tensor to element-wise multiply to this tensor.- Returns:
- The result of the element-wise tensor multiplication.
- Throws:
IllegalArgumentException
- If the tensors do not have the same shape.
-
elemMult
Computes the element-wise multiplication between two tensors.- Specified by:
elemMult
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
B
- Tensor to element-wise multiply to this tensor.- Returns:
- The result of the element-wise tensor multiplication.
- Throws:
IllegalArgumentException
- If the tensors do not have the same shape.
-
elemMult
Computes the element-wise multiplication between two tensors.- Specified by:
elemMult
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
B
- Tensor to element-wise multiply to this tensor.- Returns:
- The result of the element-wise tensor multiplication.
- Throws:
IllegalArgumentException
- If the tensors do not have the same shape.
-
elemDiv
Computes the element-wise division between two tensors.- Specified by:
elemDiv
in interfaceTensorExclusiveMixin<Tensor,
Tensor, CooTensor, CTensor> - Parameters:
B
- Tensor to element-wise divide with this tensor.- Returns:
- The result of the element-wise tensor division.
- Throws:
IllegalArgumentException
- If the tensors do not have the same shape.
-
tensorInv
Computes the 'inverse' of this tensor. That is, computes the tensorX=this.tensorInv()
such thatthis.tensorDot(X, numIndices)
is the 'identity' tensor for the tensor dot product operation. A tensorI
is the identity for a tensor dot product ifthis.tensorDot(I, numIndices).equals(this)
. -
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
-
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 is larger than 2, it will be flattened to a single row.
-
toString
-