main
|
Last change
on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago |
|
Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
3.0 KB
|
| Line | |
|---|
| 1 | /*
|
|---|
| 2 | * CG 3x3 case with potitive definite assumption based on Cholesky decomposition.
|
|---|
| 3 | * From wiki:
|
|---|
| 4 | * The matrix M is positive definite if and only if there exists a unique lower
|
|---|
| 5 | * triangular matrix L, with real and strictly positive diagonal elements, such
|
|---|
| 6 | * that M = LL*. This factorization is called the Cholesky decomposition of M.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | #include <civlc.cvh>
|
|---|
| 10 | #include <stdio.h>
|
|---|
| 11 | #define n 3
|
|---|
| 12 |
|
|---|
| 13 | $input double diag1,diag2,diag3,off1,off2,off3;
|
|---|
| 14 | $input double b[n];
|
|---|
| 15 | double x[n];
|
|---|
| 16 | double xcg[n];
|
|---|
| 17 |
|
|---|
| 18 | void cg(double A[n][n], double b[n], double x[n], int steps) {
|
|---|
| 19 | double r[n];
|
|---|
| 20 | double p[n];
|
|---|
| 21 | double temp[n];
|
|---|
| 22 | double tempp[n];
|
|---|
| 23 | double rsold;
|
|---|
| 24 | double rsnew;
|
|---|
| 25 | double rsfrac;
|
|---|
| 26 | double alpha;
|
|---|
| 27 |
|
|---|
| 28 | // x = 0
|
|---|
| 29 | for(int i=0; i<n; i++) x[i] = 0;
|
|---|
| 30 |
|
|---|
| 31 | // temp = A*x
|
|---|
| 32 | for(int i=0; i<n; i++) {
|
|---|
| 33 | temp[i] = 0.0;
|
|---|
| 34 | for(int j=0; j<n; j++) {
|
|---|
| 35 | temp[i] += A[i][j]*x[j];
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | // r = b-temp
|
|---|
| 40 | for(int i=0; i<n; i++) {
|
|---|
| 41 | r[i] = b[i] -temp[i];
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | // rsold = <r,r>
|
|---|
| 45 | rsold = 0.0;
|
|---|
| 46 | for(int i=0; i<n; i++) {
|
|---|
| 47 | rsold += r[i]*r[i];
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | // p=r
|
|---|
| 51 | for(int i=0; i<n; i++) {
|
|---|
| 52 | p[i] = r[i];
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | for(int i=0; i<steps; i++) {
|
|---|
| 56 | // temp = A*p
|
|---|
| 57 | for(int i=0; i<n; i++) {
|
|---|
| 58 | temp[i] = 0.0;
|
|---|
| 59 | for(int j=0; j<n; j++) {
|
|---|
| 60 | temp[i] += A[i][j]*p[j];
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | alpha = 0.0;
|
|---|
| 64 | for(int i=0; i<n; i++) {
|
|---|
| 65 | alpha += p[i]*temp[i];
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | $assume(alpha !=0);
|
|---|
| 69 |
|
|---|
| 70 | alpha = rsold/alpha;
|
|---|
| 71 | // tempp = r-alpha*temp
|
|---|
| 72 | for(int i=0; i<n; i++) {
|
|---|
| 73 | tempp[i] = r[i] -alpha*temp[i];
|
|---|
| 74 | }
|
|---|
| 75 | for(int i=0; i<n; i++) {
|
|---|
| 76 | r[i] = tempp[i];
|
|---|
| 77 | }
|
|---|
| 78 | for(int i=0; i<n; i++) {
|
|---|
| 79 | tempp[i] = x[i] +alpha*p[i];
|
|---|
| 80 | }
|
|---|
| 81 | for(int i=0; i<n; i++) {
|
|---|
| 82 | x[i] = tempp[i];
|
|---|
| 83 | }
|
|---|
| 84 | if(i<steps-1) {
|
|---|
| 85 | // rsnew = <r,r>
|
|---|
| 86 | rsnew = 0.0;
|
|---|
| 87 | for(int i=0; i<n; i++) {
|
|---|
| 88 | rsnew += r[i]*r[i];
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | $assume(rsold !=0);
|
|---|
| 92 |
|
|---|
| 93 | rsfrac = rsnew/rsold;
|
|---|
| 94 | for(int i=0; i<n; i++) {
|
|---|
| 95 | tempp[i] = r[i] +rsfrac*p[i];
|
|---|
| 96 | }
|
|---|
| 97 | for(int i=0; i<n; i++) {
|
|---|
| 98 | p[i] = tempp[i];
|
|---|
| 99 | }
|
|---|
| 100 | rsold = rsnew;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | void main() {
|
|---|
| 106 | double bncg[n];
|
|---|
| 107 | double L[n][n]; //lower triangular matrix
|
|---|
| 108 | double LT[n][n];//its transpose
|
|---|
| 109 | double A[n][n];
|
|---|
| 110 |
|
|---|
| 111 | L[0][0] = diag1;
|
|---|
| 112 | L[1][1] = diag2;
|
|---|
| 113 | L[2][2] = diag3;
|
|---|
| 114 | L[0][1] = 0;
|
|---|
| 115 | L[1][0] = off1;
|
|---|
| 116 | L[0][2] = 0;
|
|---|
| 117 | L[2][0] = off2;
|
|---|
| 118 | L[1][2] = 0;
|
|---|
| 119 | L[2][1] = off3;
|
|---|
| 120 |
|
|---|
| 121 | LT[0][0] = diag1;
|
|---|
| 122 | LT[1][1] = diag2;
|
|---|
| 123 | LT[2][2] = diag3;
|
|---|
| 124 | LT[0][1] = off1;
|
|---|
| 125 | LT[1][0] = 0;
|
|---|
| 126 | LT[0][2] = off2;
|
|---|
| 127 | LT[2][0] = 0;
|
|---|
| 128 | LT[1][2] = off3;
|
|---|
| 129 | LT[2][1] = 0;
|
|---|
| 130 |
|
|---|
| 131 | for(int i=0; i<n; i++) {
|
|---|
| 132 | for(int j=0; j<n;j++) {
|
|---|
| 133 | A[i][j] = 0.0;
|
|---|
| 134 | for(int k=0;k<n;k++) {
|
|---|
| 135 | A[i][j] += L[i][k] * LT[k][j]; //form the input matrix A
|
|---|
| 136 | } // to ensure A is Positive Definite
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | cg(A,b,xcg,n);
|
|---|
| 141 | printf("\n================Solution x:================\n");
|
|---|
| 142 | for(int i=0; i<n; i++) {
|
|---|
| 143 | printf("x[%d] = %f\n\n",i, xcg[i]);
|
|---|
| 144 | }
|
|---|
| 145 | for(int i=0; i<n; i++) {
|
|---|
| 146 | bncg[i] = 0;
|
|---|
| 147 | for(int j=0; j<n; j++) {
|
|---|
| 148 | bncg[i] += A[i][j]*xcg[j];
|
|---|
| 149 | }
|
|---|
| 150 | $assert(bncg[i] == b[i]);
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.