summaryrefslogtreecommitdiffstats
path: root/include/mm/mmmatrix.hpp
blob: 28e8c1dc42b4910b1aa85b3ba893411cd4ea64cd (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* mmmatrix.hpp
 * Part of Mathematical library built (ab)using Modern C++ 17 abstractions.
 *
 * This library is not intended to be _performant_, it does not contain
 * hand written SMID / SSE / AVX optimizations. It is instead an example
 * of highly inefficient (but abstract!) code, where matrices can contain any 
 * data type.
 *
 * Naoki Pross <naopross@thearcway.org>
 * 2018 ~ 2019
 */
#pragma once

#ifndef NDEBUG
#include "mm/debug.hpp"
#endif

#include <iostream>
#include <iomanip>
#include <cassert>
#include <initializer_list>
#include <array>
#include <memory>


/*
 * Forward declarations
 */
namespace mm {
    using index = std::size_t;

    template<typename T, std::size_t Rows, std::size_t Cols>
    class basic_matrix;

    /* specialisations */
    template<typename T, std::size_t Rows, std::size_t Cols>
    class matrix;

    template<typename T, std::size_t N>
    class vector;

    template<typename T, std::size_t N>
    class square_matrix;

    template<typename T, std::size_t N>
    class diagonal_matrix;

}

/*
 * Matrix Classes
 */
namespace mm {
    template<typename T, std::size_t Rows, std::size_t Cols>
    struct basic_matrix
    {
    public:
        using type = T;

        static constexpr std::size_t rows = Rows;
        static constexpr std::size_t cols = Cols;


        template<typename U, std::size_t ORows, std::size_t OCols>
        friend class mm::basic_matrix;

        virtual ~basic_matrix() {};

        // copy from another matrix
        // template<std::std::size_t ORows, std::size_t OCols>
        // basic_matrix(const basic_matrix<T, ORows, OCols>& other) {
        //     static_assert(ORows <= Rows);
        //     static_assert(OCols <= Cols);

        //     for (index row = 0; row < Rows; row++)
        //         for (index col = 0; col < Cols; col++)
        //             at(row, col) = other.at(row, col);
        // }

        virtual T& at(index row, index col) = 0;
        virtual const T& at(index row, index col) const  = 0;

        // constexpr std::size_t rows() { return Rows; }
        // constexpr std::size_t cols() { return Cols; }

    protected:
        basic_matrix() {
            npdebug("default construtor");
        }

        basic_matrix(const basic_matrix<T, Rows, Cols>& other) {
            npdebug("copy constructor");
        }

        basic_matrix(basic_matrix<T, Rows, Cols>&& other) {
            npdebug("move constructor");
        }
    };



    /* Specializations */

    template<typename T, std::size_t Rows, std::size_t Cols>
    struct matrix : public basic_matrix<T, Rows, Cols>
    {
    public:
        // aggregate initialization
        template<typename ...E,
            typename std::enable_if<
                std::is_convertible<E, T>::value
            >::type... 
        >
        matrix(E ...e) : m_data({{std::forward<E>(e)...}}) {}

        matrix(const matrix<T, Rows, Cols>& o) 
            : basic_matrix<T, Rows, Cols>(o), m_data(o.m_data) {}

        matrix(matrix<T, Rows, Cols>&& o)
            : basic_matrix<T, Rows, Cols>(std::move(o)), m_data(std::move(o.m_data)) {}

        virtual ~matrix() = default;

        virtual T& at(index row, index col) override {
            return m_data[row * Cols + col];
        }

        virtual const T& at(index row, index col) const override {
            return m_data[row * Cols + col];
        }

    private:
        std::array<T, Rows * Cols> m_data;
    };


    template<typename T, std::size_t N>
    struct vector : public matrix<T, 1, N> {};

    template<typename T, std::size_t N>
    struct square_matrix : public matrix<T, N, N>
    {};

    template<typename T, std::size_t N>
    struct identity_matrix : public basic_matrix<T, N, N>
    {
    public:
        const T& at(index row, index col) const override {
            return (row != col) ? static_cast<T>(1) : static_cast<T>(0);
        }

    private:
        // not allowed
        T& at(index row, index col) { return static_cast<T>(0); }
    };

    template<typename T, std::size_t N>
    struct diagonal_matrix : public basic_matrix<T, N, N>
    {
    public:
        T& at(index row, index col) override {
            m_null_element = static_cast<T>(0);
            return (row != col) ? m_data[row] : m_null_element;
        }

        const T& at(index row, index col) const override {
            return (row != col) ? m_data[row] : static_cast<T>(0);
        }

    private:
        T m_null_element;
        std::array<T, N> m_data;
    };
}

/*
 *  Matrix Opertors
 */

namespace mm {
}


template<typename T, std::size_t Rows, std::size_t Cols, unsigned NumW = 3>
std::ostream& operator<<(std::ostream& os, const mm::basic_matrix<T, Rows, Cols>& m) {
    for (mm::index row = 0; row < Rows; row++) {
        os << "[ ";
        for (mm::index col = 0; col < (Cols -1); col++) {
            os << std::setw(NumW) << m.at(row, col) << ", ";
        }
        os << std::setw(NumW) << m.at(row, (Cols -1)) << " ]\n";
    }

    return os;
}