Tannic
A C++ Tensor Library
Loading...
Searching...
No Matches
tannic::expression::Expansion< Source > Class Template Reference

Expression template for expanding (broadcasting) singleton dimensions of a tensor. More...

#include <views.hpp>

Public Member Functions

template<Integral... Sizes>
constexpr Expansion (typename Trait< Source >::Reference source, Sizes... sizes)
 Construct an expansion view.
 
constexpr type dtype () const
 
constexpr Shape const & shape () const
 
constexpr Strides const & strides () const
 
std::ptrdiff_t offset () const
 
Tensor forward () const
 

Detailed Description

template<Expression Source>
class tannic::expression::Expansion< Source >

Expression template for expanding (broadcasting) singleton dimensions of a tensor.

Template Parameters
SourceThe expression or tensor type being expanded.

The Expansion view allows a tensor to be broadcast along dimensions where the original size is 1, enabling operations with larger tensors without copying data. Only dimensions with size 1 in the source tensor can be expanded; other dimensions must match the target shape.

Example:

Tensor X(float32, {1, 3});
auto Y = expand(X, 4, 3); // shape becomes (4, 3)
A multidimensional, strided tensor data structure.
Definition: tensor.hpp:105
constexpr auto expand(Source &&source, Sizes... sizes)
Creates an expanded view of a tensor, broadcasting singleton dimensions.
Definition: views.hpp:915

Constructor & Destructor Documentation

◆ Expansion()

template<Expression Source>
template<Integral... Sizes>
constexpr tannic::expression::Expansion< Source >::Expansion ( typename Trait< Source >::Reference  source,
Sizes...  sizes 
)
inlineconstexpr

Construct an expansion view.

Stores reference to the source tensor and the requested target sizes.

Template Parameters
SizesIntegral dimension sizes of the target shape.
Parameters
sourceReference to the source tensor or expression.
sizesDimension sizes for the expanded view.

Member Function Documentation

◆ dtype()

template<Expression Source>
constexpr type tannic::expression::Expansion< Source >::dtype ( ) const
inlineconstexpr
Returns
The data type of the tensor elements.

Broadcasting does not change the element type, so the dtype is forwarded from the source tensor.

◆ forward()

template<Expression Source>
Tensor tannic::expression::Expansion< Source >::forward

◆ offset()

template<Expression Source>
std::ptrdiff_t tannic::expression::Expansion< Source >::offset ( ) const
inline
Returns
The byte offset of the expanded view from the start of the storage.

The offset is the same as the source tensor, because broadcasting does not change the underlying data location.

◆ shape()

template<Expression Source>
constexpr Shape const & tannic::expression::Expansion< Source >::shape ( ) const
inlineconstexpr
Returns
The shape of the expanded view.

Calculation:

  • Returns the requested target shape stored in the constructor.
  • Validates that non-singleton dimensions of the source match the requested size.
Exceptions
Exceptionif a non-singleton dimension in the source does not match the target.

◆ strides()

template<Expression Source>
constexpr Strides const & tannic::expression::Expansion< Source >::strides ( ) const
inlineconstexpr
Returns
The strides of the expanded view.

Calculation:

  • Copy the source strides.
  • For each dimension where the source size is 1 and target size > 1, set stride to 0. This ensures the same memory element is repeated along expanded dimensions.

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