Tannic
A C++ Tensor Library
Loading...
Searching...
No Matches
tannic::expression Namespace Reference

Classes

struct  Argmax
 Finds the indices of maximum values along an axis. More...
 
struct  Argmean
 Computes the mean along an axis. More...
 
struct  Argmin
 Finds the indexes of minimum values along an axis. More...
 
struct  Argsum
 Sums tensor values along an axis. More...
 
struct  Cartesian
 Tag type for Cartesian (real/imaginary) complex number representation. More...
 
class  Comparison
 
class  Complexification
 Creates a complex tensor view from real components. More...
 
class  Complexification< Coordinates, Real, Imaginary >
 
class  Complexification< Coordinates, Source >
 
struct  EQ
 
class  Expansion
 Expression template for expanding (broadcasting) singleton dimensions of a tensor. More...
 
class  Flatten
 Expression template for flattening a contiguous range of dimensions. More...
 
struct  GE
 
struct  GT
 
struct  LE
 
struct  LT
 
struct  NE
 
class  Permutation
 Expression template for reordering tensor dimensions according to a specified permutation. More...
 
struct  Polar
 Tag type for Polar (magnitude/angle) complex number representation. More...
 
class  Realification
 Creates a real-valued view of complex tensor data. More...
 
class  Reduction
 Lazy reduction expression. More...
 
class  Slice
 Expression template representing a tensor slice or subview. More...
 
class  Squeeze
 Expression template for removing singleton dimensions from a tensor. More...
 
class  Transpose
 Expression template for transposing two dimensions of a tensor. More...
 
class  Unsqueeze
 Expression template for inserting singleton dimensions into a tensor. More...
 
class  View
 Expression template for viewing a tensor with a new shape. More...
 

Functions

template<Expression First, Expression Second>
constexpr auto operator== (First &&lhs, Second &&rhs)
 
template<Expression First, Expression Second>
constexpr auto operator!= (First &&lhs, Second &&rhs)
 
template<Expression First, Expression Second>
constexpr auto operator< (First &&lhs, Second &&rhs)
 
template<Expression First, Expression Second>
constexpr auto operator<= (First &&lhs, Second &&rhs)
 
template<Expression First, Expression Second>
constexpr auto operator> (First &&lhs, Second &&rhs)
 
template<Expression First, Expression Second>
constexpr auto operator>= (First &&lhs, Second &&rhs)
 
template<Expression Real>
constexpr auto complexify (Real &&real)
 Creates a complex tensor view from interleaved real/imaginary data.
 
template<Expression Real, Expression Imaginary>
constexpr auto complex (Real &&real, Imaginary &&imaginary)
 Creates complex tensor from separate real and imaginary tensors

 
template<Expression Magnitude, Expression Angle>
constexpr auto polar (Magnitude &&rho, Angle &&theta)
 Creates complex tensor from polar coordinates (magnitude/angle)
 
template<Expression Complex>
constexpr auto realify (Complex &&complex)
 Creates a real-valued view of complex tensor data.
 
template<Expression Source>
constexpr auto argmax (Source &&source, int axis=-1, bool keepdim=false)
 Creates an Argmax reduction.
 
template<Expression Source>
constexpr auto argmin (Source &&source, int axis=-1, bool keepdim=false)
 Creates an Argmin reduction.
 
template<Expression Source>
constexpr auto sum (Source &&source, int axis=-1, bool keepdim=false)
 Creates a sum reduction.
 
template<Expression Source>
constexpr auto mean (Source &&source, int axis=-1, bool keepdim=false)
 Creates a mean reduction.
 
template<typename T >
std::byte const * tobytes (T const &reference)
 
template<Expression Source, Integral ... Indexes>
constexpr auto view (Source &&source, Indexes ... indexes)
 Creates a reshaped view of a tensor or expression.
 
template<Expression Source>
constexpr auto transpose (Source &&source, int first, int second)
 Creates a transposed view of a tensor or expression by swapping two dimensions.
 
template<Expression Source, Integral ... Indexes>
constexpr auto permute (Source &&source, Indexes... indexes)
 Creates a permuted view of a tensor or expression.
 
template<Expression Source, Integral... Sizes>
constexpr auto expand (Source &&source, Sizes... sizes)
 Creates an expanded view of a tensor, broadcasting singleton dimensions.
 
template<Expression Source>
constexpr auto squeeze (Source &&source)
 Removes all singleton dimensions from a tensor (squeeze).
 
template<Expression Source, Integral... Axes>
constexpr auto unsqueeze (Source &&source, Axes... axes)
 Inserts singleton dimensions at the specified axes (unsqueeze).
 
template<Expression Source>
constexpr auto flatten (Source &&source, int start=0, int end=-1)
 Flattens dimensions of a tensor into a single dimension.
 

Function Documentation

◆ argmax()

template<Expression Source>
constexpr auto tannic::expression::argmax ( Source &&  source,
int  axis = -1,
bool  keepdim = false 
)
constexpr

Creates an Argmax reduction.

Parameters
axisAxis to reduce (-1 for last axis).
keepdimIf true, keeps reduced axis as size 1.

Example:

Tensor X = {{3, 1, 4},
{1, 5, 9}}; // shape(2, 3)
Tensor Y = argmax(X, 0); // Reduce axis 0 (rows)
std::cout << Y << std::endl;
// Y = [0, 1, 1] // Indexes of max values per column
Tensor Z = argmax(X, 1, /*keepdim=* /true); // Reduce axis 1 (columns), keep dims
std::cout << Z << std::endl;
// Z = [[2],
// [2]] // Indexes of max values per row
A multidimensional, strided tensor data structure.
Definition: tensor.hpp:105
constexpr auto argmax(Source &&source, int axis=-1, bool keepdim=false)
Creates an Argmax reduction.
Definition: reductions.hpp:302

◆ argmin()

template<Expression Source>
constexpr auto tannic::expression::argmin ( Source &&  source,
int  axis = -1,
bool  keepdim = false 
)
constexpr

Creates an Argmin reduction.

Parameters
axisAxis to reduce (-1 for last axis).
keepdimIf true, keeps reduced axis as size 1.

Example:

Tensor X = {{3, 1, 4},
{1, 5, 9}};
Tensor Y = argmin(X, 1);
// Y = [1, 0] // Min indexes per row
constexpr auto argmin(Source &&source, int axis=-1, bool keepdim=false)
Creates an Argmin reduction.
Definition: reductions.hpp:324

◆ complex()

template<Expression Real, Expression Imaginary>
constexpr auto tannic::expression::complex ( Real &&  real,
Imaginary &&  imaginary 
)
constexpr

Creates complex tensor from separate real and imaginary tensors

Parameters
realTensor containing real components
imagTensor containing imaginary components
Returns
Complex tensor combining both components

Requirements:

  • Both tensors must have identical shapes and strides
  • Dtypes must match (float32→complex64, float64→complex128)

Example:

Tensor r = {1.0, 2.0}; // real parts
Tensor i = {3.0, 4.0}; // imag parts
Tensor c = complex(r, i);
// c = [1+3i, 2+4i]

◆ complexify()

template<Expression Real>
constexpr auto tannic::expression::complexify ( Real &&  real)
constexpr

Creates a complex tensor view from interleaved real/imaginary data.

Parameters
realTensor with shape [...,2N] containing N real/imaginary pairs
Returns
Complex tensor view with shape [...,N] and dtype complex64/128

Requirements:

  • Last dimension must be even-sized (pairs of values)
  • Elements must be contiguous (stride 1 in last dimension)
  • Dtype must be float32 or float64

Example:

// float32 input with shape [4]
Tensor real = {1.0f, 2.0f, 3.0f, 4.0f};
// complex64 output with shape [2]
Tensor cplx = complexify(real);
// cplx = [1+2i, 3+4i]

◆ expand()

template<Expression Source, Integral... Sizes>
constexpr auto tannic::expression::expand ( Source &&  source,
Sizes...  sizes 
)
constexpr

Creates an expanded view of a tensor, broadcasting singleton dimensions.

This function returns an Expansion expression that allows a tensor to be “expanded” along dimensions of size 1 without copying data. Expansion is only allowed along singleton dimensions; other dimensions must match the requested size.

Template Parameters
SourceThe tensor or expression type to expand.
SizesIntegral dimension sizes for the expanded view.
Parameters
sourceThe source tensor or expression.
sizesThe target shape for the expanded view.
Returns
An Expansion object representing the broadcasted view.
Exceptions
Exceptionif:
  • The number of dimensions does not match the source rank.
  • A non-singleton dimension in the source does not match the requested size.

Example usage:

Tensor X(float32, {1, 3}); // shape: (1, 3)
auto Y = expand(X, 4, 3); // shape: (4, 3), broadcasts along the first dimension
std::cout << Y.shape() << std::endl; // prints (4, 3)
std::cout << Y.strides() << std::endl; // prints (0, original_stride[1])

◆ flatten()

template<Expression Source>
constexpr auto tannic::expression::flatten ( Source &&  source,
int  start = 0,
int  end = -1 
)
constexpr

Flattens dimensions of a tensor into a single dimension.

Template Parameters
SourceThe expression or tensor type.
Parameters
sourceThe source tensor or expression.
start_dimFirst dimension to flatten (default = 0).
end_dimLast dimension to flatten (default = -1, meaning last dim).
Returns
A Flatten view expression.

Example:

Tensor X(float32, {2, 3, 4});
auto Y = flatten(X, 1, -1); // shape: (2, 12)
auto Z = flatten(X); // shape: (24)

◆ mean()

template<Expression Source>
constexpr auto tannic::expression::mean ( Source &&  source,
int  axis = -1,
bool  keepdim = false 
)
constexpr

Creates a mean reduction.

Parameters
axisAxis to reduce (-1 for last axis).
keepdimIf true, keeps reduced axis as size 1.

Example:

Tensor X = {{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0}}; // shape(2, 3)
Tensor Y = mean(X, 0); // Reduce axis 0 (rows)
std::cout << Y << std::endl;
// Y = [2.5, 3.5, 4.5] // Mean of values per column
Tensor Z = mean(X, 1, /*keepdim=* /true); // Reduce axis 1 (columns), keep dims
std::cout << Z << std::endl;
// Z = [[2.0],
// [5.0]] // Mean of values per row
constexpr auto mean(Source &&source, int axis=-1, bool keepdim=false)
Creates a mean reduction.
Definition: reductions.hpp:379

◆ operator!=()

template<Expression First, Expression Second>
constexpr auto tannic::expression::operator!= ( First &&  lhs,
Second &&  rhs 
)
constexpr

◆ operator<()

template<Expression First, Expression Second>
constexpr auto tannic::expression::operator< ( First &&  lhs,
Second &&  rhs 
)
constexpr

◆ operator<=()

template<Expression First, Expression Second>
constexpr auto tannic::expression::operator<= ( First &&  lhs,
Second &&  rhs 
)
constexpr

◆ operator==()

template<Expression First, Expression Second>
constexpr auto tannic::expression::operator== ( First &&  lhs,
Second &&  rhs 
)
constexpr

◆ operator>()

template<Expression First, Expression Second>
constexpr auto tannic::expression::operator> ( First &&  lhs,
Second &&  rhs 
)
constexpr

◆ operator>=()

template<Expression First, Expression Second>
constexpr auto tannic::expression::operator>= ( First &&  lhs,
Second &&  rhs 
)
constexpr

◆ permute()

template<Expression Source, Integral ... Indexes>
constexpr auto tannic::expression::permute ( Source &&  source,
Indexes...  indexes 
)
constexpr

Creates a permuted view of a tensor or expression.

Template Parameters
SourceThe expression or tensor type.
IndexesIntegral indices specifying the permutation order.
Parameters
sourceThe source expression.
indexesSequence of dimension indices indicating the new axis order.
Returns
A Permutation view expression.

Example:

Tensor X(float32, {2, 3, 4});
auto Y = permute(X, 2, 0, 1); // shape becomes (4, 2, 3)

◆ polar()

template<Expression Magnitude, Expression Angle>
constexpr auto tannic::expression::polar ( Magnitude &&  rho,
Angle &&  theta 
)
constexpr

Creates complex tensor from polar coordinates (magnitude/angle)

Parameters
rhoTensor containing magnitudes
thetaTensor containing angles in radians
Returns
Complex tensor in Cartesian form

Requirements:

  • Both tensors must have identical shapes
  • Angles must be in radians

Example:

Tensor mag = {1.0, 2.0}; // magnitudes
Tensor ang = {0.0, M_PI/2}; // angles
Tensor c = polar(mag, ang);
// c = [1+0i, 0+2i] // cos(0)=1, sin(π/2)=1

◆ realify()

template<Expression Complex>
constexpr auto tannic::expression::realify ( Complex &&  complex)
constexpr

Creates a real-valued view of complex tensor data.

Parameters
complexComplex tensor to reinterpret
Returns
Real tensor view exposing [real, imag] components

Transformation Rules:

  • Dtype: complex64 → float32, complex128 → float64
  • Shape: [...,N] → [...,N,2] (adds dimension for components)
  • Memory: Maintains same storage with adjusted strides

Requirements:

  • Input must be complex64 or complex128
  • Must have stride 1 in last dimension (contiguous complex pairs)

Example:

// complex64 input with shape [2]
Tensor cplx = {1+2i, 3+4i};
// float32 output with shape [2,2]
Tensor real_view = realify(cplx);
// real_view = [[1, 2], // real, imag components
// [3, 4]]

◆ squeeze()

template<Expression Source>
constexpr auto tannic::expression::squeeze ( Source &&  source)
constexpr

Removes all singleton dimensions from a tensor (squeeze).

This function returns a Squeeze expression that reinterprets the source tensor without its size-1 dimensions.

Template Parameters
SourceThe expression or tensor type.
Parameters
sourceThe source tensor or expression.
Returns
A Squeeze view expression.

Example:

Tensor X(float32, {1, 3, 1});
auto Y = squeeze(X); // shape: (3)

◆ sum()

template<Expression Source>
constexpr auto tannic::expression::sum ( Source &&  source,
int  axis = -1,
bool  keepdim = false 
)
constexpr

Creates a sum reduction.

Parameters
axisAxis to reduce (-1 for last axis).
keepdimIf true, keeps reduced axis as size 1.

Example:

Tensor X = {{1, 2, 3},
{4, 5, 6}}; // shape(2, 3)
Tensor Y = sum(X, 0); // Reduce axis 0 (rows)
std::cout << Y << std::endl;
// Y = [5, 7, 9] // Sum of values per column
Tensor Z = sum(X, 1, /*keepdim=* /true); // Reduce axis 1 (columns), keep dims
std::cout << Z << std::endl;
// Z = [[6],
// [15]] // Sum of values per row
constexpr auto sum(Source &&source, int axis=-1, bool keepdim=false)
Creates a sum reduction.
Definition: reductions.hpp:351

◆ tobytes()

template<typename T >
std::byte const * tannic::expression::tobytes ( T const &  reference)
inline

◆ transpose()

template<Expression Source>
constexpr auto tannic::expression::transpose ( Source &&  source,
int  first,
int  second 
)
constexpr

Creates a transposed view of a tensor or expression by swapping two dimensions.

Template Parameters
SourceThe expression or tensor type.
Parameters
sourceThe source expression.
firstFirst dimension index to swap.
secondSecond dimension index to swap.
Returns
A Transpose view expression.

◆ unsqueeze()

template<Expression Source, Integral... Axes>
constexpr auto tannic::expression::unsqueeze ( Source &&  source,
Axes...  axes 
)
constexpr

Inserts singleton dimensions at the specified axes (unsqueeze).

This function returns an Unsqueeze expression that reinterprets the source tensor with new dimensions of size 1 added.

Template Parameters
SourceThe expression or tensor type.
AxesOne or more integral indices where new dimensions will be inserted.
Parameters
sourceThe source tensor or expression.
axesAxis indices (negative indices allowed).
Returns
An Unsqueeze view expression.

Example:

Tensor X(float32, {3});
auto Y = unsqueeze(X, 0); // shape: (1, 3)
auto Z = unsqueeze(X, -1); // shape: (3, 1)

◆ view()

template<Expression Source, Integral ... Indexes>
constexpr auto tannic::expression::view ( Source &&  source,
Indexes ...  indexes 
)
constexpr

Creates a reshaped view of a tensor or expression.

Template Parameters
SourceThe expression or tensor type.
IndexesNew shape dimensions (integral values).
Parameters
sourceThe source expression.
indexesDimension sizes for the new shape.
Returns
A View view expression.