-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgrad.c
3494 lines (3108 loc) · 106 KB
/
cgrad.c
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define CGRAD_ZERO 1e-15
#define CGRAD_TREIG_PREPROCESS /* Function trofrm will fail if no preprocessing is done */
/**
* Sparse tridiagonal save in octave format
*/
int stmsave( char *fn, int n, double *alpha, double *beta, double *gamma )
{
int i;
FILE *fp = fopen( fn, "w" );
fprintf( fp, "# name: T\n" );
fprintf( fp, "# type: sparse matrix\n" );
fprintf( fp, "# rows: %d\n", n );
fprintf( fp, "# columns: %d\n", n );
for(i=0;i<n;i++)
{
if( i > 0 )
fprintf( fp, "%d %d %18.10f\n", i, i + 1, beta[i] );
fprintf( fp, "%d %d %18.10f\n", i + 1, i + 1, alpha[i] );
if( i < n - 1 )
fprintf( fp, "%d %d %18.10f", i + 2, i + 1, gamma[i+1] );
fprintf( fp, "\n" );
}
fclose( fp );
return 0;
}
/**
* Save sparse matrices in a readable format
* @param fn File name to which output is directed
* @param n Number of rows in matrix a
* @param k Number of columns in matrix a
* @param ia Sparse matrix parameters; list of beginning of each row
* @param ja List of column numbers corresponding to entries in a
* @param a Actual values corresponding to ja indexes
* @param mode Mode of output; 0 means output coordinates, 1 means output in octave/matlab format
* @return Returns -1 if error in opening the output file, 0 otherwise
*/
int smsave( char *fn, long n, long k, long *ia, long *ja, double *a, int mode )
{
long i,j;
static int idx = 1;
char vn[512];
FILE *fp = fopen( fn, "w" );
if( fp == NULL )
return -1;
switch( mode )
{
case 0:
fprintf( fp, "%d %d\n", n, ia[n] );
break;
case 1:
snprintf( vn, 512, "A%d", idx );
fprintf( fp, "# name: %s\n# type: sparse matrix\n# nnz: %d\n# rows: %d\n# columns: %d\n", vn, ia[n], n, k );
++idx;
break;
}
for(i=0;i<n;i++)
{
for(j=ia[i];j<ia[i+1];j++)
{
switch( mode )
{
case 0:
fprintf( fp, "%d %d %17.12f\n", i, ja[j], a[j] );
break;
case 1:
fprintf( fp, "%d %d %17.12f\n", ja[j] + 1, i + 1, a[j] );
break;
}
}
}
fclose( fp );
return 0;
}
/**
* Save a dense matrix into a file
*/
int msave( char *fn, long n, long k, double *a, int mode )
{
long i,j;
static int idx = 1;
char vn[512];
FILE *fp = fopen( fn, "w" );
if( fp == NULL )
return -1;
switch( mode )
{
case 0:
fprintf( fp, "%d %d\n", n, k );
break;
case 1:
snprintf( vn, 512, "M%d", idx );
fprintf( fp, "# name: %s\n# type: matrix\n# rows: %d\n# columns: %d\n", vn, n, k );
++idx;
break;
}
for(i=0;i<n;i++)
{
for(j=0;j<k;j++)
fprintf( fp, "%17.12f", a[i*k+j] );
fprintf( fp, "\n" );
}
fclose( fp );
return 0;
}
/**
* Print a vector to a stream
* @param fp File stream pointer to which to direct output
* @param n The number of entries in the vector x
* @param x Pointer to the start of the entries of the vector
*/
void vprint( FILE *fp, int n, double *x )
{
int i;
for(i=0;i<n;i++)
fprintf( fp, "%20.13f", x[i] );
fprintf( fp, "\n" );
}
/**
* Return 1 if x is within CGRAD_ZERO of zero
* @param x Double precision floating point to check for zero
* @return Returns 1 if zero, 0 otherwise
*/
int iszero( double x )
{
if( fabs( x ) < CGRAD_ZERO )
return 1;
else
return 0;
}
/**
* Vector copy
* @param n Length of the vector
* @param u Origin vector
* @param v Destination to which to copy u
*/
void copy( int n, double *u, double *v )
{
int i;
for(i=0;i<n;i++)
v[i] = u[i];
}
/**
* Vector copy and divide by constant
* @param n Length of vector
* @param u Origin vector to copy
* @param c Constant by which to divide u
* @param v Destination for u / c
*/
void copyvdiv( int n, double *u, double c, double *v )
{
int i;
for(i=0;i<n;i++)
v[i] = u[i] / c;
}
/**
* Divide every element of a vector for a constant
* @param n Vector length
* @param x Vector to divide
* @param c Constant by which to divide x; result is in x as well
*/
void vdiv( int n, double *x, double c )
{
int i;
for(i=0;i<n;i++)
x[i] /= c;
}
/**
* Zero a double precision vector
* @param n Length of vector
* @param x Pointer to entries to zero
*/
void zerov( int n, double *x )
{
int i;
for(i=0;i<n;i++)
x[i] = 0.0;
}
/**
* Is norm of x less than or greater than input tol
* @param n Length of vector
* @param x Vector to check against tol
* @param tol Tolerance to use
* @param res Returns -1 if norm(x) < tol else 1
*/
void normchk( int n, double *x, double tol, int *res )
{
int i;
double sum = 0.0;
const double tolsq = tol * tol;
for(i=0;i<n;i++)
sum += x[i] * x[i];
if( sum < tolsq )
*res = -1;
else
*res = 1;
}
/**
* Is the dot product of x and y less than or greater than tol
* @param n Dimension of vectors
* @param x First vector
* @param y Second vector
* @param tol Tolerance with which to compare x dot y
* @param res Returns -1 if x dot y < tol else 1
*/
void dotchk( int n, double *x, double *y, double tol, int *res )
{
int i;
double sum = 0.0;
for(i=0;i<n;i++)
sum += x[i] * y[i];
if( fabs( sum ) < tol )
*res = -1;
else
*res = 1;
}
/**
* Calculate and return the norm of the vector
* @param n Dimension of vector x
* @param x Vector whose norm is calculated
* @param res Value of the norm of x
*/
void norm( int n, double *x, double *res )
{
int i;
(*res) = 0.0;
for(i=0;i<n;i++)
(*res) += x[i] * x[i];
(*res) = sqrt( *res );
}
/**
* Calculate and return the dot product
* @param n Dimension of vectors x and y
* @param x First vector
* @param y Second vector
* @param res Returns x dot y
*/
void dotp( int n, double *x, double *y, double *res )
{
int i;
(*res) = 0.0;
for(i=0;i<n;i++)
(*res) += x[i] * y[i];
}
/**
* Calculate and return the dot product divided by c
* @param n Dimension of x and y
* @param x First vector
* @param y Second vector
* @param c Scalar by which to divide x dot y
* @param res Contains x dot y / c
*/
void dotpdiv( int n, double *x, double *y, double c, double *res )
{
int i;
(*res) = 0.0;
for(i=0;i<n;i++)
(*res) += x[i] * y[i];
(*res) = *res / c;
}
/**
* Normalize a double vector
* @param n Dimension of x
* @param x Vector to normalize
*/
void normalize( int n, double *x )
{
int i;
double sum = 0.0;
for(i=0;i<n;i++)
sum += x[i] * x[i];
sum = sqrt( sum );
for(i=0;i<n;i++)
x[i] /= sum;
}
/**
* Generate a random double precision n-vector normalized to unity;
* make sure to call srand() before running this
* @param n Vector length
* @param x Vector
*/
void nrandv( int n, double *x )
{
int i;
for(i=0;i<n;i++)
x[i] = 0.5 - ( (double) rand() / (double) RAND_MAX );
normalize( n, x );
}
/**
* Project a subspace out of the vector
* @param n System dimension
* @param x Vector from which to project vector space in V
* @param nv Number of vectors in V to project out of x
* @param V Vector space to project out of x
*/
void project( int n, double *x, int nv, double *V )
{
int i,j;
double f;
for(i=0;i<nv;i++)
{
f = 0.0;
for(j=0;j<n;j++)
f += x[j] * V[i*n+j];
for(j=0;j<n;j++)
x[j] -= f * V[i*n+j];
}
}
void bicgstab( int, int, double *, double *, double *, int, double, int, int * );
/**
* Project a vector into a subspace via least-squares calculation
*/
void projectls( int n, double *x, int nv, double *V, int max )
{
int i,j,k,res;
double *C = (double*) malloc( n * n * sizeof(double) );
double *b = (double*) malloc( nv * sizeof(double) );
double *y = (double*) malloc( nv * sizeof(double) );
/* Form the V**T V and put it in C */
for(i=0;i<nv;i++)
{
for(j=0;j<nv;j++)
{
C[i*n+j] = 0.0;
for(k=0;k<n;k++)
C[i*n+j] += V[i*n+k] * V[j*n+k];
}
}
for(i=0;i<nv;i++)
{
b[i] = 0.0;
for(j=0;j<n;j++)
b[i] += V[i*n+j] * x[j];
}
/* Solve the least squares problem in full rank form */
nrandv( nv, y );
bicgstab( nv, 0, C, b, y, max, 1e-9, 0, &res );
/* Now calculate the actual projection as x - Cy */
for(i=0;i<nv;i++)
for(j=0;j<n;j++)
x[j] -= y[i] * V[i*n+j];
}
/**
* Check to make sure vector x contains no component in
* the given vector subspace
*/
void spcheck( int n, double *x, int nv, double *V, double tol, int *res )
{
int i,j;
double f;
*res = 0;
for(i=0;i<nv;i++)
{
f = 0.0;
for(j=0;j<n;j++)
f += x[j] * V[i*n+j];
if( fabs( f ) > tol )
*res += 1;
}
}
/**
* Vector sum combination; calculate res = a*x + b*y
* @param n Dimension of vectors x and y
* @param a First constant
* @param x First vector
* @param b Second constant
* @param y Second vector
* @param res Returns a*x + b*y
*/
void vsum( int n, double a, double *x, double b, double *y, double *res )
{
int i;
for(i=0;i<n;i++)
res[i] = a * x[i] + b * y[i];
}
/**
* Matrix vector product
*/
void dgemv( int n, int tt, double *A, double *x, double *res )
{
int i,j;
for(i=0;i<n;i++)
res[i] = 0.0;
if( tt == 0 )
for(i=0;i<n;i++)
for(j=0;j<n;j++)
res[i] += A[i*n+j] * x[j];
else
for(i=0;i<n;i++)
for(j=0;j<n;j++)
res[i] += A[j*n+i] * x[j];
}
/**
* A very nice and VERY simple algorithm!
*/
void sdgemv( long n, long m, long *ia, long *ja, double *a, long rb, double *b, long rc, double *c )
{
int i,j;
for(i=0;i<n;i++)
{
c[i*rc] = 0.0;
for(j=ia[i];j<ia[i+1];j++)
c[i*rc] = c[i*rc] + a[j] * b[ja[j]*rb];
}
}
/**
* Sparse symmetric dgemv; only lower triangular part is stored in
* the sparse data structure
*/
void ssdgemv( long n, long *ia, long *ja, double *A, long rx, double *x, long ry, double *y )
{
int i,j;
double stemp,rtemp;
for(i=0;i<n;i++)
y[i*ry] = 0.0;
for(i=0;i<n;i++)
{
j = ia[i];
rtemp = x[i*rx];
stemp = 0.0;
if( ja[j] == i )
{
stemp = A[j] * x[i*rx];
j++;
}
for(;j<ia[i+1];j++)
{
stemp += A[j] * x[ja[j]*rx];
y[ja[j]*ry] += A[j] * rtemp;
}
y[i*ry] += stemp;
}
}
/**
* Sparse matrix transpose
*/
void stransp( char move, long n, long m, long *ia, long *ja, double *a, long *ib, long *jb, double *b )
{
long i,j,jj;
for(i=0;i<m+1;i++)
ib[i] = 0;
if( move == 1 )
for(i=0;i<m;i++)
b[i] = 0.0;
/* count number of new columns in each row */
ib[0] = 0;
for(i=0;i<n;i++)
for(j=ia[i];j<ia[i+1];j++)
ib[ja[j]+1] = ib[ja[j]+1] + 1;
/* "Integrate" entries forward to get final end positions */
for(i=0;i<m;i++)
ib[i+1] = ib[i] + ib[i+1];
/* Counting row sizes in b done; now construct jb */
for(i=0;i<n;i++)
{
for(j=ia[i];j<ia[i+1];j++)
{
jj = ja[j];
jb[ib[jj]] = i;
if( move == 1 )
b[ib[jj]] = a[j];
ib[jj] = ib[jj] + 1;
}
}
for(i=m;i>0;i--)
ib[i] = ib[i-1];
ib[0] = 0;
}
/**
* Compute an inner product w.r.t. a given operator
* @param n Dimension of space
* @param A Matrix defining the inner product
* @param x Left vector
* @param y Right vector
* @param res Inner product value
*/
void mdotp( int n, double *A, double *x, double *y, double *res )
{
int i,j;
(*res) = 0.0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
(*res) += x[i] * A[i*n+j] * y[j];
}
/**
* Calculate the norm of the difference between two vectors
*/
void vnormdiff( int n, double *x, double *y, double *res )
{
int i;
double sum = 0.0;
for(i=0;i<n;i++)
sum += pow( x[i] - y[i], 2.0 );
*res = sqrt( sum );
}
/**
* Calculate the residual of a linear system
*/
void residual( int n, double *A, double *x, double *b, double *r )
{
int i,j;
for(i=0;i<n;i++)
r[i] = b[i];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
r[i] -= A[i*n+j] * x[j];
}
/**
* Calculate shifted-matrix-vector product with an addition;
* res = (A-sI)**(t) x + c y
* @param n Dimension of space
* @param t No transpose if t = 0, otherwise transpose A
* @param A Matrix input
* @param s Value to shift the diagonal of A
* @param x Vector input one
* @param y Vector input two
* @param c Multiplier parameter on y
* @param res Output vector
*/
void smmadd( int n, int tt, double *A, double s, double *x, double *y, double c, double *res )
{
int i,j;
for(i=0;i<n;i++)
res[i] = c * y[i];
if( tt == 0 ) /* Not transposed */
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if( j == i )
res[i] += ( A[i*n+j] - s ) * x[j];
else
res[i] += A[i*n+j] * x[j];
}
}
}
else /* If not zero assume transposed */
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if( j == i )
res[i] += ( A[j*n+i] - s ) * x[j];
else
res[i] += A[j*n+i] * x[j];
}
}
}
}
/**
* Calculate shifted-matrix-vector product with an addition;
* res = (A-sB)**(t) x + c y
* @param n Dimension of space
* @param t No transpose if t = 0, otherwise transpose A
* @param A Matrix 1 input
* @param B Matrix 2 input
* @param s Value to shift the diagonal of A
* @param x Vector input one
* @param y Vector input two
* @param c Multiplier parameter on y
* @param res Output vector
*/
void gsmmadd( int n, int tt, double *A, double *B, double s, double *x, double *y, double c, double *res )
{
int i,j;
for(i=0;i<n;i++)
res[i] = c * y[i];
if( tt == 0 ) /* Not transposed */
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
res[i] += ( A[i*n+j] - s * B[i*n+j] ) * x[j];
}
else /* If not zero assume transposed */
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
res[i] += ( A[j*n+i] - s * B[j*n+i] ) * x[j];
}
}
void spdiag( long n, long *ia, long *ja, double *A, long *d, int *ret )
{
long i,j;
/* Find indexes of the diagonal elements */
for(i=0;i<n;i++)
{
d[i] = -1;
for(j=ia[i];j<ia[i+1];j++)
{
if( ja[j] == i )
{
d[i] = j;
break;
}
}
if( d[i] == -1 )
{
*ret = -1; /* Missing a diagonal element */
break;
}
}
}
/**
* Apply a sparse lower triangular inverse preconditioner to a given input vector
* and overwrite it with the result
* @param n Dimension of the vector and sparse square operator
* @param ia List of row start positions
* @param ja List of column indexes
* @param A Entries in matrix A
* @param x Vector to which to apply lower triangular preconditioner
* @param d Positions of the diagonal elements in each row
*/
void spclw( long n, long *ia, long *ja, double *A, double *x, long *d )
{
long i,j;
/* Do the forward substitution */
for(i=0;i<n;i++)
{
for(j=ia[i];j<d[i];j++)
x[i] = x[i] - A[j] * x[ja[j]];
x[i] = x[i] / A[d[i]];
}
}
/**
* Apply a sparse upper triangular inverse preconditioner to a given input vector
* and overwrite it with the result
* @param n Dimension of the vector and sparse square operator
* @param ia List of row start positions
* @param ja List of column indexes
* @param A Entries in matrix A
* @param x Vector to which to apply lower triangular preconditioner
* @param d Positions of the diagonal elements in each row
*/
void spcup( long n, long *ia, long *ja, double *A, double *x, long *d )
{
long i,j;
/* Do the back substitution */
for(i=n-1;i>=0;i--)
{
for(j=ia[i+1]-1;j>d[i];j--)
x[i] = x[i] - A[j] * x[ja[j]];
x[i] = x[i] / A[d[i]];
}
}
/**
* Regular sparse symmetric conjugate gradient algorithm; A must be
* in CRS format with only upper triangular and diagonal entries
* @param n Dimension of sparse square matrix
* @param ia Row beginning indexes
* @param ja Column index list
* @param A Entries of the matrix
* @param b Righthand side
* @param x Input/output solution
* @param tol Tolerance to indicate when to converge
* @param vb Verbose or not
* @param ret Returns 0 if all went well, < 0 otherwise
*/
void scg( int n, long *ia, long *ja, double *A, double *b, double *x, int max, double tol, int vb, int *ret )
{
int i,j;
double alpha,tmp,rso,rsn,*r,*p,*t;
/* Start out okay and change if necessary */
*ret = 0;
/* Allocate stuff */
r = (double*) malloc( n * sizeof(double) );
p = (double*) malloc( n * sizeof(double) );
t = (double*) malloc( n * sizeof(double) );
/* Set up initial vectors and coefficients */
ssdgemv( (long) n, ia, ja, A, 1, x, 1, r );
for(i=0;i<n;i++)
r[i] = b[i] - r[i], p[i] = r[i];
dotp( n, r, r, &rso );
/* Start iterating */
for(i=0;i<max;i++)
{
ssdgemv( (long) n, ia, ja, A, 1, p, 1, t );
dotp( n, p, t, &tmp );
alpha = rso / tmp;
for(j=0;j<n;j++)
x[j] = x[j] + alpha * p[j],
r[j] = r[j] - alpha * t[j];
dotp( n, r, r, &rsn );
if( rsn < tol * tol )
break;
for(j=0;j<n;j++)
p[j] = r[j] + rsn / rso * p[j];
rso = rsn;
}
if( vb )
fprintf( stderr, "%d: residual = %15.7f\n", i, sqrt( rsn ) );
/* Clean up */
free( r ); free( p ); free( t );
}
/**
* Regular sparse symmetric conjugate gradient algorithm; A must be
* in CRS format; Jacobi preconditioner applied
* @param n Dimension of sparse square matrix
* @param ia Row beginning indexes
* @param ja Column index list
* @param A Entries of the matrix
* @param b Righthand side
* @param x Input/output solution
* @param tol Tolerance to indicate when to converge
* @param vb Verbose or not
* @param ret Returns 0 if all went well, < 0 otherwise
*/
void jpscg( int n, long *ia, long *ja, double *A, double *bb, double *x, int max, double tol, int vb, int *ret )
{
int i,j,res;
double alpha,tmp,rso,rsn,*r,*p,*t,*b,*pc;
/* Start out okay and change if necessary */
*ret = 0;
/* Allocate stuff */
r = (double*) malloc( n * sizeof(double) );
p = (double*) malloc( n * sizeof(double) );
t = (double*) malloc( n * sizeof(double) );
b = (double*) malloc( n * sizeof(double) );
pc = (double*) malloc( n * sizeof(double) );
/* Create the Jacobi preconditioner */
for(i=0;i<n;i++)
for(j=ia[i];j<ia[i+1];j++)
if( j == ia[i] || fabs( A[j] ) > fabs( pc[i] ) )
pc[i] = fabs( A[j] );
/* Set up the preconditioned system */
copy( n, bb, b );
for(i=0;i<n;i++)
b[i] = b[i] / pc[i];
/* Set up initial vectors and coefficients */
ssdgemv( (long) n, ia, ja, A, 1, x, 1, r );
for(i=0;i<n;i++)
r[i] = r[i] / pc[i];
for(i=0;i<n;i++)
r[i] = b[i] - r[i], p[i] = r[i];
dotp( n, r, r, &rso );
/* Start iterating */
for(i=0;i<max;i++)
{
ssdgemv( (long) n, ia, ja, A, 1, p, 1, t );
for(j=0;j<n;j++)
t[j] = t[j] / pc[j];
dotp( n, p, t, &tmp );
alpha = rso / tmp;
for(j=0;j<n;j++)
x[j] = x[j] + alpha * p[j],
r[j] = r[j] - alpha * t[j];
dotp( n, r, r, &rsn );
if( rsn < tol * tol )
break;
for(j=0;j<n;j++)
p[j] = r[j] + rsn / rso * p[j];
rso = rsn;
if( vb )
fprintf( stderr, "%d: residual = %15.7f\n", i, sqrt( rsn ) );
}
/* Clean up */
free( r ); free( p ); free( t ); free( b ); free( pc );
}
/**
* Biconjugate gradient algorithm
*/
void bicg( int n, double *A, double *b, double *x, int max, double tol, int vb, int *ret )
{
}
/**
* Preconditioned biconjugate gradient algorithm
*/
void pbicg( int n, double *A, double *b, double *M, double *x, int max, double tol, int vb, int *ret )
{
}
/**
* Biconjugate gradient method projected
*/
void bicgp( int n, double *A, double *b, int nv, double *V, double *x, int max, double tol, int vb, int *ret )
{
int i,j,m;
double f,g,rho,rhon,alpha,beta,omega;
double *xh,*r,*rh,*p,*ph;
}
/**
* Preconditioned biconjugate gradient projected method
*/
void pbicgp( int n, double *A, double *b, double *M, int nv, double *V, double *x, int max, double tol, int vb, int *ret )
{
}
/**
* Standard biconjugate gradient stabilized algorithm
*/
void bicgstab( int n, int tt, double *A, double *b, double *x, int max, double tol, int vb, int *ret )
{
int i,j,m;
double f,g,rho,rhon,alpha,beta,omega;
double *r0,*r,*p,*v,*s,*t;
r0 = (double*) malloc( n * sizeof(double) );
r = (double*) malloc( n * sizeof(double) );
p = (double*) malloc( n * sizeof(double) );
v = (double*) malloc( n * sizeof(double) );
s = (double*) malloc( n * sizeof(double) );
t = (double*) malloc( n * sizeof(double) );
/* Initialize */
rho = 1.0;
alpha = 1.0;
omega = 1.0;
for(i=0;i<n;i++)
{
r[i] = b[i];
if( tt == 0 )
for(j=0;j<n;j++)
r[i] -= A[i*n+j] * x[j];
else
for(j=0;j<n;j++)
r[i] -= A[j*n+i] * x[j];
r0[i] = r[i];
v[i] = 0.0;
p[i] = 0.0;
}
/* Iterate */
if( vb )
{
fprintf( stderr, "\n" );
fprintf( stderr, "----------------------------------------------------------------------\n" );
fprintf( stderr, "%10s%15s%15s%15s%15s\n", " Iteration", "Residual ", "Alpha ", "Beta ", "Omega " );
fprintf( stderr, "----------------------------------------------------------------------\n" );
}
*ret = 0;
for(m=0;m<max;m++)
{
rhon = 0.0;
for(i=0;i<n;i++)
rhon += r0[i] * r[i];
beta = ( rhon / rho ) * ( alpha / omega );
rho = rhon;
for(i=0;i<n;i++)
p[i] = r[i] + beta * ( p[i] - omega * v[i] );
for(i=0;i<n;i++)
{
v[i] = 0.0;
if( tt == 0 )
for(j=0;j<n;j++)
v[i] += A[i*n+j] * p[j];
else
for(j=0;j<n;j++)
v[i] += A[j*n+i] * p[j];
}
f = 0.0;
for(i=0;i<n;i++)
f += r0[i] * v[i];
alpha = rho / f;
if( isnan( alpha ) )
{
*ret = -1; /* Failed to converge */
break;
}
for(i=0;i<n;i++)
s[i] = r[i] - alpha * v[i];
for(i=0;i<n;i++)
{
t[i] = 0.0;
if( tt == 0 )
for(j=0;j<n;j++)
t[i] += A[i*n+j] * s[j];
else
for(j=0;j<n;j++)
t[i] += A[j*n+i] * s[j];
}
f = 0.0, g = 0.0;
for(i=0;i<n;i++)
f += t[i] * s[i],
g += t[i] * t[i];
omega = f / g;
for(i=0;i<n;i++)
x[i] += alpha * p[i] + omega * s[i];
/* Check for convergence here */
f = 0.0;
for(i=0;i<n;i++)
f += r[i] * r[i];
f = sqrt( f );
if( vb )
fprintf( stderr, "%10d%15.7f%15.7f%15.7f%15.7f\n", m, f, alpha, beta, omega );
if( f < tol )
break;
for(i=0;i<n;i++)
r[i] = s[i] - omega * t[i];
}
if( f > tol )
*ret = -2;
if( vb )
{
fprintf( stderr, "----------------------------------------------------------------------\n" );
if( *ret == 0 )
fprintf( stderr, " Converged System size: %15d\n", n );
else
fprintf( stderr, " Failed System size: %15d\n", n );
fprintf( stderr, " Tolerance: %15.3e\n", tol );
fprintf( stderr, " Iterations: %15d\n", m );
fprintf( stderr, " Residual: %15.3e\n", f );
fprintf( stderr, " ------------------------------\n" );
}