Package org.tensorics.core.tensor
Class ImmutableTensor<V>
- java.lang.Object
-
- org.tensorics.core.tensor.AbstractTensor<V>
-
- org.tensorics.core.tensor.ImmutableTensor<V>
-
- Type Parameters:
V
- type of values in Tensor.
public class ImmutableTensor<V> extends AbstractTensor<V> implements Mappable<V>, java.io.Serializable
Default Implementation ofTensor
.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 viaShape
.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ImmutableTensor.Builder<V>
A builder for an immutable tensor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Map<Position,V>
asMap()
By implementing this method, a tensor can provide an efficient way to convert the tensor to a map from position to values.static <T> ImmutableTensor.Builder<T>
builder(java.lang.Class<?>... dimensions)
Returns a builder for anImmutableTensor
.static <T> ImmutableTensor.Builder<T>
builder(java.lang.Iterable<java.lang.Class<?>> dimensions)
Returns a builder for anImmutableTensor
.static <T> ImmutableTensor.Builder<T>
builderFrom(Tensor<T> tensor)
Returns a builder for anImmutableTensor
which is initiliased with the givenImmutableTensor
.boolean
contains(Position position)
Returnstrue
if the tensoric object contains the given position,false
otherwise.Position
context()
Retrieves the context of the tensor, which is nothing else than a position.static <T> Tensor<T>
copyOf(Tensor<T> tensor)
Creates an immutable copy of the given tensor.static <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.V
get(Position position)
Shape
shape()
Retrieves the shape of the tensor.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.-
Methods inherited from class org.tensorics.core.tensor.AbstractTensor
equals, hashCode
-
-
-
-
Method Detail
-
builder
public static final <T> ImmutableTensor.Builder<T> builder(java.lang.Iterable<java.lang.Class<?>> dimensions)
Returns a builder for anImmutableTensor
. 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 anImmutableTensor
. 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
-
builderFrom
public static <T> ImmutableTensor.Builder<T> builderFrom(Tensor<T> tensor)
Returns a builder for anImmutableTensor
which is initiliased with the givenImmutableTensor
.- Type Parameters:
T
- type of values in Tensor.- Parameters:
tensor
- a Tensor with which theImmutableTensor.Builder
is initialized- Returns:
- a
ImmutableTensor.Builder
for anImmutableTensor
-
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
.
-
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.
TensorInternals.mapFrom(Tensor)
(internal clients) orTensorics.mapFrom(Tensor)
(external clients)- Specified by:
asMap
in interfaceMappable<V>
- Returns:
- a (preferrably immutable) map representing the content of the tensor
- See Also:
TensorInternals.mapFrom(Tensor)
,Tensorics.mapFrom(Tensor)
- this method must not return
-
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
.
-
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 classjava.lang.Object
-
-