Class IndexedTensor

java.lang.Object
com.yahoo.tensor.IndexedTensor
All Implemented Interfaces:
Tensor

public abstract class IndexedTensor extends Object implements Tensor
An indexed (dense) tensor.

Some methods on indexed tensors make use of a standard value order: Cells are ordered by increasing index where dimensions to the right are incremented before indexes to the left, where the order of dimensions are alphabetical by name. In consequence, tensor value ordering is independent of the order in which dimensions are specified, and the values of the right-most dimension are adjacent.

Author:
bratseth
  • Method Details

    • cellIterator

      public Iterator<Tensor.Cell> cellIterator()
      Returns an iterator over the cells of this in the standard value order.
      Specified by:
      cellIterator in interface Tensor
    • cellIterator

      public IndexedTensor.SubspaceIterator cellIterator(PartialAddress partialAddress, DimensionSizes iterationSizes)
      Returns an iterator over all the cells in this tensor which matches the given partial address
    • valueIterator

      public Iterator<Double> valueIterator()
      Returns an iterator over the values of this returned in the standard value order
      Specified by:
      valueIterator in interface Tensor
    • subspaceIterator

      public Iterator<IndexedTensor.SubspaceIterator> subspaceIterator(Set<String> dimensions, DimensionSizes sizes)
      Returns an iterator over value iterators where the outer iterator is over each unique value of the dimensions given and the inner iterator is over each unique value of the rest of the dimensions, in the standard value order
      Parameters:
      dimensions - the names of the dimensions of the superspace
      sizes - the size of each dimension in the space we are returning values for, containing one value per dimension of this tensor (in order). Each size may be the same or smaller than the corresponding size of this tensor
    • subspaceIterator

      public Iterator<IndexedTensor.SubspaceIterator> subspaceIterator(Set<String> dimensions)
      Returns a subspace iterator having the sizes of the dimensions of this tensor
    • get

      public double get(long... indexes)
      Returns the value at the given indexes as a double
      Parameters:
      indexes - the indexes into the dimensions of this. Must be one number per dimension of this
      Throws:
      IllegalArgumentException - if any of the indexes are out of bound or a wrong number of indexes are given
    • getFloat

      public float getFloat(long... indexes)
      Returns the value at the given indexes as a float
      Parameters:
      indexes - the indexes into the dimensions of this. Must be one number per dimension of this
      Throws:
      IllegalArgumentException - if any of the indexes are out of bound or a wrong number of indexes are given
    • get

      public double get(TensorAddress address)
      Returns the value at this address, or 0.0 if there is no value at this address
      Specified by:
      get in interface Tensor
    • has

      public boolean has(TensorAddress address)
      Description copied from interface: Tensor
      Returns true if this cell exists
      Specified by:
      has in interface Tensor
    • get

      public abstract double get(long valueIndex)
      Returns the value at the given standard value order index as a double.
      Parameters:
      valueIndex - the direct index into the underlying data.
      Throws:
      IllegalArgumentException - if index is out of bounds
    • getFloat

      public abstract float getFloat(long valueIndex)
      Returns the value at the given standard value order index as a float.
      Parameters:
      valueIndex - the direct index into the underlying data.
      Throws:
      IllegalArgumentException - if index is out of bounds
    • type

      public TensorType type()
      Specified by:
      type in interface Tensor
    • withType

      public abstract IndexedTensor withType(TensorType type)
      Description copied from interface: Tensor
      Returns this tensor with the given type if types are compatible
      Specified by:
      withType in interface Tensor
    • dimensionSizes

      public DimensionSizes dimensionSizes()
    • shape

      public long[] shape()
    • cells

      public Map<TensorAddress,Double> cells()
      Description copied from interface: Tensor
      Returns an immutable map of the cells of this in no particular order. This may be expensive for some implementations - avoid when possible
      Specified by:
      cells in interface Tensor
    • remove

      public Tensor remove(Set<TensorAddress> addresses)
      Description copied from interface: Tensor
      Returns a new tensor where existing cells in this tensor have been removed according to the given set of addresses. Only valid for sparse or mixed tensors. For mixed tensors, addresses are assumed to only contain the sparse dimensions, as the entire dense subspace is removed.
      Specified by:
      remove in interface Tensor
      Parameters:
      addresses - list of addresses to remove
      Returns:
      a new tensor where cells have been removed
    • toString

      public String toString()
      Description copied from interface: Tensor
      Returns this tensor on the tensor literal form with type included.
      Specified by:
      toString in interface Tensor
      Overrides:
      toString in class Object
    • toString

      public String toString(boolean withType, boolean shortForms)
      Description copied from interface: Tensor
      Returns this tensor on the tensor literal form.
      Specified by:
      toString in interface Tensor
      Parameters:
      withType - whether to prefix the value by the type of this
      shortForms - whether to use short forms where applicable, or always using the verbose form
    • toAbbreviatedString

      public String toAbbreviatedString(boolean withType, boolean shortForms)
      Description copied from interface: Tensor
      Returns an abbreviated string representation of this tensor suitable for human-readable messages
      Specified by:
      toAbbreviatedString in interface Tensor
      Parameters:
      withType - whether to prefix the value by the type of this
      shortForms - whether to use short forms where applicable, or always using the verbose form
    • equals

      public boolean equals(Object other)
      Description copied from interface: Tensor
      Returns whether this tensor and the given tensor is mathematically equal: That they have the same dimension *names* and the same content.
      Specified by:
      equals in interface Tensor
      Overrides:
      equals in class Object