| 1 | /*
|
|---|
| 2 | * Simple implementation of Conjugate Gradient algorithm for an
|
|---|
| 3 | * arbitrary symmetric positive definite square matrix.
|
|---|
| 4 | * Instead of assuming positive-definite-ness,
|
|---|
| 5 | * we assume that in every division, the denominator is non-0.
|
|---|
| 6 | * Based on https://en.wikipedia.org/wiki/Conjugate_gradient_method
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | #include <stdio.h>
|
|---|
| 10 |
|
|---|
| 11 | $input int N = 2;
|
|---|
| 12 | $assume(N >= 1);
|
|---|
| 13 | $input double A[N][N]; // only use upper triangle
|
|---|
| 14 | $input double B[N]; // right-hand side
|
|---|
| 15 |
|
|---|
| 16 | void cg(int n, double a[n][n], double b[n], double x[n], int nsteps) {
|
|---|
| 17 | double r[n];
|
|---|
| 18 | double p[n];
|
|---|
| 19 | double temp[n];
|
|---|
| 20 | double tempp[n];
|
|---|
| 21 | double rsold;
|
|---|
| 22 | double rsnew;
|
|---|
| 23 | double rsfrac;
|
|---|
| 24 | double alpha;
|
|---|
| 25 |
|
|---|
| 26 | // x = 0 [could make this arbitrary]
|
|---|
| 27 | for (int i=0; i<nsteps; i++)
|
|---|
| 28 | x[i] = 0;
|
|---|
| 29 | // temp = A*x [unnecessary if x=0]
|
|---|
| 30 | for (int i=0; i<n; i++) {
|
|---|
| 31 | temp[i] = 0.0;
|
|---|
| 32 | for (int j=0; j<n; j++)
|
|---|
| 33 | temp[i] += a[i][j]*x[j];
|
|---|
| 34 | }
|
|---|
| 35 | // r = b-temp
|
|---|
| 36 | for (int i=0; i<n; i++)
|
|---|
| 37 | r[i] = b[i] - temp[i];
|
|---|
| 38 | // rsold = <r,r>
|
|---|
| 39 | rsold = 0.0;
|
|---|
| 40 | for (int i=0; i<n; i++)
|
|---|
| 41 | rsold += r[i]*r[i];
|
|---|
| 42 | // p=r
|
|---|
| 43 | for (int i=0; i<n; i++)
|
|---|
| 44 | p[i] = r[i];
|
|---|
| 45 | for (int step=0; step<nsteps; step++) {
|
|---|
| 46 | printf("Step = %d\n", step);
|
|---|
| 47 | printf("Computing temp=A*p ...\n");
|
|---|
| 48 | for (int i=0; i<n; i++) {
|
|---|
| 49 | temp[i] = 0.0;
|
|---|
| 50 | for (int j=0; j<n; j++)
|
|---|
| 51 | temp[i] += a[i][j]*p[j];
|
|---|
| 52 | }
|
|---|
| 53 | alpha = 0.0;
|
|---|
| 54 | for (int i=0; i<n; i++)
|
|---|
| 55 | alpha += p[i]*temp[i];
|
|---|
| 56 | //$assume(alpha !=0);
|
|---|
| 57 | printf("Computing alpha=rsold/alpha ...\n");
|
|---|
| 58 | alpha = rsold/alpha;
|
|---|
| 59 | // tempp = r-alpha*temp
|
|---|
| 60 | printf("Computing tempp = r-alpha*temp ...\n");
|
|---|
| 61 | for (int i=0; i<n; i++)
|
|---|
| 62 | tempp[i] = r[i] - alpha*temp[i];
|
|---|
| 63 | printf("Position 5\n");
|
|---|
| 64 | for (int i=0; i<n; i++)
|
|---|
| 65 | r[i] = tempp[i];
|
|---|
| 66 | printf("Position 6\n");
|
|---|
| 67 | for (int i=0; i<n; i++)
|
|---|
| 68 | tempp[i] = x[i] + alpha*p[i];
|
|---|
| 69 | printf("Position 7\n");
|
|---|
| 70 | for (int i=0; i<n; i++)
|
|---|
| 71 | x[i] = tempp[i];
|
|---|
| 72 | printf("Position 8\n");
|
|---|
| 73 | if (step < nsteps-1) {
|
|---|
| 74 | // rsnew = <r,r>
|
|---|
| 75 | rsnew = 0.0;
|
|---|
| 76 | printf("Position 9\n");
|
|---|
| 77 | for (int i=0; i<n; i++)
|
|---|
| 78 | rsnew += r[i]*r[i];
|
|---|
| 79 | printf("Position 10\n");
|
|---|
| 80 | $assume(rsold !=0);
|
|---|
| 81 | rsfrac = rsnew/rsold;
|
|---|
| 82 | printf("Position 11\n");
|
|---|
| 83 | for (int i=0; i<n; i++)
|
|---|
| 84 | tempp[i] = r[i] + rsfrac*p[i];
|
|---|
| 85 | printf("Position 12\n");
|
|---|
| 86 | for (int i=0; i<n; i++)
|
|---|
| 87 | p[i] = tempp[i];
|
|---|
| 88 | rsold = rsnew;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void main() {
|
|---|
| 94 | double matrix[N][N];
|
|---|
| 95 | double solution[N];
|
|---|
| 96 |
|
|---|
| 97 | for (int i=0; i<N; i++) {
|
|---|
| 98 | for (int j=0; j<i; j++) {
|
|---|
| 99 | matrix[i][j] = A[i][j];
|
|---|
| 100 | matrix[j][i] = A[i][j];
|
|---|
| 101 | }
|
|---|
| 102 | matrix[i][i] = A[i][i];
|
|---|
| 103 | }
|
|---|
| 104 | cg(N, matrix, B, solution, N);
|
|---|
| 105 | // check the solution is a solution...
|
|---|
| 106 | for (int i=0; i<N; i++) {
|
|---|
| 107 | double check = 0;
|
|---|
| 108 |
|
|---|
| 109 | for (int j=0; j<N; j++)
|
|---|
| 110 | check += matrix[i][j]*solution[j];
|
|---|
| 111 | $assert(check == B[i]);
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|