Package org.tensorics.core.tensor
Class Shape
- java.lang.Object
-
- org.tensorics.core.tensor.Shape
-
- All Implemented Interfaces:
java.io.Serializable
public final class Shape extends java.lang.Object implements java.io.Serializable
Defines the shape of a tensor. This implies all the positions for which the tensor stores values.This class is immutable
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Shape.Builder
The builder for a tensor shape.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Shape.Builder
builder()
Creates a new builder for a shape.static Shape.Builder
builder(java.util.Set<? extends java.lang.Class<?>> dimensions)
Creates a new builder for a shape with predefined dimensions.boolean
contains(java.lang.Object... coordinates)
Convenience method forcontains(Position)
, with the position constructed from the given coordinates.boolean
contains(Position position)
Returnstrue
if the shape contains the given position,false
otherwise.<T> java.util.Set<T>
coordinatesOfType(java.lang.Class<T> ofClass)
Extracts the provided class of the coordinate from the provided shape.boolean
covers(Shape other)
returnstrue
if this shape contains at least all the positions of the the other.int
dimensionality()
Returns the number of dimensions of a tensor of this shape.java.util.Set<java.lang.Class<?>>
dimensionSet()
Retrieves all the dimensions of this shape.static Shape
empty()
Returns an empty shape with no dimensions and no positions.boolean
equals(java.lang.Object obj)
int
hashCode()
boolean
hasSameDimensionsAs(Shape other)
returnstrue
if this shape has the same dimensions as the other shape.static Shape
of(java.lang.Iterable<Position> positions)
Deprecated.Using this method to construct a shape guesses the dimensionality from the passed positions.static Shape
of(java.util.Set<java.lang.Class<?>> dimensions, java.lang.Iterable<Position> positions)
Creates a shape from the given set of positions and dimensions (types of coordinates).static Shape
of(java.util.Set<java.lang.Class<?>> dimensions, Position... positions)
This is a convenience method to be used instead ofof(Set,Iterable)
.static Shape
of(Position... positions)
Deprecated.Using this method to construct a shape guesses the dimensionality from the passed positions.java.util.Set<Position>
positionSet()
Retrieves all the positions which are available in a tensor for this shape.int
size()
Returns the number of entries of a tensor of this shape.java.lang.String
toString()
static Shape
viewOf(java.util.Set<? extends java.lang.Class<?>> dimensions, java.util.Set<Position> positions)
Directly creates a shape using the given dimensions and positions.static Shape
zeroDimensional()
Returns a shape corresponding to a zero-dimensional tensor with one entry (an empty position).
-
-
-
Method Detail
-
positionSet
public java.util.Set<Position> positionSet()
Retrieves all the positions which are available in a tensor for this shape.- Returns:
- an immutable set of the positions for a tensor of this shape.
-
dimensionSet
public java.util.Set<java.lang.Class<?>> dimensionSet()
Retrieves all the dimensions of this shape.- Returns:
- the dimensions
-
dimensionality
public int dimensionality()
Returns the number of dimensions of a tensor of this shape. Other words are 'rank' or 'order' of the tensor. This has to be consistent with the size of the collections returned bydimensionSet()
;- Returns:
- the number of dimensions of the tensor (rank, order)
-
size
public int size()
Returns the number of entries of a tensor of this shape. This has to be consistent with the size of the collection returned bypositionSet()
;- Returns:
- the number of positions.
-
covers
public boolean covers(Shape other)
returnstrue
if this shape contains at least all the positions of the the other.- Parameters:
other
- the set which to compare to.- Returns:
true
if the other shape is covered,false
otherwise.
-
contains
public boolean contains(Position position)
Returnstrue
if the shape contains the given position,false
otherwise.- Parameters:
position
- the position to be checked- Returns:
true
if the position is contained in the shape,false
otherwise.
-
contains
public boolean contains(java.lang.Object... coordinates)
Convenience method forcontains(Position)
, with the position constructed from the given coordinates.- Parameters:
coordinates
- the coordinates which represent the position to be checked- Returns:
true
if the position represented by the given coordinates is contained in the shape,false
otherwise.
-
hasSameDimensionsAs
public boolean hasSameDimensionsAs(Shape other)
returnstrue
if this shape has the same dimensions as the other shape.- Parameters:
other
- the other shape with which to compare this shape- Returns:
true
if the dimensions are the same,false
otherwise
-
builder
public static Shape.Builder builder()
Creates a new builder for a shape. The dimensions of the resulting shape will be retrieved from the first position that will be added. If no elements will be added the resulting shape will have zero dimensions.- Returns:
- a new builder for a shape.
-
builder
public static Shape.Builder builder(java.util.Set<? extends java.lang.Class<?>> dimensions)
Creates a new builder for a shape with predefined dimensions.- Parameters:
dimensions
- the dimensions for the new shape- Returns:
- the builder with predefined dimensions
-
viewOf
public static Shape viewOf(java.util.Set<? extends java.lang.Class<?>> dimensions, java.util.Set<Position> positions)
Directly creates a shape using the given dimensions and positions. The consistency of dimensions and positions is not checked! Thus this is only for internal use.- Parameters:
dimensions
- the dimensions for the shapepositions
- the positions of the shape- Returns:
- a new shape instance
-
of
@Deprecated public static Shape of(java.lang.Iterable<Position> positions)
Deprecated.Using this method to construct a shape guesses the dimensionality from the passed positions.Creates a shape from the given set of positions. The dimensions (types of coordinates) are taken from the first element in the provided iterable. If the given positions do not correspond to the same dimensions, then anIllegalArgumentException
will be thrown.- Parameters:
positions
- the positions which shall be used for the new shape- Returns:
- a new shape, containing the given positions
- Throws:
java.lang.IllegalArgumentException
- if the dimensions of the individual positions are not consistent.
-
of
@SafeVarargs @Deprecated public static Shape of(Position... positions)
Deprecated.Using this method to construct a shape guesses the dimensionality from the passed positions.This is a convenience method to be used instead ofof(Iterable)
.- Parameters:
positions
- the positions for the new shape- Returns:
- a new shape containing the given positions
- Throws:
java.lang.IllegalArgumentException
- if the dimensions of the positions are inconsistent- See Also:
of(Iterable)
-
of
public static Shape of(java.util.Set<java.lang.Class<?>> dimensions, java.lang.Iterable<Position> positions)
Creates a shape from the given set of positions and dimensions (types of coordinates). If the given positions do not correspond to the same dimensions, then anIllegalArgumentException
will be thrown.- Parameters:
dimensions
- the dimensions for the new shapepositions
- the positions which shall be used for the new shape- Returns:
- a new shape, containing the given positions
- Throws:
java.lang.IllegalArgumentException
- if the dimensions of the individual positions are not consistent.
-
of
@SafeVarargs public static Shape of(java.util.Set<java.lang.Class<?>> dimensions, Position... positions)
This is a convenience method to be used instead ofof(Set,Iterable)
.- Parameters:
dimensions
- the dimensions for the new shapepositions
- the positions for the new shape- Returns:
- a new shape containing the given positions
- Throws:
java.lang.IllegalArgumentException
- if the dimensions of the positions are inconsistent- See Also:
of(Iterable)
-
empty
public static Shape empty()
Returns an empty shape with no dimensions and no positions. The returned shape is equal to the shape of an zero dimensional tensor. It is further equivalent to the call toof()
(with no arguments). However, using this method is preferred, since no unnecessary instances are created this way.- Returns:
- an empty shape
-
zeroDimensional
public static Shape zeroDimensional()
Returns a shape corresponding to a zero-dimensional tensor with one entry (an empty position).- Returns:
- a shape corresponding to a zero - dimensional tensor with one entry (at
Position.empty()
)
-
coordinatesOfType
public <T> java.util.Set<T> coordinatesOfType(java.lang.Class<T> ofClass)
Extracts the provided class of the coordinate from the provided shape.- Parameters:
ofClass
- a class of the coordinate to be extracted- Returns:
- a set of the extracted coordinates from provided shape
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-