Class RealDenseOps

java.lang.Object
org.flag4j.linalg.ops.dense.real.RealDenseOps

public final class RealDenseOps extends Object
This class provides low level methods for computing ops on real dense tensors.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[]
    add(double[] src1, double[] src2, double[] dest)
    Computes the element-wise addition of two tensors.
    static double[]
    add(double[] src, double b, double[] dest)
    Adds a scalar to every element of a tensor.
    static void
    addEq(double[] src, double b)
    Adds a scalar from each entry of this tensor and stores the result in the tensor.
    static void
    addEq(double[] src1, Shape shape1, double[] src2, Shape shape2)
    Computes element-wise addition between tensors and stores the result in the first tensor.
    static double
    prod(double[] src)
    Multiplies all data in a tensor.
    static int
    prod(int[] src)
    Multiplies all data in a tensor.
    static double[]
    recip(double[] src)
    Computes the reciprocals, element-wise, of a tensor.
    static double[]
    sub(double[] src1, double[] src2, double[] dest)
    Computes the element-wise subtraction of two tensors.
    static double[]
    sub(double[] src, double b, double[] dest)
    Subtracts a scalar from every element of a tensor.
    static void
    subEq(double[] src, double b)
    Subtracts a scalar from each entry of this tensor and stores the result in the tensor.
    static void
    subEq(double[] src1, Shape shape1, double[] src2, Shape shape2)
    Computes element-wise subtraction between tensors and stores the result in the first tensor.
    static void
    swapColsUnsafe(Shape shape, double[] data, int colIdx1, int colIdx2, int start, int stop)
    Swaps two columns, over a specified range of rows, within a matrix.
    static void
    swapRowsUnsafe(Shape shape, double[] data, int rowIdx1, int rowIdx2, int start, int stop)
    Swaps two rows, over a specified range of columns, within a matrix.
    static void
    tensorTr(Shape shape, double[] src, int axis1, int axis2, Shape destShape, double[] dest)
    Computes the generalized trace of this tensor along the specified axes.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • add

      public static double[] add(double[] src1, double[] src2, double[] dest)
      Computes the element-wise addition of two tensors.
      Parameters:
      src1 - Entries of first Tensor of the addition.
      src2 - Entries of second Tensor of the addition.
      dest - Array to store the result in. May be null or the same array as src1 or src2.
      Returns:
      If dest != null then a reference to dest is returned. Otherwise, a new array will be created and returned.
      Throws:
      IllegalArgumentException - If src1.length != src2.length.
    • sub

      public static double[] sub(double[] src1, double[] src2, double[] dest)
      Computes the element-wise subtraction of two tensors.
      Parameters:
      src1 - Entries of first tensor.
      src2 - Entries of second tensor.
      dest - Array to store the result in. May be null or the same array as src1 or src2.
      Returns:
      If dest != null then a reference to dest is returned. Otherwise, a new array will be created and returned.
      Throws:
      IllegalArgumentException - If src1.length != src2.length.
    • sub

      public static double[] sub(double[] src, double b, double[] dest)
      Subtracts a scalar from every element of a tensor.
      Parameters:
      src - Entries of tensor to add scalar to.
      b - Scalar to subtract from tensor.
      Array - to store the result in. May be null or the same array as src.
      Returns:
      If dest != null then a reference to dest is returned. Otherwise, a new array will be created and returned.
    • subEq

      public static void subEq(double[] src1, Shape shape1, double[] src2, Shape shape2)
      Computes element-wise subtraction between tensors and stores the result in the first tensor.
      Parameters:
      src1 - First tensor in subtraction. Also, where the result will be stored.
      shape1 - Shape of the first tensor.
      src2 - Second tensor in the subtraction.
      shape2 - Shape of the second tensor.
      Throws:
      IllegalArgumentException - If tensors are not the same shape.
    • subEq

      public static void subEq(double[] src, double b)
      Subtracts a scalar from each entry of this tensor and stores the result in the tensor.
      Parameters:
      src - Tensor in subtraction. Also, where the result will be stored.
      b - Scalar to subtract.
    • addEq

      public static void addEq(double[] src1, Shape shape1, double[] src2, Shape shape2)
      Computes element-wise addition between tensors and stores the result in the first tensor.
      Parameters:
      src1 - First tensor in addition. Also, where the result will be stored.
      shape1 - Shape of the first tensor.
      src2 - Second tensor in the addition.
      shape2 - Shape of the second tensor.
      Throws:
      IllegalArgumentException - If tensors are not the same shape.
    • addEq

      public static void addEq(double[] src, double b)
      Adds a scalar from each entry of this tensor and stores the result in the tensor.
      Parameters:
      src - Tensor in addition. Also, where the result will be stored.
      b - Scalar to add.
    • prod

      public static double prod(double[] src)
      Multiplies all data in a tensor.
      Parameters:
      src - The data of the tensor.
      Returns:
      The product of all data in the tensor.
    • prod

      public static int prod(int[] src)
      Multiplies all data in a tensor.
      Parameters:
      src - The data of the tensor.
      Returns:
      The product of all data in the tensor.
    • recip

      public static double[] recip(double[] src)
      Computes the reciprocals, element-wise, of a tensor.
      Parameters:
      src - Elements of the tensor.
      Returns:
      The element-wise reciprocals of the tensor.
    • add

      public static double[] add(double[] src, double b, double[] dest)
      Adds a scalar to every element of a tensor.
      Parameters:
      src - src of tensor to add scalar to.
      b - Scalar to add to tensor.
      dest - Array to store the result in. May be null or the same array as src.
      Returns:
      If dest != null then a reference to dest is returned. If dest == null then a new array will be created and returned.
    • tensorTr

      public static void tensorTr(Shape shape, double[] src, int axis1, int axis2, Shape destShape, double[] dest)

      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:
      shape - Shape of the tensor to compute the trace of.
      src - Entries of the tensor to compute the trace of.
      axis1 - First axis for 2D sub-array.
      axis2 - Second axis for 2D sub-array.
      destShape - The resulting shape of the tensor trace.
      dest - Array to store the result of the generalized tensor trace of. Must satisfy dest.length == destShape.totalEntriesIntValueExact().
      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.)
      IllegalArgumentException - If dest.length == destShape.totalEntriesIntValueExact().
    • swapRowsUnsafe

      public static void swapRowsUnsafe(Shape shape, double[] data, int rowIdx1, int rowIdx2, int start, int stop)

      Swaps two rows, over a specified range of columns, within a matrix. Specifically, all elements in the matrix within rows rowIdx1 and rowIdx2 and between columns start (inclusive) and stop (exclusive). This operation is done in place.

      No bounds checking is done within this method to ensure that the indices provided are valid.

      Parameters:
      shape - Shape of the matrix.
      data - Data of the matrix (modified).
      rowIdx1 - Index of the first row to swap.
      rowIdx2 - Index of the second row to swap.
      start - Index of the column specifying the start of the range for the row swap (inclusive).
      stop - Index of the column specifying the end of the range for the row swap (exclusive).
    • swapColsUnsafe

      public static void swapColsUnsafe(Shape shape, double[] data, int colIdx1, int colIdx2, int start, int stop)

      Swaps two columns, over a specified range of rows, within a matrix. Specifically, all elements in the matrix within columns colIdx1 and colIdx2 and between rows start (inclusive) and stop (exclusive). This operation is done in place.

      No bounds checking is done within this method to ensure that the indices provided are valid.

      Parameters:
      shape - Shape of the matrix.
      data - Data of the matrix (modified).
      colIdx1 - Index of the first column to swap.
      colIdx2 - Index of the second column to swap.
      start - Index of the row specifying the start of the range for the row swap (inclusive).
      stop - Index of the row specifying the end of the range for the row swap (exclusive).