Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
Loading...
Searching...
No Matches
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/matrix.hpp

A templated matrix class for mathematical operations in deep learning.

A templated matrix class for mathematical operations in deep learningThis class provides a comprehensive matrix implementation with support for common mathematical operations required in deep learning algorithms including matrix multiplication, element-wise operations, transpose, and various initialization methods.

This implementation uses xtensor as the backend for efficient matrix operations.

Template Parameters
TThe data type for matrix elements (typically float or double)
// Create a 3x3 identity matrix
auto identity = Matrix<float>::identity(3);
// Create a random matrix
auto random_matrix = Matrix<float>::random(2, 3, 0.0f, 1.0f);
// Matrix multiplication
auto result = identity * random_matrix;
#pragma once
#include <initializer_list>
#include <iostream>
#include <tuple>
#include <xtensor/containers/xarray.hpp>
#include <xtensor/core/xmath.hpp>
#include <xtensor/generators/xrandom.hpp>
#include <xtensor/io/xio.hpp>
#include <xtensor/reducers/xreducer.hpp>
#include <xtensor/views/xview.hpp>
namespace utils {
template<typename T>
class Matrix {
public:
Matrix() : rows_(0), cols_(0) {}
Matrix(size_t rows, size_t cols);
Matrix(size_t rows, size_t cols, T value);
Matrix(std::initializer_list<std::initializer_list<T>> list);
template<typename U>
friend std::ostream &operator<<(std::ostream &os, const Matrix<U> &matrix);
template<typename U>
friend Matrix<U> dot(const Matrix<U> &a, const Matrix<U> &b);
template<typename U>
friend U sum(const Matrix<U> &matrix);
template<typename U>
friend U mean(const Matrix<U> &matrix);
T &operator()(size_t row, size_t col);
const T &operator()(size_t row, size_t col) const;
Matrix operator+(const Matrix &other) const;
Matrix operator-(const Matrix &other) const;
Matrix operator*(const Matrix &other) const;
Matrix transpose() const;
Matrix reshape(size_t new_rows, size_t new_cols) const;
T determinant() const;
Matrix inverse() const;
auto eigenvalues() const;
[[nodiscard]] size_t rows() const { return rows_; }
[[nodiscard]] size_t cols() const { return cols_; }
[[nodiscard]] size_t size() const { return rows_ * cols_; }
[[nodiscard]] std::tuple<size_t, size_t> shape() const { return {rows_, cols_}; }
static Matrix zeros(size_t rows, size_t cols);
static Matrix ones(size_t rows, size_t cols);
static Matrix identity(size_t size);
static Matrix random(size_t rows, size_t cols, T min, T max);
xt::xarray<T> &data() { return data_; }
const xt::xarray<T> &data() const { return data_; }
private:
xt::xarray<T> data_;
size_t rows_, cols_;
};
template<typename T>
std::ostream &operator<<(std::ostream &os, const Matrix<T> &matrix);
template<typename T>
Matrix<T> dot(const Matrix<T> &a, const Matrix<T> &b);
template<typename T>
T sum(const Matrix<T> &matrix);
template<typename T>
T mean(const Matrix<T> &matrix);
using MatrixF = Matrix<float>;
using MatrixD = Matrix<double>;
} // 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
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
Matrix< float > MatrixF
Single-precision floating point matrix.
Definition matrix.hpp:329
Matrix< double > MatrixD
Double-precision floating point matrix.
Definition matrix.hpp:334
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