summary refs log tree commit diff
path: root/src/Math/Matrix.hpp
blob: adc34a90c6887c68bda29cca1edfcf93775e3954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once

#include <sstream>
#include <stddef.h>
#include <iostream>

template <size_t R, size_t C, typename T = float>
struct Matrix {
public:
    Matrix<R, C, T>() : elements{} {};

    template<typename ...Args>
    explicit Matrix<R, C, T>(Args... args): elements{ args... } {};

    Vector<C, T> row(size_t index) {
        return { &elements[index * C] };
    }

    Vector<R, T> col(size_t index) {
        Vector<R, T> result{};
        for (int i = 0; i < R; i++) {
            result[i] = this->operator()(index, i);
        }
        return result;
    }

    template<size_t N>
    Matrix<R, N, T> operator*(Matrix<C, N, T> other) {
        Matrix<R, N, T> 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];
};