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>
Direct Known Subclasses:
Matrix, Tensor, Vector

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

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractDenseDoubleTensor(Shape shape, double[] entries)
    Creates a tensor with the specified data and shape.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(T b)
    Computes the element-wise sum between two tensors of the same shape.
    void
    addEq(T b)
    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
    allClose(T b, double relTol, double absTol)
    Checks if all data of this matrix are 'close' as defined below.
    int[]
    Finds the indices of the maximum value in this tensor.
    int[]
    Finds the indices of the maximum absolute value in this tensor.
    int[]
    Finds the indices of the minimum value in this tensor.
    int[]
    Finds the indices of the minimum absolute value in this tensor.
    div(T b)
    Computes the element-wise quotient between two tensors.
    void
    divEq(T b)
    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
    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.
    reshape(Shape newShape)
    Copies and reshapes this tensor.
    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 within tol of zero.
    set(Double value, int... indices)
    Sets the element of this tensor at the specified indices.
    sub(T b)
    Computes the element-wise difference between two tensors of the same shape.
    void
    subEq(T b)
    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 exchanging axis1 and axis2.
    tensorDot(T src2, int[] aAxes, int[] bAxes)
    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

      protected AbstractDenseDoubleTensor(Shape shape, double[] entries)
      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

      public Double get(int... indices)
      Gets the element of this tensor at the specified indices.
      Specified by:
      get in class AbstractTensor<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

      public T set(Double value, int... indices)
      Sets the element of this tensor at the specified indices.
      Specified by:
      set in class AbstractTensor<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 - If indices is not within the bounds of this tensor.
    • T

      public T T(int axis1, int axis2)
      Computes the transpose of a tensor by exchanging axis1 and axis2.
      Specified by:
      T in class AbstractTensor<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 either axis1 or axis2 are out of bounds for the rank of this tensor.
      See Also:
    • T

      public T T(int... axes)
      Computes the transpose of this tensor. That is, permutes the axes of this tensor so that it matches the permutation specified by axes.
      Specified by:
      T in class AbstractTensor<T extends AbstractDoubleTensor<T>,double[],Double>
      Parameters:
      axes - Permutation of tensor axis. If the tensor has rank N, then this must be an array of length N 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 of axes is out of bounds for the rank of this tensor.
      IllegalArgumentException - If axes is not a permutation of {1, 2, 3, ... N-1}.
      See Also:
    • sub

      public T sub(T b)
      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 and b do not have the same shape.
    • subEq

      public void subEq(T b)
      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 and b do not have the same shape.
    • add

      public T add(T b)
      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 and b do not have the same shape.
    • addEq

      public void addEq(T b)
      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

      public T elemMult(T b)
      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 and b do not have the same shape.
    • elemMultEq

      public void elemMultEq(T b)
      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 and b do not have the same shape.
    • tensorDot

      public T tensorDot(T src2, int[] aAxes, int[] bAxes)
      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 for src2 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 in aAxes and bAxes.
      IllegalArgumentException - If aAxes and bAxes do not match in length, or if any of the axes are out of bounds for the corresponding tensor.
    • tensorTr

      public Tensor tensorTr(int axis1, int axis2)

      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 and axis2. The shape of the resulting tensor is equal to this tensor with the axis1 and axis2 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 and axis2.
      Throws:
      IndexOutOfBoundsException - If the two axes are not both larger than zero and less than this tensors rank.
      IllegalArgumentException - If axis1 == axis2 or this.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

      public T div(T b)
      Computes the element-wise quotient between two tensors.
      Parameters:
      b - Second tensor in the element-wise quotient.
      Returns:
      The element-wise quotient of this tensor with b.
    • divEq

      public void divEq(T b)
      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

      public T reshape(Shape newShape)
      Copies and reshapes this tensor.
      Specified by:
      reshape in class AbstractTensor<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 - If newShape is not broadcastable to this.shape.
    • round

      public T round()
      Rounds all data in this matrix to the nearest integer.
      Overrides:
      round in class AbstractDoubleTensor<T extends AbstractDoubleTensor<T>>
      Returns:
      A new matrix containing the data of this matrix rounded to the nearest integer.
      See Also:
    • round

      public T round(int precision)
      Rounds all data within this matrix to the specified precision.
      Overrides:
      round in class AbstractDoubleTensor<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

      public T roundToZero(double tolerance)
      Sets all elements of this matrix to zero if they are within tol of zero. This is not done in place.
      Overrides:
      roundToZero in class AbstractDoubleTensor<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

      public boolean allClose(T b)
      Checks if all data of this matrix are 'close' as defined below. Custom tolerances may be specified using allClose(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 and y at the same positions in the two tensors respectively and satisfy |x-y| <= (1E-08 + 1E-05*|y|). Otherwise, returns false.
      See Also:
    • allClose

      public boolean allClose(T b, double relTol, double absTol)
      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 and y at the same positions in the two tensors respectively and satisfy |x-y| <= (absTol + relTol*|y|). Otherwise, returns false.
      See Also: