Class SparseNdArray<T, U extends NdArray<T>>

java.lang.Object
org.tensorflow.ndarray.impl.sparse.AbstractSparseNdArray<T,U>
org.tensorflow.ndarray.impl.sparse.SparseNdArray<T,U>
All Implemented Interfaces:
NdArray<T>, Shaped, SparseNdArray<T,U>

public class SparseNdArray<T, U extends NdArray<T>> extends AbstractSparseNdArray<T,U> implements SparseNdArray<T,U>
sparse array for the any data type

A sparse array has two separate dense arrays: indices, values, and a shape that represents the dense shape.

NOTE: all Sparse Arrays are readonly for the AbstractSparseNdArray.set(NdArray, long...) and AbstractSparseNdArray.setObject(Object, long...) methods

SparseNdArray<String> st = new SparseNdArray<>(
     StdArrays.of(new long[][] {{0, 0}, {1, 2}}),
     NdArrays.vectorOf("first", "second"),
     Shape.of(3, 4));

represents the dense array:

[[true, false, false, false]
 [false, false, true, false]
 [false, false, false, false]]

  • Field Details

  • Constructor Details

    • SparseNdArray

      protected SparseNdArray(Class<T> type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions)
      Creates a SparseNdArray
      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], [2,4]] specifies that the elements with indexes of [1,3] and [2,4] have non-default values.
      values - A 1-D NdArray of Boolean type and shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3], [2,4]], the parameter values=[18, 3.6] specifies that element [1,3] of the sparse NdArray has a value of 18, and element [2,4] of the NdArray has a value of 3.6.
      defaultValue - Scalar value to set for indices not specified in AbstractSparseNdArray.getIndices()
      dimensions - the dimensional space for the dense object represented by this sparse array,
  • Method Details

    • create

      public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, LongNdArray indices, U values, DimensionalSpace dimensions)
      Creates a new SparseNdArray
      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], [2,4]] specifies that the elements with indexes of [1,3] and [2,4] have non-default values.
      values - A 1-D NdArray of any type and shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3], [2,4]], the parameter values=[18, 3.6] specifies that element [1,3] of the sparse NdArray has a value of 18, and element [2,4] of the NdArray has a value of 3.6.
      dimensions - the dimensional space for the dense object represented by this sparse array.
      Returns:
      the new Sparse Array
    • create

      public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions)
      Creates a new SparseNdArray
      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], [2,4]] specifies that the elements with indexes of [1,3] and [2,4] have non-default values.
      values - A 1-D NdArray of any type and shape [N], which supplies the values for each element in indices. For example, given indices=[[1,3], [2,4]], the parameter values=[18, 3.6] specifies that element [1,3] of the sparse NdArray has a value of 18, and element [2,4] of the NdArray has a value of 3.6.
      defaultValue - Scalar value to set for indices not specified in AbstractSparseNdArray.getIndices()
      dimensions - the dimensional space for the dense object represented by this sparse array.
      Returns:
      the new Sparse Array
    • create

      public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> dataBuffer, DimensionalSpace dimensions)
      Creates a new SparseNdArray from a data buffer
      Parameters:
      dataBuffer - the databuffer containing the dense array
      dimensions - the dimensional space for the sparse array
      Returns:
      the new Sparse Array
    • create

      public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> dataBuffer, T defaultValue, DimensionalSpace dimensions)
      Creates a new SparseNdArray from a data buffer
      Parameters:
      dataBuffer - the databuffer containing the dense array
      defaultValue - Scalar value to set for indices not specified in AbstractSparseNdArray.getIndices()
      dimensions - the dimensional space for the sparse array
      Returns:
      the new Sparse Array
    • create

      public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DimensionalSpace dimensions)
      Creates a new empty SparseNdArray from a data buffer
      Parameters:
      dimensions - the dimensions array
      Returns:
      the new Sparse Array
    • create

      public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, T defaultValue, DimensionalSpace dimensions)
      Creates a new empty SparseNdArray from a data buffer
      Parameters:
      defaultValue - Scalar value to set for indices not specified in AbstractSparseNdArray.getIndices()
      dimensions - the dimensions array
      Returns:
      the new Sparse Array
    • create

      public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> buffer, Shape shape)
      Creates a new empty SparseNdArray from a float data buffer
      Parameters:
      buffer - the data buffer
      shape - the shape of the sparse array.
      Returns:
      the new Sparse Array
    • create

      public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> buffer, T defaultValue, Shape shape)
      Creates a new empty SparseNdArray from a float data buffer
      Parameters:
      buffer - the data buffer
      defaultValue - Scalar value to set for indices not specified in AbstractSparseNdArray.getIndices()
      shape - the shape of the sparse array.
      Returns:
      the new Sparse Array
    • create

      public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, U src)
      Creates a new SparseNdArray from a NdArray
      Parameters:
      src - the NdArray
      Returns:
      the new Sparse Array
    • create

      public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, U src, T defaultValue)
      Creates a new SparseNdArray from a NdArray
      Parameters:
      src - the NdArray
      defaultValue - Scalar value to set for indices not specified in AbstractSparseNdArray.getIndices()
      Returns:
      the new Sparse Array
    • createDefaultArray

      public U createDefaultArray()
      Creates the NdArray with the default value as a scalar
      Specified by:
      createDefaultArray in class AbstractSparseNdArray<T, U extends NdArray<T>>
      Returns:
      the default NdArray of the default value as a scalar
    • createValues

      public U createValues(Shape shape)
      Creates a NdArray of the specified shape
      Specified by:
      createValues in class AbstractSparseNdArray<T, U extends NdArray<T>>
      Parameters:
      shape - the shape of the dense array.
      Returns:
      a NdArray of the specified shape
    • slice

      public U slice(long position, DimensionalSpace sliceDimensions)
    • copyTo

      public NdArray<T> copyTo(DataBuffer<T> dst)
      Copy the content of this N-dimensional array into the destination buffer.

      The size of the buffer must be equal or greater to the Shaped.size() of this array, or an exception is thrown. After the copy, content of the buffer and of the array can be altered independently, without affecting each other.

      Note: in version 0.4.0 and earlier, this method was named read(DataBuffer<T>). It has been renamed to explicitly indicate the direction of the data flow to avoid confusion.

      Specified by:
      copyTo in interface NdArray<T>
      Parameters:
      dst - the destination buffer
      Returns:
      this array
      See Also:
    • copyFrom

      public NdArray<T> copyFrom(DataBuffer<T> src)
      Copy the content of the source buffer into this N-dimensional array.

      The size of the buffer must be equal or greater to the Shaped.size() of this array, or an exception is thrown. After the copy, content of the buffer and of the array can be altered independently, without affecting each other.

      Note: in version 0.4.0 and earlier, this method was named write(DataBuffer<T>). It has been renamed to explicitly indicate the direction of the data flow to avoid confusion.

      Specified by:
      copyFrom in interface NdArray<T>
      Parameters:
      src - the source buffer
      Returns:
      this array
      See Also:
    • toDense

      public U toDense()
      Converts the sparse array to a dense array
      Specified by:
      toDense in class AbstractSparseNdArray<T, U extends NdArray<T>>
      Returns:
      the dense array
    • fromDense

      public NdArray<T> fromDense(NdArray<T> src)
      Populates this sparse array from a dense array
      Parameters:
      src - the dense array
      Returns:
      this sparse array
    • getType

      public Class<T> getType()
      Gets the class type for this sparse array
      Returns:
      the class type for this sparse array.
    • toString

      public String toString()
      A String showing the type, default value, number of elements and the dense shape of this sparse ndarray.
      Overrides:
      toString in class AbstractSparseNdArray<T, U extends NdArray<T>>
      Returns:
      A string containing the type, default value, number of elements and shape.
    • dimensions

      public DimensionalSpace dimensions()
    • shape

      public Shape shape()
      Specified by:
      shape in interface Shaped
      Returns:
      the shape of this container
    • scalars

      public NdArraySequence<U> scalars()
      Description copied from interface: NdArray
      Returns a sequence of all scalars in this array.

      This is equivalent to call elements(shape().numDimensions() - 1)

      Specified by:
      scalars in interface NdArray<T>
      Returns:
      an NdArray sequence
    • slowHashCode

      protected int slowHashCode()
    • slowEquals

      protected boolean slowEquals(NdArray<?> array)