Class NdArrays

java.lang.Object
org.tensorflow.ndarray.NdArrays

public final class NdArrays extends Object
Utility class for instantiating NdArray objects.
  • Constructor Details

    • NdArrays

      public NdArrays()
  • Method Details

    • scalarOf

      public static ByteNdArray scalarOf(byte value)
      Creates byte scalar (rank 0) initialized with the given value.
      Parameters:
      value - scalar value
      Returns:
      new byte scalar
    • vectorOf

      public static ByteNdArray vectorOf(byte... values)
      Creates a byte vector (rank 1) initialized with the given values.

      Modifying the data of the returned vector will also impact the values in the array passed in parameter.

      Parameters:
      values - vector values
      Returns:
      new byte vector
      Throws:
      IllegalArgumentException - if values is null
    • ofBytes

      public static ByteNdArray ofBytes(Shape shape)
      Creates an N-dimensional array of bytes of the given shape.

      All values are initialized to zeros.

      Parameters:
      shape - shape of the array
      Returns:
      new byte N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null or has unknown dimensions
    • wrap

      public static ByteNdArray wrap(Shape shape, ByteDataBuffer buffer)
      Wraps a buffer in a byte N-dimensional array of a given shape.
      Parameters:
      shape - shape of the array
      buffer - buffer to wrap
      Returns:
      new byte N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null, has unknown dimensions or has size bigger in the buffer size
    • sparseOf

      public static ByteSparseNdArray sparseOf(LongNdArray indices, ByteNdArray values, Shape shape)
      Creates a Sparse array of byte values with a default value of zero
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D ByteNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18, 3] specifies that element [1,3,1] of the sparse NdArray has a value of 18, and element [2,4,0] of the NdArray has a value of 3.
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the byte sparse array.
    • sparseOf

      public static ByteSparseNdArray sparseOf(LongNdArray indices, ByteNdArray values, byte defaultValue, Shape shape)
      Creates a Sparse array of byte values
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non default values.
      values - A 1-D ByteNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18, 3] specifies that element [1,3,1] of the sparse NdArray has a value of 18, and element [2,4,0] of the NdArray has a value of 3.
      defaultValue - Scalar value to set for indices not specified in 'indices'
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the byte sparse array.
    • scalarOf

      public static LongNdArray scalarOf(long value)
      Creates long scalar (rank 0) initialized with the given value.
      Parameters:
      value - scalar value
      Returns:
      new long scalar
    • vectorOf

      public static LongNdArray vectorOf(long... values)
      Creates a long vector (rank 1) initialized with the given values.

      Modifying the data of the returned vector will also impact the values in the array passed in parameter.

      Parameters:
      values - vector values
      Returns:
      new long vector
      Throws:
      IllegalArgumentException - if values is null
    • ofLongs

      public static LongNdArray ofLongs(Shape shape)
      Creates an N-dimensional array of longs of the given shape.

      All values are initialized to zeros.

      Parameters:
      shape - shape of the array
      Returns:
      new long N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null or has unknown dimensions
    • wrap

      public static LongNdArray wrap(Shape shape, LongDataBuffer buffer)
      Wraps a buffer in a long N-dimensional array of a given shape.
      Parameters:
      shape - shape of the array
      buffer - buffer to wrap
      Returns:
      new long N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null, has unknown dimensions or has size bigger in the buffer size
    • sparseOf

      public static LongSparseNdArray sparseOf(LongNdArray indices, LongNdArray values, Shape shape)
      Creates a Sparse array of long values with a default value of zero
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D LongNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18L, 3L] specifies that element [1,3,1] of the sparse NdArray has a value of 18L, and element [2,4,0] of the NdArray has a value of 3L.
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the long sparse array.
    • sparseOf

      public static LongSparseNdArray sparseOf(LongNdArray indices, LongNdArray values, long defaultValue, Shape shape)
      Creates a Sparse array of long values with a default value of zero
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D LongNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18L, 3L] specifies that element [1,3,1] of the sparse NdArray has a value of 18L, and element [2,4,0] of the NdArray has a value of 3L.
      defaultValue - Scalar value to set for indices not specified in 'indices'
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the long sparse array.
    • scalarOf

      public static IntNdArray scalarOf(int value)
      Creates long scalar (rank 0) initialized with the given value.
      Parameters:
      value - scalar value
      Returns:
      new long scalar
    • vectorOf

      public static IntNdArray vectorOf(int... values)
      Creates a int vector (rank 1) initialized with the given values.

      Modifying the data of the returned vector will also impact the values in the array passed in parameter.

      Parameters:
      values - vector values
      Returns:
      new int vector
      Throws:
      IllegalArgumentException - if values is null
    • ofInts

      public static IntNdArray ofInts(Shape shape)
      Creates an N-dimensional array of ints of the given shape.

      All values are initialized to zeros.

      Parameters:
      shape - shape of the array
      Returns:
      new int N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null or has unknown dimensions
    • wrap

      public static IntNdArray wrap(Shape shape, IntDataBuffer buffer)
      Wraps a buffer in an int N-dimensional array of a given shape.
      Parameters:
      shape - shape of the array
      buffer - buffer to wrap
      Returns:
      new int N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null, has unknown dimensions or has size bigger in the buffer size
    • sparseOf

      public static IntSparseNdArray sparseOf(LongNdArray indices, IntNdArray values, Shape shape)
      Creates a Sparse array of int values with a default value of zero.
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D IntNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18, 3] specifies that element [1,3,1] of the sparse NdArray has a value of 18, and element [2,4,0] of the NdArray has a value of 3.
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the int sparse array.
    • sparseOf

      public static IntSparseNdArray sparseOf(LongNdArray indices, IntNdArray values, int defaultValue, Shape shape)
      Creates a Sparse array of int values
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D IntNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18, 3] specifies that element [1,3,1] of the sparse NdArray has a value of 18, and element [2,4,0] of the NdArray has a value of 3.
      defaultValue - Scalar value to set for indices not specified in 'indices'
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the int sparse array.
    • scalarOf

      public static ShortNdArray scalarOf(short value)
      Creates short scalar (rank 0) initialized with the given value.
      Parameters:
      value - scalar value
      Returns:
      new short scalar
    • vectorOf

      public static ShortNdArray vectorOf(short... values)
      Creates a short vector (rank 1) initialized with the given values.

      Modifying the data of the returned vector will also impact the values in the array passed in parameter.

      Parameters:
      values - vector values
      Returns:
      new short vector
      Throws:
      IllegalArgumentException - if values is null
    • ofShorts

      public static ShortNdArray ofShorts(Shape shape)
      Creates an N-dimensional array of shorts of the given shape.

      All values are initialized to zeros.

      Parameters:
      shape - shape of the array
      Returns:
      new short N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null or has unknown dimensions
    • wrap

      public static ShortNdArray wrap(Shape shape, ShortDataBuffer buffer)
      Wraps a buffer in a short N-dimensional array of a given shape.
      Parameters:
      shape - shape of the array
      buffer - buffer to wrap
      Returns:
      new short N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null, has unknown dimensions or has size bigger in the buffer size
    • sparseOf

      public static ShortSparseNdArray sparseOf(LongNdArray indices, ShortNdArray values, Shape shape)
      Creates a Sparse array of short values with a default value of zero
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D ShortNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18, 3] specifies that element [1,3,1] of the sparse NdArray has a value of 18, and element [2,4,0] of the NdArray has a value of 3.
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the short sparse array.
    • sparseOf

      public static ShortSparseNdArray sparseOf(LongNdArray indices, ShortNdArray values, short defaultValue, Shape shape)
      Creates a Sparse array of short values
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D ShortNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18, 3] specifies that element [1,3,1] of the sparse NdArray has a value of 18, and element [2,4,0] of the NdArray has a value of 3.
      defaultValue - Scalar value to set for indices not specified in 'indices'
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the short sparse array.
    • scalarOf

      public static FloatNdArray scalarOf(float value)
      Creates float scalar (rank 0) initialized with the given value.
      Parameters:
      value - scalar value
      Returns:
      new float scalar
    • vectorOf

      public static FloatNdArray vectorOf(float... values)
      Creates a float vector (rank 1) initialized with the given values.

      Modifying the data of the returned vector will also impact the values in the array passed in parameter.

      Parameters:
      values - vector values
      Returns:
      new float vector
      Throws:
      IllegalArgumentException - if values is null
    • ofFloats

      public static FloatNdArray ofFloats(Shape shape)
      Creates an N-dimensional array of floats of the given shape.

      All values are initialized to zeros.

      Parameters:
      shape - shape of the array
      Returns:
      new float N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null or has unknown dimensions
    • wrap

      public static FloatNdArray wrap(Shape shape, FloatDataBuffer buffer)
      Wraps a buffer in a float N-dimensional array of a given shape.
      Parameters:
      shape - shape of the array
      buffer - buffer to wrap
      Returns:
      new float N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null, has unknown dimensions or has size bigger in the buffer size
    • sparseOf

      public static FloatSparseNdArray sparseOf(LongNdArray indices, FloatNdArray values, Shape shape)
      Creates a Sparse array of float values with a default value of zero
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D FloatNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18f, 3.8f] specifies that element [1,3,1] of the sparse NdArray has a value of 18f, and element [2,4,0] of the NdArray has a value of 3.8f.
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the float sparse array.
    • sparseOf

      public static FloatSparseNdArray sparseOf(LongNdArray indices, FloatNdArray values, float defaultValue, Shape shape)
      Creates a Sparse array of float values
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D FloatNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18f, 3.8f] specifies that element [1,3,1] of the sparse NdArray has a value of 18f, and element [2,4,0] of the NdArray has a value of 3.8f.
      defaultValue - Scalar value to set for indices not specified in 'indices'
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the float sparse array.
    • scalarOf

      public static DoubleNdArray scalarOf(double value)
      Creates double scalar (rank 0) initialized with the given value.
      Parameters:
      value - scalar value
      Returns:
      new double scalar
    • vectorOf

      public static DoubleNdArray vectorOf(double... values)
      Creates a double vector (rank 1) initialized with the given values.

      Modifying the data of the returned vector will also impact the values in the array passed in parameter.

      Parameters:
      values - vector values
      Returns:
      new double vector
      Throws:
      IllegalArgumentException - if values is null
    • ofDoubles

      public static DoubleNdArray ofDoubles(Shape shape)
      Creates an N-dimensional array of doubles of the given shape.

      All values are initialized to zeros.

      Parameters:
      shape - shape of the array
      Returns:
      new double N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null or has unknown dimensions
    • wrap

      public static DoubleNdArray wrap(Shape shape, DoubleDataBuffer buffer)
      Wraps a buffer in a double N-dimensional array of a given shape.
      Parameters:
      shape - shape of the array
      buffer - buffer to wrap
      Returns:
      new double N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null, has unknown dimensions or has size bigger in the buffer size
    • sparseOf

      public static DoubleSparseNdArray sparseOf(LongNdArray indices, DoubleNdArray values, Shape shape)
      Creates a Sparse array of double values with a default value of zero
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D DoubleNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18, 3.8] specifies that element [1,3,1] of the sparse NdArray has a value of 18, and element [2,4,0] of the NdArray has a value of 3.8.
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the float sparse array.
    • sparseOf

      public static DoubleSparseNdArray sparseOf(LongNdArray indices, DoubleNdArray values, double defaultValue, Shape shape)
      Creates a Sparse array of double values
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D DoubleNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[18, 3.8] specifies that element [1,3,1] of the sparse NdArray has a value of 18, and element [2,4,0] of the NdArray has a value of 3.8.
      defaultValue - Scalar value to set for indices not specified in 'indices'
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the float sparse array.
    • scalarOf

      public static BooleanNdArray scalarOf(boolean value)
      Creates boolean scalar (rank 0) initialized with the given value.
      Parameters:
      value - scalar value
      Returns:
      new boolean scalar
    • vectorOf

      public static BooleanNdArray vectorOf(boolean... values)
      Creates a boolean vector (rank 1) initialized with the given values.

      Modifying the data of the returned vector will also impact the values in the array passed in parameter.

      Parameters:
      values - vector values
      Returns:
      new boolean vector
      Throws:
      IllegalArgumentException - if values is null
    • ofBooleans

      public static BooleanNdArray ofBooleans(Shape shape)
      Creates an N-dimensional array of booleans of the given shape.

      All values are initialized to zeros.

      Parameters:
      shape - shape of the array
      Returns:
      new boolean N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null or has unknown dimensions
    • wrap

      public static BooleanNdArray wrap(Shape shape, BooleanDataBuffer buffer)
      Wraps a buffer in a boolean N-dimensional array of a given shape.
      Parameters:
      shape - shape of the array
      buffer - buffer to wrap
      Returns:
      new boolean N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null, has unknown dimensions or has size bigger in the buffer size
    • sparseOf

      public static BooleanSparseNdArray sparseOf(LongNdArray indices, BooleanNdArray values, Shape shape)
      Creates a Sparse array of boolean values with a default value of 'false'
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D BooleanNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[true, true] specifies that element [1,3,1] of the sparse NdArray has a value of true, and element [2,4,0] of the NdArray has a value of true. All other values are false.
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the float sparse array.
    • sparseOf

      public static BooleanSparseNdArray sparseOf(LongNdArray indices, BooleanNdArray values, boolean defaultValue, Shape shape)
      Creates a Sparse array of boolean values
      Parameters:
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D BooleanNdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=[true, true] specifies that element [1,3,1] of the sparse NdArray has a value of true, and element [2,4,0] of the NdArray has a value of true. All other values are false.
      defaultValue - Scalar value to set for indices not specified in 'indices'
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the float sparse array.
    • scalarOfObject

      public static <T> NdArray<T> scalarOfObject(T value)
      Creates scalar (rank 0) initialized with the given value.
      Type Parameters:
      T - the data type
      Parameters:
      value - scalar value
      Returns:
      new scalar
    • vectorOfObjects

      @SafeVarargs public static <T> NdArray<T> vectorOfObjects(T... values)
      Creates a vector (rank 1) initialized with the given values.

      Modifying the data of the returned vector will also impact the values in the array passed in parameter.

      Type Parameters:
      T - the data type
      Parameters:
      values - vector values
      Returns:
      new vector
      Throws:
      IllegalArgumentException - if values is null
    • ofObjects

      public static <T> NdArray<T> ofObjects(Class<T> clazz, Shape shape)
      Creates an N-dimensional array of the given shape.

      All values are initialized to zeros.

      Type Parameters:
      T - the data type
      Parameters:
      clazz - class of the data to be stored in this array
      shape - shape of the array
      Returns:
      new N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null or has unknown dimensions
    • wrap

      public static <T> NdArray<T> wrap(Shape shape, DataBuffer<T> buffer)
      Wraps a buffer in an N-dimensional array of a given shape.
      Type Parameters:
      T - the data type
      Parameters:
      shape - shape of the array
      buffer - buffer to wrap
      Returns:
      new N-dimensional array
      Throws:
      IllegalArgumentException - if shape is null, has unknown dimensions or has size bigger in the buffer size
    • sparseOfObjects

      public static <T> NdArray<T> sparseOfObjects(Class<T> type, LongNdArray indices, NdArray<T> values, Shape shape)
      Creates a Sparse array of values with a null default value
      Parameters:
      type - the class type represented by this sparse array.
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D NdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=["one", "two"] specifies that element [1,3,1] of the sparse NdArray has a value of "one", and element [2,4,0] of the NdArray has a value of "two"". All other values are null.
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the float sparse array.
    • sparseOfObjects

      public static <T> NdArray<T> sparseOfObjects(Class<T> type, LongNdArray indices, NdArray<T> values, T defaultValue, Shape shape)
      Creates a Sparse array of values
      Parameters:
      type - the class type represented by this sparse array.
      indices - A 2-D LongNdArray of shape [N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example, indices=[[1,3,1], [2,4,0]] specifies that the elements with indexes of [1,3,1] and [2,4,0] have non-default values.
      values - A 1-D NdArray of shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3,1], [2,4,0]], the parameter values=["one", "two"] specifies that element [1,3,1] of the sparse NdArray has a value of "one", and element [2,4,0] of the NdArray has a value of "two"". All other values are null.
      defaultValue - Scalar value to set for indices not specified in 'indices'
      shape - the shape of the dense array represented by this sparse array.
      Returns:
      the float sparse array.