Class Tensor
java.lang.Object
org.flag4j.arrays.backend.AbstractTensor<Tensor,double[],Double>
org.flag4j.arrays.backend.primitive_arrays.AbstractDoubleTensor<Tensor>
org.flag4j.arrays.backend.primitive_arrays.AbstractDenseDoubleTensor<Tensor>
org.flag4j.arrays.dense.Tensor
- All Implemented Interfaces:
Serializable
,TensorOverField<Tensor,
,Tensor, double[], Double> TensorOverRing<Tensor,
,Tensor, double[], Double> TensorOverSemiring<Tensor,
Tensor, double[], Double>
A real dense tensor backed by a primitive double array.
A tensor is a multidimensional array. If N indices are required to uniquely identify all elements of a tensor, then the tensor is considered an N-dimensional tensor/array or a rank-N tensor.
The AbstractTensor.data
of a Tensor are mutable but the AbstractTensor.shape
is fixed.
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a tensor from an nD array.Constructs a copy of the specified tensor.Creates a zero tensor with the shape.Creates a tensor with the specified shape filled withfillValue
.Creates a tensor with the specified data and shape.Creates a tensor with the specified data and shape.Creates a tensor with the specified data and shape.Creates a tensor with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescriptionadd
(Complex128 b) Adds a complex-valued scalar value to each entry of this tensor.Computes the element-wise sum between two tensors of the same shape.add
(CooCTensor b) Computes the element-wise sum between two tensors of the same shape.Computes the element-wise sum between two tensors of the same shape.div
(Complex128 b) Divides each entry of this tensor by a complex scalar value.Computes the element-wise division of two tensors.Computes the element-wise multiplication of 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 while preserving order of data.flatten
(int axis) Flattens a tensor along the specified axis.int
hashCode()
makeLikeTensor
(Shape shape, double[] entries) Constructs a tensor of the same type as this tensor with the given the shape and data.mult
(Complex128 b) Multiplies a complex scalar value to each entry of this tensor.sub
(Complex128 b) Subtracts a complex-valued scalar from each element of this tensor.Computes the element-wise difference between two tensors of the same shape.sub
(CooCTensor b) Computes the element-wise difference between two tensors of the same shape and stores the result in this tensor.Computes the element-wise difference between two tensors of the same shape.Converts this tensor to an equivalent complex valued tensor.toCoo()
Converts this tensor to an equivalent sparse COO tensor.toCoo
(double estimatedSparsity) Converts this tensor to an equivalent sparse COO tensor.toMatrix()
Converts this tensor to an equivalent matrix.Converts this tensor to an equivalent 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.arrays.backend.primitive_arrays.AbstractDenseDoubleTensor
add, addEq, allClose, allClose, argmax, argmaxAbs, argmin, argminAbs, div, divEq, elemMult, elemMultEq, get, reshape, round, round, roundToZero, set, sub, subEq, T, T, tensorDot, tensorTr
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
getData, getRank, getShape, reshape, sameShape, T, totalEntries
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.flag4j.arrays.backend.ring_arrays.TensorOverRing
H
-
Constructor Details
-
Tensor
Creates a zero tensor with the shape.- Parameters:
shape
- Shape of this tensor.
-
Tensor
Creates a tensor from an nD array. The tensors shape will be inferred from.- Parameters:
nDArray
- Array to construct tensor from. Must be a rectangular array.- Throws:
IllegalArgumentException
- IfnDArray
is not an array or not rectangular.
-
Tensor
Creates a tensor with the specified shape filled withfillValue
.- Parameters:
shape
- Shape of this tensor.fillValue
- Value to fill this tensor with.
-
Tensor
Creates a tensor with the specified data and shape.- Parameters:
shape
- Shape of this tensor.entries
- Entries of this tensor.
-
Tensor
Creates a tensor with the specified data and shape.- Parameters:
shape
- Shape of this tensor.entries
- Entries of this tensor.
-
Tensor
Constructs a copy of the specified tensor.- Parameters:
src
- The tensor to make a copy of.
-
Tensor
-
Tensor
-
-
Method Details
-
flatten
Flattens tensor to single dimension while preserving order of data.- Specified by:
flatten
in classAbstractTensor<Tensor,
double[], Double> - Returns:
- The flattened tensor.
- See Also:
-
flatten
Flattens a tensor along the specified axis.- Specified by:
flatten
in classAbstractTensor<Tensor,
double[], Double> - Parameters:
axis
- Axis along which to flatten tensor.- Throws:
ArrayIndexOutOfBoundsException
- If the axis is not positive or larger thanthis.{@link #getRank()}-1
.- See Also:
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the given the shape and data.- Specified by:
makeLikeTensor
in interfaceTensorOverSemiring<Tensor,
Tensor, double[], Double> - Specified by:
makeLikeTensor
in classAbstractTensor<Tensor,
double[], Double> - Parameters:
shape
- Shape of the tensor to construct.entries
- Entries of the tensor to construct.- Returns:
- A tensor of the same type as this tensor with the given the shape and data.
-
toVector
Converts this tensor to an equivalent vector. If this vector is not rank-1 it will first be flattened then converted to a vector.- Returns:
- A vector with data equivalent to this vector.
-
toMatrix
Converts this tensor to an equivalent matrix. If this matrix is not rank-2 it will first be flattened to a row vector then converted to a matrix.- Returns:
- A matrix with data equivalent to this tensor.
-
toMatrix
Converts this tensor to an equivalent matrix with the specified shape.- Parameters:
shape
- New shape for the matrix. Must be rank-2 and broadcastable tothis.shape
.- Returns:
- A matrix with the specified shape and data equivalent to this tensor.
- Throws:
IllegalArgumentException
- Ifshape
is not broadcastable tothis.shape
.TensorShapeException
- Ifshape.getRank() != 2
.
-
toCoo
Converts this tensor to an equivalent sparse COO tensor.- Returns:
- A sparse COO tensor that is equivalent to this dense tensor.
- See Also:
-
add
Adds a complex-valued scalar value to each entry of this tensor. If the tensor is sparse, the scalar will only be added to the non-zero data of the tensor.- Parameters:
b
- Scalar value in sum.- Returns:
- The sum of this tensor with the scalar
b
.
-
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.
-
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.
-
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.
-
sub
Subtracts a complex-valued scalar from each element of this tensor.- Parameters:
b
- Scalar value in vector-scalar difference.- Returns:
- The tensor resulting from subtracting
b
from each entry of this tensor.
-
sub
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.
-
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.
-
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.
-
elemMult
-
elemMult
Computes the element-wise multiplication of two tensors.- Parameters:
b
- Second tensor in the element-wise product.- Returns:
- The element-wise product of this tensor and
b
. - Throws:
IllegalArgumentException
- If!this.shape.equals(b.shape)
-
elemMult
Computes the element-wise multiplication between two tensors.- Parameters:
b
- Second tensor in the element-wise multiplication.- Returns:
- The element-wise product of this matrix and
b
.
-
toCoo
Converts this tensor to an equivalent sparse COO tensor.- Parameters:
estimatedSparsity
- Estimated sparsity of the tensor. Must be between 0 and 1 inclusive. If this is an accurate estimation it may provide a slight speedup and can reduce unneeded memory consumption. If memory is a concern, it is better to over-estimate the sparsity. If speed is the concern it is better to under-estimate the sparsity.- Returns:
- A sparse COO tensor that is equivalent to this dense tensor.
- See Also:
-
toComplex
Converts this tensor to an equivalent complex valued tensor.- Returns:
- A complex tensor whose real components are the same as the data of this tensor and the imaginary components are zero.
-
mult
Multiplies a complex scalar value to each entry of this tensor.- Parameters:
b
- Scalar value in product.- Returns:
- The product of this tensor with
b
.
-
div
Divides each entry of this tensor by a complex scalar value.- Parameters:
b
- Scalar value in quotient.- Returns:
- The tensor-scalar quotient of this tensor with
b
.
-
div
Computes the element-wise division of two tensors.- Parameters:
b
- The second tensor in the element-wise quotient.- Returns:
- The element-wise quotient of this tensor with
b
. - Throws:
IllegalArgumentException
- If!this.shape.equals(b.shape)
-
equals
Checks if an object is equal to this tensor object. -
hashCode
-
toString
-