-
Notifications
You must be signed in to change notification settings - Fork 27
/
matrix.hpp
288 lines (234 loc) · 9.56 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* This file is part of aither.
Copyright (C) 2015-18 Michael Nucci (michael.nucci@gmail.com)
Aither is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Aither is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef MATRIXHEADERDEF // only if the macro MATRIXHEADERDEF is not defined
// execute these lines of code
#define MATRIXHEADERDEF // define the macro
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include "macros.hpp"
using std::ostream;
using std::vector;
// forward class declaration
class varArray;
// ---------------------------------------------------------------------------
// matrix functions
// function for matrix multiplication
// using cache efficient implimentation
void MatrixMultiply(const vector<double>::const_iterator &matL,
const vector<double>::const_iterator &matR,
const vector<double>::iterator &result, const int &size);
double MaximumAbsValOnDiagonal(const vector<double>::const_iterator &mat,
const int &size);
void IdentityMatrix(const vector<double>::iterator &mat, const int &size);
int FindMaxInColumn(const vector<double>::const_iterator &mat, const int &size,
const int &c, const int &start, const int &end);
void RowMultiplyFactor(const vector<double>::iterator &mat, const int &size,
const int &r, const int &c, const double &factor);
void LinearCombRow(const vector<double>::iterator &mat, const int &size,
const int &r1, const double &factor, const int &r2);
void SwapMatRows(const vector<double>::iterator &mat, const int &size,
const int &r1, const int &r2);
void MatrixInverse(const vector<double>::iterator &mat, const int &size);
void MultiplyFacOnDiagonal(const vector<double>::iterator &mat, const int &size,
const double &val);
void AddFacOnDiagonal(const vector<double>::iterator &mat, const int &size,
const double &val);
// ---------------------------------------------------------------------------
// class to store a square matrix
class squareMatrix {
int size_;
vector<double> data_;
// private member functions
int GetLoc(const int &r, const int &c) const {
return r * size_ + c;
}
public:
// constructor
explicit squareMatrix(const int &a) : size_(a), data_(a * a, 0.0) {}
squareMatrix() : squareMatrix(0) {}
// move constructor and assignment operator
squareMatrix(squareMatrix &&) noexcept = default;
squareMatrix& operator=(squareMatrix &&) = default;
// copy constructor and assignment operator
squareMatrix(const squareMatrix &) = default;
squareMatrix& operator=(const squareMatrix &) = default;
// member functions
// provide begin and end so std::begin and std::end can be used
// use lower case to conform with std::begin, std::end
auto begin() noexcept {return data_.begin();}
const auto begin() const noexcept {return data_.begin();}
auto end() noexcept {return data_.end();}
const auto end() const noexcept {return data_.end();}
int Size() const {return size_;}
void SwapRows(const int &r1, const int &r2) {
SwapMatRows(this->begin(), size_, r1, r2);
}
void Inverse() { MatrixInverse(this->begin(), size_); }
int FindMaxInCol(const int &c, const int &start, const int &end) const {
return FindMaxInColumn(this->begin(), size_, c, start, end);
}
void RowMultiply(const int &r, const int &c, const double &fac) {
RowMultiplyFactor(this->begin(), size_, r, c, fac);
}
void LinCombRow(const int &r1, const double &fac, const int &r2) {
LinearCombRow(this->begin(), size_, r1, fac, r2);
}
void Zero() { std::fill(this->begin(), this->end(), 0.0); }
void Identity() { IdentityMatrix(this->begin(), size_); }
squareMatrix MatMult(const squareMatrix &) const;
template <typename T,
typename = std::enable_if_t<std::is_base_of<varArray, T>::value>>
T ArrayMult(const T &, const int = 0) const;
double MaxAbsValOnDiagonal() const {
return MaximumAbsValOnDiagonal(this->begin(), size_);
}
// operator overloads
double & operator()(const int &r, const int &c) {
return data_[this->GetLoc(r, c)];
}
const double & operator()(const int &r, const int &c) const {
return data_[this->GetLoc(r, c)];
}
inline squareMatrix & operator+=(const squareMatrix &);
inline squareMatrix & operator-=(const squareMatrix &);
inline squareMatrix & operator*=(const squareMatrix &);
inline squareMatrix & operator/=(const squareMatrix &);
inline squareMatrix & operator+=(const double &);
inline squareMatrix & operator-=(const double &);
inline squareMatrix & operator*=(const double &);
inline squareMatrix & operator/=(const double &);
inline squareMatrix operator+(const double &s) const {
auto lhs = *this;
return lhs += s;
}
inline squareMatrix operator-(const double &s) const {
auto lhs = *this;
return lhs -= s;
}
inline squareMatrix operator*(const double &s) const {
auto lhs = *this;
return lhs *= s;
}
inline squareMatrix operator/(const double &s) const {
auto lhs = *this;
return lhs /= s;
}
// destructor
~squareMatrix() noexcept {}
};
// function declarations
// member function to do matrix/vector multplication with varArray type
template <typename T, typename TT>
T squareMatrix::ArrayMult(const T &vec, const int pos) const {
// vec -- vector to multiply with
auto product = vec;
// zero out portion of array that will be written over
if (pos == 0) {
for (auto ii = 0; ii < vec.TurbulenceIndex(); ii++) {
product[ii] = 0.0;
}
} else {
for (auto ii = pos; ii < vec.Size(); ii++) {
product[ii] = 0.0;
}
}
for (auto rr = 0; rr < size_; rr++) {
for (auto cc = 0; cc < size_; cc++) {
product[pos + rr] += (*this)(rr, cc) * vec[pos + cc];
}
}
return product;
}
ostream &operator<<(ostream &os, const squareMatrix &);
// operator overload for addition
squareMatrix & squareMatrix::operator+=(const squareMatrix &mat) {
MSG_ASSERT(this->Size() == mat.Size(), "matrix sizes must be equal");
std::transform(this->begin(), this->end(), mat.begin(), this->begin(),
std::plus<double>());
return *this;
}
// operator overload for subtraction
squareMatrix & squareMatrix::operator-=(const squareMatrix &mat) {
MSG_ASSERT(this->Size() == mat.Size(), "matrix sizes must be equal");
std::transform(this->begin(), this->end(), mat.begin(), this->begin(),
std::minus<double>());
return *this;
}
// operator overload for elementwise multiplication
squareMatrix & squareMatrix::operator*=(const squareMatrix &mat) {
MSG_ASSERT(this->Size() == mat.Size(), "matrix sizes must be equal");
std::transform(this->begin(), this->end(), mat.begin(), this->begin(),
std::multiplies<double>());
return *this;
}
// operator overload for elementwise multiplication
squareMatrix & squareMatrix::operator/=(const squareMatrix &mat) {
MSG_ASSERT(this->Size() == mat.Size(), "matrix sizes must be equal");
std::transform(this->begin(), this->end(), mat.begin(), this->begin(),
std::divides<double>());
return *this;
}
inline const squareMatrix operator+(squareMatrix lhs, const squareMatrix &rhs) {
return lhs += rhs;
}
inline const squareMatrix operator-(squareMatrix lhs, const squareMatrix &rhs) {
return lhs -= rhs;
}
inline const squareMatrix operator*(squareMatrix lhs, const squareMatrix &rhs) {
return lhs *= rhs;
}
inline const squareMatrix operator/(squareMatrix lhs, const squareMatrix &rhs) {
return lhs /= rhs;
}
// operator overloads for double --------------------------------------------
// operator overload for addition
squareMatrix & squareMatrix::operator+=(const double &scalar) {
std::for_each(this->begin(), this->end(),
[&scalar](auto &val) { val += scalar; });
return *this;
}
// operator overload for subtraction
squareMatrix & squareMatrix::operator-=(const double &scalar) {
std::for_each(this->begin(), this->end(),
[&scalar](auto &val) { val -= scalar; });
return *this;
}
// operator overload for multiplication
squareMatrix & squareMatrix::operator*=(const double &scalar) {
std::for_each(this->begin(), this->end(),
[&scalar](auto &val) { val *= scalar; });
return *this;
}
// operator overload for division
squareMatrix & squareMatrix::operator/=(const double &scalar) {
std::for_each(this->begin(), this->end(),
[&scalar](auto &val) { val /= scalar; });
return *this;
}
inline const squareMatrix operator+(const double &lhs, squareMatrix rhs) {
return rhs += lhs;
}
inline const squareMatrix operator-(const double &lhs, squareMatrix rhs) {
std::for_each(rhs.begin(), rhs.end(), [&lhs](auto &val) { val = lhs - val; });
return rhs;
}
inline const squareMatrix operator*(const double &lhs, squareMatrix rhs) {
return rhs *= lhs;
}
inline const squareMatrix operator/(const double &lhs, squareMatrix rhs) {
std::for_each(rhs.begin(), rhs.end(), [&lhs](auto &val) { val = lhs / val; });
return rhs;
}
#endif