forked from ColorsWind/algorimatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
429 lines (368 loc) · 10.8 KB
/
Matrix.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
#include "Matrix.h"
#include "MatrixFunc.h"
#include <sstream>
Matrix::Matrix(int row, int col) : ObjectMatrix<double>(row, col) {}
Matrix::Matrix(const Matrix &matrix) = default;
Matrix::Matrix(int row, int col, double *array) : ObjectMatrix<double>(row, col, array) {}
Matrix &Matrix::operator=(const Matrix & m) {
ObjectMatrix::operator=(m);
return *this;
}
Matrix &Matrix::operator+=(const Matrix & m) {
if (m_row != m.m_row || m_col != m.m_col)
throw MatrixException("The number of rows and cols of two matrices is not equal");
for (int i = 0; i < size(); i++)
this->m_array[i] += m.m_array[i];
return *this;
}
Matrix &Matrix::operator-=(const Matrix & m) {
if (m_row != m.m_row || m_col != m.m_col)
throw MatrixException("The number of rows and cols of two matrices is not equal");
for (int i = 0; i < size(); i++)
this->m_array[i] -= m.m_array[i];
return *this;
}
Matrix &Matrix::operator*=(const Matrix & m) {
*this = *this * m;
return *this;
}
Matrix &Matrix::operator/=(const Matrix & m) {
*this *= m.inverse();
return *this;
}
Matrix Matrix::cofactor(int row, int col) const {
if (row < 0 || row >= m_row)
throw MatrixException("Row must be integer 1 to " + to_string(m_row));
if (col < 0 || col >= m_col)
throw MatrixException("Col must be integer 1 to " + to_string(m_col));
Matrix matrix(m_row - 1, m_col - 1);
int k = 0;
for (int i = 0; i < m_row; i++) {
if (i == row) continue;
for (int j = 0; j < m_col; j++) {
if (j == col) continue;
matrix[k] = at(i, j);
k++;
}
}
return matrix;
}
Matrix Matrix::traverse(double (func)(double)) const{
Matrix matrix(m_row, m_col);
for (int k = 0; k < matrix.size(); k++) {
matrix[k] = func(m_array[k]);
}
return matrix;
}
Matrix Matrix::traverse(double (*func)(double, double), const Matrix ¶meter) const {
Matrix matrix(m_row, m_col);
for(int k=0;k<matrix.size();k++) {
matrix[k] = func(m_array[k], parameter.m_array[k]);
}
return matrix;
}
Matrix operator+(const Matrix& m1, const Matrix& m2) {
Matrix matrix = m1;
matrix += m2;
return m1;
}
Matrix operator-(const Matrix& m1, const Matrix& m2) {
Matrix matrix = m1;
matrix-= m2;
return matrix;
}
Matrix operator*(double factor, const Matrix& matrix) {
Matrix multiply(matrix);
for(int k=0;k<multiply.size();k++) multiply[k] *= factor;
return multiply;
}
Matrix operator*(const Matrix& matrix, double factor) {
return operator*(factor, matrix);
}
Matrix operator*(const Matrix& m1, const Matrix& m2) {
if (m1.size() == 1) {
return operator*(m1[0], m2);
}
if (m2.size() == 1) {
return operator*(m2, m1[0]);
}
if (m1.m_col != m2.rows()) {
throw MatrixException("Matrices cannot be multiplied (op1 is "
+ m1.sizeString() + ", op2 is" + m2.sizeString() + ")");
}
Matrix matrix(m1.m_row, m2.m_col);
for(int i=0;i<matrix.m_row;i++) {
for(int j=0;j<matrix.m_col;j++) {
double v = 0;
for(int k=0;k<m1.m_col;k++)
v += m1.at(i, k) * m2.at(k, j);
matrix.at(i, j) = v;
}
}
return matrix;
}
Matrix operator/(const Matrix& m1, const Matrix& m2) {
Matrix matrix = m1;
matrix /= m2;
return matrix;
}
Matrix::Matrix(double number) : Matrix(1, 1, &number) {}
Matrix operator+(const Matrix& m) {
return Matrix(m);
}
Matrix operator-(const Matrix& m) {
Matrix matrix(m);
for(int k=0;k<matrix.size();k++) {
matrix[k] = -matrix[k];
}
return matrix;
}
Matrix identity(int n, double factor) {
if (n < 0)
throw MatrixException("The order of a square matrix must be a nonnegative number.");
Matrix matrix = zeros(n);
for(int k=0;k<n;k++) {
matrix.at(k, k) = factor;
}
return matrix;
}
Matrix zeros(int n) {
if (n < 0)
throw MatrixException("The order of a square matrix must be a nonnegative number.");
return zeros(n, n);
}
Matrix zeros(int row, int col) {
if (row < 0 || col < 0)
throw MatrixException("The number of rows and columns of a matrix must be nonnegative");
Matrix matrix(row, col);
for(int k=0;k<matrix.size();k++) matrix[k] = 0.0;
return matrix;
}
Matrix fromBlock(ObjectMatrix<Matrix> block) {
int totalRow = 0, totalCol = 0; // total;
int* rows = new int[block.rows()];
int* cols = new int[block.cols()];
for(int i=0;i<block.rows();i++) totalRow += rows[i] = block.at(i, 0).rows();
for(int j=0;j<block.cols();j++) totalCol += cols[j] = block.at(0, j).cols();
Matrix matrix(totalRow, totalCol);
int row = 0, col = 0; // line vector index
// ergodic
for(int i=0;i<block.rows();i++) {
for(int j=0;j<block.cols();j++) {
Matrix &sub = block.at(i, j);
// check size
if (rows[i] != sub.rows())
throw MatrixException("Cannot create matrix from blocks, unexpected matrix size: " + sub.sizeString()
+ " (Expect row = " + to_string(rows[i]) + ")");
if (cols[j] != sub.cols())
throw MatrixException("Cannot create matrix from blocks, unexpected matrix size: " + sub.sizeString()
+ " (Expect col = " + to_string(cols[j]) + ")");
for(int k=0;k<sub.rows();k++) {
copy(sub.m_array + sub.first(k), sub.m_array + sub.first(k+1),
matrix.m_array + matrix.first(row + k) + col);
}
col += sub.cols();
}
row += block.at(i, 0).rows();
col = 0;
}
delete [] rows;
delete [] cols;
return matrix;
}
string Matrix::toString() const {
if (this->size() == 0) {
return "[]";
} else if (this->size() == 1) {
return to_string(m_array[0]);
} else {
string str;
for(int i=0;i<m_row;i++) {
for(int j=0;j<m_col;j++) {
str.append(" ");
str.append(to_string(at(i, j)));
}
str.append("\n");
}
return str;
}
}
Matrix::Matrix() : Matrix(0,0){}
void Matrix::addByAnother(int i, int j, double factor) {
for(int k=0; k < m_col; k++) {
at(i, k) += factor * at(j, k);
}
}
void Matrix::multiplyLine(int i, double factor) {
for(int k=0; k < m_col; k++) {
at(i, k) *= factor;
}
}
void Matrix::swapLine(int i, int j) {
double tmp;
for(int k=0; k < m_col; k++) {
tmp = at(i, k);
at(i, k) = at(j, k);
at(j, k) = tmp;
}
}
void Matrix::upperTriangular() {
if (size() <= 1) return;
int k=0;
for(int j=0;j<m_col;j++) {
int noneZero = -1;
for(int i=k;i<m_row;i++) {
if (!isZero(at(i, j))) {
noneZero = i;
break;
}
}
if (noneZero < 0) {
continue;
} else {
if (noneZero != k) addByAnother(k, noneZero);
for(int i=j+1;i<m_row;i++) {
addByAnother(i, j, -at(i, j) / at(k, j));
}
k++;
}
}
}
Matrix Matrix::adjoint() const {
if (!square())
throw MatrixException("Matrix is not a square matrix");
double d = det();
if (isZero(d)) {
Matrix matrix(*this);
for (int i = 0; i < m_row; i++) {
for (int j = 0; j < m_col; j++) {
matrix.at(i, j) = cofactor(i, j).det();
}
}
return matrix;
} else {
return d * inverse();
}
}
double Matrix::det() const {
if (!square())
throw MatrixException("Matrix is not a square matrix");
Matrix upper = Matrix(*this);
upper.upperTriangular();
double result = 1;
for(int k=0;k<m_row;k++) {
result *= upper.at(k, k);
}
return result;
}
Matrix Matrix::inverse() const{
Matrix inv = identity(m_row);
Matrix matrix(*this);
if (size() < 0) return *this;
if (size() == 1) {
matrix[0] = 1 / matrix[0];
return matrix;
}
// upper triangular
int k=0;
for(int j=0;j<m_col;j++) {
int noneZero = -1;
for(int i=k;i<m_row;i++) {
if (!isZero(at(i, j))) {
noneZero = i;
break;
}
}
if (noneZero < 0) {
continue;
} else {
if (noneZero != k) {
matrix.addByAnother(k, noneZero);
inv.addByAnother(k, noneZero);
}
for(int i=k+1;i<m_row;i++) {
double factor = - matrix.at(i, j) / matrix.at(k, j);
matrix.addByAnother(i, j, factor);
inv.addByAnother(i, j, factor);
}
k++;
}
}
// check principal diagonal and unitize
for(int l=0; l < m_row; l++) {
double d = matrix.at(l, l);
if (isZero(d)) {
throw MatrixException("Matrix is not invertible");
} else {
double factor = 1/d;
matrix.multiplyLine(l, factor);
inv.multiplyLine(l, factor);
}
}
// lower triangular
for(int j=1;j<m_col;j++) {
for(int i=0;i<j;i++) {
double factor = -matrix.at(i, j); // matrix.at(j, j) = 1.0
matrix.addByAnother(i, j, factor);
inv.addByAnother(i, j, factor);
}
}
return inv;
}
Matrix Matrix::sub(int row1, int row2, int col1, int col2) const {
// check rows and cols
if (row1 < 0 || row1 >= m_row) {
throw MatrixException("Row1 must be integer 1 to " + to_string(m_row));
}
if (row1 < 0 || row2 >= m_row) {
throw MatrixException("Row2 must be integer 1 to " + to_string(m_row));
}
if (col1 < 0 || row1 >= m_col) {
throw MatrixException("Col1 must be integer 1 to " + to_string(m_row));
}
if (col2 < 0 || row2 >= m_col) {
throw MatrixException("Col2 must be integer 1 to " + to_string(m_row));
}
// ensure op1 < op2
if (row1 > row2) {
int tmp = row2;
row2 = row1;
row1 = tmp;
}
if (col1 > col2) {
int tmp = col2;
col2 = col1;
col1 = tmp;
}
// sub matrix
Matrix matrix(row2-row1, col2-col1);
for(int i=0;i<matrix.m_row;i++) {
for(int j=0;j<matrix.m_col;j++) {
matrix.at(i, j) = at(i + row1, j + col1);
}
}
return matrix;
}
int Matrix::rank() const {
Matrix matrix(*this);
matrix.upperTriangular();
int r = m_row;
int j = m_col - 1;
for(int i = m_row -1; i >= 0; i--) {
if (isZero(at(i, j))) {
r--;
} else {
break;
}
}
return r;
}
Matrix Matrix::transpose() const {
Matrix matrix(m_col, m_row);
for(int i=0;i<m_row;i++) {
for(int j=0;j<m_col;j++) {
matrix.at(j, i) = at(i, j);
}
}
return matrix;
}