-
Notifications
You must be signed in to change notification settings - Fork 3
/
Matrix.hpp
215 lines (166 loc) · 4.06 KB
/
Matrix.hpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#pragma once
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
namespace CholUp {
// column major matrix
template<class T>
class Matrix
{
public:
T* data = 0;
size_t nrows, ncols;
bool isWrapper = false;
Matrix()
: nrows(0), ncols(0)
{}
Matrix(const Matrix<T>& m)
: nrows(m.nrows), ncols(m.ncols)
{
data = new T[nrows * ncols];
std::copy_n(m.data, nrows * ncols, data);
}
Matrix(T* vals, const int nrows_, const int ncols_)
: data(vals), nrows(nrows_), ncols(ncols_), isWrapper(true)
{
}
Matrix operator+(const Matrix& m) const
{
if(nrows != m.nrows || ncols != m.ncols)
{
std::cout << "matrix sizes do not match" << std::endl;
return *this;
}
Matrix ret(nrows, ncols);
for(int i = 0; i < nrows * ncols; ++i)
ret[i] = data[i] + m.data[i];
return std::move(ret);
}
Matrix operator-(const Matrix& m) const
{
if(nrows != m.nrows || ncols != m.ncols)
{
std::cout << "matrix sizes do not match" << std::endl;
return *this;
}
Matrix ret(nrows, ncols);
for(int i = 0; i < nrows * ncols; ++i)
ret[i] = data[i] - m.data[i];
return std::move(ret);
}
Matrix& operator+=(const Matrix& m)
{
if(nrows != m.nrows || ncols != m.ncols)
{
std::cout << "matrix sizes do not match" << std::endl;
return *this;
}
for(int i = 0; i < nrows * ncols; ++i)
data[i] += m.data[i];
return *this;
}
Matrix operator-=(const Matrix& m)
{
if(nrows != m.nrows || ncols != m.ncols)
{
std::cout << "matrix sizes do not match" << std::endl;
return *this;
}
for(int i = 0; i < nrows * ncols; ++i)
data[i] -= m.data[i];
return *this;
}
Matrix& operator=(const Matrix<T>& m)
{
if(this != &m)
{
nrows = m.nrows;
ncols = m.ncols;
if(data) delete[] data;
data = new T[nrows * ncols];
std::copy_n(m.data, nrows * ncols, data);
}
return *this;
}
Matrix& operator=(Matrix&& m)
{
if(this != &m)
{
nrows = m.nrows;
ncols = m.ncols;
data = m.data;
m.data = nullptr;
}
return *this;
}
Matrix(Matrix&& m)
{
*this = m;
}
~Matrix()
{
if(data && !isWrapper) delete[] data;
}
Matrix(size_t _rows, size_t _cols)
: data(new T[_rows * _cols]), nrows(_rows), ncols(_cols)
{
}
void resize(size_t _rows, size_t _cols)
{
nrows = _rows;
ncols = _cols;
if(data && !isWrapper) delete[] data;
data = new T[nrows * ncols];
}
void resize(size_t _rows, size_t _cols, T init)
{
nrows = _rows;
ncols = _cols;
if(data && !isWrapper) delete[] data;
data = new T[nrows * ncols];
std::fill_n(data, nrows * ncols, init);
}
T& operator()(const int i, const int j)
{
return data[j * nrows + i];
}
const T& operator()(const int i, const int j) const
{
return data[j * nrows + i];
}
size_t cols() const
{
return ncols;
}
size_t rows() const
{
return nrows;
}
void clear()
{
nrows = 0;
ncols = 0;
if(data && !isWrapper) delete[] data;
data = nullptr;
}
void fill(const T val = T(0))
{
std::fill_n(data, nrows * ncols, val);
}
void write(std::string fname) const
{
std::ofstream file(fname);
auto ptr = data;
file << std::setiosflags(std::ios::fixed);
file.precision(20);
for(int i = 0; i < ncols; ++i)
{
for(int j = 0; j < nrows; ++j)
file << *ptr++ << " ";
file << "\n";
}
file.close();
}
};
} /* namespace CholUp */