Package org.flag4j.core.sparse_base
Class RealSparseTensorBase<T extends TensorBase<T,?,?,?,?,?,?>,U,W,Z>
java.lang.Object
org.flag4j.core.TensorBase<T,U,W,Z,T,double[],Double>
org.flag4j.core.sparse_base.SparseTensorBase<T,U,W,Z,T,double[],Double>
org.flag4j.core.sparse_base.RealSparseTensorBase<T,U,W,Z>
- Type Parameters:
T
- Type of this tensor.U
- Dense Tensor type.W
- Complex Tensor type.Z
- Dense complex tensor type.
- All Implemented Interfaces:
Serializable
,RealTensorMixin<T,
,W> SparseTensorMixin
,TensorComparisonsMixin
,TensorManipulationsMixin<T>
,TensorOperationsMixin<T,
,U, W, Z, T, Double> TensorPropertiesMixin
public abstract class RealSparseTensorBase<T extends TensorBase<T,?,?,?,?,?,?>,U,W,Z>
extends SparseTensorBase<T,U,W,Z,T,double[],Double>
implements RealTensorMixin<T,W>
Base class for all sparse tensor.
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.core.sparse_base.SparseTensorBase
indices, nnz
Fields inherited from class org.flag4j.core.TensorBase
DEFAULT_ROUND_TO_ZERO_THRESHOLD, entries, shape
-
Constructor Summary
ModifierConstructorDescriptionprotected
RealSparseTensorBase
(Shape shape, int nonZeroEntries, double[] entries, int[][] indices) Creates a sparse tensor with specified shape.protected
RealSparseTensorBase
(Shape shape, int nonZeroEntries, double[] entries, int[] initIndices, int[]... restIndices) Creates a sparse tensor with specified shape. -
Method Summary
Modifier and TypeMethodDescriptionabs()
Computes the element-wise absolute value/magnitude of a tensor.int[]
argMax()
Finds the indices of the maximum value in this tensor.int[]
argMin()
Finds the indices of the minimum value in this tensor.private int[][]
Creates a deep copy of the indices of this sparse tensor.div
(double divisor) Computes the scalar division of a tensor.Computes the scalar division of a tensor.boolean
isNeg()
Checks if this tensor contains only non-positive values.boolean
isOnes()
Checks if this sparse tensors non-zero values are all ones.boolean
isPos()
Checks if this tensor contains only non-negative values.boolean
isZeros()
Checks if this tensor only contains zeros.protected abstract W
makeComplexTensor
(Shape shape, CNumber[] entries, int[][] indices) A factory for creating a complex sparse tensor.protected abstract U
makeDenseTensor
(Shape shape, double[] entries) A factory for creating a real dense tensor.protected abstract T
makeTensor
(Shape shape, double[] entries, int[][] indices) A factory for creating a real sparse tensor.double
max()
Finds the maximum value in this tensor.double
maxAbs()
Finds the maximum value, in absolute value, in this tensor.double
min()
Finds the minimum value in this tensor.double
minAbs()
Finds the minimum value, in absolute value, in this tensor.mult
(double factor) Computes scalar multiplication of a tensor.Computes scalar multiplication of a tensor.int
Gets the number of non-zero entries stored in this sparse tensor.recip()
Computes the reciprocals, element-wise, of a tensor.reshape
(int... dims) Copies and reshapes tensor if possible.round()
Rounds each entry of this tensor to the nearest whole number.round
(int precision) Rounds each entry in this tensor to the nearest whole number.Rounds values in this tensor which are close to zero in absolute value to zero.roundToZero
(double threshold) Rounds values which are close to zero in absolute value to zero.void
Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index.sqrt()
Computes the element-wise square root of a tensor.sum()
Sums together all entries in the tensor.Methods inherited from class org.flag4j.core.sparse_base.SparseTensorBase
density, sparsity, toDense
Methods inherited from class org.flag4j.core.TensorBase
allClose, allClose, getEntries, getRank, getSelf, getShape, sameLength, sameShape, tensorEquals, 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.core.RealTensorMixin
toComplex
Methods inherited from interface org.flag4j.core.TensorComparisonsMixin
equals
Methods inherited from interface org.flag4j.core.TensorManipulationsMixin
flatten, flatten, reshape, set
-
Constructor Details
-
RealSparseTensorBase
Creates a sparse tensor with specified shape. Note, this constructor stores indices for each element in the same array. That is, for a shape with rankm
andn
non-zero entries, the indices array will have shapen-by-m
. This is the opposite ofRealSparseTensorBase(Shape, int, double[], int[], int[][])
.- Parameters:
shape
- Shape of this tensor.nonZeroEntries
- Number of non-zero entries in the sparse tensor.entries
- Non-zero entries of this sparse tensor.indices
- Indices of the non-zero entries.- Throws:
IllegalArgumentException
- If the number of rows in the indices array is not equal to the number of elements in the entries array.IllegalArgumentException
- If the number of columns in the entries array is not equal to the rank of this tensor.
-
RealSparseTensorBase
protected RealSparseTensorBase(Shape shape, int nonZeroEntries, double[] entries, int[] initIndices, int[]... restIndices) Creates a sparse tensor with specified shape. Note, this constructor stores indices for each element in different arrays. That is, for a shape with rankm
andn
non-zero entries, the indices array will have shapem-by-n
. This is the opposite ofRealSparseTensorBase(Shape, int, double[], int[][])
.- Parameters:
shape
- ShapenonZeroEntries
- The number of non-zero entries of the tensor.entries
- Non-zero entries of the sparse tensor.initIndices
- Non-zero indices of the first axis of the tensor.restIndices
- Non-zero indices of the rest of this tensor's axes.
-
-
Method Details
-
copyIndices
private int[][] copyIndices()Creates a deep copy of the indices of this sparse tensor.- Returns:
- A deep copy of the indices of this sparse tensor.
-
makeTensor
A factory for creating a real sparse tensor.- Parameters:
shape
- Shape of the sparse tensor to make.entries
- Non-zero entries of the sparse tensor to make.indices
- Non-zero indices of the sparse tensor to make.- Returns:
- A tensor created from the specified parameters.
-
makeDenseTensor
-
makeComplexTensor
A factory for creating a complex sparse tensor.- Parameters:
shape
- Shape of the tensor to make.entries
- Non-zero entries of the sparse tensor to make.indices
- Non-zero indices of the sparse tensor to make.- Returns:
- A tensor created from the specified parameters.
-
sortIndices
public void sortIndices()Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index.- Specified by:
sortIndices
in interfaceSparseTensorMixin
-
nonZeroEntries
public int nonZeroEntries()Gets the number of non-zero entries stored in this sparse tensor.- Overrides:
nonZeroEntries
in classSparseTensorBase<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U, W, Z, T extends TensorBase<T, ?, ?, ?, ?, ?, ?>, double[], Double> - Returns:
- The number of non-zero entries stored in this tensor.
-
min
public double min()Description copied from interface:TensorPropertiesMixin
Finds the minimum value in this tensor. If this tensor is complex, then this method finds the smallest value in magnitude.- Specified by:
min
in interfaceTensorPropertiesMixin
- Returns:
- The minimum value (smallest in magnitude for a complex valued tensor) in this tensor.
-
max
public double max()Description copied from interface:TensorPropertiesMixin
Finds the maximum value in this tensor. If this tensor is complex, then this method finds the largest value in magnitude.- Specified by:
max
in interfaceTensorPropertiesMixin
- Returns:
- The maximum value (largest in magnitude for a complex valued tensor) in this tensor.
-
minAbs
public double minAbs()Description copied from interface:TensorPropertiesMixin
Finds the minimum value, in absolute value, in this tensor. If this tensor is complex, then this method is equivalent toTensorPropertiesMixin.min()
.- Specified by:
minAbs
in interfaceTensorPropertiesMixin
- Returns:
- The minimum value, in absolute value, in this tensor.
-
maxAbs
public double maxAbs()Description copied from interface:TensorPropertiesMixin
Finds the maximum value, in absolute value, in this tensor. If this tensor is complex, then this method is equivalent toTensorPropertiesMixin.max()
.- Specified by:
maxAbs
in interfaceTensorPropertiesMixin
- Returns:
- The maximum value, in absolute value, in this tensor.
-
isPos
public boolean isPos()Description copied from interface:RealTensorMixin
Checks if this tensor contains only non-negative values.- Specified by:
isPos
in interfaceRealTensorMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U> - Returns:
- True if this tensor only contains non-negative values. Otherwise, returns false.
-
isNeg
public boolean isNeg()Description copied from interface:RealTensorMixin
Checks if this tensor contains only non-positive values.- Specified by:
isNeg
in interfaceRealTensorMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U> - Returns:
- trie if this tensor only contains non-positive values. Otherwise, returns false.
-
isZeros
public boolean isZeros()Description copied from interface:TensorComparisonsMixin
Checks if this tensor only contains zeros.- Specified by:
isZeros
in interfaceTensorComparisonsMixin
- Returns:
- True if this tensor only contains zeros. Otherwise, returns false.
-
isOnes
public boolean isOnes()Checks if this sparse tensors non-zero values are all ones.- Specified by:
isOnes
in interfaceTensorComparisonsMixin
- Returns:
- True if this sparse tensors non-zero values are all ones. False otherwise.
-
sum
Description copied from interface:TensorOperationsMixin
Sums together all entries in the tensor.- Specified by:
sum
in interfaceTensorOperationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U, W, Z, T extends TensorBase<T, ?, ?, ?, ?, ?, ?>, Double> - Returns:
- The sum of all entries in this tensor.
-
mult
Description copied from interface:TensorOperationsMixin
Computes scalar multiplication of a tensor.- Specified by:
mult
in interfaceTensorOperationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U, W, Z, T extends TensorBase<T, ?, ?, ?, ?, ?, ?>, Double> - Parameters:
factor
- Scalar value to multiply with tensor.- Returns:
- The result of multiplying this tensor by the specified scalar.
-
mult
Description copied from interface:TensorOperationsMixin
Computes scalar multiplication of a tensor.- Specified by:
mult
in interfaceTensorOperationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U, W, Z, T extends TensorBase<T, ?, ?, ?, ?, ?, ?>, Double> - Parameters:
factor
- Scalar value to multiply with tensor.- Returns:
- The result of multiplying this tensor by the specified scalar.
-
div
Description copied from interface:TensorOperationsMixin
Computes the scalar division of a tensor.- Specified by:
div
in interfaceTensorOperationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U, W, Z, T extends TensorBase<T, ?, ?, ?, ?, ?, ?>, Double> - Parameters:
divisor
- The scalar value to divide tensor by.- Returns:
- The result of dividing this tensor by the specified scalar.
-
div
Description copied from interface:TensorOperationsMixin
Computes the scalar division of a tensor.- Specified by:
div
in interfaceTensorOperationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U, W, Z, T extends TensorBase<T, ?, ?, ?, ?, ?, ?>, Double> - Parameters:
divisor
- The scalar value to divide tensor by.- Returns:
- The result of dividing this tensor by the specified scalar.
-
sqrt
Description copied from interface:TensorOperationsMixin
Computes the element-wise square root of a tensor.- Specified by:
sqrt
in interfaceTensorOperationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U, W, Z, T extends TensorBase<T, ?, ?, ?, ?, ?, ?>, 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.
-
abs
Description copied from interface:TensorOperationsMixin
Computes the element-wise absolute value/magnitude of a tensor. If the tensor contains complex values, the magnitude will be computed.- Specified by:
abs
in interfaceTensorOperationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U, W, Z, T extends TensorBase<T, ?, ?, ?, ?, ?, ?>, Double> - Returns:
- The result of applying an element-wise absolute value/magnitude to this tensor.
-
reshape
Description copied from interface:TensorManipulationsMixin
Copies and reshapes tensor if possible. The total number of entries in this tensor must match the total number of entries in the reshaped tensor.- Specified by:
reshape
in interfaceTensorManipulationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>> - Parameters:
dims
- Shape of the new tensor.- Returns:
- A tensor which is equivalent to this tensor but with the specified shape.
-
argMax
public int[] argMax()Description copied from interface:TensorPropertiesMixin
Finds the indices of the maximum value in this tensor.- Specified by:
argMax
in interfaceTensorPropertiesMixin
- 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.
-
argMin
public int[] argMin()Description copied from interface:TensorPropertiesMixin
Finds the indices of the minimum value in this tensor.- Specified by:
argMin
in interfaceTensorPropertiesMixin
- 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.
-
recip
Description copied from interface:TensorOperationsMixin
Computes the reciprocals, element-wise, of a tensor.- Specified by:
recip
in interfaceTensorOperationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>, U, W, Z, T extends TensorBase<T, ?, ?, ?, ?, ?, ?>, Double> - Returns:
- A tensor containing the reciprocal elements of this tensor.
-
round
Description copied from interface:TensorManipulationsMixin
Rounds each entry of this tensor to the nearest whole number.- Specified by:
round
in interfaceTensorManipulationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>> - Returns:
- A copy of this tensor with each entry rounded to the nearest whole number.
- See Also:
-
round
Description copied from interface:TensorManipulationsMixin
Rounds each entry in this tensor to the nearest whole number.- Specified by:
round
in interfaceTensorManipulationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>> - Parameters:
precision
- The number of decimal places to round to. This value must be non-negative.- Returns:
- A copy of this matrix with rounded values.
- See Also:
-
roundToZero
Description copied from interface:TensorManipulationsMixin
Rounds values in this tensor which are close to zero in absolute value to zero. If the matrix is complex, both the real and imaginary components will be rounded independently. By default, the values must be within 1.0*E^-12 of zero. To specify a threshold value seeTensorManipulationsMixin.roundToZero(double)
.- Specified by:
roundToZero
in interfaceTensorManipulationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>> - Returns:
- A copy of this matrix with rounded values.
- See Also:
-
roundToZero
Description copied from interface:TensorManipulationsMixin
Rounds values which are close to zero in absolute value to zero. If the matrix is complex, both the real and imaginary components will be rounded independently.- Specified by:
roundToZero
in interfaceTensorManipulationsMixin<T extends TensorBase<T,
?, ?, ?, ?, ?, ?>> - Parameters:
threshold
- 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.- Returns:
- A copy of this matrix with rounded values.
- See Also:
-