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

Expression template for viewing a tensor with a new shape. More...

#include <views.hpp>

Public Member Functions

template<Integral... Sizes>
constexpr View (typename Trait< Source >::Reference source, Sizes... sizes)
 Construct a reshaped view of the source tensor.
 
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::View< Source >

Expression template for viewing a tensor with a new shape.

Template Parameters
SourceThe expression or tensor type being reshaped.

The View view changes how the elements of a tensor are indexed by updating its shape and recomputing its strides without moving data.

This operation requires that the total number of elements in the new shape matches the original tensor's total number of elements.

Example:

Tensor X(float32, {2, 3});
auto Y = view(X, 3, 2); // new shape: (3, 2)
A multidimensional, strided tensor data structure.
Definition: tensor.hpp:105
constexpr auto view(Source &&source, Indexes ... indexes)
Creates a reshaped view of a tensor or expression.
Definition: views.hpp:840

Constructor & Destructor Documentation

◆ View()

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

Construct a reshaped view of the source tensor.

Template Parameters
IndexesIntegral dimension sizes of the new shape.
Parameters
sourceReference to the source expression or tensor.
sizesDimension sizes for the reshaped view.
Exceptions
Assertionfailure if the number of elements in the new shape does not match the number of elements in the source.

Member Function Documentation

◆ dtype()

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

This simply forwards the call to the underlying source expression’s dtype. Since a view (reshape or transpose) does not alter the actual stored values.

◆ forward()

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

◆ offset()

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

This value is forwarded from the source expression’s offset() method. The offset represents the memory position (in bytes) where the first element of the view begins relative to the start of the underlying storage buffer. Since reshape do not move elements in memory, the offset is unchanged from the source tensor.

◆ shape()

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

In a reshape, this value is explicitly provided in the constructor through indexes... and stored in shape_.

◆ strides()

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

For a reshape, strides are recomputed from the new shape_ so that the element layout in memory matches the original data ordering.


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