Txeo v0.1
A Developer-Friendly TensorFlow C++ Wrapper
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1#ifndef MATRIX_H
2#define MATRIX_H
3#pragma once
4
5#include "txeo/Tensor.h"
6#include "txeo/TensorShape.h"
7#include "txeo/Vector.h"
8#include "txeo/types.h"
9
10#include <cstddef>
11#include <initializer_list>
12#include <stdexcept>
13#include <utility>
14#include <vector>
15
16namespace txeo {
17
18namespace detail {
19class TensorHelper;
20}
21
22enum class NormalizationType;
23template <typename T>
24class Predictor;
25template <typename T>
26class TensorAgg;
27template <typename T>
28class TensorFunc;
29template <typename T>
30class TensorOp;
31template <typename T>
32class TensorPart;
33template <typename T>
34class Vector;
35
45template <typename T>
46class Matrix : public txeo::Tensor<T> {
47 public:
48 explicit Matrix();
49 ~Matrix() = default;
50
53
56 return *this;
57 };
58
61 return *this;
62 };
63
77 explicit Matrix(size_t row_size, size_t col_size)
79
92 explicit Matrix(size_t row_size, size_t col_size, const T &fill_value)
94
108 explicit Matrix(size_t row_size, size_t col_size, const std::vector<T> &values)
109 : txeo::Tensor<T>({row_size, col_size}, values) {};
110
124 explicit Matrix(size_t row_size, size_t col_size, const std::initializer_list<T> &values)
125 : txeo::Tensor<T>({row_size, col_size}, std::vector<T>(values)) {};
126
138 explicit Matrix(const std::initializer_list<std::initializer_list<T>> &values)
139 : txeo::Tensor<T>(values) {};
140
153
165 [[nodiscard]] size_t size() const { return txeo::Tensor<T>::dim(); };
166
178 [[nodiscard]] size_t row_size() const { return this->shape().axis_dim(0); };
179
191 [[nodiscard]] size_t col_size() const { return this->shape().axis_dim(1); };
192
215
238
240
241 void reshape(const std::vector<size_t> &shape) { this->reshape(txeo::TensorShape(shape)); };
242
243 void reshape(const std::initializer_list<size_t> &shape) {
244 this->reshape(std::vector<size_t>(shape));
245 };
246
270
294
310
312
329
346
363
380
381 template <typename U>
383
384 template <typename U>
385 friend Matrix<U> operator+(const Matrix<U> &left, const U &right);
386
387 template <typename U>
389
390 template <typename U>
391 friend Matrix<U> operator-(const Matrix<U> &left, const U &right);
392
393 template <typename U>
394 friend Matrix<U> operator-(const U &left, const Matrix<U> &right);
395
396 template <typename U>
397 friend Matrix<U> operator*(const Matrix<U> &matrix, const U &scalar);
398
399 template <typename U>
400 friend Matrix<U> operator*(const U &scalar, const Matrix<U> &matrix);
401
402 template <typename U>
403 friend Matrix<U> operator/(const Matrix<U> &left, const U &right);
404
405 template <typename U>
406 friend Matrix<U> operator/(const U &left, const Matrix<U> &right);
407
408 private:
409 friend class txeo::Predictor<T>;
410 friend class txeo::TensorAgg<T>;
411 friend class txeo::TensorPart<T>;
412 friend class txeo::TensorOp<T>;
413 friend class txeo::TensorFunc<T>;
415};
416
421class MatrixError : public std::runtime_error {
422 public:
423 using std::runtime_error::runtime_error;
424};
425
426} // namespace txeo
427#endif
A container for managing training, evaluation, and test data splits.
Definition DataTable.h:24
Exceptions concerning txeo::Matrix.
Definition Matrix.h:421
A class representing a matrix, derived from Tensor.
Definition Matrix.h:46
friend Matrix< U > operator-(const Matrix< U > &left, const U &right)
Matrix(Matrix &&matrix) noexcept
Definition Matrix.h:52
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.
Definition Matrix.h:108
Matrix & operator=(const Matrix &matrix)
Definition Matrix.h:54
void reshape(const std::vector< size_t > &shape)
Definition Matrix.h:241
friend Matrix< U > operator*(const U &scalar, const Matrix< U > &matrix)
static Matrix< T > to_matrix(txeo::Tensor< T > &&tensor)
Converts a tensor to a matrix by moving data.
Matrix< T > & transpose()
Transposes the matrix (swaps rows and columns) in-place.
friend Matrix< U > operator+(const Matrix< U > &left, const Matrix< U > &right)
size_t size() const
Returns the size of the matrix.
Definition Matrix.h:165
Tensor< T > dot(const txeo::Vector< T > &vector) const
Performs matrix-vector multiplication.
friend class txeo::detail::TensorHelper
Definition Matrix.h:414
void normalize_columns(txeo::NormalizationType type)
Normalizes matrix columns in-place using specified normalization method.
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.
Definition Matrix.h:124
static Matrix< T > to_matrix(const txeo::Tensor< T > &tensor)
Creates a matrix from a tensor (preforms copy).
Matrix(const std::initializer_list< std::initializer_list< T > > &values)
Constructs a matrix from a nested initializer list.
Definition Matrix.h:138
Matrix(txeo::Tensor< T > &&tensor)
Constructs a matrix by moving data from a Tensor.
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.
Definition Matrix.h:92
Matrix(const Matrix &matrix)
Definition Matrix.h:51
static txeo::Tensor< T > to_tensor(Matrix< T > &&matrix)
Converts a matrix to a tensor by moving data.
void normalize_rows(txeo::NormalizationType type)
Normalizes matrix rows in-place using specified normalization method.
void reshape(const std::initializer_list< size_t > &shape)
Definition Matrix.h:243
friend Matrix< U > operator/(const Matrix< U > &left, const U &right)
size_t row_size() const
Returns the row size of the matrix.
Definition Matrix.h:178
friend Matrix< U > operator-(const U &left, const Matrix< U > &right)
Matrix(size_t row_size, size_t col_size)
Constructs a matrix with the specified row and column sizes.
Definition Matrix.h:77
Matrix & operator=(Matrix &&matrix) noexcept
Definition Matrix.h:59
size_t col_size() const
Returns the column size of the matrix.
Definition Matrix.h:191
static txeo::Tensor< T > to_tensor(const Matrix< T > &matrix)
Creates a tensor from a matrix (performs copy).
~Matrix()=default
friend Matrix< U > operator/(const U &left, const Matrix< U > &right)
friend Matrix< U > operator*(const Matrix< U > &matrix, const U &scalar)
friend Matrix< U > operator+(const Matrix< U > &left, const U &right)
void reshape(const txeo::TensorShape &shape)
Matrix< T > clone() const
Matrix< T > dot(const Matrix< T > &matrix) const
Performs matrix multiplication with another matrix.
friend Matrix< U > operator-(const Matrix< U > &left, const Matrix< U > &right)
Class that deals with the main tasks of prediction (inference)
Definition Predictor.h:24
A utility class for aggregation functions on tensors.
Definition TensorAgg.h:25
A utility class for common math functions on tensors.
Definition TensorFunc.h:27
Implements the mathematical concept of tensor, which is a magnitude of multiple order....
Definition Tensor.h:50
Tensor & operator=(const Tensor &tensor)
constexpr std::type_identity_t< T > type() const
Returns the data type of this tensor.
const txeo::TensorShape & shape() const
Returns the shape of this tensor.
size_t dim() const
Returns the dimension of this tensor.
A utility class for performing operations on tensors and vectors.
Definition TensorOp.h:25
A utility class for partitioning tensors.
Definition TensorPart.h:24
The shape of a tensor is an ordered collection of dimensions of mathematical vector spaces.
Definition TensorShape.h:31
A class representing a vector, derived from Tensor.
Definition Vector.h:43
NormalizationType
Normalization types to be used in normalization functions.
Definition types.h:38