Class AbstractDenseDoubleTensor<T extends AbstractDoubleTensor<T>>
java.lang.Object
org.flag4j.arrays.backend.AbstractTensor<T,double[],Double>
org.flag4j.arrays.backend.primitive_arrays.AbstractDoubleTensor<T>
org.flag4j.arrays.backend.primitive_arrays.AbstractDenseDoubleTensor<T>
- Type Parameters:
T
- The type of the tensor.
- All Implemented Interfaces:
Serializable
,TensorOverField<T,
,T, double[], Double> TensorOverRing<T,
,T, double[], Double> TensorOverSemiring<T,
T, double[], Double>
public abstract class AbstractDenseDoubleTensor<T extends AbstractDoubleTensor<T>>
extends AbstractDoubleTensor<T>
This is the base class of all real primitive double tensors. The methods implemented in this class are agnostic to weather the
tensor is dense or sparse.
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractDenseDoubleTensor
(Shape shape, double[] entries) Creates a tensor with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescriptionComputes the element-wise sum between two tensors of the same shape.void
Computes the element-wise sum between two tensors of the same shape and stores the result in this tensor.boolean
Checks if all data of this matrix are 'close' as defined below.boolean
Checks if all data of this matrix are 'close' as defined below.int[]
argmax()
Finds the indices of the maximum value in this tensor.int[]
Finds the indices of the maximum absolute value in this tensor.int[]
argmin()
Finds the indices of the minimum value in this tensor.int[]
Finds the indices of the minimum absolute value in this tensor.Computes the element-wise quotient between two tensors.void
Computes the element-wise quotient between two tensors and stores the result in this tensor.Computes the element-wise multiplication of two tensors of the same shape.void
elemMultEq
(T b) Computes the element-wise multiplication of two tensors of the same shape and stores the result in this tensor.get
(int... indices) Gets the element of this tensor at the specified indices.Copies and reshapes this tensor.round()
Rounds all data in this matrix to the nearest integer.round
(int precision) Rounds all data within this matrix to the specified precision.roundToZero
(double tolerance) Sets all elements of this matrix to zero if they are withintol
of zero.Sets the element of this tensor at the specified indices.Computes the element-wise difference between two tensors of the same shape.void
Computes the element-wise difference between two tensors of the same shape and stores the result in this tensor.T
(int... axes) Computes the transpose of this tensor.T
(int axis1, int axis2) Computes the transpose of a tensor by exchangingaxis1
andaxis2
.Computes the tensor contraction of this tensor with a specified tensor over the specified set of axes.tensorTr
(int axis1, int axis2) Computes the generalized trace of this tensor along the specified axes.Methods inherited from class org.flag4j.arrays.backend.primitive_arrays.AbstractDoubleTensor
abs, add, add, addEq, addEq, conj, copy, div, div, divEq, divEq, H, H, isFinite, isInfinite, isNaN, isNeg, isOnes, isPos, isZeros, max, maxAbs, min, minAbs, mult, mult, multEq, multEq, prod, recip, roundToZero, sqrt, sub, sub, subEq, subEq, sum
Methods inherited from class org.flag4j.arrays.backend.AbstractTensor
flatten, flatten, getData, getRank, getShape, makeLikeTensor, reshape, sameShape, T, 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.ring_arrays.TensorOverRing
H
Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.TensorOverSemiring
getData, getRank, getShape, makeLikeTensor, tensorDot, tensorDot, tensorDot, tensorTr
-
Constructor Details
-
AbstractDenseDoubleTensor
Creates a tensor with the specified data and shape.- Parameters:
shape
- Shape of this tensor.entries
- Entries of this tensor. If this tensor is dense, this specifies all data within the tensor. If this tensor is sparse, this specifies only the non-zero data of the tensor.
-
-
Method Details
-
get
Gets the element of this tensor at the specified indices.- Specified by:
get
in classAbstractTensor<T extends AbstractDoubleTensor<T>,
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 tensor.
-
set
Sets the element of this tensor at the specified indices.- Specified by:
set
in classAbstractTensor<T extends AbstractDoubleTensor<T>,
double[], Double> - Parameters:
value
- New value to set the specified index of this tensor to.indices
- Indices of the element to set.- Returns:
- A reference to this tensor.
- Throws:
IndexOutOfBoundsException
- Ifindices
is not within the bounds of this tensor.
-
T
Computes the transpose of a tensor by exchangingaxis1
andaxis2
.- Specified by:
T
in classAbstractTensor<T extends AbstractDoubleTensor<T>,
double[], Double> - Parameters:
axis1
- First axis to exchange.axis2
- Second axis to exchange.- Returns:
- The 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:
-
T
Computes the transpose of this tensor. That is, permutes the axes of this tensor so that it matches the permutation specified byaxes
.- Specified by:
T
in classAbstractTensor<T extends AbstractDoubleTensor<T>,
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 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:
-
sub
Computes the element-wise difference between two tensors of the same shape.- Parameters:
b
- Second tensor in the element-wise difference.- Returns:
- The difference of this tensor with
b
. - Throws:
TensorShapeException
- If this tensor andb
do not have the same shape.
-
subEq
Computes the element-wise difference between two tensors of the same shape and stores the result in this tensor.- Parameters:
b
- Second tensor in the element-wise difference.- Throws:
TensorShapeException
- If this tensor andb
do not have the same shape.
-
add
Computes the element-wise sum between two tensors of the same shape.- Parameters:
b
- Second tensor in the element-wise sum.- Returns:
- The sum of this tensor with
b
. - Throws:
TensorShapeException
- If this tensor andb
do not have the same shape.
-
addEq
Computes the element-wise sum between two tensors of the same shape and stores the result in this tensor.- Parameters:
b
- Second tensor in the element-wise sum.
-
elemMult
Computes the element-wise multiplication of two tensors of the same shape.- 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.
-
elemMultEq
Computes the element-wise multiplication of two tensors of the same shape and stores the result in this tensor.- Parameters:
b
- Second tensor in the element-wise product.- Throws:
IllegalArgumentException
- If this tensor andb
do not have the same shape.
-
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.- 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.
-
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 of this tensor specified by
axis1
andaxis2
. The shape of the resulting tensor is equal to this tensor with theaxis1
andaxis2
removed.- 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 == 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.)
-
div
-
divEq
Computes the element-wise quotient between two tensors and stores the result in this tensor.- Parameters:
b
- Second tensor in the element-wise quotient.
-
argminAbs
public int[] argminAbs()Finds the indices of the minimum absolute value in this tensor.- Returns:
- The indices of the minimum value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
argmaxAbs
public int[] argmaxAbs()Finds the indices of the maximum absolute value in this tensor.- Returns:
- The indices of the maximum absolute value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
argmin
public int[] argmin()Finds the indices of the minimum value in this tensor.- Returns:
- The indices of the minimum value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
argmax
public int[] argmax()Finds the indices of the maximum value in this tensor.- Returns:
- The indices of the maximum value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
reshape
Copies and reshapes this tensor.- Specified by:
reshape
in classAbstractTensor<T extends AbstractDoubleTensor<T>,
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
.
-
round
Rounds all data in this matrix to the nearest integer.- Overrides:
round
in classAbstractDoubleTensor<T extends AbstractDoubleTensor<T>>
- Returns:
- A new matrix containing the data of this matrix rounded to the nearest integer.
- See Also:
-
round
Rounds all data within this matrix to the specified precision.- Overrides:
round
in classAbstractDoubleTensor<T extends AbstractDoubleTensor<T>>
- Parameters:
precision
- The precision to round to (i.e. the number of decimal places to round to). Must be non-negative.- Returns:
- A new matrix containing the data of this matrix rounded to the specified precision.
- See Also:
-
roundToZero
Sets all elements of this matrix to zero if they are withintol
of zero. This is not done in place.- Overrides:
roundToZero
in classAbstractDoubleTensor<T extends AbstractDoubleTensor<T>>
- Parameters:
tolerance
- Threshold for rounding values to zero. That is, if a value in this matrix is less than the threshold in absolute value then it will be rounded to zero. This value must be non-negative.precision
- The precision to round to (i.e. the number of decimal places to round to). Must be non-negative.- Returns:
- A copy of this matrix with all data within
tol
of zero set to zero. - See Also:
-
allClose
Checks if all data of this matrix are 'close' as defined below. Custom tolerances may be specified usingallClose(AbstractDoubleTensor, double, double)
.- Parameters:
b
- Second tensor in the comparison.- Returns:
- True if both tensors have the same shape and all data are 'close' element-wise, i.e.
elements
x
andy
at the same positions in the two tensors respectively and satisfy|x-y| <= (1E-08 + 1E-05*|y|)
. Otherwise, returns false. - See Also:
-
allClose
Checks if all data of this matrix are 'close' as defined below.- Parameters:
b
- Second tensor in the comparison.- Returns:
- True if both tensors have the same length and all data are 'close' element-wise, i.e.
elements
x
andy
at the same positions in the two tensors respectively and satisfy|x-y| <= (absTol + relTol*|y|)
. Otherwise, returns false. - See Also:
-