Stratax
Scientific computing containers and operations
Loading...
Searching...
No Matches
stratax::container::Tensor< T > Class Template Reference

#include <Tensor.hpp>

Public Types

using value_type = T
template<typename U>
using rebind = Tensor<U>

Public Member Functions

 Tensor () noexcept=default
 Creates an empty tensor with rank 0.
 Tensor (const core::Shape &shape)
 Creates a tensor with default-initialized storage for a shape.
 Tensor (const core::Shape &shape, const T &value)
 Creates a tensor from a shape and fills it with a value.
 Tensor (const Tensor &)=default
 Creates a copy of another tensor.
 Tensor (Tensor &&) noexcept=default
 Transfers ownership from another tensor.
Tensoroperator= (const Tensor &)=default
 Replaces this tensor with a copy of another tensor.
Tensoroperator= (Tensor &&) noexcept=default
 Replaces this tensor by taking ownership from another tensor.
 ~Tensor ()=default
 Destroys the tensor.
std::size_t size () const noexcept
 Returns the total number of elements in the tensor.
bool empty () const noexcept
 Returns whether the tensor contains no elements.
std::size_t rank () const noexcept
 Returns the tensor rank.
const core::Shapeshape () const noexcept
 Returns the tensor shape.
const core::Stridesstrides () const noexcept
 Returns the tensor strides.
T & operator() (std::size_t index) noexcept
 Returns a flat element without bounds checking.
const T & operator() (std::size_t index) const noexcept
 Returns a flat element without bounds checking.
T & operator[] (std::size_t index) noexcept
 Returns a flat element without bounds checking.
const T & operator[] (std::size_t index) const noexcept
 Returns a flat element without bounds checking.
template<typename... Rest>
T & operator() (std::size_t first, std::size_t second, Rest... rest)
 Returns an element by multidimensional index with bounds checking.
template<typename... Rest>
const T & operator() (std::size_t first, std::size_t second, Rest... rest) const
 Returns an element by multidimensional index with bounds checking.
T & at (std::size_t index)
 Returns an element by rank-1 flat index with bounds checking.
const T & at (std::size_t index) const
 Returns an element by rank-1 flat index with bounds checking.
template<typename... Rest>
T & at (std::size_t first, std::size_t second, Rest... rest)
 Returns an element by multi-index with bounds checking.
template<typename... Rest>
const T & at (std::size_t first, std::size_t second, Rest... rest) const
 Returns an element by multi-index with bounds checking.
T & front ()
 Returns the first element.
const T & front () const
 Returns the first element as a const reference.
T & back ()
 Returns the last element.
const T & back () const
 Returns the last element as a const reference.
T * data () noexcept
 Returns the raw data pointer.
const T * data () const noexcept
 Returns the raw data pointer as a const pointer.
auto begin () noexcept
 Returns an iterator to the first element.
auto begin () const noexcept
 Returns a const iterator to the first element.
auto cbegin () const noexcept
 Returns a const iterator to the first element.
auto end () noexcept
 Returns an iterator one past the last element.
auto end () const noexcept
 Returns a const iterator one past the last element.
auto cend () const noexcept
 Returns a const iterator one past the last element.
auto rbegin () noexcept
 Returns a reverse iterator to the last element.
auto rbegin () const noexcept
 Returns a const reverse iterator to the last element.
auto crbegin () const noexcept
 Returns a const reverse iterator to the last element.
auto rend () noexcept
 Returns a reverse iterator before the first element.
auto rend () const noexcept
 Returns a const reverse iterator before the first element.
auto crend () const noexcept
 Returns a const reverse iterator before the first element.
void fill (const T &value)
 Fills every element with the same value.
void swap (Tensor &other) noexcept
 Swaps the contents of two tensors.

Detailed Description

template<typename T>
requires Numeric<T>
class stratax::container::Tensor< T >

Definition at line 29 of file Tensor.hpp.

Member Typedef Documentation

◆ rebind

template<typename T>
template<typename U>
using stratax::container::Tensor< T >::rebind = Tensor<U>

Definition at line 40 of file Tensor.hpp.

◆ value_type

template<typename T>
using stratax::container::Tensor< T >::value_type = T

Definition at line 37 of file Tensor.hpp.

Constructor & Destructor Documentation

◆ Tensor() [1/5]

template<typename T>
stratax::container::Tensor< T >::Tensor ( )
defaultnoexcept

Creates an empty tensor with rank 0.

The tensor has no dimensions, no strides, and owns no elements.

◆ Tensor() [2/5]

template<typename T>
stratax::container::Tensor< T >::Tensor ( const core::Shape & shape)
inlineexplicit

Creates a tensor with default-initialized storage for a shape.

The tensor rank and strides are derived directly from shape.

Parameters
shapeSource shape.

Definition at line 58 of file Tensor.hpp.

◆ Tensor() [3/5]

template<typename T>
stratax::container::Tensor< T >::Tensor ( const core::Shape & shape,
const T & value )
inline

Creates a tensor from a shape and fills it with a value.

Parameters
shapeSource shape.
valueValue copied into every element.

Definition at line 72 of file Tensor.hpp.

◆ Tensor() [4/5]

template<typename T>
stratax::container::Tensor< T >::Tensor ( const Tensor< T > & )
default

Creates a copy of another tensor.

◆ Tensor() [5/5]

template<typename T>
stratax::container::Tensor< T >::Tensor ( Tensor< T > && )
defaultnoexcept

Transfers ownership from another tensor.

◆ ~Tensor()

template<typename T>
stratax::container::Tensor< T >::~Tensor ( )
default

Destroys the tensor.

Member Function Documentation

◆ at() [1/4]

template<typename T>
template<typename... Rest>
T & stratax::container::Tensor< T >::at ( std::size_t first,
std::size_t second,
Rest... rest )
inline

Returns an element by multi-index with bounds checking.

Parameters
firstFirst index component.
secondSecond index component.
restRemaining index components.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf the index rank or bounds are invalid.

Definition at line 300 of file Tensor.hpp.

◆ at() [2/4]

template<typename T>
template<typename... Rest>
const T & stratax::container::Tensor< T >::at ( std::size_t first,
std::size_t second,
Rest... rest ) const
inline

Returns an element by multi-index with bounds checking.

Parameters
firstFirst index component.
secondSecond index component.
restRemaining index components.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf the index rank or bounds are invalid.

Definition at line 334 of file Tensor.hpp.

◆ at() [3/4]

template<typename T>
T & stratax::container::Tensor< T >::at ( std::size_t index)
inline

Returns an element by rank-1 flat index with bounds checking.

Parameters
indexFlat element index.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf the index is out of bounds.

Definition at line 268 of file Tensor.hpp.

◆ at() [4/4]

template<typename T>
const T & stratax::container::Tensor< T >::at ( std::size_t index) const
inline

Returns an element by rank-1 flat index with bounds checking.

Parameters
indexFlat element index.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf the index is out of bounds.

Definition at line 282 of file Tensor.hpp.

◆ back() [1/2]

template<typename T>
T & stratax::container::Tensor< T >::back ( )
inline

Returns the last element.

Returns
Mutable reference to the last stored element.
Exceptions
Exceptions::IndexErrorIf the tensor is empty.

Definition at line 384 of file Tensor.hpp.

◆ back() [2/2]

template<typename T>
const T & stratax::container::Tensor< T >::back ( ) const
inline

Returns the last element as a const reference.

Returns
Const reference to the last stored element.
Exceptions
Exceptions::IndexErrorIf the tensor is empty.

Definition at line 395 of file Tensor.hpp.

◆ begin() [1/2]

template<typename T>
auto stratax::container::Tensor< T >::begin ( ) const
inlinenodiscardnoexcept

Returns a const iterator to the first element.

Returns
Const iterator to the beginning of the tensor.

Definition at line 435 of file Tensor.hpp.

◆ begin() [2/2]

template<typename T>
auto stratax::container::Tensor< T >::begin ( )
inlinenodiscardnoexcept

Returns an iterator to the first element.

Returns
Iterator to the beginning of the tensor.

Definition at line 425 of file Tensor.hpp.

◆ cbegin()

template<typename T>
auto stratax::container::Tensor< T >::cbegin ( ) const
inlinenodiscardnoexcept

Returns a const iterator to the first element.

Returns
Const iterator to the beginning of the tensor.

Definition at line 445 of file Tensor.hpp.

◆ cend()

template<typename T>
auto stratax::container::Tensor< T >::cend ( ) const
inlinenodiscardnoexcept

Returns a const iterator one past the last element.

Returns
Const iterator to the end of the tensor.

Definition at line 475 of file Tensor.hpp.

◆ crbegin()

template<typename T>
auto stratax::container::Tensor< T >::crbegin ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator to the last element.

Returns
Const reverse iterator starting at the last element.

Definition at line 505 of file Tensor.hpp.

◆ crend()

template<typename T>
auto stratax::container::Tensor< T >::crend ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator before the first element.

Returns
Const reverse iterator representing the end sentinel.

Definition at line 535 of file Tensor.hpp.

◆ data() [1/2]

template<typename T>
const T * stratax::container::Tensor< T >::data ( ) const
inlinenodiscardnoexcept

Returns the raw data pointer as a const pointer.

Returns
Pointer to the first stored element, or nullptr when empty.

Definition at line 415 of file Tensor.hpp.

◆ data() [2/2]

template<typename T>
T * stratax::container::Tensor< T >::data ( )
inlinenodiscardnoexcept

Returns the raw data pointer.

Returns
Pointer to the first stored element, or nullptr when empty.

Definition at line 405 of file Tensor.hpp.

◆ empty()

template<typename T>
bool stratax::container::Tensor< T >::empty ( ) const
inlinenodiscardnoexcept

Returns whether the tensor contains no elements.

Returns
true when the tensor size is zero.

Definition at line 124 of file Tensor.hpp.

Referenced by slice().

◆ end() [1/2]

template<typename T>
auto stratax::container::Tensor< T >::end ( ) const
inlinenodiscardnoexcept

Returns a const iterator one past the last element.

Returns
Const iterator to the end of the tensor.

Definition at line 465 of file Tensor.hpp.

◆ end() [2/2]

template<typename T>
auto stratax::container::Tensor< T >::end ( )
inlinenodiscardnoexcept

Returns an iterator one past the last element.

Returns
Iterator to the end of the tensor.

Definition at line 455 of file Tensor.hpp.

◆ fill()

template<typename T>
void stratax::container::Tensor< T >::fill ( const T & value)
inline

Fills every element with the same value.

Parameters
valueValue to assign to each element.

Definition at line 545 of file Tensor.hpp.

◆ front() [1/2]

template<typename T>
T & stratax::container::Tensor< T >::front ( )
inline

Returns the first element.

Returns
Mutable reference to the first stored element.
Exceptions
Exceptions::IndexErrorIf the tensor is empty.

Definition at line 362 of file Tensor.hpp.

◆ front() [2/2]

template<typename T>
const T & stratax::container::Tensor< T >::front ( ) const
inline

Returns the first element as a const reference.

Returns
Const reference to the first stored element.
Exceptions
Exceptions::IndexErrorIf the tensor is empty.

Definition at line 373 of file Tensor.hpp.

◆ operator()() [1/4]

template<typename T>
template<typename... Rest>
T & stratax::container::Tensor< T >::operator() ( std::size_t first,
std::size_t second,
Rest... rest )
inline

Returns an element by multidimensional index with bounds checking.

At least two index components are required; use the flat overload for rank-1 or flat storage access.

Parameters
firstFirst index component.
secondSecond index component.
restRemaining index components.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::DimensionErrorIf the number of indices does not match the tensor rank.
Exceptions::IndexErrorIf any index component is out of bounds or the offset overflows.

Definition at line 226 of file Tensor.hpp.

◆ operator()() [2/4]

template<typename T>
template<typename... Rest>
const T & stratax::container::Tensor< T >::operator() ( std::size_t first,
std::size_t second,
Rest... rest ) const
inline

Returns an element by multidimensional index with bounds checking.

Parameters
firstFirst index component.
secondSecond index component.
restRemaining index components.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::DimensionErrorIf the number of indices does not match the tensor rank.
Exceptions::IndexErrorIf any index component is out of bounds or the offset overflows.

Definition at line 249 of file Tensor.hpp.

◆ operator()() [3/4]

template<typename T>
const T & stratax::container::Tensor< T >::operator() ( std::size_t index) const
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Const reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 180 of file Tensor.hpp.

◆ operator()() [4/4]

template<typename T>
T & stratax::container::Tensor< T >::operator() ( std::size_t index)
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Mutable reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 167 of file Tensor.hpp.

◆ operator=() [1/2]

template<typename T>
Tensor & stratax::container::Tensor< T >::operator= ( const Tensor< T > & )
default

Replaces this tensor with a copy of another tensor.

◆ operator=() [2/2]

template<typename T>
Tensor & stratax::container::Tensor< T >::operator= ( Tensor< T > && )
defaultnoexcept

Replaces this tensor by taking ownership from another tensor.

◆ operator[]() [1/2]

template<typename T>
const T & stratax::container::Tensor< T >::operator[] ( std::size_t index) const
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Const reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 206 of file Tensor.hpp.

◆ operator[]() [2/2]

template<typename T>
T & stratax::container::Tensor< T >::operator[] ( std::size_t index)
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Mutable reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 193 of file Tensor.hpp.

◆ rank()

template<typename T>
std::size_t stratax::container::Tensor< T >::rank ( ) const
inlinenodiscardnoexcept

Returns the tensor rank.

Returns
Number of dimensions described by the tensor shape.

Definition at line 134 of file Tensor.hpp.

Referenced by slice().

◆ rbegin() [1/2]

template<typename T>
auto stratax::container::Tensor< T >::rbegin ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator to the last element.

Returns
Const reverse iterator starting at the last element.

Definition at line 495 of file Tensor.hpp.

◆ rbegin() [2/2]

template<typename T>
auto stratax::container::Tensor< T >::rbegin ( )
inlinenodiscardnoexcept

Returns a reverse iterator to the last element.

Returns
Reverse iterator starting at the last element.

Definition at line 485 of file Tensor.hpp.

◆ rend() [1/2]

template<typename T>
auto stratax::container::Tensor< T >::rend ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator before the first element.

Returns
Const reverse iterator representing the end sentinel.

Definition at line 525 of file Tensor.hpp.

◆ rend() [2/2]

template<typename T>
auto stratax::container::Tensor< T >::rend ( )
inlinenodiscardnoexcept

Returns a reverse iterator before the first element.

Returns
Reverse iterator representing the end sentinel.

Definition at line 515 of file Tensor.hpp.

◆ shape()

template<typename T>
const core::Shape & stratax::container::Tensor< T >::shape ( ) const
inlinenoexcept

Returns the tensor shape.

Returns
Shape describing the tensor.

Definition at line 144 of file Tensor.hpp.

Referenced by astype(), stratax::container::operator<<(), stratax::container::detail::print_tensor_recursive(), and slice().

◆ size()

template<typename T>
std::size_t stratax::container::Tensor< T >::size ( ) const
inlinenodiscardnoexcept

Returns the total number of elements in the tensor.

Returns
Number of stored elements.

Definition at line 114 of file Tensor.hpp.

Referenced by astype(), stratax::container::Tensor< U >::at(), stratax::container::Tensor< U >::at(), reshape(), and slice().

◆ strides()

template<typename T>
const core::Strides & stratax::container::Tensor< T >::strides ( ) const
inlinenoexcept

Returns the tensor strides.

Returns
Row-major strides describing the tensor layout.

Definition at line 154 of file Tensor.hpp.

Referenced by stratax::container::detail::print_tensor_recursive(), and slice().

◆ swap()

template<typename T>
void stratax::container::Tensor< T >::swap ( Tensor< T > & other)
inlinenoexcept

Swaps the contents of two tensors.

Parameters
otherTensor to exchange state with.

Definition at line 555 of file Tensor.hpp.


The documentation for this class was generated from the following file: