Module flag4j
Package org.flag4j.arrays.backend
Class PrimitiveDoubleTensorBase<T extends PrimitiveDoubleTensorBase<T,U>,U extends PrimitiveDoubleTensorBase<U,U>>
java.lang.Object
org.flag4j.arrays.backend.AbstractTensor<T,double[],Double>
org.flag4j.arrays.backend.TensorOverSemiRing<T,U,double[],Double>
org.flag4j.arrays.backend.TensorOverRing<T,U,double[],Double>
org.flag4j.arrays.backend.TensorOverField<T,U,double[],Double>
org.flag4j.arrays.backend.DoubleTensorBase<T,U>
- Type Parameters:
T
- Type of this tensor.U
- Type of dense tensor equivalent toT
. IfT
is dense, then this should be the same type asT
. This type parameter is required because some operations (e.g.TensorOverSemiRing.tensorDot(TensorOverSemiRing)
) between two sparse tensors result in a dense tensor.
- All Implemented Interfaces:
Serializable
,TensorBinaryOpsMixin<T,
,T> TensorMixin<T,
,double[], Double> TensorPrimitiveOpsMixin<T>
- Direct Known Subclasses:
CooMatrix
,CooTensor
,CooVector
,CsrMatrix
,DensePrimitiveDoubleTensorBase
public abstract class PrimitiveDoubleTensorBase<T extends PrimitiveDoubleTensorBase<T,U>,U extends PrimitiveDoubleTensorBase<U,U>>
extends TensorOverField<T,U,double[],Double>
implements TensorPrimitiveOpsMixin<T>
Base class for all real tensors which are backed by a primitive double array.
The entries of PrimitiveDoubleTensorBase's are mutable but the tensor has a fixed shape.
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
entries, rank, shape
-
Constructor Summary
ModifierConstructorDescriptionprotected
PrimitiveDoubleTensorBase
(Shape shape, double[] entries) Creates a tensor with the specified entries and shape. -
Method Summary
Modifier and TypeMethodDescriptionabs()
Computes the element-wise absolute value of this tensor.add
(double b) Adds a scalar value to each element of this tensor.Adds a scalar field value to each entry of this tensor.Computes the element-wise sum between two tensors of the same shape.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 deep copy of this tensor.div
(double divisor) Computes the scalar division of this tensor and the specified scalardivisor
.Divides each entry of this tensor by a scalar field element.void
Divides each entry of this tensor by a 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.H()
Computes the conjugate transpose of a tensor by exchanging the first and last axes of this tensor and conjugating the exchanged values.H
(int... axes) Computes the conjugate transpose of this tensor.H
(int axis1, int axis2) Computes the conjugate transpose of a tensor by conjugating and exchangingaxis1
andaxis2
.boolean
isOnes()
Checks if this tensor only contains zeros.boolean
isZeros()
Checks if this tensor only contains zeros.mult
(double factor) Computes the scalar multiplication between this tensor and the specified scalarfactor
.Multiplies a scalar 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.round()
Rounds all entries of this tensor to the nearest whole number.round
(int precision) Rounds all entries of this tensor to the number of decimal specified byprecision
.sqrt()
Computes the element-wise square root of a tensor.sub
(double b) Subtracts a scalar value from each element of this tensor.Subtracts a scalar value from each entry of this tensor.Computes the element-wise difference between two tensors of the same shape.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.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
-
Constructor Details
-
PrimitiveDoubleTensorBase
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
-
get
Gets the element of this tensor at the specified indices.- Specified by:
get
in interfaceTensorMixin<T extends PrimitiveDoubleTensorBase<T,
U>, double[], Double> - Specified by:
get
in classAbstractTensor<T extends PrimitiveDoubleTensorBase<T,
U>, double[], Double> - 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
Flattens tensor to single dimension while preserving order of entries.- Specified by:
flatten
in classAbstractTensor<T extends PrimitiveDoubleTensorBase<T,
U>, double[], Double> - Returns:
- The flattened tensor.
- See Also:
-
flatten
Flattens a tensor along the specified axis.- Specified by:
flatten
in classAbstractTensor<T extends PrimitiveDoubleTensorBase<T,
U>, double[], Double> - 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 PrimitiveDoubleTensorBase<T,
U>, double[], Double> - 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
.
-
sub
Subtracts a scalar value from each entry of this tensor.- Specified by:
sub
in classTensorOverRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Scalar value in difference.- Returns:
- The difference of this tensor and the scalar
b
.
-
subEq
Subtracts a scalar value from each entry of this tensor and stores the result in this tensor.- Specified by:
subEq
in classTensorOverRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Scalar value in difference.
-
add
Adds a scalar field value to each entry of this tensor.- Specified by:
add
in classTensorOverSemiRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Scalar field value in sum.- Returns:
- The sum of this tensor with the scalar
b
.
-
addEq
Adds a scalar value to each entry of this tensor and stores the result in this tensor.- Specified by:
addEq
in classTensorOverSemiRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Scalar field value in sum.
-
add
Computes the element-wise sum between two tensors of the same shape.- Specified by:
add
in classTensorOverSemiRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - 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.
-
mult
Multiplies a scalar value to each entry of this tensor.- Specified by:
mult
in classTensorOverSemiRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Scalar value in product.- Returns:
- The product of this tensor with
b
.
-
multEq
Multiplies a scalar value to each entry of this tensor and stores the result in this tensor.- Specified by:
multEq
in classTensorOverSemiRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Scalar value in product.
-
sub
Computes the element-wise difference between two tensors of the same shape.- Specified by:
sub
in classTensorOverRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Second tensor in the element-wise difference.- Returns:
- The difference of this tensor with
b
. - Throws:
IllegalArgumentException
- If this tensor andb
do not have the same shape.
-
abs
Computes the element-wise absolute value of this tensor.- Specified by:
abs
in classTensorOverRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Returns:
- The element-wise absolute value of this tensor.
-
elemMult
Computes the element-wise multiplication of two tensors of the same shape.- Specified by:
elemMult
in classTensorOverSemiRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - 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 PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - 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
. This will be a tensor of rankthis.getRank() - 2
with the same shape as this tensor but withaxis1
andaxis2
removed. - 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.- Specified by:
isZeros
in classTensorOverSemiRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Returns:
- True if this tensor only contains zeros. Otherwise, returns false.
-
isOnes
public boolean isOnes()Checks if this tensor only contains zeros. If this tensor is sparse, then only the non-zero values are considered.- Specified by:
isOnes
in classTensorOverSemiRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Returns:
- True if this tensor only contains zeros. Otherwise, returns false.
-
sum
Computes the sum of all values in this tensor.- Specified by:
sum
in classTensorOverSemiRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Returns:
- The sum of all values in this tensor.
-
prod
Computes the product of all values in this tensor.- Specified by:
prod
in classTensorOverSemiRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Returns:
- The product of all values in this tensor.
-
sqrt
Computes the element-wise square root of a tensor.- Specified by:
sqrt
in classTensorOverField<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - 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.- Specified by:
T
in interfaceTensorMixin<T extends PrimitiveDoubleTensorBase<T,
U>, double[], Double> - Returns:
- The transpose of this tensor.
- See Also:
-
conj
Computes the element-wise conjugation of this tensor.- Specified by:
conj
in classTensorOverRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Returns:
- The element-wise conjugation of this tensor.
-
H
Computes the conjugate transpose of a tensor by exchanging the first and last axes of this tensor and conjugating the exchanged values.- Specified by:
H
in classTensorOverRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Returns:
- The conjugate transpose of this tensor.
- See Also:
-
H
Computes the conjugate transpose of a tensor by conjugating and exchangingaxis1
andaxis2
.- Specified by:
H
in classTensorOverRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
axis1
- First axis to exchange and conjugate.axis2
- Second axis to exchange and conjugate.- Returns:
- The conjugate transpose of this tensor according to the specified axes.
- Throws:
IndexOutOfBoundsException
- If eitheraxis1
oraxis2
are out of bounds for the rank of this tensor.- See Also:
-
H
Computes the conjugate transpose of this tensor. That is, conjugates and permutes the axes of this tensor so that it matches the permutation specified byaxes
.- Specified by:
H
in classTensorOverRing<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - 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:
IndexOutOfBoundsException
- If any element ofaxes
is out of bounds for the rank of this tensor.IllegalArgumentException
- Ifaxes
is not a permutation of{1, 2, 3, ... N-1}
.- See Also:
-
recip
Computes the element-wise reciprocals of this tensor.- Specified by:
recip
in classTensorOverField<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Returns:
- A tensor containing the reciprocal elements of this tensor.
-
div
Divides each entry of this tensor by a scalar field element.- Specified by:
div
in classTensorOverField<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Scalar field value in quotient.- Returns:
- The quotient of this tensor with
b
.
-
divEq
Divides each entry of this tensor by a scalar field element and stores the result in this tensor.- Specified by:
divEq
in classTensorOverField<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Scalar field value in quotient.
-
copy
Creates a deep copy of this tensor.- Specified by:
copy
in interfaceTensorMixin<T extends PrimitiveDoubleTensorBase<T,
U>, double[], Double> - Returns:
- A deep copy of this tensor.
-
add
Adds a scalar value to each element of this tensor.- Specified by:
add
in interfaceTensorPrimitiveOpsMixin<T extends PrimitiveDoubleTensorBase<T,
U>> - Specified by:
add
in classTensorOverField<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Value to add to each entry of this tensor.- Returns:
- The result of adding the specified scalar value to each entry of this tensor.
-
sub
Subtracts a scalar value from each element of this tensor.- Specified by:
sub
in interfaceTensorPrimitiveOpsMixin<T extends PrimitiveDoubleTensorBase<T,
U>> - Specified by:
sub
in classTensorOverField<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
b
- Value to subtract from each entry of this tensor.- Returns:
- The result of subtracting the specified scalar value from each entry of this tensor.
-
mult
Computes the scalar multiplication between this tensor and the specified scalarfactor
.- Specified by:
mult
in interfaceTensorPrimitiveOpsMixin<T extends PrimitiveDoubleTensorBase<T,
U>> - Specified by:
mult
in classTensorOverField<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
factor
- Scalar factor to apply to this tensor.- Returns:
- The scalar product of this tensor and
factor
.
-
div
Computes the scalar division of this tensor and the specified scalardivisor
.- Specified by:
div
in interfaceTensorPrimitiveOpsMixin<T extends PrimitiveDoubleTensorBase<T,
U>> - Specified by:
div
in classTensorOverField<T extends PrimitiveDoubleTensorBase<T,
U>, U extends PrimitiveDoubleTensorBase<U, U>, double[], Double> - Parameters:
divisor
- The scalar value to divide this tensor by.- Returns:
- The result of dividing this tensor by the specified scalar.
-
round
Rounds all entries of this tensor to the nearest whole number.- Returns:
- A copy of this tensor with all entries rounded to the nearest whole number.
- See Also:
-
round
Rounds all entries of this tensor to the number of decimal specified byprecision
.- Parameters:
precision
- The number of decimal places to round the tensor to.- Returns:
- A copy of this tensor with all entries rounded to the number of decimal specified by
precision
. - See Also:
-