Template Class Tensor¶
Defined in File Tensor.hpp
Class Documentation¶
-
template<class
T= std::complex<float>>
classJet::Tensor¶ Tensorrepresents an \(n\)-rank data structure of complex-valued data for tensor operations.The following conventions are used:
- "Rank" and "order" are used interchangeably and refer to the number of tensor indices. - "Dimension" refers to the number of elements along a tensor index. - "Shape" refers to the dimensions of a tensor; the number of dimensions is the rank of the tensor.
- tparam T
Underlying complex tensor data type (
complex<float>orcomplex<double>).
Public Functions
-
inline
Tensor()¶ Constructs a default
Tensorobject.Default tensor objects have a single zero-initialized data value.
Warning
The shape and indices of a default
Tensorobject are not set.
-
inline
Tensor(const std::vector<size_t> &shape)¶ Constructs a shaped
Tensorobject.Shaped
Tensorobjects have zero-initialized data values and a size of ( \(\prod_i{\textrm{shape}_i}\)). The indices of a shapedTensorobject default to the values from the set?[a-zA-Z].- Parameters
shape – Dimension of each
Tensorindex.
-
inline
Tensor(const std::vector<std::string> &indices, const std::vector<size_t> &shape)¶ Constructs a shaped and labeled
Tensorobject.Shaped and labeled
Tensorobjects have zero-initialized data values and a size of ( \(\prod_i{\textrm{shape}_i}\)).- Parameters
indices – Label of each
Tensorindex.shape – Dimension of each
Tensorindex.
-
inline
Tensor(const std::vector<std::string> &indices, const std::vector<size_t> &shape, const std::vector<T> &data)¶ Constructs a shaped, labeled, and populated
Tensorobject.The size of a shaped, indexed, and populated
Tensorobject is ( \(\prod_i{\textrm{shape}_i}\)).- Parameters
indices – Label of each
Tensorindex.shape – Dimension of each
Tensorindex.data – Row-major encoded complex data representation of the
Tensorobject.
-
inline
Tensor(const Tensor &other)¶ Constructs a
Tensorobject by copying anotherTensorobject.- Parameters
other –
Tensorobject to be copied.
-
inline
Tensor(Tensor &&other)¶ Constructs a
Tensorobject by moving anotherTensorobject.- Parameters
other –
Tensorobject to be moved.
-
inline virtual
~Tensor()¶ Destructs this
Tensorobject.
-
inline void
InitIndicesAndShape(const std::vector<std::string> &indices, const std::vector<size_t> &shape) noexcept¶ Initializes the indices and shape of a
Tensorobject.The indices and shapes must be ordered to map directly such that
indices[i]has sizeshape[i].Note
This function updates the internal index-to-dimension map.
- Parameters
indices – Label of each
Tensorindex.shape – Dimension of each
Tensorindex.
-
inline void
SetShape(const std::vector<size_t> &shape) noexcept¶ Sets the shape of a
Tensorobject.The shape of a
Tensordefines the number of elements per rank (index).- Parameters
shape – Number of elements in each
Tensorindex.
-
inline const std::vector<size_t> &
GetShape() const noexcept¶ Returns the shape of a
Tensortensor object.- Returns
Number of elements in each
Tensorindex.
-
inline T &
operator[](size_t pos)¶ Returns a reference to a
Tensorobject datum.Warning
Supplying an index greater than or equal to
GetSize()is undefined behaviour.- Parameters
pos – Position of the datum to retrieve, encoded as a 1D row-major index (lexicographic ordering).
- Returns
Reference to the complex data value at the specified position.
-
inline void
RenameIndex(size_t pos, std::string new_label) noexcept¶ Renames a
Tensorindex label.- Parameters
pos – Position of the
Tensorlabel.new_label – New
Tensorlabel.
-
inline bool
operator==(const Tensor<T> &other) const noexcept¶ Equality operator for
Tensorobjects.- Parameters
other –
Tensorobject to be compared to thisTensorobject.- Returns
True if the two
Tensorobjects are equivalent.
-
inline bool
operator!=(const Tensor<T> &other) const¶ Inequality operator for
Tensorobjects.- Parameters
other –
Tensorobject to be compared to thisTensorobject.- Returns
True if the two
Tensorobjects are not equivalent.
-
inline const Tensor<T> &
operator=(const Tensor<T> &other)¶ Assignment operator for
Tensorobjects.- Parameters
other –
Tensorobject to be assigned from.- Returns
Reference to this
Tensorobject.
-
inline const Tensor<T> &
operator=(Tensor<T> &&other)¶ Assignment operator for
Tensorobjects using move semantics.- Parameters
other –
Tensorobject to take ownership of.- Returns
Reference to this
Tensorobject.
-
inline const std::unordered_map<std::string, size_t> &
GetIndexToDimension() const¶ Returns the index-to-dimension map.
- Returns
Mapping from
Tensorindex labels to dimension sizes.
-
inline void
SetValue(const std::vector<size_t> &indices, const T &value)¶ Sets the
Tensordata value at the given \(n\)-dimensional index.- Parameters
indices – \(n\)-dimensional
Tensordata index in row-major order.value – Data value to set at given index.
-
inline T
GetValue(const std::vector<size_t> &indices) const¶ Returns the
Tensordata value at the given \(n\)-dimensional index.- Parameters
indices – \(n\)-dimensional
Tensordata index in row-major order.- Returns
Complex data value.
-
inline void
SetData(const std::vector<T> &data)¶ Sets the data of a
Tensor.- Parameters
data – Data of the
Tensorin row-major order.- Throws
Jet::Exception – Data has the wrong size.
-
inline const std::vector<T> &
GetData() const noexcept¶ Returns the
Tensordata in row-major order.- Returns
Vector of complex data values.
-
inline const std::vector<std::string> &
GetIndices() const noexcept¶ Returns the
Tensorindex labels.- Returns
Vector of index labels.
-
inline size_t
GetSize() const¶ Returns the size of a
Tensorobject.- Returns
Number of data elements.
-
inline const T &
GetScalar() const¶ Returns a single scalar value from the
Tensorobject.Note
This is equivalent to calling
GetValue({}).- Returns
Complex data value.
-
inline bool
IsScalar() const noexcept¶ Reports whether a
Tensorobject is a scalar.- Returns
True if this
Tensorobject is rank-0 (and false otherwise).
-
inline void
FillRandom(size_t seed)¶ Assigns random values to the
Tensorobject data.The real and imaginary components of each datum will be independently sampled from a uniform distribution with support over [-1, 1].
Note
This overload enables reproducible random number generation for a given seed.
- Parameters
seed – Seed to supply to the RNG engine.
-
inline void
FillRandom()¶ Assigns random values to the
Tensorobject data.The real and imaginary components of each datum will be independently sampled from a uniform distribution with support over [-1, 1].
-
inline Tensor<T>
AddTensor(const Tensor<T> &other) const¶ Adds current and other
Tensorobject with the same index sets.
-
inline Tensor<T>
SliceIndex(const std::string &index, size_t value) const¶ Slices current
Tensorobject index.
-
inline Tensor<T>
Reshape(const std::vector<size_t> &new_shape) const¶ Reshapes
Tensorobject to the given dimensions.
-
inline Tensor<T>
Transpose(const std::vector<size_t> &new_ordering) const¶ Transposes the indices of the
Tensorobject to a new ordering.
-
inline Tensor<T>
Transpose(const std::vector<std::string> &new_indices) const¶ Transposes the indices of the
Tensorobject to a new ordering.
Public Static Functions
-
template<class
U= T>
static inline Tensor<U>AddTensors(const Tensor<U> &A, const Tensor<U> &B)¶ Adds two
Tensorobjects with the same index sets.The resulting tensor will have the same index set as the operand tensors. The order of the indices follows that of the first argument (i.e.,
A).Example: Given a 2x3 tensor A(i,j) and a 2x3 tensor B(i,j), the addition of A and B is a 2x3 tensor C(i,j):
Tensor A({"i", "j"}, {2, 3}, {0, 1, 2, 3, 4, 5}); Tensor B({"i", "j}, {2, 3}, {5, 5, 5, 6, 6, 6}); Tensor C = AddTensors(A, B); // {5, 6, 7, 9, 10, 11}
Warning
The program is aborted if the index sets of the given
Tensorobjects to not match.- Template Parameters
U –
Tensordata type.- Parameters
A – tensor on the LHS of the addition.
B – tensor on the RHS of the addition.
- Returns
Tensorobject representing the element-wise sum of the given tensors.
-
template<class
U= T>
static inline Tensor<U>SliceIndex(const Tensor<U> &tensor, const std::string &index, size_t value)¶ Slices a
Tensorobject index.The result is a
Tensorobject whose given indices and data are a subset of the provided tensor object, sliced along the given index argument.Example: Consider a 2x3 tensor
A(i,j). The following example slices along each index with the resulting slices selected as required:Tensor A({"i", "j"}, {2, 3}); A.FillRandom(); SliceIndex(A, "i", 0); // [1x3] tensor, slice 0 SliceIndex(A, "i", 1); // [1x3] tensor, slice 1 SliceIndex(A, "j", 0); // [2x1] tensor, slice 0 SliceIndex(A, "j", 1); // [2x1] tensor, slice 1 SliceIndex(A, "j", 2); // [2x1] tensor, slice 2
- Template Parameters
U –
Tensordata type.- Parameters
tensor –
Tensorobject to slice.index –
Tensorindex label on which to slice.value – Value to slice the
Tensorindex on.
- Returns
Slice of the
Tensorobject.
-
template<class
U= T>
static inline Tensor<U>Reshape(const Tensor<U> &old_tensor, const std::vector<size_t> &new_shape)¶ Reshapes a
Tensorobject to the given dimensions.- Template Parameters
U –
Tensordata type.- Parameters
old_tensor – Original tensor object to reshape.
new_shape – Index dimensionality for new tensor object.
- Returns
Reshaped copy of the
Tensorobject.
-
template<class
U= T, size_tBLOCKSIZE= 1024, size_tMINSIZE= 32>
static inline Tensor<U>Transpose(const Tensor<U> &A, const std::vector<std::string> &new_indices)¶ Transposes the indices of a
Tensorobject to a new ordering.- Template Parameters
U –
Tensordata type.- Parameters
A – Reference
Tensorobject.new_indices – New
Tensorindex label ordering.
- Returns
Transposed
Tensorobject.
-
template<class
U= T, size_tBLOCKSIZE= 1024, size_tMINSIZE= 32>
static inline Tensor<U>Transpose(const Tensor<U> &A, const std::vector<size_t> &new_ordering)¶ Transposes the indices of a
Tensorto a new ordering.Warning
The program is aborted if the number of elements in the new ordering does match the number of indices in the tensor.
- Template Parameters
U –
Tensordata type.- Parameters
A – Reference
Tensorobject.new_ordering – New
Tensorindex permutation.
- Returns
Transposed
Tensorobject.
-
template<class
U= T>
static inline Tensor<U>Conj(const Tensor<U> &A)¶ Returns the conjugate of a
Tensorobject.- Template Parameters
U –
Tensordata type.- Parameters
A – Reference
Tensorobject.- Returns
Tensorobject representing the conjugate ofA.
-
template<class
U= T>
static inline Tensor<U>ContractTensors(const Tensor<U> &A, const Tensor<U> &B)¶ Contracts two
Tensorobjects over the intersection of their index sets.The resulting tensor will be formed with indices given by the symmetric difference of the index sets.
Example: Given a 3x2x4 tensor A(i,j,k) and a 2x4x2 tensor B(j,k,l), the common indices are (j,k) and the symmetric difference of the sets is (i,l). The result of the contraction is a 3x2 tensor C(i,l).
Tensor A({"i", "j", "k"}, {3, 2, 4}); Tensor B({"j", "k", "l"}, {2, 4, 2}); A.FillRandom(); B.FillRandom(); Tensor C = ContractTensors(A, B);
- See
TODO: Link to documentation
- Template Parameters
U –
Tensordata type.- Parameters
A – tensor on the LHS of the contraction.
B – tensor on the RHS of the contraction.
- Returns
Tensorobject representing the contraction of the tensors.