-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathmatrix.cpp
467 lines (417 loc) · 14.3 KB
/
mathmatrix.cpp
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "mathmatrix.h"
#include <QStringList>
#include <QStringBuilder>
struct Data {
Data(unsigned int r, unsigned int c) :
refCounter(1),
rows(r),
columns(c),
vsize(r*c)
{
if (vsize) v = new double[vsize];
double* pv = &(v[0]);
for (unsigned int i=0; i<vsize; ++i, ++pv)
*pv = 0;
}
~Data() {
if (vsize) delete[] v;
}
unsigned int refCounter;
unsigned int rows;
unsigned int columns;
unsigned int vsize;
double* v;
unsigned int position(unsigned int r, unsigned int c) const {
return r * columns + c;
}
void detach(Data**d) {
if (refCounter > 1) {
refCounter--;
Data* nd = new Data(rows, columns);
for (unsigned int i = 0; i<vsize; ++i)
nd->v[i] = v[i];
*d = nd;
}
}
};
MathMatrix::MathMatrix() :
d(new Data(0, 0))
{
}
MathMatrix::MathMatrix(unsigned int rows, unsigned int cols) :
d(new Data(rows, cols))
{
}
MathMatrix::MathMatrix(const MathMatrix &matrix) :
d(matrix.d)
{
d->refCounter++;
}
MathMatrix::~MathMatrix() {
d->refCounter--;
if (d->refCounter == 0)
delete d;
}
double* MathMatrix::internal_pointer() const {
return &(d->v[0]);
}
unsigned int MathMatrix::rows() const {
return d->rows;
}
unsigned int MathMatrix::columns() const {
return d->columns;
}
double MathMatrix::at(unsigned int r, unsigned int c) const {
Q_ASSERT(r < d->rows && c < d->columns);
return d->v[d->position(r, c)];
}
void MathMatrix::setItem(unsigned int r, unsigned int c, double value) {
Q_ASSERT(r < d->rows && c < d->columns);
d->detach(&d);
d->v[d->position(r, c)] = value;
}
void MathMatrix::setRow(unsigned int r, const std::initializer_list<double>& list) {
Q_ASSERT(r < d->rows && list.size() >= d->columns);
d->detach(&d);
// this can be sliced in multithreaded applications
auto ilist = list.begin();
double *pv = &(d->v[d->position(r,0)]);
for (unsigned int c=0; c<d->columns; ++c, ++ilist, ++pv)
*pv = *ilist;
}
void MathMatrix::setColumn(unsigned int c, const std::initializer_list<double>& list) {
Q_ASSERT(c < d->columns && list.size() >= d->rows);
d->detach(&d);
// this can be sliced in multithreaded applications
auto ilist = list.begin();
double *pv = &(d->v[d->position(0, c)]);
for (unsigned int r=0; r<d->rows; ++r, ++ilist, pv+=d->columns)
*pv = *ilist;
}
void MathMatrix::setMatrix(const std::initializer_list<std::initializer_list<double>>& matrix) {
Q_ASSERT(matrix.size() >= d->rows);
d->detach(&d);
// this can be sliced in multithreaded applications
auto rlist = matrix.begin();
double *pv = &(d->v[0]);
for (unsigned int r=0; r<d->rows; ++r, ++rlist) {
Q_ASSERT(rlist->size() >= d->columns);
auto ilist = rlist->begin();
for (unsigned int c=0; c<d->columns; ++c, ++ilist, ++pv)
*pv = *ilist;
}
}
MathMatrix MathMatrix::clone() const {
MathMatrix c(*this);
d->detach(&(c.d));
return c;
}
MathMatrix& MathMatrix::swapRows(unsigned int row1, unsigned int row2) {
Q_ASSERT(row1 != row2 && row1 < d->rows && row2 < d->rows);
double *p1 = internal_pointer() + d->position(row1, 0);
double *p2 = internal_pointer() + d->position(row2, 0);
double temp;
// copy content from row 1 to temp, row 2 to row 1, temp to row 2
for (unsigned int i=0; i<d->columns; ++i, ++p1, ++p2) {
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
return *this;
}
MathMatrix& MathMatrix::swapColumns(unsigned int col1, unsigned int col2) {
Q_ASSERT(col1 != col2 && col1 < d->columns && col2 < d->columns);
double *p1 = internal_pointer() + d->position(0, col1);
double *p2 = internal_pointer() + d->position(0, col2);
double temp;
// copy content from col 1 to temp, col 2 to col 1, temp to col 2
for (unsigned int i=0; i<d->columns; ++i, p1+=(d->columns), p2+=(d->columns)) {
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
return *this;
}
MathMatrix& MathMatrix::operator=(const MathMatrix& matrix) {
d->refCounter--;
if (d->refCounter == 0)
delete d;
d = matrix.d;
d->refCounter++;
return *this;
}
const double& MathMatrix::operator()(unsigned int r, unsigned int c) const {
Q_ASSERT(r < d->rows && c < d->columns);
return d->v[d->position(r, c)];
}
double& MathMatrix::operator()(unsigned int r, unsigned int c) {
Q_ASSERT(r < d->rows && c < d->columns);
d->detach(&d);
return d->v[d->position(r, c)];
}
MathMatrix MathMatrix::operator+(const MathMatrix& m) const {
Q_ASSERT(d->rows == m.rows() && d->columns == m.columns());
MathMatrix result(d->rows, d->columns);
double* pvR = result.internal_pointer();
double* pv0 = internal_pointer();
double* pv1 = m.internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pvR, ++pv0, ++pv1)
*pvR = *pv0 + *pv1;
return result;
}
MathMatrix MathMatrix::operator-(const MathMatrix& m) const {
Q_ASSERT(d->rows == m.rows() && d->columns == m.columns());
MathMatrix result(d->rows, d->columns);
double* pvR = result.internal_pointer();
double* pv0 = internal_pointer();
double* pv1 = m.internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pvR, ++pv0, ++pv1)
*pvR = *pv0 - *pv1;
return result;
}
MathMatrix MathMatrix::operator*(double x) const {
MathMatrix result(d->rows, d->columns);
double* pvR = result.internal_pointer();
double* pv0 = internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pvR, ++pv0)
*pvR = *pv0 * x;
return result;
}
MathMatrix MathMatrix::operator*(const MathMatrix& m) const {
Q_ASSERT(d->columns == m.rows());
MathMatrix result(d->rows, m.columns());
double *pvR = result.internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<result.rows(); ++i) {
for (unsigned int j=0; j<result.columns(); ++j, ++pvR) {
double *pv0 = internal_pointer() + d->columns * i;
double *pv1 = m.internal_pointer() + j;
for (unsigned int k=0; k<d->columns; ++k, ++pv0, pv1+=m.columns())
*pvR += (*pv0 * *pv1);
}
}
return result;
}
MathMatrix MathMatrix::operator/(double x) const {
Q_ASSERT(x != 0);
MathMatrix result(d->rows, d->columns);
double* pvR = result.internal_pointer();
double* pv0 = internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pvR, ++pv0)
*pvR = *pv0 / x;
return result;
}
MathMatrix& MathMatrix::operator+=(const MathMatrix& m) {
Q_ASSERT(d->rows == m.rows() && d->columns == m.columns());
d->detach(&d);
double* pv0 = internal_pointer();
double* pv1 = m.internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pv0, ++pv1)
*pv0 += *pv1;
return *this;
}
MathMatrix& MathMatrix::operator-=(const MathMatrix& m) {
Q_ASSERT(d->rows == m.rows() && d->columns == m.columns());
d->detach(&d);
double* pv0 = internal_pointer();
double* pv1 = m.internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pv0, ++pv1)
*pv0 -= *pv1;
return *this;
}
MathMatrix& MathMatrix::operator*=(double x) {
d->detach(&d);
double* pv0 = internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pv0)
*pv0 *= x;
return *this;
}
MathMatrix& MathMatrix::operator/=(double x) {
Q_ASSERT(x != 0);
d->detach(&d);
double* pv0 = internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pv0)
*pv0 /= x;
return *this;
}
MathMatrix& MathMatrix::operator++() {
d->detach(&d);
double* pv0 = internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pv0)
(*pv0)++;
return *this;
}
MathMatrix& MathMatrix::operator--() {
d->detach(&d);
double* pv0 = internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pv0)
(*pv0)--;
return *this;
}
double MathMatrix::trace() const {
Q_ASSERT(d->rows == d->columns && d->vsize);
double result = 0;
double *p = internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->rows; ++i, p+=(d->rows+1))
result += (*p);
return result;
}
double MathMatrix::determinant() const {
Q_ASSERT(d->rows == d->columns && d->vsize > 0);
if (d->vsize == 1) return d->v[0];
if (d->vsize == 4) return (d->v[0] * d->v[3] - d->v[1] * d->v[2]);
// Gauss elimination;
// this can be sliced in multithreaded applications
double result = 1;
MathMatrix mod = clone();
unsigned int n = d->rows;
for (unsigned int j=0; j<n-1; ++j) {
for (unsigned int i=0; (i+j)<(n-1); ++i) {
if (mod.at(i,j) != 0) {
if (mod.at(i+1, j) == 0) {
mod.swapRows(i, i+1);
result = -result; // swapping rows changes the determinant signal
}
else {
double scalar = mod.at(i, j) / mod.at(i+1, j);
for (unsigned int k=j; k<n; ++k)
mod.setItem(i, k, (mod.at(i+1, k) * scalar) - mod.at(i, k));
result = -result; // because you are multiplying the row i by -1 before adding the multiple of the row i+1
}
}
}
}
double *p = mod.internal_pointer() + (n-1);
for (unsigned int i=0; i<n; ++i, p+=(n-1))
result *= *p;
return std::roundl((-result)*1e10)/1.0e10;
}
MathMatrix MathMatrix::subMatrix(unsigned int row, unsigned int column) const {
Q_ASSERT(d->rows > 1 && d->columns > 1);
MathMatrix result(d->rows-1, d->columns-1);
double* pvR = result.internal_pointer();
double* pv0 = internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<result.rows(); ++i) {
pv0 += (i==row ? d->columns : 0);
for (unsigned int j=0; j<result.columns(); ++j, ++pvR, ++pv0) {
pv0 += (j==column ? 1 : 0);
*pvR = *pv0;
}
}
return result;
}
MathMatrix MathMatrix::transposed() const {
Q_ASSERT(d->vsize > 0);
MathMatrix result(d->columns, d->rows);
double* pvR = result.internal_pointer();
double* pv0 = internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pvR, pv0+=d->columns) {
pv0 = i % d->rows ? pv0 : internal_pointer() + (i/d->rows);
*pvR = *pv0;
}
return result;
}
MathMatrix& MathMatrix::transpose() {
Q_ASSERT_X(d->vsize > 0, "MathMatrix::transpose", "This matrix has no dimensions, cannot be transposed;");
d->detach(&d);
double* nv = new double[d->vsize];
double* pv0 = internal_pointer();
double* pvR = &(nv[0]);
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<d->vsize; ++i, ++pvR, pv0+=d->columns) {
pv0 = i % d->rows ? pv0 : internal_pointer() + (i/d->rows);
*pvR = *pv0;
}
// Replacing the internal vector and swap rows and columns values
unsigned int t = d->rows;
d->rows = d->columns;
d->columns = t;
delete[] d->v;
d->v = nv;
return *this;
}
MathMatrix MathMatrix::inverse() const {
Q_ASSERT_X(d->rows == d->columns, "MathMatrix::inverted", "This matrix is not square, cannot be inverted");
double det = determinant();
Q_ASSERT_X(det != 0, "MathMatrix::inverted", "Cannot be inverted because its determinant is zero.");
MathMatrix adjunct(d->rows, d->columns);
for (unsigned int i=0; i<d->rows; ++i) {
for (unsigned int j=0; j<d->columns; ++j) {
MathMatrix sub = subMatrix(i, j);
adjunct.setItem(i, j, std::pow(-1, i+j+2)*sub.determinant());
}
}
adjunct.transpose();
return (1.0/det)*adjunct;
}
MathMatrix& MathMatrix::invert() {
MathMatrix inv = inverse();
d->refCounter--;
if (d->refCounter == 0) delete d;
d = inv.d;
d->refCounter++;
return (*this);
}
MathMatrix MathMatrix::identity(unsigned int size) {
Q_ASSERT(size > 0);
MathMatrix result(size, size);
double* p = result.internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<size; ++i, p+=(size+1))
*p = 1.0;
return result;
}
MathMatrix MathMatrix::diagonal(const std::initializer_list<double>& ditems) {
Q_ASSERT(ditems.size() > 0);
MathMatrix result(ditems.size(), ditems.size());
double *p = result.internal_pointer();
auto iter = ditems.begin();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<ditems.size(); ++i, ++iter, p+=(ditems.size()+1))
*p = *iter;
return result;
}
MathMatrix::operator QString() const {
if (!d->vsize) return "[]";
double *p = internal_pointer();
QStringList ll;
for (unsigned int i=0; i<d->rows; ++i) {
QStringList l;
for (unsigned int j=0; j<d->columns; ++j, ++p) {
l << QString("%1").arg(*p);
}
ll << "[" % l.join(", ") % "]";
}
return "[" % ll.join(", ") % "]";
}
MathMatrix operator*(double x, const MathMatrix& m) {
MathMatrix result(m.rows(), m.columns());
double* pvR = result.internal_pointer();
double* pv0 = m.internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<(m.rows() * m.columns()); ++i, ++pvR, ++pv0)
*pvR = *pv0 * x;
return result;
}
MathMatrix operator/(double x, const MathMatrix& m) {
MathMatrix result(m.rows(), m.columns());
double* pv0 = m.internal_pointer();
double* pvR = result.internal_pointer();
// this can be sliced in multithreaded applications
for (unsigned int i=0; i<(m.rows() * m.columns()); ++i, ++pvR, ++pv0)
*pvR = x / *pv0;
return result;
}