-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmatrix.hpp
631 lines (586 loc) · 21.5 KB
/
libmatrix.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#pragma once
#include <iostream>
#include <string>
namespace Matrix
{
template <typename type> class matrix;
// type Exception
typedef const int Exception;
// possible exceptions
static const int EX_0ROWS = 10; // matrix can't have 0 rows
static const int EX_0COLS = 20; // matrix can't have 0 columns
static const int EX_ROUTB = 30; // row index out of bounds
static const int EX_COUTB = 40; // column index out of bounds
static const int EX_INCMP = 50; // incompatible matrices
static const int EX_NOSQR = 60; // not a square matrix
static const int EX_DETR0 = 70; // during inversion, determinant is 0
// more general exceptions
static const int EX_NULLPTR = 100; // null pointer exception
/**
* Throw an exception with value = exception code.
* @throws Matrix::Exception
*/
void throwException(Exception ex, const std::string& msg)
{
#ifdef DEBUG
std::cout << "Matrix::Exception::" << ex << ": " << msg << "\n";
#endif
throw ex;
}
template <typename type>
class matrix
{
#ifdef ALLOW_PRIMITIVES_ONLY
static_assert(std::is_fundamental<type>::value, "Matrix::matrix: template argument should be a primitive type");
#endif
public:
/**
* Only another matrix can access properties of a matrix directly
*/
template <typename T> friend class matrix;
/**
* Get row count of matrix.
* @return int
*/
int rows() const
{
return (int) *this->rows_ptr;
}
/**
* Get column count of matrix.
* @return int
*/
int cols() const
{
return (int) *this->cols_ptr;
}
/**
* Create a new matrix object.
* @param rows If DDA is unknown, pass no. of rows
* @param cols If DDA is unknown, pass no. of cols
* @throws Matrix::Exception Matrix can't have 0 rows - EX_0ROWS
* @throws Matrix::Exception Matrix can't have 0 columns - EX_0COLS
*/
matrix(int rows, int cols)
{
if (rows < 1)
Matrix::throwException(EX_0ROWS, "matrix can't have 0 rows");
if (cols < 1)
Matrix::throwException(EX_0COLS, "matrix can't have 0 columns");
this->rows_ptr = new int(rows);
this->cols_ptr = new int(cols);
this->mtx_ptr = new type*[rows];
for (int i = 0; i < this->rows(); i++) {
this->mtx_ptr[i] = new type[cols];
for (int j = 0; j < this->cols(); j++)
this->mtx_ptr[i][j] = 0;
}
this->refcnt_ptr = new int(1);
}
/**
* Create a new matrix object.
* @param lst 2D Initialiser list
* @throws Matrix::Exception Matrix can't have 0 rows - EX_0ROWS
* @throws Matrix::Exception Matrix can't have 0 columns - EX_0COLS
*/
matrix(std::initializer_list<std::initializer_list<type>> lst)
{
int rows = lst.size();
int cols = lst.begin()->size();
if (rows < 1)
Matrix::throwException(EX_0ROWS, "matrix can't have 0 rows");
if (cols < 1)
Matrix::throwException(EX_0COLS, "matrix can't have 0 columns");
this->rows_ptr = new int(rows);
this->cols_ptr = new int(cols);
this->mtx_ptr = new type*[rows];
int i = 0;
for (const auto& lrow : lst) {
this->mtx_ptr[i] = new type[cols];
int j = 0;
for (const auto& el : lrow) {
this->mtx_ptr[i][j] = el;
j++;
}
i++;
}
this->refcnt_ptr = new int(1);
}
/**
* Create a new matrix object.
* @param rows Row size of DDA
* @param cols Column size of DDA
* @param arr If DDA is known, pass &dda[0][0]
* @throws Matrix::Exception Matrix can't have 0 rows - EX_0ROWS
* @throws Matrix::Exception Matrix can't have 0 columns - EX_0COLS
*/
matrix(int rows, int cols, type *arr)
{
if (rows < 1)
Matrix::throwException(EX_0ROWS, "matrix can't have 0 rows");
if (cols < 1)
Matrix::throwException(EX_0COLS, "matrix can't have 0 columns");
this->rows_ptr = new int(rows);
this->cols_ptr = new int(cols);
this->mtx_ptr = new type*[rows];
for (int i = 0; i < this->rows(); i++) {
this->mtx_ptr[i] = new type[cols];
for (int j = 0; j < this->cols(); j++)
this->mtx_ptr[i][j] = arr[i*cols +j];
}
this->refcnt_ptr = new int(1);
}
/**
* Copy a matrix object via constructor.
* matrix uses smart reference counting approach. When all references are cleared, the memory is auto deleted.
* @param m2 The source matrix
*/
matrix(const matrix<type>& m2)
{
if (!m2.refcnt_ptr) Matrix::throwException(EX_NULLPTR, "null pointer exception");
this->mtx_ptr = m2.mtx_ptr;
this->rows_ptr = m2.rows_ptr;
this->cols_ptr = m2.cols_ptr;
this->refcnt_ptr = m2.refcnt_ptr;
(*this->refcnt_ptr)++;
}
/**
* Copy a matrix object through assignment.
* matrix uses smart reference counting approach. When all references are cleared, the memory is auto deleted.
* @param m2 The source matrix
*/
matrix<type> operator=(const matrix<type>& m2)
{
if (!m2.refcnt_ptr) Matrix::throwException(EX_NULLPTR, "null pointer exception");
// release old matrix data
(*this->refcnt_ptr)--;
if ((*this->refcnt_ptr) == 0) {
for (int i = 0; i < this->rows(); i++)
delete[] this->mtx_ptr[i];
delete[] this->mtx_ptr;
delete this->rows_ptr;
delete this->cols_ptr;
delete this->refcnt_ptr;
}
// catch new matrix data
this->mtx_ptr = m2.mtx_ptr;
this->rows_ptr = m2.rows_ptr;
this->cols_ptr = m2.cols_ptr;
this->refcnt_ptr = m2.refcnt_ptr;
(*this->refcnt_ptr)++;
return *this;
}
/**
* Construct a matrix<double> from a matrix<type>.
*/
matrix<double> toDoubleMatrix()
{
matrix<double> dm;
dm.rows_ptr = new int(this->rows());
dm.cols_ptr = new int(this->cols());
dm.refcnt_ptr = new int(1);
dm.mtx_ptr = new double*[this->rows()];
for (int i = 0; i < dm.rows(); i++) {
dm.mtx_ptr[i] = new double[this->cols()];
for (int j = 0; j < dm.cols(); j++)
dm[i][j] = (double) this->mtx_ptr[i][j];
}
return dm;
}
/**
* Clears the array of the matrix instance on scope exit.
*/
~matrix()
{
(*this->refcnt_ptr)--;
if ((*this->refcnt_ptr) == 0) {
for (int i = 0; i < this->rows(); i++)
delete[] this->mtx_ptr[i];
delete[] this->mtx_ptr;
delete this->rows_ptr;
delete this->cols_ptr;
delete this->refcnt_ptr;
}
}
/**
* Get an element of the matrix from an index.
* @param i Row wise position of element
* @param j Column wise position of element
* @return <type> The value at index i, j
* @throws Matrix::Exception Row index out of bounds - EX_ROUTB
* @throws Matrix::Exception Column index out of bounds - EX_COUTB
*/
type get(int i, int j)
{
if (i < 0 || i > this->rows())
Matrix::throwException(EX_ROUTB, "row index out of bounds");
if (j < 0 || j > this->rows())
Matrix::throwException(EX_COUTB, "column index out of bounds");
return this->mtx_ptr[i][j];
}
/**
* Set an element of the matrix to an index.
* @param i Row wise position of element
* @param j Column wise position of element
* @param val Value to be set
* @throws Matrix::Exception Row index out of bounds - EX_ROUTB
* @throws Matrix::Exception Column index out of bounds - EX_COUTB
*/
void set(int i, int j, type val)
{
if (i < 0 || i > this->rows())
Matrix::throwException(EX_ROUTB, "row index out of bounds");
if (j < 0 || j > this->cols())
Matrix::throwException(EX_COUTB, "column index out of bounds");
this->mtx_ptr[i][j] = val;
}
/**
* Access a posn of the matrix.
* @param i Row of matrix
*/
type*& operator[](int i) const
{
return this->mtx_ptr[i];
}
/*
* Compares two matrices for equality.
* @param m2 The matrix to compare to
* @return boolean True if equal
*/
bool equals(const matrix<type>& m2)
{
if (this->rows() != m2.rows() || this->cols() != m2.cols())
return false;
for (int i = 0; i < this->rows(); i++)
for (int j = 0; j < this->cols(); j++)
if (this->mtx_ptr[i][j] != m2[i][j])
return false;
return true;
}
/**
* Add two compatible matrices.
* @param m2 The matrix to add
* @return matrix<type> The matrix of sums
* @throws Matrix::Exception If matrices aren't compatible - EX_INCMP
*/
matrix<type> add(const matrix<type>& m2, bool sub = false)
{
if (m2.rows() != this->rows() || m2.cols() != this->cols())
Matrix::throwException(EX_INCMP, "incompatible matrices for addition");
matrix<type> nm = matrix<type>(this->rows(), this->cols());
for (int i = 0; i < this->rows(); i++)
for (int j = 0; j < this->cols(); j++) {
type rslt = this->mtx_ptr[i][j] + (sub ? (-1) * m2[i][j] : m2[i][j]);
nm[i][j] = rslt;
}
return nm;
}
/**
* Subtract 2nd from 1st matrix.
* @param m2 The matrix to subtract
* @return matrix<type> The matrix of differences
* @throws Matrix::Exception If matrices aren't compatible - EX_INCMP
*/
matrix<type> subtract(const matrix<type>& m2)
{
if (m2.rows() != this->rows() || m2.cols() != this->cols())
Matrix::throwException(EX_INCMP, "incompatible matrices for subtraction");
return this->add(m2, true);
}
/**
* Multiply a matrix by a scalar.
* @param scalar Scalar to multiply by
* @return matrix The matrix of products
*/
matrix<type> scale(type scalar)
{
matrix<type> nm = matrix<type>(this->rows(), this->cols());
for (int i = 0; i < this->rows(); i++)
for (int j = 0; j < this->cols(); j++) {
type rslt = scalar * this->mtx_ptr[i][j];
nm[i][j] = rslt;
}
return nm;
}
/**
* Multiply two compatible matrices.
* @param m2 The matrix to multiply by
* @return matrix<type> The matrix after multiplication
* @throws Matrix::Exception If matrices aren't compatible - EX_INCMP
*/
matrix<type> multiply(const matrix<type>& m2)
{
if (this->cols() != m2.rows())
Matrix::throwException(EX_INCMP, "incompatible matrices for multiplication");
int m = this->rows();
int n = this->cols(); // same
n = m2.rows(); // same
int o = m2.cols();
matrix<type> nm = matrix<type>(m, o);
for (int i = 0; i < m; i++)
for (int j = 0; j < o; j++) {
double sum = 0;
for (int k = 0; k < n; k++)
sum += this->mtx_ptr[i][k] * m2[k][j];
nm[i][j] = sum;
}
return nm;
}
/**
* Calculate matrix to the power of +ve integer.
* @param index Power of matrix
* @return matrix<type> The resulting matrix
* @throws Matrix::Exception Same as Exceptions of matrix::multiply method
*/
matrix<type> power(int index)
{
matrix<type> nm = *this;
for (int i = 0; i < index - 1; i++) {
matrix<type> tmp = nm.multiply(*this);
nm = tmp;
}
return nm;
}
/*
* Compares two matrices for equality.
* @param m2 The matrix to compare to
* @return boolean True if equal
*/
bool operator==(const matrix<type>& m2)
{
return this->equals(m2);
}
/**
* Add two compatible matrices.
* @param m2 The matrix to add
* @return matrix<type> The matrix of sums
* @throws Matrix::Exception If matrices aren't compatible - EX_INCMP
*/
matrix<type> operator+(const matrix<type>& m2)
{
return this->add(m2);
}
/**
* Subtract 2nd from 1st matrix.
* @param m2 The matrix to subtract
* @return matrix<type> The matrix of differences
* @throws Matrix::Exception If matrices aren't compatible - EX_INCMP
*/
matrix<type> operator-(const matrix<type>& m2)
{
return this->subtract(m2);
}
/**
* Multiply a matrix by a scalar.
* @param scalar Scalar to multiply by
* @return matrix The matrix of products
*/
matrix<type> operator*(type scalar)
{
return this->scale(scalar);
}
/**
* Multiply two compatible matrices.
* @param m2 The matrix to multiply by
* @return matrix<type> The matrix after multiplication
* @throws Matrix::Exception If matrices aren't compatible - EX_INCMP
*/
matrix<type> operator*(const matrix<type>& m2)
{
return this->multiply(m2);
}
/**
* Calculate matrix to the power of +ve integer
* @param index Power of matrix
* @return matrix<type> The resulting matrix
* @throws Matrix::Exception Same as Exceptions of matrix::multiply method
*/
matrix<type> operator^(int index)
{
return this->power(index);
}
/**
* Excludes a row and a column and generates a sub matrix.
* Useful for calculating determinants and cofactor matrices.
* @param row The row to exclude
* @param col The column to exclude
* @return matrix<type> The sub matrix
* @throws Matrix::Exception Row index out of bounds - EX_ROUTB
* @throws Matrix::Exception Column index out of bounds - EX_COUTB
*/
matrix<type> excludeRowCol(int row, int col)
{
if (row < 0 || row > this->rows())
Matrix::throwException(EX_ROUTB, "row index out of bounds");
if (col < 0 || col > this->rows())
Matrix::throwException(EX_COUTB, "column index out of bounds");
matrix<type> subm = matrix<type>(this->rows() - 1, this->cols() - 1);
bool skipRow = false;
for (int j = 0; j < this->rows() - 1; j++) {
bool skipCol = false;
int jSelf = j;
if (j == row)
skipRow = true;
if (skipRow)
jSelf++;
for (int k = 0; k < this->cols() - 1; k++) {
int kSelf = k;
if (k == col)
skipCol = true;
if (skipCol)
kSelf++;
subm[j][k] = this->mtx_ptr[jSelf][kSelf];
}
}
return subm;
}
/**
* Calculate determinant of this matrix.
* @return double The determinant
* @throw Exception If matrix isn't a square matrix - EX_NOSQR
*/
double determinant()
{
if (this->rows() != this->cols())
Matrix::throwException(EX_NOSQR, "not a square matrix");
int n = this->rows();
double det = 0;
if (n == 1)
return this->mtx_ptr[0][0];
else if (n == 2)
return this->mtx_ptr[0][0] * this->mtx_ptr[1][1] - this->mtx_ptr[0][1] * this->mtx_ptr[1][0];
else
for (int i = 0; i < n; i++) {
double coeff = pow(-1, i) * this->mtx_ptr[0][i];
matrix<type> subm = this->excludeRowCol(0, i);
double subdet = subm.determinant();
double term = coeff * subdet;
det += term;
}
return det;
}
/**
* Calculate the transpose of this matrix.
* @return matrix<type> The transpose
*/
matrix<type> transpose()
{
matrix<type> nm = matrix<type>(this->cols(), this->rows());
for (int i = 0; i < this->rows(); i++)
for (int j = 0; j < this->cols(); j++)
nm[j][i] = this->mtx_ptr[i][j];
return nm;
}
/**
* Calculate the cofactor matrix of this matrix.
* @return matrix<type> The cofactor matrix
* @throws Matrix::Exception If matrix isn't a square matrix - EX_NOSQR
*/
matrix<type> cofactor()
{
if (this->rows() != this->cols())
Matrix::throwException(EX_NOSQR, "not a square matrix");
matrix<type> nm = matrix<type>(this->rows(), this->cols());
for (int i = 0; i < this->rows(); i++)
for (int j = 0; j < this->cols(); j++) {
double coeff = pow(-1, i + 1 + j + 1);
matrix<type> subm = this->excludeRowCol(i, j);
nm[i][j] = coeff * subm.determinant();
}
return nm;
}
/**
* Calculate the adjoint of this matrix.
* @return matrix<type> The adjoint
* @throws Matrix::Exception If matrix isn't a square matrix - EX_NOSQR
*/
matrix<type> adjoint()
{
if (this->rows() != this->cols())
Matrix::throwException(EX_NOSQR, "not a square matrix");
return this->cofactor().transpose();
}
/**
* Calculate the inverse of this matrix.
* @return matrix<double> The inverse
* @throws Matrix::Exception If matrix isn't a square matrix - EX_NOSQR
* @throws Matrix::Exception If determinant is 0 - EX_DETR0
*/
matrix<double> inverse()
{
if (this->rows() != this->cols())
Matrix::throwException(EX_NOSQR, "not a square matrix");
double determinant = this->determinant();
if (determinant == 0)
Matrix::throwException(EX_DETR0, "determinant is 0");
return this->toDoubleMatrix().adjoint().scale(1/determinant);
}
/**
* Display the matrix.
* @param msg? The message to print
*/
void print(const std::string& msg = "")
{
if (msg != "")
std::cout << msg << "\n";
for (int i = 0; i < this->rows(); i++) {
for (int j = 0; j < this->cols(); j++)
std::cout << this->mtx_ptr[i][j] << ((j < this->cols() -1)? ", " : "");
std::cout << "\n";
}
}
private:
/**
* Row number of this matrix.
*/
int *rows_ptr;
/**
* Column number of this matrix.
*/
int *cols_ptr;
/**
* Double pointer to matrix data location.
*/
type **mtx_ptr;
/**
* Stores a count of references.
*/
int *refcnt_ptr;
/**
* Default constructor.
*/
matrix()
: rows_ptr(nullptr), cols_ptr(nullptr), mtx_ptr(nullptr), refcnt_ptr(nullptr)
{}
};
/**
* Create a null matrix of given size.
* @param n Rows of matrix
* @param cols? Cols of matrix
* @return matrix<type> A null matrix
* @throws Matrix::Exception Row index out of bounds - EX_ROUTB
* @throws Matrix::Exception Column index out of bounds - EX_COUTB
*/
template <typename type>
matrix<type> O(int n, int cols = 0)
{
if (cols == 0)
cols = n;
return matrix<type>(n, cols);
}
/**
* Create a unit matrix of given size.
* @param n Size of matrix
* @return matrix<type> A unit matrix
* @throws Matrix::Exception Row index out of bounds - EX_ROUTB
* @throws Matrix::Exception Column index out of bounds - EX_COUTB
*/
template <typename type>
matrix<type> I(int n)
{
matrix<type> im = matrix<type>(n, n);
for (int i = 0; i < n; i++)
im[i][i] = 1;
return im;
}
}