Class ImmutableTensor<V>

  • Type Parameters:
    V - type of values in Tensor.
    All Implemented Interfaces:
    java.io.Serializable, Mappable<V>, Tensor<V>, Tensoric<V>

    public class ImmutableTensor<V>
    extends AbstractTensor<V>
    implements Mappable<V>, java.io.Serializable
    Default Implementation of Tensor.

    By constraint of creation it holds a map of Position of certain type to values of type T, such that ALL Positions contains the same number and type of coordinates. Number and type of coordinates can be accessed and explored via Shape.

    There is a special type of Tensor that has ZERO dimensiality. Can be obtained via factory method.

    ImmutableTensor is immutable.

    The toString() method does not print all the tensor entries.

    See Also:
    Serialized Form
    • Method Detail

      • builder

        public static final <T> ImmutableTensor.Builder<T> builder​(java.lang.Iterable<java.lang.Class<?>> dimensions)
        Returns a builder for an ImmutableTensor. As argument it takes set of class of coordinates which represent the dimensions of the tensor.
        Type Parameters:
        T - type of values in Tensor.
        Parameters:
        dimensions - a set of classes that can later be used as coordinates for the tensor entries.
        Returns:
        a builder for ImmutableTensor
      • builder

        public static final <T> ImmutableTensor.Builder<T> builder​(java.lang.Class<?>... dimensions)
        Returns a builder for an ImmutableTensor. The dimensions (classes of coordinates) of the future tensor have to be given as arguments here.
        Type Parameters:
        T - the type of values of the tensor
        Parameters:
        dimensions - the dimensions of the tensor to create
        Returns:
        a builder for an immutable tensor
      • fromMap

        public static final <T> Tensor<T> fromMap​(java.lang.Iterable<java.lang.Class<?>> dimensions,
                                                  java.util.Map<Position,​T> map)
        Creates a tensor from the given map, where the map has to contain the positions as keys and the values as values.
        Parameters:
        dimensions - the desired dimensions of the tensor. This has to be consistent with the position - keys in the map.
        map - the map from which to construct a tensor
        Returns:
        a new immutable tensor
      • copyOf

        public static final <T> Tensor<T> copyOf​(Tensor<T> tensor)
        Creates an immutable copy of the given tensor.
        Parameters:
        tensor - the tensor whose element to copy
        Returns:
        new immutable Tensor
      • get

        public V get​(Position position)
        Specified by:
        get in interface Tensoric<V>
        Parameters:
        position - the position in the N-dimensional space where to find the value.
        Returns:
        the value at the given position
      • context

        public Position context()
        Description copied from interface: Tensor
        Retrieves the context of the tensor, which is nothing else than a position. As context of the tensor we understand coordinates within a higher dimensional space than than the tensor has itself. This coordinates can e.g. transport information when e.g. a higher-dimensional tensor is split into smaller ones, so that it can potentially be reconstructed afterwards.

        Implementations have to guarantee that the returned value here is never null.

        Specified by:
        context in interface Tensor<V>
        Returns:
        the context of the tensor.
      • asMap

        public java.util.Map<Position,​V> asMap()
        Description copied from interface: Mappable
        By implementing this method, a tensor can provide an efficient way to convert the tensor to a map from position to values. Implementing instances must follow the following contract:
        • this method must not return null
        • It is recommended to return either a copy or an immutable map. Despite clients are not supposed to manipulate the returned maps, it cannot be guaranteed and this could lead to unexpected results.
        Usually, clients will not use this method directly, but should use TensorInternals.mapFrom(Tensor) (internal clients) or Tensorics.mapFrom(Tensor) (external clients)
        Specified by:
        asMap in interface Mappable<V>
        Returns:
        a (preferrably immutable) map representing the content of the tensor
        See Also:
        TensorInternals.mapFrom(Tensor), Tensorics.mapFrom(Tensor)
      • shape

        public Shape shape()
        Description copied from interface: Tensor
        Retrieves the shape of the tensor. As shape we understand simply the structure of a tensor: Its dimensions and the available positions.

        Implementations have to take care that the returned value here is never null.

        Specified by:
        shape in interface Tensor<V>
        Returns:
        the shape of the tensor.
      • toString

        public java.lang.String toString()
        When printing the tensor content output is automatically not larger then N ant the beginning and N at the end of the Tensor entries.
        Overrides:
        toString in class java.lang.Object
      • contains

        public boolean contains​(Position position)
        Description copied from interface: Tensoric
        Returns true if the tensoric object contains the given position, false otherwise.
        Specified by:
        contains in interface Tensoric<V>
        Parameters:
        position - the position to be checked
        Returns:
        true if the position is contained in the tensoric object, false otherwise.