41 #ifndef MATRIX_TMPL_HPP
42 #define MATRIX_TMPL_HPP
61 template <
typename Type>
class Matrix {
67 std::tuple<size_t, size_t>
dim;
77 Matrix(
size_t mtx_rows,
size_t mtx_cols)
94 assert(0 <= row && row <
rows);
95 assert(0 <= col && col <
cols);
111 for (
size_t r = 0;
res.rows; ++r) {
112 for (
size_t c = 0;
res.cols; ++c) {
113 for (
size_t n = 0; n < target.
rows; ++n) {
114 res(r, c) += (*this)(r, n) * target(n, c);
127 for (
size_t r = 0; r <
res.rows; ++r) {
128 for (
size_t c = 0; c <
res.cols; ++c) {
129 res(r, c) = scalar * (*this)(r, c);
140 assert(
dim == target.
dim);
143 for (
size_t r = 0; r <
res.rows; ++r) {
144 for (
size_t c = 0; c <
res.cols; ++c) {
145 res(r, c) = target(r, c) * (*this)(r, c);
166 assert(
dim == target.
dim);
169 for (
size_t r = 0; r <
res.rows; ++r) {
170 for (
size_t c = 0; c <
res.cols; ++c) {
171 res(r, c) = (*this)(r, c) + target(r, c);
186 for (
size_t r = 0; r <
rows; ++r) {
187 for (
size_t c = 0; c <
cols; ++c) {
188 res(r, c) = (*this)(r, c) + scalar;
197 for (
size_t r = 0; r <
rows; ++r) {
198 for (
size_t c = 0; c <
cols; ++c) {
199 res(r, c) = -(*this)(r, c);
209 Matrix target_neg = -target;
210 return add(target_neg);
217 assert(
dim == target.
dim);
220 for (int64_t r = 0; r <
rows; ++r) {
221 for (int64_t c = 0; c <
cols; ++c) {
222 if ((*
this)(r, c) - target(r, c) == 0.)
238 for (int64_t r = 0; r <
rows; ++r) {
239 for (int64_t c = 0; c <
cols; ++c) {
251 size_t new_rows{
cols}, new_cols{
rows};
252 Matrix transposed(new_rows, new_cols);
254 for (
size_t r = 0; r < new_rows; ++r) {
255 for (
size_t c = 0; c < new_cols; ++c) {
256 transposed(r, c) = (*this)(c, r);
272 for (
size_t r = 0; r <
rows; ++r) {
273 for (
size_t c = 0; c <
cols; ++c) {
274 res(0, 0) += (*this)(r, c);
284 assert(0 <= dimension && dimension < 2);
287 if (dimension == 0) {
288 for (
size_t c = 0; c <
cols; ++c) {
289 for (
size_t r = 0; r <
rows; ++r) {
290 res(0, c) += (*this)(r, c);
294 for (
size_t r = 0; r <
rows; ++r) {
295 for (
size_t c = 0; c <
cols; ++c) {
296 res(r, 0) += (*this)(r, c);
313 auto m = (dimension == 0) ? Type(
rows) : Type(
cols);
321 (dimension == 0) ? assert(
rows == target.
rows)
328 for (
size_t r = 0; r <
rows; ++r) {
329 for (
size_t c = 0; c <
cols; ++c) {
330 res(r, c) = (*this)(r, c);
335 if (dimension == 0) {
336 for (
size_t r = 0; r < target.
rows; ++r) {
337 for (
size_t c = 0; c <
cols; ++c) {
338 res(r +
rows, c) = target(r, c);
342 for (
size_t r = 0; r <
rows; ++r) {
343 for (
size_t c = 0; c < target.
cols; ++c) {
344 res(r, c +
cols) = target(r, c);
356 for (
size_t i = 0; i <
rows; ++i)
357 res(i, i) = (*this)(i, 0);
362 for (
size_t i = 0; i <
rows; ++i)
363 res(i, 0) = (*this)(i, i);
371 for (
size_t r = 0; r <
rows; ++r) {
372 for (
size_t c = 0; c <
cols; ++c) {
373 res(r, c) =
function((*this)(r, c));
380 std::cout <<
"Matrix Size([" <<
rows <<
", " <<
cols <<
"])"
385 for (
size_t r = 0; r <
rows; ++r) {
386 for (int64_t c = 0; c <
cols; ++c) {
387 std::cout << (*this)(r, c) <<
" ";
389 std::cout << std::endl;
391 std::cout << std::endl;
395 for (
size_t r = 0; r <
rows; ++r) {
396 for (int64_t c = 0; c <
cols; ++c) {
403 template <
typename T>
415 MTX.fill_index(T(0));
421 MTX.fill_index(T(1));
428 std::random_device rd{};
429 std::mt19937 gen{rd()};
430 T n(MTX.num_elements);
431 T stdev{1 / sqrt(n)};
432 std::normal_distribution<T> d{0, stdev};
434 for (
size_t r = 0; r <
rows; ++r) {
435 for (uint64_t c = 0; c <
cols; ++c) {
444 std::random_device rd{};
445 std::mt19937 gen{rd()};
446 std::uniform_real_distribution<T> d{0, 1};
448 for (
size_t r = 0; r <
rows; ++r) {
449 for (int64_t c = 0; c <
cols; ++c) {
Matrix and Scalar operations.
Matrix(size_t mtx_rows, size_t mtx_cols)
Matrix Class constructor initializing empty vector.
Matrix operator-(Matrix &target)
Matrix< unsigned short > operator==(Matrix &target)
Matrix mean(size_t dimension)
Matrix scalar_add(Type scalar)
void fill_index(Type val)
Matrix add(Matrix &target)
Matrix hadamard(Matrix &target)
Type & operator()(size_t row, size_t col)
Overload operator.
Matrix sum(size_t dimension)
Matrix operator+(Matrix &target)
std::tuple< size_t, size_t > dim
Matrix concatenate(Matrix target, size_t dimension)
Matrix sub(Matrix &target)
Matrix apply_func(const std::function< Type(const Type &)> &function)
Matrix mult(Matrix &target)
Matrix Multiplication.
Matrix scalar_mult(Type scalar)
The source C++ openGPMP namespace.
static Matrix< T > zeros(size_t rows, size_t cols)
Matrix zeros method.
static Matrix< T > randn(size_t rows, size_t cols)
static Matrix< T > ones(size_t rows, size_t cols)
static Matrix< T > rand(size_t rows, size_t cols)