Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
Loading...
Searching...
No Matches
matrix.cpp
Go to the documentation of this file.
1#include "../include/utils/matrix.hpp"
2#include <xtensor-blas/xlinalg.hpp>
3#include <xtensor/containers/xadapt.hpp>
4
5namespace utils {
6 template<typename T>
7 Matrix<T>::Matrix(size_t rows, size_t cols) : rows_(rows), cols_(cols), data_(xt::zeros<T>({rows, cols})) {}
8
9 template<typename T>
10 Matrix<T>::Matrix(size_t rows, size_t cols, T value) :
11 rows_(rows), cols_(cols), data_(xt::ones<T>({rows, cols}) * value) {}
12
13 template<typename T>
14 Matrix<T>::Matrix(std::initializer_list<std::initializer_list<T>> list) {
15 rows_ = list.size();
16 cols_ = list.begin()->size();
17
18 std::vector<T> flat_data;
19 flat_data.reserve(rows_ * cols_);
20
21 for (const auto &row: list) {
22 std::copy(row.begin(), row.end(), std::back_inserter(flat_data));
23 }
24
25 data_ = xt::adapt(flat_data, {rows_, cols_});
26 }
27
28 template<typename T>
29 T &Matrix<T>::operator()(size_t row, size_t col) {
30 if (row >= rows_ || col >= cols_) {
31 throw std::out_of_range("Matrix index out of bounds");
32 }
33 return data_(row, col);
34 }
35
36 template<typename T>
37 const T &Matrix<T>::operator()(size_t row, size_t col) const {
38 if (row >= rows_ || col >= cols_) {
39 throw std::out_of_range("Matrix index out of bounds");
40 }
41 return data_(row, col);
42 }
43
44 template<typename T>
46 if (rows_ != other.rows_ || cols_ != other.cols_) {
47 throw std::invalid_argument("Matrix dimensions must match for addition");
48 }
49
50 Matrix<T> result(rows_, cols_);
51 result.data_ = data_ + other.data_;
52 return result;
53 }
54
55 template<typename T>
57 if (rows_ != other.rows_ || cols_ != other.cols_) {
58 throw std::invalid_argument("Matrix dimensions must match for subtraction");
59 }
60
61 Matrix<T> result(rows_, cols_);
62 result.data_ = data_ - other.data_;
63 return result;
64 }
65
66 template<typename T>
68 if (cols_ != other.rows_) {
69 throw std::invalid_argument("Invalid dimensions for matrix multiplication");
70 }
71
72 Matrix<T> result(rows_, other.cols_);
73 result.data_ = xt::linalg::dot(data_, other.data_);
74 return result;
75 }
76
77 template<typename T>
79 Matrix<T> result(cols_, rows_);
80 result.data_ = xt::transpose(data_);
81 return result;
82 }
83
84 template<typename T>
85 Matrix<T> Matrix<T>::reshape(size_t new_rows, size_t new_cols) const {
86 if (new_rows * new_cols != rows_ * cols_) {
87 throw std::invalid_argument("New dimensions must have same total size");
88 }
89
90 Matrix<T> result(new_rows, new_cols);
91 result.data_ = xt::reshape_view(data_, {new_rows, new_cols});
92 return result;
93 }
94
95 template<typename T>
97 if (rows_ != cols_) {
98 throw std::invalid_argument("Determinant only defined for square matrices");
99 }
100
101 return xt::linalg::det(data_);
102 }
104 template<typename T>
106 if (rows_ != cols_) {
107 throw std::invalid_argument("Inverse only defined for square matrices");
108 }
109
110 Matrix<T> result(rows_, cols_);
111 result.data_ = xt::linalg::inv(data_);
112 return result;
113 }
114
115 template<typename T>
117 return xt::linalg::eigh(data_);
118 }
119
120 template<typename T>
121 Matrix<T> Matrix<T>::zeros(size_t rows, size_t cols) {
122 Matrix<T> result(rows, cols);
123 result.data_ = xt::zeros<T>({rows, cols});
124 return result;
125 }
126
127 template<typename T>
128 Matrix<T> Matrix<T>::ones(size_t rows, size_t cols) {
129 Matrix<T> result(rows, cols);
130 result.data_ = xt::ones<T>({rows, cols});
131 return result;
132 }
133
134 template<typename T>
136 Matrix<T> result(size, size);
137 result.data_ = xt::eye<T>(size);
138 return result;
139 }
140
141 template<typename T>
142 Matrix<T> Matrix<T>::random(size_t rows, size_t cols, T min, T max) {
143 Matrix<T> result(rows, cols);
144 result.data_ = xt::random::rand<T>({rows, cols}, min, max);
145 return result;
146 }
147
148 template<typename T>
149 std::ostream &operator<<(std::ostream &os, const Matrix<T> &matrix) {
150 os << matrix.data();
151 return os;
152 }
153
154 template<typename T>
155 Matrix<T> dot(const Matrix<T> &a, const Matrix<T> &b) {
156 if (a.cols() != b.rows()) {
157 throw std::invalid_argument("Invalid dimensions for dot product");
158 }
159
160 Matrix<T> result(a.rows(), b.cols());
161 result.data() = xt::linalg::dot(a.data(), b.data());
162 return result;
163 }
164
165 template<typename T>
166 T sum(const Matrix<T> &matrix) {
167 return xt::sum(matrix.data())(0);
168 }
169
170 template<typename T>
171 T mean(const Matrix<T> &matrix) {
172 return xt::mean(matrix.data())(0);
173 }
174
175 // Explicit template instantiations
176 template class Matrix<float>;
177 template class Matrix<double>;
178
179 // Non-member function implementations
180 // Explicit instantiation of non-member functions
181 template std::ostream &operator<< <float>(std::ostream &, const Matrix<float> &);
182 template std::ostream &operator<< <double>(std::ostream &, const Matrix<double> &);
183
186
187 template float sum<float>(const Matrix<float> &);
188 template double sum<double>(const Matrix<double> &);
189
190 template float mean<float>(const Matrix<float> &);
191 template double mean<double>(const Matrix<double> &);
192} // 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
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
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
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
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
template double sum< double >(const Matrix< double > &)
template std::ostream & operator<<< double >(std::ostream &, const Matrix< double > &)
template Matrix< double > dot< double >(const Matrix< double > &, const Matrix< double > &)
std::ostream & operator<<(std::ostream &os, const Matrix< T > &matrix)
Output stream operator for matrix visualization.
Definition matrix.cpp:149
template Matrix< float > dot< float >(const Matrix< float > &, const Matrix< float > &)
template double mean< double >(const Matrix< double > &)
Matrix< T > dot(const Matrix< T > &a, const Matrix< T > &b)
Compute dot product of two matrices.
Definition matrix.cpp:155
template std::ostream & operator<<< float >(std::ostream &, const Matrix< float > &)
template float mean< float >(const Matrix< float > &)
template float sum< float >(const Matrix< float > &)