Class Tensor

All Implemented Interfaces:
Serializable, TensorOverField<Tensor,Tensor,double[],Double>, TensorOverRing<Tensor,Tensor,double[],Double>, TensorOverSemiring<Tensor,Tensor,double[],Double>

public class Tensor extends AbstractDenseDoubleTensor<Tensor>

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:
  • Constructor Details

    • Tensor

      public Tensor(Shape shape)
      Creates a zero tensor with the shape.
      Parameters:
      shape - Shape of this tensor.
    • Tensor

      public Tensor(Object nDArray)
      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 - If nDArray is not an array or not rectangular.
    • Tensor

      public Tensor(Shape shape, double fillValue)
      Creates a tensor with the specified shape filled with fillValue.
      Parameters:
      shape - Shape of this tensor.
      fillValue - Value to fill this tensor with.
    • Tensor

      public Tensor(Shape shape, double... entries)
      Creates a tensor with the specified data and shape.
      Parameters:
      shape - Shape of this tensor.
      entries - Entries of this tensor.
    • Tensor

      public Tensor(Shape shape, int... entries)
      Creates a tensor with the specified data and shape.
      Parameters:
      shape - Shape of this tensor.
      entries - Entries of this tensor.
    • Tensor

      public Tensor(Tensor src)
      Constructs a copy of the specified tensor.
      Parameters:
      src - The tensor to make a copy of.
    • Tensor

      public Tensor(Shape shape, Double[] entries)
      Creates a tensor with the specified data and shape.
      Parameters:
      shape - Shape of this tensor.
      entries - Entries of this tensor.
    • Tensor

      public Tensor(Shape shape, Integer[] entries)
      Creates a tensor with the specified data and shape.
      Parameters:
      shape - Shape of this tensor.
      entries - Entries of this tensor.
  • Method Details

    • flatten

      public Tensor flatten()
      Flattens tensor to single dimension while preserving order of data.
      Specified by:
      flatten in class AbstractTensor<Tensor,double[],Double>
      Returns:
      The flattened tensor.
      See Also:
    • flatten

      public Tensor flatten(int axis)
      Flattens a tensor along the specified axis.
      Specified by:
      flatten in class AbstractTensor<Tensor,double[],Double>
      Parameters:
      axis - Axis along which to flatten tensor.
      Throws:
      ArrayIndexOutOfBoundsException - If the axis is not positive or larger than this.{@link #getRank()}-1.
      See Also:
    • makeLikeTensor

      public Tensor makeLikeTensor(Shape shape, double[] entries)
      Constructs a tensor of the same type as this tensor with the given the shape and data.
      Specified by:
      makeLikeTensor in interface TensorOverSemiring<Tensor,Tensor,double[],Double>
      Specified by:
      makeLikeTensor in class AbstractTensor<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

      public Vector 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

      public Matrix 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

      public Matrix toMatrix(Shape shape)
      Converts this tensor to an equivalent matrix with the specified shape.
      Parameters:
      shape - New shape for the matrix. Must be rank-2 and broadcastable to this.shape.
      Returns:
      A matrix with the specified shape and data equivalent to this tensor.
      Throws:
      IllegalArgumentException - If shape is not broadcastable to this.shape.
      TensorShapeException - If shape.getRank() != 2.
    • toCoo

      public CooTensor 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

      public CTensor add(Complex128 b)
      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

      public Tensor add(CooTensor 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.
    • add

      public CTensor add(CTensor 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.
    • add

      public CTensor add(CooCTensor 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.
    • sub

      public CTensor sub(Complex128 b)
      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

      public CTensor sub(CooCTensor 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.
    • sub

      public Tensor sub(CooTensor 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.
    • sub

      public CTensor sub(CTensor 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.
    • elemMult

      public CooTensor elemMult(CooTensor b)
      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.
    • elemMult

      public CTensor elemMult(CTensor b)
      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

      public CooCTensor elemMult(CooCTensor b)
      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

      public CooTensor toCoo(double estimatedSparsity)
      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

      public CTensor 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

      public CTensor mult(Complex128 b)
      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

      public CTensor div(Complex128 b)
      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

      public CTensor div(CTensor b)
      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

      public boolean equals(Object object)
      Checks if an object is equal to this tensor object.
      Overrides:
      equals in class Object
      Parameters:
      object - Object to check equality with this tensor.
      Returns:
      True if the two tensors have the same shape, are numerically equivalent, and are of type Tensor. False otherwise.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Formats this tensor as a human-readable string. Specifically, a string containing the shape and flattened data of this tensor.
      Overrides:
      toString in class Object
      Returns:
      A human-readable string representing this tensor.