Txeo v0.1
A Developer-Friendly TensorFlow C++ Wrapper
Loading...
Searching...
No Matches
txeo::Tensor< T > Class Template Reference

Implements the mathematical concept of tensor, which is a magnitude of multiple order. A tensor of order zero is defined to be a scalar, of order one a vector, of order two a matrix and so on. Each order of the tensor has a dimension. Elements are addressed via multidimensional indexing. More...

#include <Tensor.h>

Inheritance diagram for txeo::Tensor< T >:
Inheritance graph
Collaboration diagram for txeo::Tensor< T >:
Collaboration graph

Public Member Functions

 Tensor (const Tensor &tensor)
 
 Tensor (Tensor &&tensor) noexcept
 
 ~Tensor ()
 
Tensoroperator= (const Tensor &tensor)
 
Tensoroperator= (Tensor &&tensor) noexcept
 
bool operator== (const Tensor &tensor)
 
bool operator!= (const Tensor &tensor)
 
 Tensor (const txeo::TensorShape &shape)
 Constructs a tensor from a specified txeo::TensorShape.
 
 Tensor (txeo::TensorShape &&shape)
 Constructs a Tensor from a specified txeo::TensorShape.
 
 Tensor (const std::vector< size_t > &shape)
 Constructs a Tensor from a specified shape std::vector.
 
 Tensor (std::vector< size_t > &&shape)
 Constructs a Tensor from a specified shape std::vector.
 
 Tensor (const std::initializer_list< size_t > &shape)
 Constructs a Tensor from a specified shape std::vector.
 
 Tensor (const txeo::TensorShape &shape, const T &fill_value)
 Constructs a Tensor from a specified txeo::TensorShape and fills it with a value.
 
 Tensor (txeo::TensorShape &&shape, const T &fill_value)
 Constructs a Tensor from a specified txeo::TensorShape and fills it with a value.
 
 Tensor (const std::vector< size_t > &shape, const T &fill_value)
 Constructs a Tensor from a specified shape std::vector and fills it with a value.
 
 Tensor (std::vector< size_t > &&shape, const T &fill_value)
 Constructs a Tensor from a specified shape std::vector and fills it with a value.
 
 Tensor (const std::initializer_list< size_t > &shape, const T &fill_value)
 Constructs a Tensor from a specified initializer list and fills it with a value.
 
 Tensor (const txeo::TensorShape &shape, const std::vector< T > &values)
 Constructs a Tensor object from a specified txeo::TensorShape and fills it with a std::vector of values in a row-major scheme.
 
 Tensor (const std::vector< size_t > &shape, const std::vector< T > &values)
 Constructs a Tensor object from a specified std::vector<size_t> and fills it with a std::vector of values in a row-major scheme.
 
 Tensor (const std::initializer_list< size_t > &shape, const std::vector< T > &values)
 Constructs a Tensor object from a specified initializer list and fills it with a std::vector of values in a row-major scheme.
 
 Tensor (const std::initializer_list< std::initializer_list< T > > &values)
 Constructs a second order Tensor from a nested std::initializer_list.
 
 Tensor (const std::initializer_list< std::initializer_list< std::initializer_list< T > > > &values)
 Constructs a third order Tensor from a nested std::initializer_list.
 
const txeo::TensorShapeshape () const
 Returns the shape of this tensor.
 
constexpr std::type_identity_t< T > type () const
 Returns the data type of this tensor.
 
int order () const
 Returns the order of this tensor.
 
size_t dim () const
 Returns the dimension of this tensor.
 
size_t number_of_elements () const
 Returns the number of elements of this tensor, which corresponds to the dimension of this tensor.
 
size_t memory_size () const
 Returns the number of bytes occupied by this tensor.
 
Tensor< T > slice (size_t first_axis_begin, size_t first_axis_end) const
 Returns a view ot this tensor from a specified range of dimensions of the first axis.
 
void view_of (const Tensor< T > &tensor, const txeo::TensorShape &shape)
 Views the content of the specified tensor according to the specified shape. There is no element copying.
 
template<typename U >
bool is_equal_shape (const Tensor< U > &other) const
 Compares the shape of this tensor with the shape of the specified tensor.
 
T & operator() ()
 Accesses the value of this tensor if it is a scalar (order zero).
 
template<typename... Args>
requires (std::convertible_to<Args, size_t> && ...)
T & operator() (Args... args)
 Accesses an element of this tensor according to the specified indexes.
 
T & at ()
 Accesses the value of this tensor if it is a scalar (order zero).
 
template<typename... Args>
requires (std::convertible_to<Args, size_t> && ...)
T & at (Args... args)
 Accesses an element of this tensor according to the specified indexes.
 
const T & operator() () const
 Reads the value of this tensor if it is a scalar (order zero).
 
template<typename... Args>
requires (std::convertible_to<Args, size_t> && ...)
const T & operator() (Args... args) const
 Reads an element of this tensor according to the specified indexes.
 
const T & at () const
 Reads the value of this tensor if it is a scalar (order zero). Order checking is performed.
 
template<typename... Args>
requires (std::convertible_to<Args, size_t> && ...)
const T & at (Args... args) const
 Reads an element of this tensor according to the specified indexes.
 
void reshape (const txeo::TensorShape &shape)
 Reshapes this tensor if the specified shape defines a number of elements equal to this tensor order.
 
void reshape (const std::vector< size_t > &shape)
 Reshapes this tensor if the specified shape vector defines a number of elements equal to this tensor order.
 
void reshape (const std::initializer_list< size_t > &shape)
 Reshapes this tensor if the specified shape vector defines a number of elements equal to this tensor order.
 
Tensor< T > flatten () const
 Returns a first order reshaped view of this tensor.
 
void fill (const T &value)
 Fills this tensor with the specified value.
 
void fill_with_uniform_random (const T &min, const T &max)
 Fills the tensor with uniformly distributed random values ranging according to the specified interval.
 
void fill_with_uniform_random (const T &min, const T &max, const size_t &seed1, const size_t &seed2)
 Fills the tensor with uniformly distributed random values ranging according to the specified interval.
 
void shuffle ()
 Shuffles the elements of this tensor.
 
void squeeze ()
 Reshapes this tensor by removing all the axes of dimension one.
 
Tensor< T > & operator= (const T &value)
 Assigns a specified value to this tensor elements.
 
T * data ()
 Acesses the raw data of this tensor.
 
const T * data () const
 Reads the raw data of this tensor.
 
Tensor< T > clone () const
 Returns a clone of this tensor.
 
Tensor< T > & operator+= (const Tensor< T > &tensor)
 
Tensor< T > & operator+= (const T &tensor)
 
Tensor< T > & operator-= (const Tensor< T > &tensor)
 
Tensor< T > & operator-= (const T &tensor)
 
Tensor< T > & operator*= (const T &scalar)
 
Tensor< T > & operator/= (const T &scalar)
 
txeo::TensorIterator< T > begin ()
 
txeo::TensorIterator< T > end ()
 
txeo::TensorIterator< const T > begin () const
 
txeo::TensorIterator< const T > end () const
 

Protected Member Functions

template<typename P >
void create_from_shape (P &&shape)
 
void fill_data_shape (const std::initializer_list< std::initializer_list< T > > &list, std::vector< T > &flat_data, std::vector< size_t > &shape)
 
void fill_data_shape (const std::initializer_list< std::initializer_list< std::initializer_list< T > > > &list, std::vector< T > &flat_data, std::vector< size_t > &shape)
 
void check_indexes (const std::vector< size_t > &indexes)
 
 Tensor ()
 

Protected Attributes

std::unique_ptr< Impl > _impl {nullptr}
 

Friends

class txeo::Predictor< T >
 
class txeo::TensorAgg< T >
 
class txeo::TensorPart< T >
 
class txeo::TensorOp< T >
 
class txeo::TensorFunc< T >
 
class txeo::detail::TensorHelper
 
template<typename U >
std::ostream & operator<< (std::ostream &os, const Tensor< U > &tensor)
 
template<typename U >
txeo::Tensor< U > operator+ (const txeo::Tensor< U > &left, const txeo::Tensor< U > &right)
 Returns the sum of two tensors.
 
template<typename U >
txeo::Tensor< U > operator+ (const txeo::Tensor< U > &left, const U &right)
 
template<typename U >
txeo::Tensor< U > operator- (const txeo::Tensor< U > &left, const txeo::Tensor< U > &right)
 Returns the subtraction of two tensors.
 
template<typename U >
txeo::Tensor< U > operator- (const txeo::Tensor< U > &left, const U &right)
 Element-wise tensor-scalar addition operator.
 
template<typename U >
txeo::Tensor< U > operator- (const U &left, const txeo::Tensor< U > &right)
 Element-wise scalar-tensor subtraction operator.
 
template<typename U >
txeo::Tensor< U > operator* (const txeo::Tensor< U > &tensor, const U &scalar)
 Returns the scalar multiplication of a tensor.
 
template<typename U >
txeo::Tensor< U > operator/ (const txeo::Tensor< U > &left, const U &right)
 Element-wise division operator (tensor / scalar)
 
template<typename U >
txeo::Tensor< U > operator/ (const U &left, const txeo::Tensor< U > &right)
 Element-wise scalar-tensor division operator.
 

Detailed Description

template<typename T>
class txeo::Tensor< T >

Implements the mathematical concept of tensor, which is a magnitude of multiple order. A tensor of order zero is defined to be a scalar, of order one a vector, of order two a matrix and so on. Each order of the tensor has a dimension. Elements are addressed via multidimensional indexing.

Definition at line 48 of file Tensor.h.

Constructor & Destructor Documentation

◆ Tensor() [1/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const Tensor< T > &  tensor)
Note
This copy constructor performs a deep copy, behaving differently from TensorFlow C++.

◆ Tensor() [2/18]

template<typename T >
txeo::Tensor< T >::Tensor ( Tensor< T > &&  tensor)
noexcept

◆ ~Tensor()

template<typename T >
txeo::Tensor< T >::~Tensor ( )

◆ Tensor() [3/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const txeo::TensorShape shape)
explicit

Constructs a tensor from a specified txeo::TensorShape.

Parameters
shapeShape of the constructed tensor

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::TensorShape shape({3, 4}); // Define a 3x4 tensor shape
txeo::Tensor<int> tensor(shape); // Create a tensor with the given shape
std::cout << "Tensor created with shape: " << tensor.shape() << std::endl;
return 0;
}
Implements the mathematical concept of tensor, which is a magnitude of multiple order....
Definition Tensor.h:48
const txeo::TensorShape & shape() const
Returns the shape of this tensor.
The shape of a tensor is an ordered collection of dimensions of mathematical vector spaces.
Definition TensorShape.h:30

◆ Tensor() [4/18]

template<typename T >
txeo::Tensor< T >::Tensor ( txeo::TensorShape &&  shape)
explicit

Constructs a Tensor from a specified txeo::TensorShape.

Parameters
shapeShape of the constructed tensor

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
// Move-construct a tensor with shape 3x4
std::cout << "Tensor created with shape: " << tensor.shape() << std::endl;
return 0;
}

◆ Tensor() [5/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const std::vector< size_t > &  shape)
explicit

Constructs a Tensor from a specified shape std::vector.

Parameters
shapeVector of dimensions

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
std::vector<size_t> shape = {3, 4};
txeo::Tensor<int> tensor(shape); // Create a tensor with shape 3x4
std::cout << "Tensor created with shape: " << tensor.shape() << std::endl;
return 0;
}

◆ Tensor() [6/18]

template<typename T >
txeo::Tensor< T >::Tensor ( std::vector< size_t > &&  shape)
explicit

Constructs a Tensor from a specified shape std::vector.

Parameters
shapeVector of dimensions

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::vector<int> aux({3,4})
txeo::Tensor<int> tensor(aux);
std::cout << "Tensor created with shape: " << tensor.shape() << std::endl;
return 0;
}

◆ Tensor() [7/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const std::initializer_list< size_t > &  shape)
inlineexplicit

Constructs a Tensor from a specified shape std::vector.

Parameters
shapeVector of dimensions

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor({3,4});
std::cout << "Tensor created with shape: " << tensor.shape() << std::endl;
return 0;
}

Definition at line 172 of file Tensor.h.

173 : Tensor(std::vector<size_t>(shape)) {}

◆ Tensor() [8/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const txeo::TensorShape shape,
const T &  fill_value 
)
explicit

Constructs a Tensor from a specified txeo::TensorShape and fills it with a value.

Parameters
shapeShape of the constructed tensor
fill_valueValue of the elements of the constructed tensor

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor(shape, 5); // Create a 3x4 tensor filled with 5
std::cout << "Tensor initialized with: " << tensor << std::endl;
return 0;
}

◆ Tensor() [9/18]

template<typename T >
txeo::Tensor< T >::Tensor ( txeo::TensorShape &&  shape,
const T &  fill_value 
)
explicit

Constructs a Tensor from a specified txeo::TensorShape and fills it with a value.

Parameters
shapeShape of the constructed tensor
fill_valueValue of the elements of the constructed tensor

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
std::cout << "Tensor initialized with: " << tensor << std::endl;
return 0;
}

◆ Tensor() [10/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const std::vector< size_t > &  shape,
const T &  fill_value 
)
explicit

Constructs a Tensor from a specified shape std::vector and fills it with a value.

Parameters
shapeShape of the constructed tensor
fill_valueValue of the elements of the constructed tensor

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
std::vector<size_t> shape = {3, 4};
txeo::Tensor<int> tensor(shape, 5); // Create a 3x4 tensor filled with 5
std::cout << "Tensor initialized with: " << tensor << std::endl;
return 0;
}

◆ Tensor() [11/18]

template<typename T >
txeo::Tensor< T >::Tensor ( std::vector< size_t > &&  shape,
const T &  fill_value 
)
explicit

Constructs a Tensor from a specified shape std::vector and fills it with a value.

Parameters
shapeShape of the constructed tensor
fill_valueValue of the elements of the constructed tensor

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor(std::vector<size_t>({3, 4}), 7);
std::cout << "Tensor initialized with: " << tensor << std::endl;
return 0;
}

◆ Tensor() [12/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const std::initializer_list< size_t > &  shape,
const T &  fill_value 
)
inlineexplicit

Constructs a Tensor from a specified initializer list and fills it with a value.

Parameters
shapeShape of the constructed tensor
fill_valueValue of the elements of the constructed tensor

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor({3, 4}, 7);
std::cout << "Tensor initialized with: " << tensor << std::endl;
return 0;
}

Definition at line 282 of file Tensor.h.

283 : Tensor(std::vector<size_t>(shape), fill_value) {}

◆ Tensor() [13/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const txeo::TensorShape shape,
const std::vector< T > &  values 
)
explicit

Constructs a Tensor object from a specified txeo::TensorShape and fills it with a std::vector of values in a row-major scheme.

Parameters
shapeShape of the constructed tensor
valuesElements of the constructed tensor
Exceptions
txeo::TensorError

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor(txeo::TensorShape({2, 3}), {1, 2, 3, 4, 5, 6}); // Create a 2x3
tensor initialized with given values
std::cout << "Tensor initialized with: " << tensor << std::endl;
return 0;
}

◆ Tensor() [14/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const std::vector< size_t > &  shape,
const std::vector< T > &  values 
)
explicit

Constructs a Tensor object from a specified std::vector<size_t> and fills it with a std::vector of values in a row-major scheme.

Parameters
shapeShape of the constructed tensor
valuesElements of the constructed tensor
Exceptions
txeo::TensorError

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor(std::vector<size_t>({2, 3}), {1, 2, 3, 4, 5, 6});
std::cout << "Tensor initialized with: " << tensor << std::endl;
return 0;
}

◆ Tensor() [15/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const std::initializer_list< size_t > &  shape,
const std::vector< T > &  values 
)
inlineexplicit

Constructs a Tensor object from a specified initializer list and fills it with a std::vector of values in a row-major scheme.

Parameters
shapeShape of the constructed tensor
valuesElements of the constructed tensor
Exceptions
txeo::TensorError

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor({2, 3}, {1, 2, 3, 4, 5, 6});
std::cout << "Tensor initialized with: " << tensor << std::endl;
return 0;
}

Definition at line 359 of file Tensor.h.

360 : Tensor(std::vector<size_t>(shape), values) {}

◆ Tensor() [16/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const std::initializer_list< std::initializer_list< T > > &  values)
explicit

Constructs a second order Tensor from a nested std::initializer_list.

Parameters
valuesNested initializer list
Exceptions
txeo::TensorError

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor{{1, 2, 3}, {4, 5, 6}};
std::cout << tensor << std::endl; // Print the tensor
return 0;
}

◆ Tensor() [17/18]

template<typename T >
txeo::Tensor< T >::Tensor ( const std::initializer_list< std::initializer_list< std::initializer_list< T > > > &  values)
explicit

Constructs a third order Tensor from a nested std::initializer_list.

Parameters
valuesNested initializer list
Exceptions
txeo::TensorError

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
// Creating a 3D tensor with shape (4,2,3)
{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}},
{{-1, -2, -3}, {-4, -5, -6}},
{{-7, -8, -9}, {-10, -11, -12}}
};
std::cout << tensor << std::endl; // Print the tensor
return 0;
}

◆ Tensor() [18/18]

template<typename T >
txeo::Tensor< T >::Tensor ( )
explicitprotected

Member Function Documentation

◆ at() [1/4]

template<typename T >
T & txeo::Tensor< T >::at ( )

Accesses the value of this tensor if it is a scalar (order zero).

Returns
T& Value of the zeroth order tensor
Exceptions
txeo::TensorError
Note
Order checking is performed.

◆ at() [2/4]

template<typename T >
const T & txeo::Tensor< T >::at ( ) const

Reads the value of this tensor if it is a scalar (order zero). Order checking is performed.

Exceptions
txeo::TensorError
Returns
T& Value of the zeroth order tensor

◆ at() [3/4]

template<typename T >
requires (std::convertible_to<Args, size_t> && ...)
template<typename... Args>
requires (std::convertible_to<Args, size_t> && ...)
T & txeo::Tensor< T >::at ( Args...  args)
inline

Accesses an element of this tensor according to the specified indexes.

Template Parameters
ArgsA variadic list of indices that must be convertible to size_t.
Parameters
argsThe indices specifying the position of the element.
Returns
Element at the specified indices.
Exceptions
txeo::TensorError
Note
Bound and order checkings are performed.

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor{{1, 2, 3}, {4, 5, 6}};
tensor(1, 2) = 42; // Modify an element at row 1, column 2
std::cout << "Updated value: " << tensor(1, 2) << std::endl;
return 0;
}

Definition at line 1107 of file Tensor.h.

1107 {
1108 if (this->order() != sizeof...(Args))
1109 throw TensorError("The number of axes specified and the order of this tensor do no match.");
1110 try {
1111 check_indexes({static_cast<size_t>(args)...});
1112 } catch (std::exception e) {
1113 throw txeo::TensorError(e.what());
1114 }
1115
1116 return (*this)(args...);
1117}
Exceptions concerning txeo::Tensor.
Definition Tensor.h:1069
void check_indexes(const std::vector< size_t > &indexes)
int order() const
Returns the order of this tensor.

◆ at() [4/4]

template<typename T >
requires (std::convertible_to<Args, size_t> && ...)
template<typename... Args>
requires (std::convertible_to<Args, size_t> && ...)
const T & txeo::Tensor< T >::at ( Args...  args) const
inline

Reads an element of this tensor according to the specified indexes.

Template Parameters
ArgsA variadic list of indices that must be convertible to size_t.
Parameters
argsThe indices specifying the position of the element.
Returns
Element at the specified indices.
Exceptions
txeo::TensorError
Note
Bound and order checkings are performed.

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor{{1, 2, 3}, {4, 5, 6}};
tensor(1, 2) = 42; // Modify an element at row 1, column 2
std::cout << "Updated value: " << tensor(1, 2) << std::endl;
return 0;
}

Definition at line 1122 of file Tensor.h.

1122 {
1123 if (this->order() != sizeof...(Args))
1124 throw TensorError("The number of axes specified and the order of this tensor do no match.");
1125 try {
1126 check_indexes({static_cast<size_t>(args)...});
1127 } catch (std::exception e) {
1128 throw txeo::TensorError(e.what());
1129 }
1130
1131 return (*this)(args...);
1132}

◆ begin() [1/2]

template<typename T >
txeo::TensorIterator< T > txeo::Tensor< T >::begin ( )

◆ begin() [2/2]

template<typename T >
txeo::TensorIterator< const T > txeo::Tensor< T >::begin ( ) const

◆ check_indexes()

template<typename T >
void txeo::Tensor< T >::check_indexes ( const std::vector< size_t > &  indexes)
protected

◆ clone()

template<typename T >
Tensor< T > txeo::Tensor< T >::clone ( ) const

Returns a clone of this tensor.

Returns
Tensor<T> A clone of this tensor
Note
A copy is performed

◆ create_from_shape()

template<typename T >
template<typename P >
void txeo::Tensor< T >::create_from_shape ( P &&  shape)
protected

◆ data() [1/2]

template<typename T >
T * txeo::Tensor< T >::data ( )

Acesses the raw data of this tensor.

Returns
const T*

◆ data() [2/2]

template<typename T >
const T * txeo::Tensor< T >::data ( ) const

Reads the raw data of this tensor.

Returns
const T*

◆ dim()

template<typename T >
size_t txeo::Tensor< T >::dim ( ) const

Returns the dimension of this tensor.

Returns
size_t
Here is the caller graph for this function:

◆ end() [1/2]

template<typename T >
txeo::TensorIterator< T > txeo::Tensor< T >::end ( )

◆ end() [2/2]

template<typename T >
txeo::TensorIterator< const T > txeo::Tensor< T >::end ( ) const

◆ fill()

template<typename T >
void txeo::Tensor< T >::fill ( const T &  value)

Fills this tensor with the specified value.

Parameters
valueValue to fill this tensor

◆ fill_data_shape() [1/2]

template<typename T >
void txeo::Tensor< T >::fill_data_shape ( const std::initializer_list< std::initializer_list< std::initializer_list< T > > > &  list,
std::vector< T > &  flat_data,
std::vector< size_t > &  shape 
)
protected

Definition at line 1151 of file Tensor.h.

1153 {
1154 shape.emplace_back(list.size());
1155 std::vector<std::initializer_list<std::initializer_list<T>>> v_list(list);
1156 for (size_t i{1}; i < v_list.size(); ++i)
1157 if (v_list[i].size() != v_list[i - 1].size())
1158 throw txeo::TensorError("Tensor initialization is inconsistent!");
1159
1160 shape.emplace_back(v_list[0].size());
1161 bool emplaced{false};
1162 for (size_t i{0}; i < v_list.size(); ++i) {
1163 std::vector<std::initializer_list<T>> v_sublist(v_list[i]);
1164 for (size_t i{1}; i < v_sublist.size(); ++i)
1165 if (v_sublist[i].size() != v_sublist[i - 1].size())
1166 throw txeo::TensorError("Tensor initialization is inconsistent!");
1167
1168 if (!emplaced) {
1169 shape.emplace_back(v_sublist[0].size());
1170 emplaced = true;
1171 }
1172 for (auto &item : v_sublist)
1173 for (auto &subitem : item)
1174 flat_data.emplace_back(subitem);
1175 }
1176}

◆ fill_data_shape() [2/2]

template<typename T >
void txeo::Tensor< T >::fill_data_shape ( const std::initializer_list< std::initializer_list< T > > &  list,
std::vector< T > &  flat_data,
std::vector< size_t > &  shape 
)
protected

Definition at line 1135 of file Tensor.h.

1136 {
1137
1138 shape.emplace_back(list.size());
1139 std::vector<std::initializer_list<T>> v_list(list);
1140 for (size_t i{1}; i < v_list.size(); ++i)
1141 if (v_list[i].size() != v_list[i - 1].size())
1142 throw txeo::TensorError("Tensor initialization is inconsistent!");
1143
1144 shape.emplace_back(v_list[0].size());
1145 for (auto &item : v_list)
1146 for (auto &subitem : item)
1147 flat_data.emplace_back(subitem);
1148}

◆ fill_with_uniform_random() [1/2]

template<typename T >
void txeo::Tensor< T >::fill_with_uniform_random ( const T &  min,
const T &  max 
)

Fills the tensor with uniformly distributed random values ranging according to the specified interval.

Template Parameters
TThe data type of the tensor elements.
Parameters
minThe minimum possible random value.
maxThe maximum possible random value.
Exceptions
txeo::TensorError

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<float> tensor{3, 3}; // Create a 3x3 tensor
tensor.fill_with_uniform_random(0.0f, 1.0f);
std::cout << "Tensor filled with random values: " << tensor << std::endl;
return 0;
}
void fill_with_uniform_random(const T &min, const T &max)
Fills the tensor with uniformly distributed random values ranging according to the specified interval...

◆ fill_with_uniform_random() [2/2]

template<typename T >
void txeo::Tensor< T >::fill_with_uniform_random ( const T &  min,
const T &  max,
const size_t &  seed1,
const size_t &  seed2 
)

Fills the tensor with uniformly distributed random values ranging according to the specified interval.

Template Parameters
TThe data type of the tensor elements.
Parameters
minThe minimum possible random value.
maxThe maximum possible random value.
seed1The first seed for random number generation (in order to enable reproducibility).
seed2The second seed for random number generation (in order to enable reproducibility).
Exceptions
txeo::TensorError

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<float> tensor{3, 3}; // Create a 3x3 tensor
tensor.fill_with_uniform_random(0.0f, 1.0f, 42, 123);
std::cout << "Tensor filled with random values: " << tensor << std::endl;
return 0;
}

◆ flatten()

template<typename T >
Tensor< T > txeo::Tensor< T >::flatten ( ) const

Returns a first order reshaped view of this tensor.

Returns
Tensor<T> First order tensor that views this tensor.
Exceptions
txeo::TensorError
Note
No copy is performed.

◆ is_equal_shape()

template<typename T >
template<typename U >
bool txeo::Tensor< T >::is_equal_shape ( const Tensor< U > &  other) const
inline

Compares the shape of this tensor with the shape of the specified tensor.

Template Parameters
UData type of the specfied tensor
Parameters
otherThe tensor to compare
Returns
true if both this tensor and the other have the same shape
false otherwise

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor1{{1, 2, 3}, {4, 5, 6}};
txeo::Tensor<double> tensor2{{7.0, 8.0, 9.0}, {10.0, 11.0, 12.0}};
if (tensor1.is_equal_shape(tensor2)) {
std::cout << "Tensors have the same shape." << std::endl;
} else {
std::cout << "Tensors have different shapes." << std::endl;
}
return 0;
}

Definition at line 519 of file Tensor.h.

519 {
520 return this->shape() == other.shape();
521 };
Here is the call graph for this function:

◆ memory_size()

template<typename T >
size_t txeo::Tensor< T >::memory_size ( ) const

Returns the number of bytes occupied by this tensor.

Returns
size_t

◆ number_of_elements()

template<typename T >
size_t txeo::Tensor< T >::number_of_elements ( ) const
inline

Returns the number of elements of this tensor, which corresponds to the dimension of this tensor.

Returns
size_t

Definition at line 447 of file Tensor.h.

447{ return this->dim(); };
size_t dim() const
Returns the dimension of this tensor.
Here is the call graph for this function:

◆ operator!=()

template<typename T >
bool txeo::Tensor< T >::operator!= ( const Tensor< T > &  tensor)

◆ operator()() [1/4]

template<typename T >
T & txeo::Tensor< T >::operator() ( )

Accesses the value of this tensor if it is a scalar (order zero).

Since this function does not perform any checking, it accesses the first value of this tensor if it has order greater than zero.

Returns
T& Value of the zeroth order tensor

◆ operator()() [2/4]

template<typename T >
const T & txeo::Tensor< T >::operator() ( ) const

Reads the value of this tensor if it is a scalar (order zero).

Returns
T& Value of the zeroth order tensor
Note
Since this function does not perform any checking, it accesses the first value of this tensor if it has order greater than zero.

◆ operator()() [3/4]

template<typename T >
requires (std::convertible_to<Args, size_t> && ...)
template<typename... Args>
requires (std::convertible_to<Args, size_t> && ...)
T & txeo::Tensor< T >::operator() ( Args...  args)
inline

Accesses an element of this tensor according to the specified indexes.

Template Parameters
ArgsA variadic list of indices that must be convertible to size_t.
Parameters
argsThe indices specifying the position of the element.
Returns
Element at the specified indices.
Note
No checking is performed.

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor{{1, 2, 3}, {4, 5, 6}};
tensor(1, 2) = 42; // Modify an element at row 1, column 2
std::cout << "Updated value: " << tensor(1, 2) << std::endl;
return 0;
}

Definition at line 1077 of file Tensor.h.

1077 {
1078 size_t indexes[] = {static_cast<size_t>(args)...};
1079 size_t size = this->order();
1080 auto *stride = this->shape().stride().data();
1081 size_t flat_index{indexes[size - 1]};
1082
1083 for (size_t i = 0; i < size - 1; ++i)
1084 flat_index += indexes[i] * stride[i];
1085
1086 return this->data()[flat_index];
1087}
T * data()
Acesses the raw data of this tensor.
const std::vector< size_t > & stride() const
Returns the stride of each dimension in the tensor.

◆ operator()() [4/4]

template<typename T >
requires (std::convertible_to<Args, size_t> && ...)
template<typename... Args>
requires (std::convertible_to<Args, size_t> && ...)
const T & txeo::Tensor< T >::operator() ( Args...  args) const
inline

Reads an element of this tensor according to the specified indexes.

Template Parameters
ArgsA variadic list of indices that must be convertible to size_t.
Parameters
argsThe indices specifying the position of the element.
Returns
Element at the specified indices.
Note
No checking is performed.

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor{{1, 2, 3}, {4, 5, 6}};
tensor(1, 2) = 42; // Modify an element at row 1, column 2
std::cout << "Updated value: " << tensor(1, 2) << std::endl;
return 0;
}

Definition at line 1092 of file Tensor.h.

1092 {
1093 size_t indexes[] = {static_cast<size_t>(args)...};
1094 size_t size = this->order();
1095 auto *stride = this->shape().stride().data();
1096 size_t flat_index{indexes[size - 1]};
1097
1098 for (size_t i = 0; i < size - 1; ++i)
1099 flat_index += indexes[i] * stride[i];
1100
1101 return this->data()[flat_index];
1102}

◆ operator*=()

template<typename T >
Tensor< T > & txeo::Tensor< T >::operator*= ( const T &  scalar)

◆ operator+=() [1/2]

template<typename T >
Tensor< T > & txeo::Tensor< T >::operator+= ( const T &  tensor)

◆ operator+=() [2/2]

template<typename T >
Tensor< T > & txeo::Tensor< T >::operator+= ( const Tensor< T > &  tensor)

◆ operator-=() [1/2]

template<typename T >
Tensor< T > & txeo::Tensor< T >::operator-= ( const T &  tensor)

◆ operator-=() [2/2]

template<typename T >
Tensor< T > & txeo::Tensor< T >::operator-= ( const Tensor< T > &  tensor)

◆ operator/=()

template<typename T >
Tensor< T > & txeo::Tensor< T >::operator/= ( const T &  scalar)

◆ operator=() [1/3]

template<typename T >
Tensor< T > & txeo::Tensor< T >::operator= ( const T &  value)

Assigns a specified value to this tensor elements.

Parameters
valueValue to be assigned
Returns
Tensor<T>&

◆ operator=() [2/3]

template<typename T >
Tensor & txeo::Tensor< T >::operator= ( const Tensor< T > &  tensor)
Note
This copy assignment performs a deep copy, behaving differently from TensorFlow C++.
Here is the caller graph for this function:

◆ operator=() [3/3]

template<typename T >
Tensor & txeo::Tensor< T >::operator= ( Tensor< T > &&  tensor)
noexcept

◆ operator==()

template<typename T >
bool txeo::Tensor< T >::operator== ( const Tensor< T > &  tensor)

◆ order()

template<typename T >
int txeo::Tensor< T >::order ( ) const

Returns the order of this tensor.

Returns
int

◆ reshape() [1/3]

template<typename T >
void txeo::Tensor< T >::reshape ( const std::initializer_list< size_t > &  shape)
inline

Reshapes this tensor if the specified shape vector defines a number of elements equal to this tensor order.

Parameters
shapeNew shape vector for this tensor
Exceptions
txeo::TensorError
Note
No copy is performed.

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor{{1, 2, 3, 4}}; // Shape (1, 4)
tensor.reshape({2, 2}); // Change shape to (2, 2)
std::cout << "Reshaped Tensor: " << tensor << std::endl;
return 0;
}
void reshape(const txeo::TensorShape &shape)
Reshapes this tensor if the specified shape defines a number of elements equal to this tensor order.

Definition at line 756 of file Tensor.h.

756 {
757 this->reshape(std::vector<size_t>(shape));
758 };
Here is the call graph for this function:

◆ reshape() [2/3]

template<typename T >
void txeo::Tensor< T >::reshape ( const std::vector< size_t > &  shape)

Reshapes this tensor if the specified shape vector defines a number of elements equal to this tensor order.

Parameters
shapeNew shape vector for this tensor
Exceptions
txeo::TensorError
Note
No copy is performed.

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor{{1, 2, 3, 4}}; // Shape (1, 4)
tensor.reshape(std::vector<size_t>{2, 2}); // Change shape to (2, 2)
std::cout << "Reshaped Tensor: " << tensor << std::endl;
return 0;
}

◆ reshape() [3/3]

template<typename T >
void txeo::Tensor< T >::reshape ( const txeo::TensorShape shape)

Reshapes this tensor if the specified shape defines a number of elements equal to this tensor order.

Parameters
shapeNew shape for this tensor
Exceptions
txeo::TensorError
Note
No copy is performed.

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor{{1, 2, 3, 4}}; // Shape (1, 4)
tensor.reshape(txeo::TensorShape({2, 2})); // Change shape to (2, 2)
std::cout << "Reshaped Tensor: " << tensor << std::endl;
return 0;
}
Here is the caller graph for this function:

◆ shape()

template<typename T >
const txeo::TensorShape & txeo::Tensor< T >::shape ( ) const

Returns the shape of this tensor.

Returns
const txeo::TensorShape&
Here is the caller graph for this function:

◆ shuffle()

template<typename T >
void txeo::Tensor< T >::shuffle ( )

Shuffles the elements of this tensor.

◆ slice()

template<typename T >
Tensor< T > txeo::Tensor< T >::slice ( size_t  first_axis_begin,
size_t  first_axis_end 
) const

Returns a view ot this tensor from a specified range of dimensions of the first axis.

This function creates a new tensor that views the content of this tensor according to the specified parameters. There is no element copying.

Parameters
first_axis_beginInitial index along the first axis (inclusive).
first_axis_endFinal index along the first axis (exclusive).
Returns
Tensor<T>

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
txeo::Tensor<int> sliced_tensor = tensor.slice(0, 2); // Extract rows 0 and 1
std::cout << "Sliced Tensor: " << sliced_tensor << std::endl; // {{1, 2}, {4, 5}, {7, 8}}
return 0;
}
Tensor< T > slice(size_t first_axis_begin, size_t first_axis_end) const
Returns a view ot this tensor from a specified range of dimensions of the first axis.

◆ squeeze()

template<typename T >
void txeo::Tensor< T >::squeeze ( )

Reshapes this tensor by removing all the axes of dimension one.

Note
If the tensor has no axes of order one, its shape remains unchanged.

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor{{1}, {2}, {3}}; // Shape (3,1)
tensor.squeeze(); // Removes singleton dimension
std::cout << "Squeezed Tensor: " << tensor << std::endl;
return 0;
}
void squeeze()
Reshapes this tensor by removing all the axes of dimension one.

◆ type()

template<typename T >
constexpr std::type_identity_t< T > txeo::Tensor< T >::type ( ) const
constexpr

Returns the data type of this tensor.

Returns
constexpr std::type_identity_t<T>

◆ view_of()

template<typename T >
void txeo::Tensor< T >::view_of ( const Tensor< T > &  tensor,
const txeo::TensorShape shape 
)

Views the content of the specified tensor according to the specified shape. There is no element copying.

Parameters
tensorViewed tensor
shapeNew shape of this tensor

Friends And Related Symbol Documentation

◆ operator*

template<typename T >
template<typename U >
txeo::Tensor< U > operator* ( const txeo::Tensor< U > &  tensor,
const U &  scalar 
)
friend

Returns the scalar multiplication of a tensor.

Template Parameters
Utype of the tensor involved
Parameters
tensorOperand to be multiplied
scalarOperand that multiplies
Returns
txeo::Tensor Result

Example Usage:

txeo::Tensor<int> a({3}, {1,2,3});
auto b = a * 2; // Result: [2,4,6]

◆ operator+ [1/2]

template<typename T >
template<typename U >
txeo::Tensor< U > operator+ ( const txeo::Tensor< U > &  left,
const txeo::Tensor< U > &  right 
)
friend

Returns the sum of two tensors.

Template Parameters
Utype of the tensors involved
Parameters
leftLeft operand
rightRight operand
Returns
txeo::Tensor Result
Exceptions
txeo::TensorOpError

Example Usage:

txeo::Tensor<float> a({2,2}, {1,2,3,4});
txeo::Tensor<float> b({2,2}, {5,6,7,8});
auto c = a + b; // Result: [[6,8],[10,12]]

◆ operator+ [2/2]

template<typename T >
template<typename U >
txeo::Tensor< U > operator+ ( const txeo::Tensor< U > &  left,
const U &  right 
)
friend

◆ operator- [1/3]

template<typename T >
template<typename U >
txeo::Tensor< U > operator- ( const txeo::Tensor< U > &  left,
const txeo::Tensor< U > &  right 
)
friend

Returns the subtraction of two tensors.

Template Parameters
Utype of the tensors involved
Parameters
leftLeft operand
rightRight operand
Returns
txeo::Tensor Result
Exceptions
txeo::TensorOpError

Example Usage:

txeo::Tensor<double> a({2}, {5,7});
txeo::Tensor<double> b({2}, {1,3});
auto c = a - b; // Result: [4,4]

◆ operator- [2/3]

template<typename T >
template<typename U >
txeo::Tensor< U > operator- ( const txeo::Tensor< U > &  left,
const U &  right 
)
friend

Element-wise tensor-scalar addition operator.

Template Parameters
UNumeric type of tensor elements
Parameters
leftInput tensor
rightScalar to add
Returns
New tensor with elements: left[i] + right

Example Usage:

txeo::Tensor<int> t({2, 2}, {1, 2, 3, 4});
auto result = t + 5;
// result contains [6, 7, 8, 9] with shape [2, 2]

◆ operator- [3/3]

template<typename T >
template<typename U >
txeo::Tensor< U > operator- ( const U &  left,
const txeo::Tensor< U > &  right 
)
friend

Element-wise scalar-tensor subtraction operator.

Template Parameters
UNumeric type of tensor elements
Parameters
leftScalar value
rightInput tensor
Returns
New tensor with elements: left - right[i]

Example Usage:

txeo::Tensor<int> t({4}, {2, 3, 5, 7});
auto result = 10 - t;
// result contains [8, 7, 5, 3]

◆ operator/ [1/2]

template<typename T >
template<typename U >
txeo::Tensor< U > operator/ ( const txeo::Tensor< U > &  left,
const U &  right 
)
friend

Element-wise division operator (tensor / scalar)

Parameters
leftInput tensor
rightDivisor value
Returns
New tensor with element-wise division results

Example Usage:

txeo::Tensor<double> t({2, 2}, {10.0, 20.0, 30.0, 40.0});
auto result = t / 2.0;
// result contains [5.0, 10.0, 15.0, 20.0] with shape [2, 2]

◆ operator/ [2/2]

template<typename T >
template<typename U >
txeo::Tensor< U > operator/ ( const U &  left,
const txeo::Tensor< U > &  right 
)
friend

Element-wise scalar-tensor division operator.

Template Parameters
UNumeric type of tensor elements
Parameters
leftScalar dividend
rightTensor divisor
Returns
New tensor with elements: left / right[i]

Example Usage:

txeo::Tensor<double> t({3}, {2.0, 4.0, 5.0});
auto result = 100.0 / t;
// result contains [50.0, 25.0, 20.0]

◆ operator<<

template<typename T >
template<typename U >
std::ostream & operator<< ( std::ostream &  os,
const Tensor< U > &  tensor 
)
friend

◆ txeo::detail::TensorHelper

template<typename T >
friend class txeo::detail::TensorHelper
friend

Definition at line 1048 of file Tensor.h.

◆ txeo::Predictor< T >

template<typename T >
friend class txeo::Predictor< T >
friend

Definition at line 1041 of file Tensor.h.

◆ txeo::TensorAgg< T >

template<typename T >
friend class txeo::TensorAgg< T >
friend

Definition at line 1041 of file Tensor.h.

◆ txeo::TensorFunc< T >

template<typename T >
friend class txeo::TensorFunc< T >
friend

Definition at line 1041 of file Tensor.h.

◆ txeo::TensorOp< T >

template<typename T >
friend class txeo::TensorOp< T >
friend

Definition at line 1041 of file Tensor.h.

◆ txeo::TensorPart< T >

template<typename T >
friend class txeo::TensorPart< T >
friend

Definition at line 1041 of file Tensor.h.

Member Data Documentation

◆ _impl

template<typename T >
std::unique_ptr<Impl> txeo::Tensor< T >::_impl {nullptr}
protected

Definition at line 1041 of file Tensor.h.

1041{nullptr};

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