Class CTensor

  • Constructor Details

    • CTensor

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

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

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

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

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

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

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

      public CTensor(Shape shape, String fillValue)
      Creates a tensor with the specified shape and filled with fillValue.
      Parameters:
      shape - Shape of this tensor.
      fillValue - Value to fill this tensor with. Must be a string representation of a complex number parsable by ComplexNumberParser.parseNumberToComplex128(String).
    • CTensor

      public CTensor(Shape shape, String[] entries)
      Creates a tensor with the specified data and shape.
      Parameters:
      shape - Shape of this tensor.
      entries - Entries of this tensor. Each value in data must be formated as a complex number such as:
      • "a"
      • "a + bi", "a - bi", "a + i", or "a - i"
      • "bi", "i", or "-i"
      where "a" and "b" are integers or decimal numbers and white space does not matter.
    • CTensor

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

      public CTensor(CTensor tensor)
      Constructs a copy of the specified tensor.
      Parameters:
      tensor - Tensor to create copy of.
  • Method Details

    • makeEmptyDataArray

      public Complex128[] makeEmptyDataArray(int length)
      Description copied from interface: SemiringTensorMixin
      Creates an empty array of the same type as the data array of this tensor.
      Parameters:
      length - The length of the array to construct.
      Returns:
      An empty array of the same type as the data array of this tensor.
    • makeLikeTensor

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

      protected CooCTensor makeLikeCooTensor(Shape shape, Complex128[] entries, int[][] indices)
      Constructs a sparse COO tensor which is of a similar type as this dense tensor.
      Specified by:
      makeLikeCooTensor in class AbstractDenseSemiringTensor<CTensor,Complex128>
      Parameters:
      shape - Shape of the COO tensor.
      entries - Non-zero data of the COO tensor.
      indices -
      Returns:
      A sparse COO tensor which is of a similar type as this dense tensor.
    • 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.
    • add

      public CTensor add(Tensor 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(Tensor b)
      Computes the element-wise sum between two tensors of the same shape and stores hte result in this tensor.
      Parameters:
      b - Second tensor in the element-wise sum.
      Throws:
      TensorShapeException - If this tensor and b do not have the same shape.
    • add

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

      public CTensor sub(CooCTensor 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(Tensor 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(Tensor b)
      Computes the element-wise difference of this tensor with a real dense tensor and stores the result in this tensor.
      Parameters:
      b - Second tensor in element-wise difference.
    • sub

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

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

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

      public CooCTensor elemMult(CooTensor 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.
    • div

      public CTensor div(Tensor 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.
    • toCoo

      public CooCTensor toCoo()
      Converts this tensor to an equivalent sparse COO tensor.
      Overrides:
      toCoo in class AbstractDenseSemiringTensor<CTensor,Complex128>
      Returns:
      A sparse COO tensor that is equivalent to this dense tensor.
      See Also:
    • toCoo

      public CooCTensor toCoo(double estimatedSparsity)
      Converts this tensor to an equivalent sparse COO tensor.
      Overrides:
      toCoo in class AbstractDenseSemiringTensor<CTensor,Complex128>
      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:
    • toReal

      public Tensor toReal()
      Converts this complex tensor to a real tensor. This conversion is done by taking the real component of each entry and ignoring the imaginary component.
      Returns:
      A real tensor containing the real components of the data of this tensor.
    • isReal

      public boolean isReal()
      Checks if all data of this tensor are real.
      Returns:
      true if all data of this tensor are real; false otherwise.
    • isComplex

      public boolean isComplex()
      Checks if any entry within this tensor has non-zero imaginary component.
      Returns:
      true if any entry of this tensor has a non-zero imaginary component.
    • round

      public CTensor round(int precision)
      Rounds all data within this tensor to the specified precision.
      Parameters:
      precision - The precision to round to (i.e. the number of decimal places to round to). Must be non-negative.
      Returns:
      A new tensor containing the data of this tensor rounded to the specified precision.
    • roundToZero

      public CTensor roundToZero(double tolerance)
      Sets all elements of this tensor to zero if they are within tol of zero. This is not done in place.
      Parameters:
      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 tensor with all data within tol of zero set to zero.
    • toMatrix

      public CMatrix toMatrix()
      Converts this tensor to an equivalent matrix. If this tensor is not rank-2, it will be flattened to a row vector before conversion.
      Returns:
      A matrix which is equivalent to this tensor.
    • toVector

      public CVector toVector()
      Converts this tensor to an equivalent vector. If the tensor is not rank-1, it will be flattened first.
      Returns:
      A vector which is equivalent to this tensor.
    • abs

      public TensorOverRing abs()
      Computes the element-wise absolute value of this tensor.
      Returns:
      The element-wise absolute value of this tensor.
    • 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 CTensor. 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.