-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
457 lines (422 loc) · 10.1 KB
/
matrix.cpp
File metadata and controls
457 lines (422 loc) · 10.1 KB
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
#include <cstdlib>
#include <cstdarg>
#include <cmath>
#include <iostream>
#include <iomanip>
#include "matrix.hpp"
namespace caams
{
matrix::matrix(long n_rows, long n_cols, matrix_flags flags) {
rows = n_rows;
cols = n_cols;
long N=n_rows*n_cols;
data = new double[N];
if(flags&init){
double *d = data;
if(flags&identity){
if(rows!=cols){
std::cout << "matrix::matrix : identity requested but rows not equal columns" << std::endl;
std::exit(1);
}
for(long r=0;r<rows;r++){
for(long c=0;c<cols;c++){
if(r==c){
*(d++) = 1.0;
}else{
*(d++) = 0.0;
}
}
}
}else{
for(long i=0;i<N;i++){
*(d++)=0.0;
}
}
}
}
matrix::matrix(long n_rows, long n_cols, double a11, ...){
rows = n_rows;
cols = n_cols;
long N=n_rows*n_cols;
data = new double[N];
va_list args;
va_start(args, a11);
double *d = data;
*(d++)=a11;
for(long i=1;i<N;i++){
*(d++) = va_arg(args, double);
}
va_end(args);
}
matrix::matrix(long n_rows, long n_cols, double *src){
rows = n_rows;
cols = n_cols;
long N=rows*cols;
data = new double[N];
double *d = data;
for(long i=N;i;i--){
*(d++) = *(src++);
}
}
matrix::matrix(matrix const & m){
rows = m.rows;
cols = m.cols;
long N=rows*cols;
data = new double[N];
double *d = data;
double *s = m.data;
for(long i=0;i<N;i++){
*(d++) = *(s++);
}
}
matrix::~matrix(void){
delete [] data;
}
matrix & matrix::operator=(matrix const & m){
if(rows!=m.rows || cols!=m.cols){
std::cout << "matrix::operator= : unequal matrix dimensions" << std::endl;
std::exit(1);
}
double *d = data;
double *s = m.data;
long N=rows*cols;
for(long i=0;i<N;i++){
*(d++)=*(s++);
}
return *this;
}
matrix & matrix::operator+=(matrix const & m){
if(rows!=m.rows || cols!=m.cols){
std::cout << "matrix::operator+= : unequal matrix dimensions" << std::endl;
std::exit(1);
}
double *d = data;
double *s = m.data;
long N=rows*cols;
for(long i=0;i<N;i++){
*(d++)+=*(s++);
}
return *this;
}
matrix & matrix::operator-=(matrix const & m){
if(rows!=m.rows || cols!=m.cols){
std::cout << "matrix::operator-= : unequal matrix dimensions" << std::endl;
std::exit(1);
}
double *d = data;
double *s = m.data;
long N=rows*cols;
for(long i=0;i<N;i++){
*(d++)-=*(s++);
}
return *this;
}
matrix & matrix::operator*=(matrix const &m){
if(rows!=cols || rows!=m.rows || cols!=m.cols){
std::cout << "matrix::operator*= : inconsistent matrix dimensions" << std::endl;
std::exit(1);
}
*this = *this * m;
return *this;
}
matrix matrix::operator~(void){
matrix res(cols,rows);
double *d = res.data;
double *s = data;
for(long r=0;r<rows;r++){
for(long c=0;c<cols;c++){
*d = *(s++);
d += res.cols;
}
d -= res.cols*res.rows-1;
}
return res;
}
long matrix::n_rows(void){
return rows;
}
long matrix::n_cols(void){
return cols;
}
matrix matrix::inverse(void){
if(rows!=cols){
std::cout << "matrix::inverse : rows not equal columns" << std::endl;
std::exit(1);
}
// create identity matrix
//dmatrixIdentity( inv );
matrix inv(rows,cols,init_identity);
// make a copy of the original
//dmatrix *m_copy = dmatrixNew( m->n_rows, m->n_cols );
//dmatrixCopy( m, m_copy );
matrix m_copy( *this );
// perform row operations to obtain echelon form
long pivot;
double *pivot0 = m_copy.data;
double *row_inv0 = inv.data;
for(pivot=0;pivot<(rows-1);pivot++){
// find the row with the largest absolute value
double *col_data = pivot0;
double *col_inv_data = row_inv0;
long row;
double d_max = 0.0;
long row_max;
double *row_max_data;
double *row_inv_max_data;
for(row=pivot;row<rows;row++){
double d_abs = std::fabs(*col_data);
if( d_abs > d_max ){
d_max = d_abs;
row_max = row;
row_max_data = col_data;
row_inv_max_data = col_inv_data;
}
col_data += cols;
col_inv_data += inv.cols;
}
// use the maximum row to reduce the other rows under the pivot
col_data = pivot0;
col_inv_data = row_inv0;
for(row=pivot;row<rows;row++){
if(row!=row_max){
long col;
double *data = col_data;
double *data_reduce = row_max_data;
double alpha = data[0]/data_reduce[0];
for(col=pivot;col<cols;col++){
*data -= alpha * *data_reduce;
data++;
data_reduce++;
}
col_data[0] = 0.0;
data = col_inv_data;
data_reduce = row_inv_max_data;
for(col=0;col<inv.cols;col++){
*data -= alpha * *data_reduce;
data++;
data_reduce++;
}
}
col_data += cols;
col_inv_data += inv.cols;
}
// exchange the maximum row with the pivot
if( row_max!=pivot ){
double *src0 = pivot0;
double *src1 = row_max_data;
long col;
double d_temp;
for(col=pivot;col<cols;col++){
d_temp = *src0;
*src0 = *src1;
*src1 = d_temp;
src0++;
src1++;
}
src0 = row_inv0;
src1 = row_inv_max_data;
for(col=0;col<inv.cols;col++){
d_temp = *src0;
*src0 = *src1;
*src1 = d_temp;
src0++;
src1++;
}
}
pivot0 += cols + 1;
row_inv0 += inv.cols;
}
//printf("Matricis after row reduction:\n");
//dmatrixPrint( m_copy, "m_copy" );
//dmatrixPrint( inv, "inv" );
//std::cout << "Matricis after row reduction:" << std::endl;
//m_copy.print("m_copy");
//inv.print("inv");
// normalize the diagonal elements
pivot0 = m_copy.data;
row_inv0 = inv.data;
for(pivot=0;pivot<rows;pivot++){
// normalize this row
double alpha = 1.0 / *pivot0;
double *data = pivot0;
long col;
for(col=pivot;col<cols;col++){
*(data++) *= alpha;
}
*pivot0 = 1.0;
data = row_inv0;
for(col=0;col<inv.cols;col++){
*(data++) *= alpha;
}
pivot0 += cols + 1;
row_inv0 += inv.cols;
}
//printf("Matricis after diagonal normalization:\n");
//dmatrixPrint( m_copy, "m_copy" );
//dmatrixPrint( inv, "inv" );
//std::cout << "Matricis after diagonal normalization:" << std::endl;
//m_copy.print("m_copy");
//inv.print("inv");
// perform row operations to obtain identity
pivot0 = m_copy.data + m_copy.cols*m_copy.rows - 1;
row_inv0 = inv.data + inv.cols*(inv.rows-1);
for(pivot=cols;pivot>1;pivot--){
double *col_data = pivot0 - cols;
double *col_inv_data = row_inv0 - inv.cols;
long row;
for(row=pivot-1;row;row--){
double alpha = *col_data;
// reduce the row in the original
*col_data -= alpha; // * *pivot0;
// reduce the row in the inverse
double *data = col_inv_data;
double *data_reduce = row_inv0;
long col;
for(col=0;col<inv.cols;col++){
*(data++) -= *(data_reduce++) * alpha;
}
col_data -= cols;
col_inv_data -= inv.cols;
}
pivot0 -= cols + 1;
row_inv0 -= inv.cols;
}
//printf("Matricis after upper row reduction:\n");
//dmatrixPrint( m_copy, "m_copy" );
//dmatrixPrint( inv, "inv" );
//std::cout << "Matricis after upper row reduction:" << std::endl;
//m_copy.print("m_copy");
//inv.print("inv");
// free the copy
//dmatrixFree( m_copy );
return inv;
}
void matrix::sub(matrix const & m, long row, long col){
row--;
col--;
if( (m.rows+row)>rows || (m.cols+col)>cols ){
std::cout << "matrix::sub : sub matrix can not fit" << std::endl;
std::exit(1);
}
double *d = data + row*cols + col;
double *s = m.data;
for(long r=0;r<m.rows;r++){
for(long c=0;c<m.cols;c++){
*(d++) = *(s++);
}
d+=cols-m.cols;
}
}
void matrix::sub(double s, long row, long col){
row--;
col--;
if(row>=rows || col>=cols){
std::cout << "matrix::sub : row or col outside of matrix" << std::endl;
std::exit(1);
}
data[row*cols+col] = s;
}
matrix matrix::sub(long nrows, long ncols, long row, long col){
row--;
col--;
if( (row+nrows)>rows || (col+ncols)>cols ){
std::cout << "matrix::sub : sub matrix beyond matrix size" << std::endl;
std::exit(1);
}
matrix res(nrows,ncols);
double *d = res.data;
double *s = &data[row*cols+col];
for(long r=0;r<nrows;r++){
for(long c=0;c<ncols;c++){
*(d++) = *(s++);
}
s+=cols-ncols;
}
return res;
}
void matrix::print(const char *m){
std::cout << m << std::endl;
double *d = data;
for(long r=0;r<rows;r++){
for(long c=0;c<cols;c++){
std::cout << std::setprecision(15) << *(d++);
if(c<(cols-1)){
std::cout << " ";
}
}
std::cout << std::endl;
}
}
matrix operator+(matrix const & m){
return m;
}
matrix operator-(matrix const & m){
return -1.0*m;
}
matrix operator+(matrix const & m1, matrix const & m2){
if(m1.rows!=m2.rows || m1.cols!=m2.cols){
std::cout << "operator+ : matricis are different dimensions" << std::endl;
std::exit(1);
}
matrix r(m1.rows,m1.cols);
double *d = r.data;
double *s1 = m1.data;
double *s2 = m2.data;
for(long i=m1.rows*m1.cols;i;i--){
*(d++) = *(s1++) + *(s2++);
}
return r;
}
matrix operator-(matrix const & m1, matrix const & m2){
if(m1.rows!=m2.rows || m1.cols!=m2.cols){
std::cout << "operator- : matricis are different dimensions" << std::endl;
std::exit(1);
}
matrix r(m1.rows,m1.cols);
double *d = r.data;
double *s1 = m1.data;
double *s2 = m2.data;
for(long i=m1.rows*m1.cols;i;i--){
*(d++) = *(s1++) - *(s2++);
}
return r;
}
matrix operator*(matrix const &m1, matrix const &m2){
if(m1.cols!=m2.rows){
std::cout << "operator*(matrix,matrix) : inconsistent matrix dimensions" << std::endl;
std::exit(1);
}
matrix res(m1.rows,m2.cols);
double *d = res.data;
double *s1 = m1.data;
double *s2 = m2.data;
for(long r=0;r<res.rows;r++){
for(long c=0;c<res.cols;c++){
*d = 0.0;
for(long i=0;i<m1.cols;i++){
*d += *s1 * *s2;
s1++;
s2+=m2.cols;
}
d++;
s1-=m1.cols;
s2-=m2.cols*m2.rows-1;
}
s1+=m1.cols;
s2=m2.data;
}
return res;
}
matrix operator*(double const &t, matrix const & m){
matrix res(m.rows,m.cols);
double *d = res.data;
double *s = m.data;
for(long i=m.rows*m.cols;i;i--){
*(d++) = *(s++) * t;
}
return res;
}
matrix operator*(matrix const &m, double const &t){
return t*m;
}
}