-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.h
151 lines (125 loc) · 3.39 KB
/
Matrix.h
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
//
// Created by André Schnabel on 05.11.15.
//
#pragma once
#include <vector>
#include <sstream>
template<class T>
class Matrix {
int m, n;
std::vector<T> data;
public:
template<class Func>
Matrix(int _m , int _n, Func code) : m(_m), n(_n), data(_m*_n) {
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
data[i*n+j] = code(i,j);
}
Matrix(const Matrix& mx) : m(mx.m), n(mx.n), data(mx.data) {}
Matrix(const std::vector<std::vector<T>> &rows)
: m(static_cast<int>(rows.size())), n(static_cast<int>(rows[0].size())), data(m*n) {
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
data[i*n+j] = rows[i][j];
}
enum class Mode {
COLUMN_VECTOR,
ROW_VECTOR
};
Matrix(Mode m, const std::vector<T> &vec)
: Matrix(
m == Mode::COLUMN_VECTOR ? static_cast<int>(vec.size()) : 1,
m == Mode::COLUMN_VECTOR ? 1 : static_cast<int>(vec.size()),
[&](int i, int j) { return vec[m == Mode::COLUMN_VECTOR ? i : j]; }) {
}
Matrix() : m(0), n(0) {}
Matrix(int _m, int _n) : m(_m), n(_n), data(_m*_n) {}
Matrix(int _m, int _n, int value) : Matrix(_m, _n, [value](int i, int j) { return value; }) {}
~Matrix() {}
int getM() const { return m; }
int getN() const { return n; }
inline T operator()(int i, int j) const { return data[i*n+j]; }
inline T &operator()(int i, int j) { return data[i*n+j]; }
bool operator==(const Matrix<T> &mx) const {
return equals(mx);
}
bool equals(const Matrix &mx) const {
if(m != mx.getM() || n != mx.getN()) return false;
return mx.forAll([this](int i, int j, int v) {
return at(i, j) == v;
});
}
template<class Func>
bool forAll(Func pred) const {
bool res = true;
foreach([&res,&pred](int i, int j, int v) {
res &= pred(i, j, v);
});
return res;
}
T at(int i, int j) const { return data[i*n + j]; }
Matrix &operator=(const Matrix &mx) {
data = mx.data;
m = mx.m;
n = mx.n;
return *this;
}
void resize(int _m, int _n) {
data.resize(_m*_n);
m = _m;
n = _n;
}
std::vector<T> row(int i) const {
std::vector<T> r(n);
for(int j=0; j<n; j++)
r[j] = data[i*n+j];
return r;
}
std::vector<T> column(int j) const {
std::vector<T> c(m);
for (int i = 0; i<m; i++)
c[i] = data[i*n + j];
return c;
}
template<class Func>
void foreach(Func f) const {
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
f(i, j, data[i*n + j]);
}
template<class Func>
void foreach2(Func f) const {
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
f(i, j);
}
template<class Func>
void foreachAssign(Func f) {
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
data[i*n + j] = f(i, j);
}
std::string toString() const {
std::stringstream out;
out << "Matrix(m=" << m << ",n=" << n << "," << std::endl << "{";
for (int i = 0; i < m; i++) {
out << "{";
for (int j = 0; j < n; j++) {
out << at(i, j) << (j + 1 == n ? "" : ",");
}
out << "}" << (i + 1 == m ? "" : ",") << std::endl;
}
out << "}" << std::endl;
return out.str();
}
std::string toStringCondensed() {
std::stringstream out;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
out << at(i,j);
}
out << "\n";
}
return out.str();
}
};