From 3b289a2f75b6e96735519a65d93b6babd1b1759f Mon Sep 17 00:00:00 2001 From: Mel Date: Sun, 2 Oct 2022 04:34:18 +0200 Subject: Expand Vector and Matrix classes --- src/Math/Matrix.hpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/Math/Matrix.hpp (limited to 'src/Math/Matrix.hpp') diff --git a/src/Math/Matrix.hpp b/src/Math/Matrix.hpp new file mode 100644 index 0000000..adc34a9 --- /dev/null +++ b/src/Math/Matrix.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include + +template +struct Matrix { +public: + Matrix() : elements{} {}; + + template + explicit Matrix(Args... args): elements{ args... } {}; + + Vector row(size_t index) { + return { &elements[index * C] }; + } + + Vector col(size_t index) { + Vector result{}; + for (int i = 0; i < R; i++) { + result[i] = this->operator()(index, i); + } + return result; + } + + template + Matrix operator*(Matrix other) { + Matrix result{}; + for (int y = 0; y < R; y++) { + for (int x = 0; x < N; x++) { + auto r = row(y); + auto c = other.col(x); + + auto dot = r * c; + + std::cout << x << ", " << y << ": " + << "(" << r.string() << ", " << c.string() << ")" + << dot << std::endl; + + result(x, y) = dot; + } + } + return result; + } + + auto& operator()(size_t x, size_t y) { + return elements[y * C + x]; + } + + std::string string() { + std::stringstream str{}; + for (int x = 0; x < R; x++) { + for (int y = 0; y < C; y++) { + str << this->operator()(x, y) << " "; + } + + str << "\n"; + } + + return str.str(); + } + + T elements[R * C]; +}; -- cgit 1.4.1