NumericAL
A header-only C++20 lexer built on REAL
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
numerical::tensor< T > Class Template Reference

A dense, row-major, N-dimensional tensor of T with value semantics. More...

#include <tensor.hpp>

Public Member Functions

 tensor (std::vector< std::size_t > shape, buffer< T > elements)
 From a shape and a flat row-major buffer.
 
 tensor (std::vector< std::size_t > shape, const std::vector< T > &elements)
 From a shape and a flat row-major std::vector, copied into the storage (a buffer moves in instead).
 
 tensor (std::vector< std::size_t > shape, std::initializer_list< T > elements)
 From a shape and a braced list of the elements, row-major.
 
 tensor (const vector< T > &v)
 A rank-1 tensor from a vector.
 
 tensor (matrix< T > &&m)
 A rank-2 tensor taking over an expiring matrix's elements, without a copy.
 
 tensor (const matrix< T > &m)
 A rank-2 tensor from a matrix.
 
std::size_t rank () const noexcept
 The number of axes (0 for a scalar).
 
std::size_t size () const noexcept
 The total number of elements.
 
const std::vector< std::size_t > & shape () const noexcept
 The size of each axis.
 
const buffer< T > & data () const noexcept
 The elements, row-major.
 
const T & operator() (const std::vector< std::size_t > &index) const
 The element at a multi-index.
 
bool operator== (const tensor &other) const
 Exact equality (same shape and equal elements).
 

Private Member Functions

void finalize ()
 Computes row-major strides and checks the buffer against the shape.
 

Private Attributes

std::vector< std::size_t > shape_
 The size of each axis.
 
std::vector< std::size_t > strides_
 Row-major strides.
 
buffer< T > data_
 The elements, row-major.
 

Detailed Description

template<typename T>
class numerical::tensor< T >

A dense, row-major, N-dimensional tensor of T with value semantics.

Template Parameters
TThe scalar element type.

Definition at line 62 of file tensor.hpp.

Constructor & Destructor Documentation

◆ tensor() [1/6]

template<typename T >
numerical::tensor< T >::tensor ( std::vector< std::size_t >  shape,
buffer< T >  elements 
)
inline

From a shape and a flat row-major buffer.

Parameters
[in]shapeThe size of each axis (empty ⇒ a rank-0 scalar).
[in]elementsThe prod(shape) elements, row-major (moved in).
Exceptions
dimension_errorIf elements.size() is not prod(shape).

Definition at line 72 of file tensor.hpp.

◆ tensor() [2/6]

template<typename T >
numerical::tensor< T >::tensor ( std::vector< std::size_t >  shape,
const std::vector< T > &  elements 
)
inline

From a shape and a flat row-major std::vector, copied into the storage (a buffer moves in instead).

Parameters
[in]shapeThe size of each axis.
[in]elementsThe prod(shape) elements, row-major.
Exceptions
dimension_errorIf elements.size() is not prod(shape).

Definition at line 85 of file tensor.hpp.

◆ tensor() [3/6]

template<typename T >
numerical::tensor< T >::tensor ( std::vector< std::size_t >  shape,
std::initializer_list< T >  elements 
)
inline

From a shape and a braced list of the elements, row-major.

Definition at line 92 of file tensor.hpp.

◆ tensor() [4/6]

template<typename T >
numerical::tensor< T >::tensor ( const vector< T > &  v)
inlineexplicit

A rank-1 tensor from a vector.

Parameters
[in]vThe vector.

Definition at line 99 of file tensor.hpp.

◆ tensor() [5/6]

template<typename T >
numerical::tensor< T >::tensor ( matrix< T > &&  m)
inlineexplicit

A rank-2 tensor taking over an expiring matrix's elements, without a copy.

Parameters
[in]mThe matrix.

Definition at line 110 of file tensor.hpp.

◆ tensor() [6/6]

template<typename T >
numerical::tensor< T >::tensor ( const matrix< T > &  m)
inlineexplicit

A rank-2 tensor from a matrix.

Parameters
[in]mThe matrix.

Definition at line 117 of file tensor.hpp.

Member Function Documentation

◆ data()

template<typename T >
const buffer< T > & numerical::tensor< T >::data ( ) const
inlinenoexcept

The elements, row-major.

Definition at line 142 of file tensor.hpp.

◆ finalize()

template<typename T >
void numerical::tensor< T >::finalize ( )
inlineprivate

Computes row-major strides and checks the buffer against the shape.

Definition at line 166 of file tensor.hpp.

◆ operator()()

template<typename T >
const T & numerical::tensor< T >::operator() ( const std::vector< std::size_t > &  index) const
inline

The element at a multi-index.

Precondition
Its length is rank, in bounds.

Definition at line 148 of file tensor.hpp.

◆ operator==()

template<typename T >
bool numerical::tensor< T >::operator== ( const tensor< T > &  other) const
inline

Exact equality (same shape and equal elements).

Definition at line 158 of file tensor.hpp.

◆ rank()

template<typename T >
std::size_t numerical::tensor< T >::rank ( ) const
inlinenoexcept

The number of axes (0 for a scalar).

Definition at line 124 of file tensor.hpp.

◆ shape()

template<typename T >
const std::vector< std::size_t > & numerical::tensor< T >::shape ( ) const
inlinenoexcept

The size of each axis.

Definition at line 136 of file tensor.hpp.

◆ size()

template<typename T >
std::size_t numerical::tensor< T >::size ( ) const
inlinenoexcept

The total number of elements.

Definition at line 130 of file tensor.hpp.

Member Data Documentation

◆ data_

template<typename T >
buffer<T> numerical::tensor< T >::data_
private

The elements, row-major.

Definition at line 181 of file tensor.hpp.

◆ shape_

template<typename T >
std::vector<std::size_t> numerical::tensor< T >::shape_
private

The size of each axis.

Definition at line 179 of file tensor.hpp.

◆ strides_

template<typename T >
std::vector<std::size_t> numerical::tensor< T >::strides_
private

Row-major strides.

Definition at line 180 of file tensor.hpp.


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