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

Expression template for flattening a contiguous range of dimensions. More...

#include <views.hpp>

Public Member Functions

constexpr Flatten (typename Trait< Source >::Reference source, int start=0, int end=-1)
 
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::Flatten< Source >

Expression template for flattening a contiguous range of dimensions.

Template Parameters
SourceThe expression or tensor type being flattened.

The Flatten view collapses dimensions between start_dim and end_dim into a single dimension. This operation only modifies tensor metadata (shape and strides) and does not move data.

Example:

Tensor X(float32, {2, 3, 4});
auto Y = flatten(X, 1, -1); // shape becomes (2, 12)
auto Z = flatten(X); // shape becomes (24)
A multidimensional, strided tensor data structure.
Definition: tensor.hpp:105
constexpr auto flatten(Source &&source, int start=0, int end=-1)
Flattens dimensions of a tensor into a single dimension.
Definition: views.hpp:984

Constructor & Destructor Documentation

◆ Flatten()

template<Expression Source>
constexpr tannic::expression::Flatten< Source >::Flatten ( typename Trait< Source >::Reference  source,
int  start = 0,
int  end = -1 
)
inlineconstexpr

Member Function Documentation

◆ dtype()

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

This is forwarded directly from the source expression. Flatten does not alter the tensor’s dtype.

◆ forward()

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

◆ offset()

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

This is forwarded from the source tensor since flatten does not change the starting position of the data.

◆ shape()

template<Expression Source>
constexpr Shape const & tannic::expression::Flatten< Source >::shape ( ) const
inlineconstexpr
Returns
The shape of the flattened tensor.

Calculation:

  • Dimensions before start are copied unchanged.
  • Dimensions between start and end (inclusive) are collapsed into a single dimension equal to the product of their sizes.
  • Dimensions after end are copied unchanged.

Example:

  • Source shape: (2, 3, 4), start_dim = 1, end_dim = -1
  • Flattened shape: (2, 12)

◆ strides()

template<Expression Source>
constexpr Strides const & tannic::expression::Flatten< Source >::strides ( ) const
inlineconstexpr
Returns
The strides of the flattened tensor.

Calculation:

  • Strides before start are copied unchanged.
  • The collapsed dimension is assigned the stride of the last flattened dimension (end).
    This ensures contiguous indexing across the merged block.
  • Strides after end_dim are copied unchanged.

Example:

  • Source strides: (12, 4, 1) with shape (2, 3, 4)
  • Flattened (start=1, end=-1) → strides: (12, 1) with shape (2, 12)

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