Template Class tensor

Nested Relationships

Class Documentation

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

General multidimensional tensor class.

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

Template Parameters
  • _ElemType: Typename of the tensor elements

  • _vector_dimension: Dimension of the vector space

  • _tensor_rank: Rank of the tensor

Public Types

template<>
using ElemType = _ElemType

Alias for typename of tensor elements.

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

Typename of an index for the tensor.

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

Typename of the underlying flat data representation.

Public Functions

tensor()

Default constructor initializes to zero.

tensor(const ElemType &s)

Constructor fill with same value.

Parameters
  • [in] s: Value to fill

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

Constructor from flat array.

Parameters
  • [in] s: Row major array to copy from

tensor(const tensor &x)

Copy constructor.

Parameters
  • [in] x: Other tensor to copy

tensor(const tensor &&x)

Move constructor.

Parameters
  • [in] x: 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>>
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

std::string to_string() const

Returns a string representation of the object.

Return

String representation of the object.

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

Get element by index.

Return

Reference to value stored at the index

Parameters
  • index: Sequence of indices

Template Parameters
  • Ts: Typenames of indices

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

Get element by index.

Return

Reference to value stored at the index

Parameters
  • index: Sequence of indices

Template Parameters
  • Ts: Typenames of indices

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

Get element by index.

Return

Reference to value stored at the index

Parameters
  • index: Sequence of indices

_ElemType &operator[](const IndexType &index)

Get element by index.

Return

Reference to value stored at the index

Parameters
  • index: Sequence of indices

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

Special overload for data accession of 1-tensors.

Return

Reference to value stored at the index

Parameters
  • index: Sequence of indices

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

Special overload for data accession of 1-tensors.

Return

Reference to value stored at the index

Parameters
  • index: Sequence of indices

bool operator==(const tensor &rhs) const

Equality comparison of tensors.

Return

True if all elements equal or False otherwise

Parameters
  • [in] rhs: The right hand side

bool operator!=(const tensor &rhs) const

Inquality comparison of tensors.

Return

False if all elements equal or True otherwise

Parameters
  • [in] rhs: The right hand side

void operator=(const tensor &rhs)

Assignment operator.

Parameters
  • [in] rhs: The right hand side

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

Assignment operator from Eigen object type.

Parameters
  • [in] rhs: 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>void gamer::tensor::operator=(const Eigen::Matrix< _ElemType, _vector_dimension, _vector_dimension > & rhs)

Assignment operator from Eigen object type.

Parameters
  • [in] rhs: Eigen object

Template Parameters
  • D: Dependent template for tensor rank

  • <unnamed>: Enabled only for 2-tensors

tensor &operator+=(const tensor &rhs)

Elementwise tensor sum operator.

Return

Reference to this

Parameters
  • [in] rhs: The right hand side

tensor &operator-=(const tensor &rhs)

Elementwise tensor difference operator.

Return

Reference to this

Parameters
  • [in] rhs: The right hand side

tensor &operator*=(ElemType x)

Scalar multiplication.

Return

Reference to this

Parameters
  • [in] x: Scalar to multiply by

tensor &operator/=(ElemType x)

Scalar division.

Return

Reference to this

Parameters
  • [in] x: Scalar to divide by

tensor operator-() const

Unary negation operator.

Return

Tensor with negated values

tensor ElementwiseProduct(const tensor &rhs) const

Elementwise product with another tensor.

Return

Tensor with elementwise product

Parameters
  • [in] rhs: The right hand side

tensor ElementwiseDivision(const tensor &rhs) const

Elementwise division with another tensor.

Return

Tensor with elementwise division

Parameters
  • [in] rhs: The right hand side

auto data()

Direct access to the underlying array data.

Return

Direct access to the underlying array

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

Returns a eigen representation of the object.

Return

Eigen representation of the object.

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

Implicit conversion of 1-tensors to Eigen Vector type.

Return

Eigen representation of the object.

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

Returns a eigen representation of the object.

Return

Eigen representation of the object.

Template Parameters
  • D: Tensor rank

  • <unnamed>: Enabled only for 2-tensors

template<std::size_t D = _tensor_rank, typename std::enable_if< D==2, void >::type * = nullptr>gamer::tensor::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

index_iterator index_begin()

Iterator to first index.

Return

Iterator to first index

index_iterator index_end()

Iterator to past the end.

Return

Iterator to past the end

index_iterator index_begin() const

Iterator to first index.

Return

Iterator to first index

index_iterator index_end() const

Iterator to past the end.

Return

Iterator to past the end

DataType::iterator begin()

Direct iterator to beginning of data.

Return

Direct iterator to beginning of data

DataType::iterator end()

Direct iterator to end of data.

Return

Direct iterator to end of data

DataType::const_iterator begin() const

Direct iterator to beginning of data.

Return

Direct iterator to beginning of data

DataType::const_iterator end() const

Direct iterator to end of data.

Return

Direct iterator to end of data

Public Static Attributes

constexpr std::size_t tensor_rank = _tensor_rank

Tensor rank of the tensor.

constexpr std::size_t vector_dimension = _vector_dimension

Dimension of the vector space.

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

Total number of tensor components.

Friends

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

Print operator overload.

Return

Output stream

Parameters
  • output: The output stream

  • [in] t: Tensor to print

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

Iterator over tensor indices.

Public Types

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

Public Functions

template<>
index_iterator(const index_iterator &iter)

Copy constructor.

Parameters
  • [in] iter: The iterator to copy from

template<>
index_iterator(const index_iterator &&iter)

Move constructor.

Parameters
  • [in] iter: The iterator to move from

template<>
index_iterator()

Constructor initialized at end of indices.

template<>
index_iterator(int)

Constructor initialized at start of indices.

template<>
index_iterator &operator++()

Prefix incrementation of the index.

Return

Iterator to the next index

template<>
index_iterator operator++(int)

Postfix incrementation of the index.

Return

Iterator to the next index

template<>
index_iterator &operator--()

Prefix decrementation of the index.

Return

Iterator to the previous index

template<>
index_iterator operator--(int)

Postfix decrementation of the index.

Return

Iterator to the previous index

template<>
bool operator==(index_iterator j) const

Equality operator for indices.

Return

True if indices are equal

Parameters
  • [in] j: Other index iterator to compare to

template<>
bool operator!=(index_iterator j) const

{ operator_description }

Return

{ description_of_the_return_value }

Parameters
  • [in] j: { parameter_description }

template<>
super::reference operator*()

Dereference the iterator.

Return

Index array of indices

template<>
super::pointer operator->()

Dereference the iterator.

Return

Index array of indices

Protected Attributes

template<>
IndexType i

Array of indices.