Package org.flag4j.arrays.dense
Class CTensor
- All Implemented Interfaces:
Serializable
,ComplexTensorExclusiveMixin<CTensor>
,ComplexTensorMixin<CTensor,
,Tensor> DenseMixin<CNumber>
,TensorComparisonsMixin
,TensorExclusiveMixin<CTensor,
,CTensor, CooCTensor, CTensor> TensorManipulationsMixin<CTensor>
,TensorOperationsMixin<CTensor,
,CTensor, CTensor, CTensor, Tensor, CNumber> TensorPropertiesMixin
public class CTensor
extends ComplexDenseTensorBase<CTensor,Tensor>
implements ComplexTensorExclusiveMixin<CTensor>
Complex dense tensor.
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.core.TensorBase
DEFAULT_ROUND_TO_ZERO_THRESHOLD, entries, shape
-
Constructor Summary
ConstructorDescriptionCreates a complex tensor whose shape and entries are specified by another tensor.Creates a complex tensor whose shape and entries are specified by another tensor.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.void
addEq
(CooCTensor B) Computes the element-wise addition of two tensors of the same rank and stores the result in this tensor.void
Computes the element-wise subtraction 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 CTensor
getSelf()
Simply returns a reference of this tensor.H()
Computes the conjugate transpose of this tensor.H
(int... axes) Computes the conjugate transpose of this tensor.H
(int axis1, int axis2) Computes the transpose of a tensor.protected Tensor
makeRealTensor
(Shape shape, double[] entries) Factory to create a real tensor with the specified shape and size.protected CTensor
makeTensor
(Shape shape, CNumber[] entries) Factory to create a tensor with the specified shape and size.Computes the element-wise addition 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.void
subEq
(CooCTensor B) Computes the element-wise subtraction of two tensors of the same rank and stores the result in this tensor.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.ComplexDenseTensorBase
abs, add, add, add, addEq, addEq, addEq, allClose, argMax, argMin, conj, copy, div, div, elemDiv, elemMult, get, hashCode, isComplex, isOnes, isReal, isZeros, max, maxAbs, min, minAbs, mult, mult, recip, reshape, reshape, round, round, roundToZero, roundToZero, set, set, sqrt, sub, sub, sub, subEq, subEq, subEq, sum, toReal, toRealSafe
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.ComplexTensorExclusiveMixin
hermTranspose, hermTranspose
Methods inherited from interface org.flag4j.core.ComplexTensorMixin
hermTranspose
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
-
CTensor
Constructs a tensor with given shape filled with zeros.- Parameters:
shape
- Shape of the tensor.
-
CTensor
Constructs a tensor with given shape filled with specified values.- Parameters:
shape
- Shape of the tensor.fillValue
- Value to fill tensor with.
-
CTensor
-
CTensor
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.
-
CTensor
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.
-
CTensor
Constructs a tensor with given shape filled with specified values. Note, unlike other constructors, the entries parameter is not copied.- Parameters:
shape
- Shape of the tensor.entries
- Entries of the vector.- Throws:
IllegalArgumentException
- If the shape does not match the number of entries.
-
CTensor
Creates a complex tensor whose shape and entries are specified by another tensor.- Parameters:
A
- Tensor specifying shape and entries.
-
CTensor
Creates a complex tensor whose shape and entries are specified by another tensor.- Parameters:
A
- Tensor specifying shape and entries.
-
-
Method Details
-
makeTensor
Factory to create a tensor with the specified shape and size.- Specified by:
makeTensor
in classDenseTensorBase<CTensor,
CTensor, Tensor, CNumber[], CNumber> - 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.
-
toCoo
Converts this dense tensor to an equivalent sparse COO tensor. -
getSelf
-
makeRealTensor
Factory to create a real tensor with the specified shape and size.- Specified by:
makeRealTensor
in classComplexDenseTensorBase<CTensor,
Tensor> - 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.
-
flatten
Flattens a tensor along the specified axis. The resulting tensor will have the same rank but only have values along the single specified axis.- Specified by:
flatten
in interfaceTensorManipulationsMixin<CTensor>
- 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
CTensor
. False otherwise.
-
H
Computes the conjugate transpose of this tensor. In the context of a tensor, this swaps the first and last axes and takes the complex conjugate of the elements along these axes. Same asComplexTensorMixin.hermTranspose()
.- Specified by:
H
in interfaceComplexTensorMixin<CTensor,
Tensor> - Returns:
- The complex transpose of this tensor.
-
H
Computes the transpose of a tensor. Same asComplexTensorExclusiveMixin.hermTranspose(int, int)
. In the context of a tensor, this exchanges the specified axes and takes the complex conjugate of elements along those axes. Also seeComplexTensorMixin.hermTranspose()
andH()
to conjugate transpose first and last axes.- Specified by:
H
in interfaceComplexTensorExclusiveMixin<CTensor>
- Parameters:
axis1
- First axis to exchange and apply complex conjugate.axis2
- Second axis to exchange and apply complex conjugate.- Returns:
- The conjugate transpose of this tensor.
-
H
Computes the conjugate transpose of this tensor. That is, interchanges the axes of this tensor so that it matches the specified axes permutation and takes the complex conjugate of the elements of these axes. Same asComplexTensorExclusiveMixin.hermTranspose(int[])
}.- Specified by:
H
in interfaceComplexTensorExclusiveMixin<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 conjugate 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}
.
-
addEq
Computes the element-wise addition of two tensors of the same rank and stores the result in this tensor.- Specified by:
addEq
in interfaceComplexTensorExclusiveMixin<CTensor>
- Parameters:
B
- Second tensor in the addition.- Throws:
IllegalArgumentException
- If this tensor andB
have different shapes.
-
addEq
Computes the element-wise addition of two tensors of the same rank and stores the result in this tensor.- Specified by:
addEq
in interfaceComplexTensorExclusiveMixin<CTensor>
- Parameters:
B
- Second tensor in the addition.- Throws:
IllegalArgumentException
- If this tensor andB
have different shapes.
-
subEq
Computes the element-wise subtraction of two tensors of the same rank and stores the result in this tensor.- Specified by:
subEq
in interfaceComplexTensorExclusiveMixin<CTensor>
- Parameters:
B
- Second tensor in the subtraction.- Throws:
IllegalArgumentException
- If this tensor andB
have different shapes.
-
subEq
Computes the element-wise subtraction of two tensors of the same rank and stores the result in this tensor.- Specified by:
subEq
in interfaceComplexTensorExclusiveMixin<CTensor>
- Parameters:
B
- Second tensor in the subtraction.- Throws:
IllegalArgumentException
- If this tensor andB
have different shapes.
-
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.- Specified by:
T
in interfaceTensorExclusiveMixin<CTensor,
CTensor, CooCTensor, CTensor> - Parameters:
axis1
- First axis to exchange.axis2
- Second axis to exchange.- Returns:
- The transpose of this tensor.
-
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<CTensor,
CTensor, CooCTensor, 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}
.
-
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<CTensor,
CTensor, CooCTensor, 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<CTensor,
CTensor, CooCTensor, 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.
-
add
Computes the element-wise addition between two tensors of the same rank.- Specified by:
add
in interfaceTensorExclusiveMixin<CTensor,
CTensor, CooCTensor, 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<CTensor,
CTensor, CooCTensor, 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 addition between two tensors of the same rank.- Specified by:
sub
in interfaceTensorExclusiveMixin<CTensor,
CTensor, CooCTensor, 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<CTensor,
CTensor, CooCTensor, 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<CTensor,
CTensor, CooCTensor, 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.
-
T
Computes the transpose of a tensor. Same asTensorOperationsMixin.transpose()
. -
sub
Computes the element-wise subtraction between two tensors of the same rank.- Specified by:
sub
in interfaceTensorExclusiveMixin<CTensor,
CTensor, CooCTensor, 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.
-
addEq
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.
-
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<CTensor,
CTensor, CooCTensor, 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<CTensor,
CTensor, CooCTensor, 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<CTensor,
CTensor, CooCTensor, 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<CTensor,
CTensor, CooCTensor, CTensor> - Parameters:
B
- Tensor to element-wise divide from 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)
.- Specified by:
tensorInv
in interfaceTensorExclusiveMixin<CTensor,
CTensor, CooCTensor, CTensor> - Parameters:
numIndices
- The number of first numIndices which are involved in the inverse sum.- Returns:
- The 'inverse' of this tensor as defined in the above sense.
- See Also:
-
flatten
Flattens tensor to single dimension. To flatten tensor along a single axis.- Specified by:
flatten
in interfaceTensorManipulationsMixin<CTensor>
- Returns:
- The flattened tensor.
-
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
-