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

A class representing a matrix, derived from Tensor. More...

#include <Matrix.h>

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

Public Member Functions

 ~Matrix ()=default
 
 Matrix (const Matrix &matrix)
 
 Matrix (Matrix &&matrix) noexcept
 
Matrixoperator= (const Matrix &matrix)
 
Matrixoperator= (Matrix &&matrix) noexcept
 
 Matrix (size_t row_size, size_t col_size)
 Constructs a matrix with the specified row and column sizes.
 
 Matrix (size_t row_size, size_t col_size, const T &fill_value)
 Constructs a matrix with the specified row and column sizes and a fill value.
 
 Matrix (size_t row_size, size_t col_size, const std::vector< T > &values)
 Constructs a matrix with the specified row and column sizes and values.
 
 Matrix (size_t row_size, size_t col_size, const std::initializer_list< T > &values)
 Constructs a matrix with the specified row and column sizes and initializer list.
 
 Matrix (const std::initializer_list< std::initializer_list< T > > &values)
 Constructs a matrix from a nested initializer list.
 
 Matrix (txeo::Tensor< T > &&tensor)
 Constructs a matrix by moving data from a Tensor.
 
size_t size () const
 Returns the size of the matrix.
 
void reshape (const txeo::TensorShape &shape)
 
void reshape (const std::vector< size_t > &shape)
 
void reshape (const std::initializer_list< size_t > &shape)
 
- Public Member Functions inherited from txeo::Tensor< T >
 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
 

Static Public Member Functions

static Matrix< T > to_matrix (txeo::Tensor< T > &&tensor)
 Converts a tensor to a matrix by moving data.
 
static Matrix< T > to_matrix (const txeo::Tensor< T > &tensor)
 Creates a matrix from a tensor (preforms copy).
 
static txeo::Tensor< T > to_tensor (Matrix< T > &&matrix)
 Converts a matrix to a tensor by moving data.
 
static txeo::Tensor< T > to_tensor (const Matrix< T > &matrix)
 Creates a tensor from a matrix (performs copy).
 

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
 

Additional Inherited Members

- Protected Member Functions inherited from txeo::Tensor< T >
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 inherited from txeo::Tensor< T >
std::unique_ptr< Impl > _impl {nullptr}
 

Detailed Description

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

A class representing a matrix, derived from Tensor.

This class provides constructors for creating matrices with various initialization methods. It inherits from txeo::Tensor<T> and specializes it for 2nd-order data.

Template Parameters
TThe data type of the matrix elements (e.g., int, double).

Definition at line 42 of file Matrix.h.

Constructor & Destructor Documentation

◆ ~Matrix()

template<typename T >
txeo::Matrix< T >::~Matrix ( )
default

◆ Matrix() [1/8]

template<typename T >
txeo::Matrix< T >::Matrix ( const Matrix< T > &  matrix)
inline

Definition at line 46 of file Matrix.h.

46: txeo::Tensor<T>{matrix} {};
Implements the mathematical concept of tensor, which is a magnitude of multiple order....
Definition Tensor.h:48

◆ Matrix() [2/8]

template<typename T >
txeo::Matrix< T >::Matrix ( Matrix< T > &&  matrix)
inlinenoexcept

Definition at line 47 of file Matrix.h.

47: txeo::Tensor<T>{std::move(matrix)} {};

◆ Matrix() [3/8]

template<typename T >
txeo::Matrix< T >::Matrix ( size_t  row_size,
size_t  col_size 
)
inlineexplicit

Constructs a matrix with the specified row and column sizes.

This constructor creates a matrix of the specified row and column sizes, with uninitialized elements.

Parameters
row_sizeThe number of rows in the matrix.
col_sizeThe number of columns in the matrix.
txeo::Matrix<int> matrix(2, 3); // Creates a 2x3 matrix
A class representing a matrix, derived from Tensor.
Definition Matrix.h:42

Definition at line 72 of file Matrix.h.

73 : txeo::Tensor<T>{txeo::TensorShape({row_size, col_size})} {};
The shape of a tensor is an ordered collection of dimensions of mathematical vector spaces.
Definition TensorShape.h:30

◆ Matrix() [4/8]

template<typename T >
txeo::Matrix< T >::Matrix ( size_t  row_size,
size_t  col_size,
const T &  fill_value 
)
inlineexplicit

Constructs a matrix with the specified row and column sizes and a fill value.

Parameters
row_sizeThe number of rows in the matrix.
col_sizeThe number of columns in the matrix.
fill_valueThe value to fill the matrix with.

Example Usage:

txeo::Matrix<int> matrix(2, 3, 5); // Creates a 2x3 matrix filled with 5

Definition at line 87 of file Matrix.h.

88 : txeo::Tensor<T>({row_size, col_size}, fill_value) {};

◆ Matrix() [5/8]

template<typename T >
txeo::Matrix< T >::Matrix ( size_t  row_size,
size_t  col_size,
const std::vector< T > &  values 
)
inlineexplicit

Constructs a matrix with the specified row and column sizes and values.

Parameters
row_sizeThe number of rows in the matrix.
col_sizeThe number of columns in the matrix.
valuesThe values to initialize the matrix with.

Example Usage:

txeo::Matrix<int> matrix(2, 3, {1, 2, 3, 4, 5, 6}); // Creates a 2x3 matrix with values [1,
2, 3, 4, 5, 6]

Definition at line 103 of file Matrix.h.

104 : txeo::Tensor<T>({row_size, col_size}, values) {};

◆ Matrix() [6/8]

template<typename T >
txeo::Matrix< T >::Matrix ( size_t  row_size,
size_t  col_size,
const std::initializer_list< T > &  values 
)
inlineexplicit

Constructs a matrix with the specified row and column sizes and initializer list.

Parameters
row_sizeThe number of rows in the matrix.
col_sizeThe number of columns in the matrix.
valuesThe values to initialize the matrix with.

Example Usage:

txeo::Matrix<int> matrix(2, 3, {1, 2, 3, 4, 5, 6}); // Creates a 2x3 matrix with values [1,
2, 3, 4, 5, 6]

Definition at line 119 of file Matrix.h.

120 : txeo::Tensor<T>({row_size, col_size}, std::vector<T>(values)) {};

◆ Matrix() [7/8]

template<typename T >
txeo::Matrix< T >::Matrix ( const std::initializer_list< std::initializer_list< T > > &  values)
inlineexplicit

Constructs a matrix from a nested initializer list.

Parameters
valuesThe values to initialize the matrix with.

Example Usage:

txeo::Matrix<int> matrix({{1, 2, 3}, {4, 5, 6}}); // Creates a 2x3 matrix with values [1, 2,
3, 4, 5, 6]

Definition at line 133 of file Matrix.h.

134 : txeo::Tensor<T>(values) {};

◆ Matrix() [8/8]

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

Constructs a matrix by moving data from a Tensor.

Parameters
tensorThe Tensor to move data from.

Example Usage:

txeo::Tensor<int> tensor({2, 3}, {1, 2, 3, 4, 5, 6});
txeo::Matrix<int> matrix(std::move(tensor)); // Moves data from tensor to matrix

Member Function Documentation

◆ operator=() [1/2]

template<typename T >
Matrix & txeo::Matrix< T >::operator= ( const Matrix< T > &  matrix)
inline

Definition at line 49 of file Matrix.h.

49 {
51 return *this;
52 };
Tensor & operator=(const Tensor &tensor)
Here is the call graph for this function:

◆ operator=() [2/2]

template<typename T >
Matrix & txeo::Matrix< T >::operator= ( Matrix< T > &&  matrix)
inlinenoexcept

Definition at line 54 of file Matrix.h.

54 {
55 txeo::Tensor<T>::operator=(std::move(matrix));
56 return *this;
57 };
Here is the call graph for this function:

◆ reshape() [1/3]

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

Definition at line 166 of file Matrix.h.

166 {
167 this->reshape(std::vector<size_t>(shape));
168 };
void reshape(const txeo::TensorShape &shape)
const txeo::TensorShape & shape() const
Returns the shape of this tensor.
Here is the call graph for this function:

◆ reshape() [2/3]

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

Definition at line 164 of file Matrix.h.

164{ this->reshape(txeo::TensorShape(shape)); };
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reshape() [3/3]

template<typename T >
void txeo::Matrix< T >::reshape ( const txeo::TensorShape shape)
Here is the caller graph for this function:

◆ size()

template<typename T >
size_t txeo::Matrix< T >::size ( ) const
inline

Returns the size of the matrix.

Returns
The total number of elements in the matrix.

Example Usage:

txeo::Matrix<int> matrix(2, 3);
size_t size = matrix.size(); // size = 6
size_t size() const
Returns the size of the matrix.
Definition Matrix.h:160

Definition at line 160 of file Matrix.h.

160{ return txeo::Tensor<T>::dim(); };
size_t dim() const
Returns the dimension of this tensor.
Here is the call graph for this function:

◆ to_matrix() [1/2]

template<typename T >
static Matrix< T > txeo::Matrix< T >::to_matrix ( const txeo::Tensor< T > &  tensor)
static

Creates a matrix from a tensor (preforms copy).

Parameters
tensorThe input tensor to copied. Must be 2-dimensional.
Returns
A matrix created from the input tensor.
Exceptions
std::MatrixError

Example Usage:

txeo::Tensor<int> tensor({2, 3}, {1, 2, 3, 4, 5, 6}); // 2D tensor with shape (2, 3)
auto matrix = Matrix<int>::to_matrix(std::move(tensor)); //
// matrix shape: (2, 3)
static Matrix< T > to_matrix(txeo::Tensor< T > &&tensor)
Converts a tensor to a matrix by moving data.

◆ to_matrix() [2/2]

template<typename T >
static Matrix< T > txeo::Matrix< T >::to_matrix ( txeo::Tensor< T > &&  tensor)
static

Converts a tensor to a matrix by moving data.

Parameters
tensorThe input tensor to convert. Must be 2-dimensional.
Returns
A matrix created from the input tensor.
Exceptions
std::MatrixError

Example Usage:

txeo::Tensor<int> tensor({2, 3}, {1, 2, 3, 4, 5, 6}); // 2D tensor with shape (2, 3)
auto matrix = Matrix<int>::to_matrix(std::move(tensor)); // Convert to matrix
// matrix shape: (2, 3)

◆ to_tensor() [1/2]

template<typename T >
static txeo::Tensor< T > txeo::Matrix< T >::to_tensor ( const Matrix< T > &  matrix)
static

Creates a tensor from a matrix (performs copy).

Parameters
matrixThe input matrix to copy. Must be second order.
Returns
A tensor created from the input matrix.
Exceptions
std::MatrixError

Example Usage:

txeo::Matrix<int> matrix(2, 3, {1, 2, 3, 4, 5, 6}); // 2x3 matrix
auto tensor = Matrix<int>::to_tensor(matrix); // Convert to tensor
// tensor shape: (2, 3)
static txeo::Tensor< T > to_tensor(Matrix< T > &&matrix)
Converts a matrix to a tensor by moving data.

◆ to_tensor() [2/2]

template<typename T >
static txeo::Tensor< T > txeo::Matrix< T >::to_tensor ( Matrix< T > &&  matrix)
static

Converts a matrix to a tensor by moving data.

Parameters
matrixThe input matrix to convert. Must be second order.
Returns
A tensor created from the input matrix.
Exceptions
std::MatrixError

Example Usage:

txeo::Matrix<int> matrix(2, 3, {1, 2, 3, 4, 5, 6}); // 2x3 matrix
auto tensor = Matrix<int>::to_tensor(std::move(matrix)); // Convert to tensor
// tensor shape: (2, 3)

Friends And Related Symbol Documentation

◆ txeo::detail::TensorHelper

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

Definition at line 246 of file Matrix.h.

◆ txeo::Predictor< T >

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

Definition at line 239 of file Matrix.h.

◆ txeo::TensorAgg< T >

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

Definition at line 239 of file Matrix.h.

◆ txeo::TensorFunc< T >

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

Definition at line 239 of file Matrix.h.

◆ txeo::TensorOp< T >

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

Definition at line 239 of file Matrix.h.

◆ txeo::TensorPart< T >

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

Definition at line 239 of file Matrix.h.


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