Template Class tensor

Nested Relationships

Class Documentation

template<typename _ElemType, std::size_t _vector_dimension, std::size_t _tensor_rank>
class gamer::tensor

General multidimensional tensor class.

The implementation is based on a flattened array. Utilities are provided to assist with indexing and basic tensor mathematics.

tparam _ElemType

Typename of the tensor elements

tparam _vector_dimension

Dimension of the vector space

tparam _tensor_rank

Rank of the tensor

Public Types

using ElemType = _ElemType

Alias for typename of tensor elements.

using IndexType = std::array<std::size_t, tensor_rank>

Typename of an index for the tensor.

using DataType = std::array<ElemType, total_dimension>

Typename of the underlying flat data representation.

Public Functions

inline tensor()

Default constructor initializes to zero.

inline tensor(const ElemType &s)

Constructor fill with same value.

Parameters

s[in] Value to fill

inline tensor(const ElemType (&s)[total_dimension])

Constructor from flat array.

Parameters

s[in] Row major array to copy from

inline tensor(const tensor &x)

Copy constructor.

Parameters

x[in] Other tensor to copy

inline tensor(const tensor &&x)

Move constructor.

Parameters

x[in] Other tensor to copy from

template<typename NumType, typename = std::enable_if_t<std::is_arithmetic<NumType>::value>, typename = std::enable_if_t<std::is_arithmetic<ElemType>::value>>
inline operator tensor<NumType, _vector_dimension, _tensor_rank>() const

Type casting for numerical typed tensors.

Template Parameters
  • NumType – Typename of new tensor data

  • <unnamed> – Check that resulting type is numeric

  • <unnamed> – Check that current type is numeric

inline std::string to_string() const

Returns a string representation of the object.

Returns

String representation of the object.

template<typename ...Ts>
inline const _ElemType &get(Ts&&... index) const

Get element by index.

Parameters

index – Sequence of indices

Template Parameters

Ts – Typenames of indices

Returns

Reference to value stored at the index

template<typename ...Ts>
inline _ElemType &get(Ts&&... index)

Get element by index.

Parameters

index – Sequence of indices

Template Parameters

Ts – Typenames of indices

Returns

Reference to value stored at the index

inline const _ElemType &operator[](const IndexType &index) const

Get element by index.

Parameters

index – Sequence of indices

Returns

Reference to value stored at the index

inline _ElemType &operator[](const IndexType &index)

Get element by index.

Parameters

index – Sequence of indices

Returns

Reference to value stored at the index

inline const _ElemType &operator[](std::size_t index) const

Special overload for data accession of 1-tensors.

Parameters

index – Sequence of indices

Returns

Reference to value stored at the index

inline _ElemType &operator[](std::size_t index)

Special overload for data accession of 1-tensors.

Parameters

index – Sequence of indices

Returns

Reference to value stored at the index

inline bool operator==(const tensor &rhs) const

Equality comparison of tensors.

Parameters

rhs[in] The right hand side

Returns

True if all elements equal or False otherwise

inline bool operator!=(const tensor &rhs) const

Inquality comparison of tensors.

Parameters

rhs[in] The right hand side

Returns

False if all elements equal or True otherwise

inline void operator=(const tensor &rhs)

Assignment operator.

Parameters

rhs[in] The right hand side

template<std::size_t D = _tensor_rank, typename std::enable_if<D == 1, void>::type* = nullptr>
inline void operator=(const Eigen::Matrix<_ElemType, _vector_dimension, 1> &rhs)

Assignment operator from Eigen object type.

Parameters

rhs[in] Eigen object

Template Parameters
  • D – Dependent template for tensor rank

  • <unnamed> – Enabled only for 1-tensors

template<std::size_t D = _tensor_rank, typename std::enable_if<D == 2, void>::type* = nullptr>
inline void operator=(const Eigen::Matrix<_ElemType, _vector_dimension, _vector_dimension> &rhs)

Assignment operator from Eigen object type.

Parameters

rhs[in] Eigen object

Template Parameters
  • D – Dependent template for tensor rank

  • <unnamed> – Enabled only for 2-tensors

inline tensor &operator+=(const tensor &rhs)

Elementwise tensor sum operator.

Parameters

rhs[in] The right hand side

Returns

Reference to this

inline tensor &operator-=(const tensor &rhs)

Elementwise tensor difference operator.

Parameters

rhs[in] The right hand side

Returns

Reference to this

inline tensor &operator*=(ElemType x)

Scalar multiplication.

Parameters

x[in] Scalar to multiply by

Returns

Reference to this

inline tensor &operator/=(ElemType x)

Scalar division.

Parameters

x[in] Scalar to divide by

Returns

Reference to this

inline tensor operator-() const

Unary negation operator.

Returns

Tensor with negated values

inline tensor ElementwiseProduct(const tensor &rhs) const

Elementwise product with another tensor.

Parameters

rhs[in] The right hand side

Returns

Tensor with elementwise product

inline tensor ElementwiseDivision(const tensor &rhs) const

Elementwise division with another tensor.

Parameters

rhs[in] The right hand side

Returns

Tensor with elementwise division

inline auto data()

Direct access to the underlying array data.

Returns

Direct access to the underlying array

template<std::size_t D = _tensor_rank, typename std::enable_if<D == 1, void>::type* = nullptr>
inline Eigen::Map<Eigen::Matrix<_ElemType, _vector_dimension, 1>> toEigen()

Returns a eigen representation of the object.

Returns

Eigen representation of the object.

template<std::size_t D = _tensor_rank, typename std::enable_if<D == 1, void>::type* = nullptr>
inline operator Eigen::Matrix<_ElemType, _vector_dimension, 1>()

Implicit conversion of 1-tensors to Eigen Vector type.

Returns

Eigen representation of the object.

template<std::size_t D = _tensor_rank, typename std::enable_if<D == 2, void>::type* = nullptr>
inline Eigen::Map<Eigen::Matrix<_ElemType, _vector_dimension, _vector_dimension>> toEigen()

Returns a eigen representation of the object.

Template Parameters
  • D – Tensor rank

  • <unnamed> – Enabled only for 2-tensors

Returns

Eigen representation of the object.

template<std::size_t D = _tensor_rank, typename std::enable_if<D == 2, void>::type* = nullptr>
inline operator Eigen::Matrix<_ElemType, _vector_dimension, _vector_dimension>()

Implicit conversion of 2-tensor to Eigen Matrix type.

Template Parameters
  • D – Tensor rank

  • <unnamed> – Enabled only for 2-tensors

inline index_iterator index_begin()

Iterator to first index.

Returns

Iterator to first index

inline index_iterator index_end()

Iterator to past the end.

Returns

Iterator to past the end

inline index_iterator index_begin() const

Iterator to first index.

Returns

Iterator to first index

inline index_iterator index_end() const

Iterator to past the end.

Returns

Iterator to past the end

inline DataType::iterator begin()

Direct iterator to beginning of data.

Returns

Direct iterator to beginning of data

inline DataType::iterator end()

Direct iterator to end of data.

Returns

Direct iterator to end of data

inline DataType::const_iterator begin() const

Direct iterator to beginning of data.

Returns

Direct iterator to beginning of data

inline DataType::const_iterator end() const

Direct iterator to end of data.

Returns

Direct iterator to end of data

Public Static Attributes

static constexpr std::size_t tensor_rank = _tensor_rank

Tensor rank of the tensor.

static constexpr std::size_t vector_dimension = _vector_dimension

Dimension of the vector space.

static constexpr std::size_t total_dimension = detail::pow<vector_dimension, tensor_rank>::value

Total number of tensor components.

Friends

inline friend friend std::ostream & operator<< (std::ostream &output, const tensor &t)

Print operator overload.

Parameters
  • output – The output stream

  • t[in] Tensor to print

Returns

Output stream

struct index_iterator : public std::iterator<std::bidirectional_iterator_tag, IndexType>

Iterator over tensor indices.

Public Types

using super = std::iterator<std::bidirectional_iterator_tag, IndexType>

Public Functions

inline index_iterator(const index_iterator &iter)

Copy constructor.

Parameters

iter[in] The iterator to copy from

inline index_iterator(const index_iterator &&iter)

Move constructor.

Parameters

iter[in] The iterator to move from

inline index_iterator()

Constructor initialized at end of indices.

inline index_iterator(int)

Constructor initialized at start of indices.

inline index_iterator &operator++()

Prefix incrementation of the index.

Returns

Iterator to the next index

inline index_iterator operator++(int)

Postfix incrementation of the index.

Returns

Iterator to the next index

inline index_iterator &operator--()

Prefix decrementation of the index.

Returns

Iterator to the previous index

inline index_iterator operator--(int)

Postfix decrementation of the index.

Returns

Iterator to the previous index

inline bool operator==(index_iterator j) const

Equality operator for indices.

Parameters

j[in] Other index iterator to compare to

Returns

True if indices are equal

inline bool operator!=(index_iterator j) const

{ operator_description }

Parameters

j[in] { parameter_description }

Returns

{ description_of_the_return_value }

inline super::reference operator*()

Dereference the iterator.

Returns

Index array of indices

inline super::pointer operator->()

Dereference the iterator.

Returns

Index array of indices

Protected Attributes

IndexType i

Array of indices.