-
Notifications
You must be signed in to change notification settings - Fork 9
/
CMatrix3.cpp
659 lines (540 loc) · 14.2 KB
/
CMatrix3.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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#include "CMatrix3.h"
CMatrix::CMatrix(CMatrix & cm)
{
Create_Mem(cm.Nrows(),cm.Ncols());
for(int i=0 ;i<Nrows();i++)
for(int j=0;j<Ncols();j++)
p->Mat[i][j] = cm.p->Mat[i][j];
}
CMatrix::CMatrix(int r, int c, double init)
{
Create_Mem(r,c);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
p->Mat[i][j] = init;
}
CMatrix::CMatrix(int mrows, int columns, double* initvalues)
{
// create the structure:
p = new matrep;
p->r= mrows;
p->c = columns;
// allocate memory for the actual matrix:
p->Mat = new double *[mrows];
for (int x = 0; x < mrows; x++)
p->Mat[x] = new double[columns];
int c = 0;
for (int i=0; i< mrows; i++)
{
for (int j = 0; j < columns; j++)
mval(i,j) = initvalues[c++];
}
}
CMatrix::CMatrix(int mrows, double* initvalues)
{
// create the structure:
// int n = sizeof(*initvalues);
p = new matrep;
p->r= mrows;
p->c = 1;
// allocate memory for the actual matrix:
p->Mat = new double *[mrows];
for (int x = 0; x < mrows; x++)
p->Mat[x] = new double[1];
int c = 0;
for (int i=0; i< mrows; i++)
{
for (int j = 0; j < 1; j++)
mval(i,j) = initvalues[c++];
}
}
CMatrix::CMatrix(char * flag, int dimension,double init)
{
int i,j;
p = new matrep;
p->r = dimension;
p->c = dimension;
p->Mat = new double *[dimension];
for (int x = 0; x < dimension; x++)
p->Mat[x] = new double[dimension];
switch (flag[0])
{
case 'I':
for (i=0; i< dimension; i++)
{
for (j = 0; j < dimension; j++)
mval(i,j) = (i == j ? 1 : 0);
}
break;
case 'D':
for (i=0; i< dimension; i++)
{
for (j = 0; j < dimension; j++)
mval(i,j) = (i == j ? init : 0);
}
break;
case 'S':
for (i=0; i< dimension; i++)
{
for (j = 0; j < dimension; j++)
mval(i,j) = (i == j ? 0 : init);
}
break;
}
}
CMatrix::CMatrix(char * flag, int dimension)
{
double init=0.0;
int i,j;
p = new matrep;
p->r = dimension;
p->c = dimension;
p->Mat = new double *[dimension];
for (int x = 0; x < dimension; x++)
p->Mat[x] = new double[dimension];
switch (flag[0])
{
case 'I':
for (i=0; i< dimension; i++)
{
for (j = 0; j < dimension; j++)
mval(i,j) = (i == j ? 1 : 0);
}
break;
case 'D':
for (i=0; i< dimension; i++)
{
for (j = 0; j < dimension; j++)
mval(i,j) = (i == j ? init : 0);
}
break;
case 'S':
for (i=0; i< dimension; i++)
{
for (j = 0; j < dimension; j++)
mval(i,j) = (i == j ? 0 : init);
}
break;
}
}
CMatrix::~CMatrix(void)
{
Release_Mem();
}
void CMatrix::Create_Mem(int rows, int cols)
{
p = new matrep;
p->r = rows;
p->c = cols;
// allocate memory for the actual matrix:
p->Mat = new double *[rows];
for (int x = 0; x < rows; x++)
p->Mat[x] = new double[cols];
for(int i = 0; i < rows; i++)
for(int j = 0;j< cols;j++)
p->Mat[i][j] = init_val;
}
void CMatrix::Release_Mem(void)
/* free a float matrix allocated by matrix() */
{
{
for(int x = 0; x < p->r ; x++)
delete p->Mat[x];
delete p->Mat;
delete p;
}
}
CMatrix CMatrix::operator = (const CMatrix & cm)
{
int i,j;
for(int x = 0; x < p->r ; x++)
delete p->Mat[x];
delete p->Mat;
delete p;
p = new matrep;
p->r = cm.Nrows();
p->c = cm.Ncols();
// allocate memory for the actual matrix:
p->Mat = new double *[cm.Nrows()];
for (i = 0; i < Nrows(); i++)
p->Mat[i] = new double[cm.Ncols()];
for(i = 0; i < Nrows(); i++)
for(j = 0;j< Ncols();j++)
p->Mat[i][j] = init_val;
if( (Nrows()!=cm.Nrows()) || (Ncols()!=cm.Ncols())) error("Wrong size matrix assig");
for(i=0;i<Nrows();i++)
for(j=0;j<Ncols();j++)
p->Mat[i][j] = cm.p->Mat[i][j];
return *this;
}
CMatrix CMatrix::operator = (const double d)
{
for(int i = 0 ;i < Nrows();i++)
for(int j=0;j<Ncols();j++)
p->Mat[i][j]=d;
return *this;
}
double & CMatrix::operator()(int m, int n)
{
if( (m > rows()) || (n > cols()) || ( m < 1) || ( n < 1))
{
printf("m=%d,rows=%d n=%d, cols=%d\n",m,rows(),n,cols());
error(" element assigment error");
}
return p->Mat[m-1][n-1];
}
double & CMatrix::operator()(int m)
{
int n=1;
if( (m > rows()) || (n > cols()) || ( m < 1) || ( n < 1))
{
printf("m=%d,rows=%d n=%d, cols=%d\n",m,rows(),n,cols());
error(" element assigment error");
}
return p->Mat[m-1][n-1];//(*this);
}
CMatrix CMatrix::operator + (const CMatrix & M)
{
if( (rows() != M.rows() ) || ( cols() != M.cols()) )error("Wrong size for matrix addition");
int i,j;
CMatrix result(rows(),cols());
for(i=0;i<rows();i++)
{
for(j=0;j<cols();j++)
{
result.p->Mat[i][j] = p->Mat[i][j] + M.p->Mat[i][j];
}
}
return result;
}
CMatrix CMatrix::operator + (const double rval)
{
int i,j;
CMatrix result(rows(),cols());
for(i=0;i<rows();i++)
{
for(j=0;j<cols();j++)
{
result.p->Mat[i][j] = p->Mat[i][j]+ rval;
}
}
return result;
}
CMatrix CMatrix::operator - (const CMatrix & M)
{
if( ( rows() != M.rows() ) || ( cols() != M.cols()))error("Wrong size for matric subtr");
int i,j;
CMatrix result(rows(),cols());
result = (*this);
for(i=0;i<rows();i++)
{
for(j=0;j<cols();j++)
{
result.p->Mat[i][j] = p->Mat[i][j] - M.p->Mat[i][j];
}
}
return result;
}
CMatrix CMatrix::operator - (const double rval)
{
int i,j;
CMatrix result(rows(),cols());
result = (*this);
for(i=0;i<rows();i++)
{
for(j=0;j<cols();j++)
{
result.p->Mat[i][j] = p->Mat[i][j] - rval;
}
}
return result;
}
CMatrix operator * (double f, CMatrix BM)
{
CMatrix m(BM.Nrows(),BM.Ncols());
m = BM*f;
return m;
}
CMatrix CMatrix::operator * (const CMatrix & M)
{
if( (cols() != M.rows() ) ) error("wrong matrix size for multiplication");
CMatrix a(rows(),M.cols());
int i,j,k, c = cols();
double sum=0;
for(i=0;i<rows();i++)
{
for(j=0;j<M.cols();j++)
{
sum=0.0;
for(k=0;k<c;k++)
{
sum = sum + p->Mat[i][k]*M.p->Mat[k][j];
}
a.p->Mat[i][j]=sum;
}
}
return a;
}
CMatrix CMatrix::operator * (double d)
{
CMatrix a(rows(),cols());
int i,j;
for(i=0;i<rows();i++)
{
for(j=0;j<cols();j++)
{
a.p->Mat[i][j]=p->Mat[i][j]*d;
}
}
return a;
}
CMatrix CMatrix::operator / (double d)
{
CMatrix a(rows(),cols());
int i,j;
for(i=0;i<rows();i++)
{
for(j=0;j<cols();j++)
{
a.p->Mat[i][j]=p->Mat[i][j]/d;
}
}
return a;
}
CMatrix CMatrix::operator / (CMatrix M)
{
CMatrix a(rows(),cols()),b(rows(),cols());
a = (*this) * M.inverse();
return a;
}
//************************************************************
double & CMatrix::val(int row, int col)
{
if (row < 0 || row >= rows() || col < 0 || col >= cols())
error("index out of range");
return (mval(row,col));
}
CMatrix CMatrix::transpose()
{
// if(rows() != cols())
// error("matrix must be square to transpose!\n");
CMatrix trans(cols(),rows());
for (int row = 0; row < rows(); row++)
{
for(int col = 0; col < cols(); col++)
{
trans.mval(col,row) = mval(row,col);
}
}
return trans;
}
double CMatrix::determinant()
{
if(rows() != cols()) error("matrix must be square for determinant()");
CMatrix indx(cols()); // create the "index vector"
CMatrix B(cols()); // see pp 38. in Numerical Recipes
int d;
// perform the decomposition once:
CMatrix decomp = lu_decompose(indx,d);
double determinant = d;
for(int i=0; i < cols() ; i++)
determinant *= decomp.mval(i,i);
return determinant;
}
CMatrix CMatrix::inverse()
{
if(rows() != cols()) error("matrix must be square for inverse()");
CMatrix Y("I",rows()); // create an identity matrix
CMatrix indx(cols()); // create the "index vector"
CMatrix B(cols()); // see Press & Flannery
int d;
// perform the decomposition once:
CMatrix decomp = lu_decompose(indx,d);
for(int col = 0; col < cols(); col++){
B.copy_column(Y,col,0);
decomp.lu_back_subst(indx,B);
Y.copy_column(B,0,col);
}
return Y.transpose();
}
/************************************************************
The private support functions for determinant & inverse.
************************************************************/
// copy the from_col of mm to the to_col of "this"
void CMatrix::copy_column(CMatrix& mm, int from_col, int to_col){
if(rows() != mm.rows()) error("number of rows must be equal for copy_column()");
for(int row=0; row < rows(); row++)
mval(row,to_col) = mm.mval(row,from_col);
}
void CMatrix::switch_columns(int col1, int col2)
{
int row;
CMatrix temp(rows());
for(row = 0; row < rows(); row++)
// temporarily store col 1:
temp.mval(row,0) = mval(row,col1);
for(row = 0; row < rows(); row++)
mval(row,col1) = mval(row,col2); // move col2 to col1
for(row = 0; row < rows(); row++)
mval(row,col2) = temp.mval(row,0); // move temp to col2
}
// make an image of a matrix (used in L-U decomposition)
void CMatrix::deepcopy(CMatrix& from, CMatrix& to)
{
if(from.rows() != to.rows() || from.cols() != to.cols())
error("matrices must be equal dimensions for deepcopy()");
for(int row = 0; row < from.rows(); row++) {
for(int col = 0; col < from.cols(); col++)
to.mval(row,col) = from.mval(row,col);
}
}
// scale a matrix (used in L-U decomposition)
CMatrix CMatrix::scale()
{
double temp;
if(rows() <= 0 || cols() <= 0) error("bad matrix size for scale()");
if(rows() != cols()) error("matrix must be square for scale()");
CMatrix scale_vector(rows());
for (int col = 0; col < cols(); col++){
double maximum = 0;
for(int row = 0; row < rows(); row++)
if ((temp = (double)fabs(mval(row,col))) > maximum)
maximum = temp; // find max column magnitude in this row
if(maximum == 0) error("singular matrix in scale()");
scale_vector.mval(col,0) = 1/maximum; // save the scaling
}
return scale_vector;
}
CMatrix CMatrix::lu_decompose(CMatrix& indx, int& d )
{
/*
Returns the L-U decomposition of a matrix. indx is an output
vector which records the row permutation effected by the
partial pivoting, d is output as +-1 depending on whether the
number of row interchanges was even or odd, respectively.
This routine is used in combination with lu_back_subst to
solve linear equations or invert a matrix.
*/
if(rows() != cols()) error("Matrix must be square to L-U decompose!\n");
d = 1; // parity check
int row,col,k,col_max; // counters
double dum; // from the book -- I don't know significance
double sum;
double maximum;
CMatrix lu_decomp(rows(),cols());
// make a direct copy of the original matrix:
deepcopy(*this,lu_decomp);
CMatrix scale_vector = lu_decomp.scale(); // scale the matrix
// The loop over columns of Crout's method:
for(row = 0; row < rows(); row++){
if (row > 0) {
// eqn 2.3.12 except for row=col:
for (col = 0; col <= row-1; col++) {
sum = lu_decomp.mval(row,col);
if(col > 0) {
for(k = 0; k <= col-1; k++)
sum -= lu_decomp.mval(row,k)*lu_decomp.mval(k,col);
lu_decomp.mval(row,col) = sum;
}
}
}
// Initialize for the search for the largest pivot element:
maximum = 0;
// i=j of eq 2.3.12 & i=j+1..N of 2.3.13:
for(col=row; col <= cols()-1; col++){
sum = lu_decomp.mval(row,col);
if(row > 0){
for(k=0; k <= row-1; k++)
sum -= lu_decomp.mval(k,col) * lu_decomp.mval(row,k);
lu_decomp.mval(row,col) = sum;
}
// figure of merit for pivot:
dum = scale_vector.mval(col,0) * fabs(sum);
if (dum >= maximum){ // is it better than the best so far?
col_max = col;
maximum = dum;
}
}
// Do we need to interchange rows?
if(row != col_max) {
lu_decomp.switch_columns(col_max,row); // Yes, do so...
d *= -1; // ... and change the parity of d
// also interchange the scale factor:
dum = scale_vector.mval(col_max,0);
scale_vector.mval(col_max,0) = scale_vector.mval(row,0);
scale_vector.mval(row,0) = dum;
}
indx.mval(row,0) = col_max;
// Now, finally, divide by the pivot element:
if(row != rows() -1){
if(lu_decomp.mval(row,row) == 0)
lu_decomp.mval(row,row) = 1e-20;
// If the pivot element is zero the matrix is
// singular (at least to the precision of the
// algorithm). For some applications on singular
// matrices, it is desirable to substitute tiny for zero
dum = 1/lu_decomp.mval(row,row);
for(col=row+1; col <= cols()-1; col++)
lu_decomp.mval(row,col) *= dum;
}
}
if(lu_decomp.mval(rows()-1,cols()-1) == 0)
lu_decomp.mval(rows()-1,cols()-1) = 1e-20;
return lu_decomp;
}
void CMatrix::lu_back_subst(CMatrix& indx, CMatrix& b)
{
/*
Solves the set of N linear equations A*X = B. Here "this"
is the LU-decomposition of the matrix A, determined by the
routine lu_decompose(). Indx is input as the permutation
vector returned by lu_decompose(). B is input as the
right-hand side vector B, and returns with the solution
vector X. This routine takes into account the possibility
that B will begin with many zero elements, so it is efficient
for use in matrix inversion. See pp 36-37 in
Press & Flannery.
*/
if(rows() != cols())
error ("non-square lu_decomp matrix in lu_back_subst()");
if(rows() != b.rows())
error("wrong size B vector passed to lu_back_subst()");
if(rows() != indx.rows())
error("wrong size indx vector passed to lu_back_subst()");
int row,col,ll;
int ii = 0;
double sum;
for(col=0;col < cols(); col++){
ll= (int)indx.mval(col,0);
sum = b.mval(ll,0);
b.mval(ll,0) = b.mval(col,0);
if (ii >= 0)
for(row = ii; row <= col-1; row++)
sum -= mval(row,col) * b.mval(row,0);
else if(sum != 0)
ii = col;
b.mval(col,0) = sum;
}
for(col = cols() -1; col >= 0; col--){
sum = b.mval(col,0);
if (col < cols() -1)
for (row = col + 1; row <= rows()-1; row++)
sum -= mval(row,col) * b.mval(row,0);
// store a component of the soln vector X:
b.mval(col,0) = sum/mval(col,col);
}
}
void DebugOut(Matrix v)
{
int n = v.Nrows();
int m = v.Ncols();
int i,j;
for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
printf("%10.6f ",v(i,j));
}
printf("\n");
}
}