-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPseudoBlockMatrix.hpp
More file actions
987 lines (891 loc) · 28.6 KB
/
Copy pathPseudoBlockMatrix.hpp
File metadata and controls
987 lines (891 loc) · 28.6 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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
#ifndef _PSEUDOBLOCKMATRIX_
#define _PSEUDOBLOCKMATRIX_
#include <iostream>
#include <cstddef>
#include <assert.h>
#include <math.h>
namespace PSEUDOBLOCKMATRIX
{
/// @brief Enum for state of the PBM
enum PBMState { undefined, initialized, allocated };
/// @brief PseudoBlockMatrix (PBM) Class
class PseudoBlockMatrix
{
private:
/// @brief State of this PBM
PBMState _state = undefined;
double*** blocks; //!< Ptr array of dense blocks
double *denseRow; //!< 1D dense final row
double *denseCol; //!< 1D dense final column
double denseCorner; //!< 0D final corner value
double **A; //!< Full size matrix for direct solve (Allocated but otherwise unused)
int *rowStart; //!< Indices of first row per block.
int *colStart; //!< Indices of first col per block.
public:
int N; //!< Number of rows
int M; //!< Number of cols
int *blockN; //!< Number of rows per block
int *blockM; //!< Number of cols per block
int *blockPicker; //!< Finds block given row address
int Nblocks; //!< Number of blocks
/// @brief Prints the entire matrix.
void Print();
/**
* @brief Prints one row of the matrix
* @param[in] row
*/
void PrintRow( int row );
/**
* @brief Sums one row of the matrix
* @param[in] row
* @return double Sum of the row
*/
double SumRow( int row );
/**
* @brief Allocates space for this PBM
*
* @param tNblocks Number of blocks
* @param tblockN Ptr array of block row sizes
* @param tblockM Ptr array of block col sizes
*/
void allocate(int tNblocks, int* tblockN, int* tblockM);
/**
* @brief Get a matrix entry over dense blocks only
*
* @param i Row
* @param j Column
* @return double A_ij
*/
double getOverBlocks( int i, int j );
/**
* @brief Get a matrix entry located within one known block
*
* @param[in] b Block index
* @param[in] i Row from block start
* @param[in] j Column from block start
* @return double B_ij
*/
double getFromBlock( int b, int i, int j );
/**
* @brief Get a matrix entry over denseRow/Col/Corner
*
* @param i Row
* @param j Column
* @return double A_ij
*/
double getOverDense( int i, int j );
/**
* @brief Get any matrix entry
*
* @param i Row
* @param j Column
* @return double A_ij
*/
double get( int i , int j );
/**
* @brief Set a matrix entry over dense blocks
*
* @param i Row
* @param j Column
* @param val Value to set
* @return true Valid i/j pair
* @return false Invalid i/j pair
*/
bool setOverBlocks( int i, int j, double val );
/**
* @brief Set a matrix entry in a known block
*
* @param b Block index
* @param i Row from row start
* @param j Column from column start
* @param val Value to set
* @return true Valid i/j pair
* @return false Invalid i/j pair
*/
bool setFromBlock( int b, int i, int j, double val );
/**
* @brief Set a matrix entry over denseRow/Col/Corner
*
* @param i Row
* @param j Column
* @param val Value to set
* @return true Valid i/j pair
* @return false Invalid i/j pair
*/
bool setOverDense( int i, int j, double val );
/**
* @brief Set any matrix entry
*
* @param i Row
* @param j Column
* @param val Value to set
* @return true Valid i/j pair
* @return false Invalid i/j pair
*/
bool set( int i , int j, double val );
/**
* @brief Set a matrix entry over dense blocks
*
* @param i Row
* @param j Column
* @param val Value to set
* @return true Valid i/j pair
* @return false Invalid i/j pair
*/
bool addOverBlocks( int i, int j, double val );
/**
* @brief Set a matrix entry in a known block
*
* @param b Block index
* @param i Row from row start
* @param j Column from column start
* @param val Value to set
* @return true Valid i/j pair
* @return false Invalid i/j pair
*/
bool addFromBlock( int b, int i, int j, double val );
/**
* @brief Set a matrix entry over denseRow/Col/Corner
*
* @param i Row
* @param j Column
* @param val Value to set
* @return true Valid i/j pair
* @return false Invalid i/j pair
*/
bool addOverDense( int i, int j, double val );
/**
* @brief Set any matrix entry
*
* @param i Row
* @param j Column
* @param val Value to set
* @return true Valid i/j pair
* @return false Invalid i/j pair
*/
bool add( int i , int j, double val );
/**
* @brief Fill the entire PBM with a value
*
* @param val Value to fill
*/
void fill( double val );
/**
* @brief Fill the entire matrix with random values up to mag
* Uniformly distributed up to value mag
*
* @param mag Max magnitude
*/
void fillRand( double mag );
/**
* @brief Fill the entire matrix with random values up to mag, assert diagonal dominance
* Asserts diagonal dominance by adding absolute value of row i to A_ii
*
* @param mag Max magnitude
*/
void fillRandDD( double mag );
/**
* @brief Check residual via L2{ A*x-B }
*
* @param x Prospective solution to Ax-B
* @param B Right-hand side of Ax=B
* @return double L2 norm of residual vector
*/
double checkRes( const double *x, const double *B );
/**
* @brief Returns a PBM of same size as this PBM (Should never be used.)
*
* @return PseudoBlockMatrix Shallow copy.
*/
PseudoBlockMatrix shallowCopy( PseudoBlockMatrix );
/**
* @brief PseudoBlockMatrix Vector Multiplication
* Computes B in Ax=B
*
* @param[in] x
* @param[out] b
*/
void PBMVM( const double *x, double *b );
/**
* @brief Solve the matrix equation directly with Gaussian Elimination
*
* @param[in] rhs The B vector in Ax=B
* @param[out] x The solution to Ax=B
* @return true Residual check passed
* @return false Residual check failed
*/
bool directsolve( const double *rhs, double *x);
/**
* @brief Solve the matrix equation using PBM-structured Gauss-Seidel iteration
*
* @param[in] rhs The B vector in Ax=B
* @param[in] x0 Initial guess for x
* @param[in] resTol Absolute L2 norm residual tolerance
* @param[in] convTol Absolute iterative convergence tolerance
* @param[out] x The solution to Ax=B
* @return true Tolerances met in less than max iterations
* @return false Tolerances not met in less than max iterations
*/
bool GaussSeidel( double *rhs, const double *x0, double resTol, double convTol, double *x );
/**
* @brief Solve the matrix equation using Gauss-Seidel iteration on the dense 2D matrix A
*
* @param[in] rhs The B vector in Ax=B
* @param[in] x0 Initial guess for x
* @param[in] resTol Absolute L2 norm residual tolerance
* @param[in] convTol Absolute iterative convergence tolerance
* @param[out] x The solution to Ax=B
* @return true Tolerances met in less than max iterations
* @return false Tolerances not met in less than max iterations
*/
bool denseGaussSeidel( double *rhs, const double *x0, double resTol, double convTol, double *x );
/**
* @brief Construct a new PseudoBlockMatrix object
*
*/
PseudoBlockMatrix()
{
srand(time(NULL));
_state = initialized;
}
/**
* @brief Construct a new PseudoBlockMatrix object
*
* @param tNblocks Number of blocks
* @param tblockN Ptr array of block row sizes
* @param tblockM Ptr array of block col sizes
*/
PseudoBlockMatrix(
int tNblocks,
int* tblockN,
int* tblockM) : PseudoBlockMatrix()
{
allocate( tNblocks, tblockN, tblockM );
}
/**
* @brief Destroy the PseudoBlockMatrix object
*/
~PseudoBlockMatrix()
{
if(_state==allocated){
for(int b=0;b<Nblocks;b++){
for(int i=0;i<blockN[b];i++){
delete [] blocks[b][i];
}
}
for(int b=0;b<Nblocks;b++){
delete [] blocks[b];
}
for(int i=0;i<N;i++)
delete [] A[i];
delete [] A;
delete [] blocks;
delete [] denseRow;
delete [] denseCol;
delete [] rowStart;
delete [] colStart;
delete [] blockN;
delete [] blockM;
}
}
};
void PseudoBlockMatrix::allocate(
int tNblocks,
int* tblockN,
int* tblockM)
{
Nblocks = tNblocks;
blockN = tblockN;
blockM = tblockM;
N = 0;
M = 0;
// Allocate blocks, compute rowstart / colStart
blocks = new double**[Nblocks]();
rowStart = new int[Nblocks];
colStart = new int[Nblocks];
for(int b=0;b<Nblocks;b++){
blocks[b] = new double*[blockN[b]];
rowStart[b] = N;
colStart[b] = M;
for(int i=0;i<blockN[b];i++){
blocks[b][i] = new double[blockM[b]];
}
N = N + blockN[b];
M = M + blockM[b];
}
blockPicker = new int[N];
for(int b=0;b<Nblocks;b++){
for(int i=0;i<blockN[b];i++){
blockPicker[rowStart[b]+i] = b;
}
}
// Adjust for dense row/col!
N = N+1;
M = M+1;
// Allocate dense col, row, corner
denseCol = new double[N-1];
denseRow = new double[M-1];
denseCorner = 0;
A = new double*[N];
for(int i=0;i<N;i++)
A[i] = new double[M];
fill(NAN);
_state= allocated;
}
double PseudoBlockMatrix::getOverBlocks( int i, int j ){
assert(i<N && i>=0 && j<M && j>=0);
int tblock = blockPicker[i];
return getFromBlock(tblock,i-rowStart[tblock],j-colStart[tblock]);
}
double PseudoBlockMatrix::getFromBlock( int b, int i, int j ){
if(!(i<blockN[b] && j<blockM[b] && i>=0 && j>=0))
assert(1==0);
return blocks[b][i][j];
}
double PseudoBlockMatrix::getOverDense( int i, int j ){
assert(i<N && i>=0 && j<M && j>=0);
// Last row or corner!
if(i==N-1){
// Corner!
if(j==M-1){
return denseCorner;
}
// Dense row!
return denseRow[j];
}
// Last col!
if(j==M-1){
return denseCol[i];
}
return NAN;
}
double PseudoBlockMatrix::get( int i , int j )
{
assert(i<N && i>=0 && j<M && j>=0);
double tmp = getOverDense(i,j);
if(!isnan(abs(tmp)))
return tmp;
return getOverBlocks(i,j);
}
bool PseudoBlockMatrix::setOverBlocks( int i, int j, double val ){
assert(i<N && i>=0 && j<M && j>=0);
int tblock = blockPicker[i];
return setFromBlock(tblock,i-rowStart[tblock],j-colStart[tblock],val);
}
bool PseudoBlockMatrix::setFromBlock( int b, int i, int j, double val ){
assert(i<blockN[b] && j<blockM[b] && i>=0 && j>=0);
blocks[b][i][j] = val;
return true;
}
bool PseudoBlockMatrix::setOverDense( int i, int j, double val ){
assert(i<N && i>=0 && j<M && j>=0);
assert(!isnan(abs(val)));
// Last row or corner!
if(i==N-1){
// Corner!
if(j==M-1){
denseCorner = val;
return true;
}
// Dense row!
denseRow[j] = val;
return true;
}
// Last col!
if(j==M-1){
denseCol[i] = val;
return true;
}
return false;
}
bool PseudoBlockMatrix::set( int i , int j, double val )
{
assert(i<N && i>=0 && j<M && j>=0);
assert(!isnan(abs(val)));
if(setOverDense(i,j,val))
return true;
return setOverBlocks(i,j,val);
}
bool PseudoBlockMatrix::addOverBlocks( int i, int j, double val ){
assert(i<N && i>=0 && j<M && j>=0);
assert(!isnan(abs(val)));
int tblock = blockPicker[i];
return addFromBlock(tblock,i-rowStart[tblock],j-colStart[tblock],val);
}
bool PseudoBlockMatrix::addFromBlock( int b, int i, int j, double val ){
assert(i<blockN[b] && j<blockM[b] && i>=0 && j>=0);
assert(!isnan(abs(val)));
blocks[b][i][j] += val;
return true;
}
bool PseudoBlockMatrix::addOverDense( int i, int j, double val ){
assert(i<N && i>=0 && j<M && j>=0);
assert(!isnan(abs(val)));
// Last row or corner!
if(i==N-1){
// Corner!
if(j==M-1){
denseCorner += val;
return true;
}
// Dense row!
denseRow[j] += val;
return true;
}
// Last col!
if(j==M-1){
denseCol[i] += val;
return true;
}
return false;
}
bool PseudoBlockMatrix::add( int i , int j, double val )
{
assert(i<N && i>=0 && j<M && j>=0);
assert(!isnan(abs(val)));
if(addOverDense(i,j,val))
return true;
return addOverBlocks(i,j,val);
}
void PseudoBlockMatrix::fill( double val )
{
for(int b=0;b<Nblocks;b++){
for(int i=0;i<blockN[b];i++){
for(int j=0;j<blockM[b];j++){
blocks[b][i][j] = val;
}
}
}
for(int i=0;i<N-1;i++)
denseCol[i] = val;
for(int j=0;j<M-1;j++)
denseRow[j] = val;
denseCorner = val;
}
void PseudoBlockMatrix::fillRand( double mag )
{
for(int b=0;b<Nblocks;b++){
for(int i=0;i<blockN[b];i++){
for(int j=0;j<blockM[b];j++){
blocks[b][i][j] = (double)rand() / RAND_MAX*mag;
}
}
}
for(int i=0;i<N-1;i++)
denseCol[i] = (double)rand() / RAND_MAX*mag;
for(int j=0;j<M-1;j++)
denseRow[j] = (double)rand() / RAND_MAX*mag;
denseCorner = (double)rand() / RAND_MAX*mag;
}
void PseudoBlockMatrix::fillRandDD( double mag )
{
for(int b=0;b<Nblocks;b++){
for(int i=0;i<blockN[b];i++){
for(int j=0;j<blockM[b];j++){
blocks[b][i][j] = (double)rand() / RAND_MAX*mag;
}
blocks[b][i][i] += mag*(N+1);
}
}
for(int i=0;i<N-1;i++)
denseCol[i] = (double)rand() / RAND_MAX*mag;
for(int j=0;j<M-1;j++)
denseRow[j] = (double)rand() / RAND_MAX*mag;
denseCorner = (double)rand() / RAND_MAX*mag + mag*(N+1);
}
PseudoBlockMatrix PseudoBlockMatrix::shallowCopy( PseudoBlockMatrix )
{
return PseudoBlockMatrix(Nblocks,blockN,blockM);
}
double PseudoBlockMatrix::checkRes(const double *x, const double *B){
double res = 0,tres=0;
int rs,cs,b,i,j;
for(b=0;b<Nblocks;b++){
rs = rowStart[b];
cs = colStart[b];
for(i=0;i<blockN[b];i++){
tres = -B[i+rs];
for(j=0;j<blockM[b];j++){
tres += blocks[b][i][j]*x[j+cs];
}
tres += denseCol[i+rs]*x[M-1];
res += tres*tres;
}
}
tres = -B[N-1];
for(j=0;j<M-1;j++)
tres += denseRow[j]*x[j];
tres += denseCorner*x[M-1];
res += tres*tres;
return sqrt(res);
}
void PseudoBlockMatrix::Print(){
int rs,cs,b,i,j;
for(b=0;b<Nblocks;b++){
rs = rowStart[b];
cs = colStart[b];
cout << "B "<<b <<endl;
for(i=0;i<blockN[b];i++){
for(j=0;j<blockM[b];j++){
cout<<blocks[b][i][j] << "\t";
}
cout << "Densecol " << denseCol[rs+i] << endl;
}
}
cout << endl << "DenseRow" << endl;
for(j=0;j<M-1;j++)
cout << denseRow[j] << "\t";
cout << endl << "DenseCorner" << endl;
cout << denseCorner << endl;
}
void PseudoBlockMatrix::PrintRow(int row){
if(row != N-1){
int b = blockPicker[row];
row -= rowStart[b];
for(int j=0;j<blockM[b];j++){
cout << blocks[b][row][j] << " ";
}
cout << "\tlast col " << denseCol[rowStart[b]+row] << endl;
}else{
for(int j=0;j<M-1;j++){
cout << denseRow[j] << " ";
}
cout << denseCorner << endl;
}
}
double PseudoBlockMatrix::SumRow(int row){
double sum = 0;
if(row != N-1){
int b = blockPicker[row];
row -= rowStart[b];
for(int j=0;j<blockM[b];j++){
sum += blocks[b][row][j];
}
sum += denseCol[rowStart[b]+row];
}else{
for(int j=0;j<M-1;j++){
sum += denseRow[j];
}
sum += denseCorner;
}
return sum;
}
void PseudoBlockMatrix::PBMVM( const double *x, double *B )
{
int rs,cs,b,i,j;
for(i=0;i<N-1;i++)
B[i] = 0;
for(b=0;b<Nblocks;b++){
rs = rowStart[b];
cs = colStart[b];
for(i=0;i<blockN[b];i++){
for(j=0;j<blockM[b];j++){
B[i+rs] += blocks[b][i][j]*x[j+cs];
}
}
}
for(i=0;i<N-1;i++)
B[i] += denseCol[i]*x[M-1];
for(j=0;j<N-1;j++)
B[N-1] += denseRow[j]*x[j];
B[N-1] += denseCorner*x[M-1];
}
bool PseudoBlockMatrix::directsolve( const double *rhs, double *x )
{
// Iterators
int rs,cs,b,i,j,row,row2,col;
// Tmp
double res=NAN,norm=NAN;
double *norms = new double[N] ,*rhsv = new double[N];
// Assert square!
assert(N==M);
// Assert GS conditions (no zero entries on diag).
// No pivoting. Mostly bc permutation would slow computation and is not necessary for body model.
for(i=0;i<N;i++){
double tmp = get(i,i);
assert(tmp!=NAN);
assert(tmp!=0);
}
for(i=0;i<N;i++){
double tmp = rhs[i];
assert(tmp!=NAN);
}
// Precondition by norming values
for(b=0;b<Nblocks;b++){
rs = rowStart[b];
cs = colStart[b];
for(i=0;i<blockN[b];i++){
row = rs+i;
norm = 0;
norm += abs(denseCol[row]);
// Subtract jth col * jth x
for(j=0;j<blockM[b];j++){
norm += abs(blocks[b][i][j]);
}
norms[row] = .9/norm;
}
}
norm = abs(denseCorner);
for(j=0;j<M-1;j++){
norm += abs(denseRow[j]);
}
norms[N-1] = 0.9/norm;
assert(!isnan(abs(norm)));
for(i=0;i<N;i++)
for(j=0;j<M;j++)
A[i][j] = 0;
for(b=0;b<Nblocks;b++){
rs = rowStart[b];
cs = colStart[b];
for(i=0;i<blockN[b];i++){
row = rs+i;
for(j=0;j<blockM[b];j++){
col = cs+j;
A[row][col] = norms[row]*blocks[b][i][j];
}
A[row][M-1] = norms[row]*denseCol[row];
}
}
for(j=0;j<M-1;j++){
A[N-1][j] = norms[N-1]*denseRow[j];
}
A[N-1][M-1] = norms[N-1]*denseCorner;
for(i=0;i<N;i++)
rhsv[i] = norms[i]*rhs[i];
for(row=0;row<N;row++){
double Aiiinv = 1.0/A[row][row];
for(row2=row+1;row2<N;row2++){
double srat = Aiiinv*A[row2][row];
//A[row2][row] = 0;
for(col=row;col<M;col++){
A[row2][col] -= srat*A[row][col];
}
rhsv[row2] -= srat*rhsv[row];
}
}
for(row=N-1;row>=0;row--){
x[row] = rhsv[row];
for(col=row+1;col<M;col++){
x[row] -= A[row][col]*x[col];
}
x[row] /= A[row][row];
}
res = checkRes(x,rhs);
assert(res<1e-1);
delete [] rhsv;
delete [] norms;
return res<1e-1;
}
bool PseudoBlockMatrix::denseGaussSeidel(double *rhs, const double *x0, double resTol, double convTol, double *x )
{
// Iterators
int rs,cs,b,i,j,row,row2,col;
// Tmp
double norm=NAN;
double *norms = new double[N] ,*rhsv = new double[N];
// Iterators
int iter = 0,maxIter = 100;
double tmpx=NAN,tmpAii=NAN,tmpres=NAN;
// Assert square!
assert(N==M);
// Assert GS conditions (no zero entries on diag).
// No pivoting. Mostly bc permutation would slow computation and is not necessary for body model.
for(i=0;i<N;i++){
double tmp = get(i,i);
assert(tmp!=NAN);
assert(tmp!=0);
}
for(i=0;i<N;i++){
double tmp = rhs[i];
assert(tmp!=NAN);
}
// Precondition by norming values
for(b=0;b<Nblocks;b++){
rs = rowStart[b];
cs = colStart[b];
for(i=0;i<blockN[b];i++){
row = rs+i;
norm = 0;
norm += abs(denseCol[row]);
// Subtract jth col * jth x
for(j=0;j<blockM[b];j++){
norm += abs(blocks[b][i][j]);
}
norms[row] = .9/norm;
}
}
norm = abs(denseCorner);
for(j=0;j<M-1;j++){
norm += abs(denseRow[j]);
}
norms[N-1] = 0.9/norm;
assert(!isnan(abs(norm)));
for(i=0;i<N;i++)
for(j=0;j<M;j++)
A[i][j] = 0;
for(b=0;b<Nblocks;b++){
rs = rowStart[b];
cs = colStart[b];
for(i=0;i<blockN[b];i++){
row = rs+i;
for(j=0;j<blockM[b];j++){
col = cs+j;
A[row][col] = norms[row]*blocks[b][i][j];
}
A[row][M-1] = norms[row]*denseCol[row];
}
}
for(j=0;j<M-1;j++){
A[N-1][j] = norms[N-1]*denseRow[j];
}
A[N-1][M-1] = norms[N-1]*denseCorner;
for(i=0;i<N;i++)
rhsv[i] = norms[i]*rhs[i];
// Copy x0 to x
for(j=0;j<M;j++)
x[j] = x0[j];
// Residuals and convergences
double res = 2*resTol;
double conv = 2*convTol;
while(res>resTol && conv>convTol && iter<maxIter){
res = 0;
conv = 0;
++iter;
// Iterate over block rows
for(row=0;row<M;row++){
// Temporary value of x
tmpx = 0;
// Add rhs
tmpx += rhsv[row];
for(col=0;col<M;col++){
tmpx -= A[row][col]*x[col];
}
tmpx += A[row][row]*x[row];
tmpx /= A[row][row];
// compute conv
conv = max(conv,abs(x[row]-tmpx));
// Update x
x[row] = tmpx;
}
// Check residual
res = checkRes(x,rhs);
}
assert(iter<maxIter-1);
delete [] rhsv;
delete [] norms;
return iter<maxIter && res<resTol && conv<convTol;
}
bool PseudoBlockMatrix::GaussSeidel(double *rhs, const double *x0, double resTol, double convTol, double *x )
{
// Iterators
int rs,cs,b,i,j,row,col,iter = 0,maxIter = 500;
// Tmp
double tmpx=NAN,tmpAii=NAN,tmpres=NAN,norm=NAN;
// Assert square!
assert(N==M);
// Assert GS conditions (no zero entries on diag).
// No pivoting. Mostly bc permutation would slow computation and is not necessary for body model.
for(i=0;i<N;i++){
double tmp = get(i,i);
assert(tmp!=NAN);
assert(tmp!=0);
}
for(i=0;i<N;i++){
double tmp = rhs[i];
assert(tmp!=NAN);
}
for(i=0;i<M;i++){
double tmp = x0[i];
assert(tmp!=NAN);
}
// Precondition by norming values
for(b=0;b<Nblocks;b++){
rs = rowStart[b];
cs = colStart[b];
for(i=0;i<blockN[b];i++){
row = rs+i;
norm = 0;
// Subtract last col*last x
norm += abs(denseCol[row]);
// Subtract jth col * jth x
for(j=0;j<blockM[b];j++){
norm += abs(blocks[b][i][j]);
}
denseCol[row] /= norm;
for(j=0;j<blockM[b];j++){
blocks[b][i][j]/=norm;
}
rhs[row]/=norm;
}
}
// Iterate over last row
// Temporary value of x
norm = abs(denseCorner);
for(j=0;j<M-1;j++){
norm += abs(denseRow[j]);
}
// Divide by corner
denseCorner /= norm;
for(j=0;j<M-1;j++){
denseRow[j] /= norm;
}
// Copy x0 to x
for(j=0;j<M;j++)
x[j] = x0[j];
// Residuals and convergences
double res = 2*resTol;
double conv = 2*convTol;
while(res>resTol && conv>convTol && iter<maxIter){
res = 0;
conv = 0;
++iter;
// Iterate over block rows
for(b=0;b<Nblocks;b++){
rs = rowStart[b];
cs = colStart[b];
for(i=0;i<blockN[b];i++){
row = rs+i;
// Temporary value of x
tmpx = 0;
// Add rhs
tmpx += rhs[row];
// Subtract last col*last x
tmpx -= denseCol[row]*x[M-1];
// Subtract jth col * jth x
for(j=0;j<blockM[b];j++){
tmpx -= blocks[b][i][j]*x[j+cs];
}
// Add back Aii * xii
tmpAii = blocks[b][i][i];
tmpx += tmpAii*x[row];
// Divide by Aii
tmpx /= tmpAii;
// compute conv
conv = max(conv,abs(x[row]-tmpx));
// Update x
x[row] = tmpx;
}
}
// Iterate over last row
// Temporary value of x
tmpx = 0;
// Add rhs
tmpx += rhs[N-1];
// Subtract jth col * jth x
for(j=0;j<M-1;j++){
tmpx -= denseRow[j]*x[j];
}
// Divide by corner
tmpx /= denseCorner;
// compute conv
conv = max(conv,abs(x[M-1]-tmpx));
// Update x
x[M-1] = tmpx;
// Check residual
res = checkRes(x,rhs);
}
assert(iter<maxIter-1);
return iter<maxIter && res<resTol && conv<convTol;
}
}
#endif