Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <initializer_list>
4#include <iostream>
5#include <tuple>
6#include <xtensor/containers/xarray.hpp>
7#include <xtensor/core/xmath.hpp>
8#include <xtensor/generators/xrandom.hpp>
9#include <xtensor/io/xio.hpp>
10#include <xtensor/reducers/xreducer.hpp>
11#include <xtensor/views/xview.hpp>
12
20namespace utils {
45 template<typename T>
46 class Matrix {
47 public:
56 Matrix() : rows_(0), cols_(0) {}
57
63 Matrix(size_t rows, size_t cols);
64
65
72 Matrix(size_t rows, size_t cols, T value);
73
78 Matrix(std::initializer_list<std::initializer_list<T>> list);
79
82 template<typename U>
83 friend std::ostream &operator<<(std::ostream &os, const Matrix<U> &matrix);
84 template<typename U>
85 friend Matrix<U> dot(const Matrix<U> &a, const Matrix<U> &b);
86 template<typename U>
87 friend U sum(const Matrix<U> &matrix);
88 template<typename U>
89 friend U mean(const Matrix<U> &matrix);
90
103 T &operator()(size_t row, size_t col);
104
112 const T &operator()(size_t row, size_t col) const;
113
127 Matrix operator+(const Matrix &other) const;
128
135 Matrix operator-(const Matrix &other) const;
136
143 Matrix operator*(const Matrix &other) const;
144
156 Matrix transpose() const;
157
165 Matrix reshape(size_t new_rows, size_t new_cols) const;
166
172 T determinant() const;
173
179 Matrix inverse() const;
180
181 auto eigenvalues() const;
182
194 [[nodiscard]] size_t rows() const { return rows_; }
195
200 [[nodiscard]] size_t cols() const { return cols_; }
201
206 [[nodiscard]] size_t size() const { return rows_ * cols_; }
207
212 [[nodiscard]] std::tuple<size_t, size_t> shape() const { return {rows_, cols_}; }
213
227 static Matrix zeros(size_t rows, size_t cols);
228
235 static Matrix ones(size_t rows, size_t cols);
236
242 static Matrix identity(size_t size);
243
252 static Matrix random(size_t rows, size_t cols, T min, T max);
253
260 xt::xarray<T> &data() { return data_; }
261
266 const xt::xarray<T> &data() const { return data_; }
267
268 private:
272 xt::xarray<T> data_;
273
277 size_t rows_, cols_;
278 };
279
291 template<typename T>
292 std::ostream &operator<<(std::ostream &os, const Matrix<T> &matrix);
293
300 template<typename T>
301 Matrix<T> dot(const Matrix<T> &a, const Matrix<T> &b);
302
308 template<typename T>
309 T sum(const Matrix<T> &matrix);
310
316 template<typename T>
317 T mean(const Matrix<T> &matrix);
318
330
335
337} // namespace utils
static Matrix zeros(size_t rows, size_t cols)
Create a matrix filled with zeros.
Definition matrix.cpp:121
xt::xarray< T > & data()
Get the underlying xtensor array.
Definition matrix.hpp:260
size_t cols() const
Get the number of columns.
Definition matrix.hpp:200
std::tuple< size_t, size_t > shape() const
Get the shape of the matric in one step.
Definition matrix.hpp:212
Matrix()
Default constructor creating an empty matrix.
Definition matrix.hpp:56
T determinant() const
Calculate the determinant of the matrix.
Definition matrix.cpp:96
Matrix transpose() const
Compute the transpose of the matrix.
Definition matrix.cpp:78
size_t size() const
Get the total number of elements.
Definition matrix.hpp:206
friend std::ostream & operator<<(std::ostream &os, const Matrix< U > &matrix)
friend U sum(const Matrix< U > &matrix)
Matrix inverse() const
Calculate the inverse of the matrix.
Definition matrix.cpp:105
T & operator()(size_t row, size_t col)
Access matrix element at specified position (mutable)
Definition matrix.cpp:29
friend Matrix< U > dot(const Matrix< U > &a, const Matrix< U > &b)
Matrix operator+(const Matrix &other) const
Matrix addition operator.
Definition matrix.cpp:45
Matrix reshape(size_t new_rows, size_t new_cols) const
Reshape the matrix to new dimensions.
Definition matrix.cpp:85
static Matrix random(size_t rows, size_t cols, T min, T max)
Create a matrix with random values.
Definition matrix.cpp:142
static Matrix identity(size_t size)
Create an identity matrix.
Definition matrix.cpp:135
static Matrix ones(size_t rows, size_t cols)
Create a matrix filled with ones.
Definition matrix.cpp:128
Matrix operator-(const Matrix &other) const
Matrix subtraction operator.
Definition matrix.cpp:56
friend U mean(const Matrix< U > &matrix)
auto eigenvalues() const
Definition matrix.cpp:116
Matrix operator*(const Matrix &other) const
Matrix multiplication operator.
Definition matrix.cpp:67
const xt::xarray< T > & data() const
Get the underlying xtensor array (const)
Definition matrix.hpp:266
size_t rows() const
Get the number of rows.
Definition matrix.hpp:194
T sum(const Matrix< T > &matrix)
Calculate sum of all matrix elements.
Definition matrix.cpp:166
T mean(const Matrix< T > &matrix)
Calculate mean of all matrix elements.
Definition matrix.cpp:171
std::ostream & operator<<(std::ostream &os, const Matrix< T > &matrix)
Output stream operator for matrix visualization.
Definition matrix.cpp:149
Matrix< T > dot(const Matrix< T > &a, const Matrix< T > &b)
Compute dot product of two matrices.
Definition matrix.cpp:155