source: CIVL/examples/cg/cg2_cholesky.cvl

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: 2.6 KB
Line 
1/*
2 * CG 2x2 case with potitive definite assumption
3 * based on Cholesky decomposition.
4 * From wiki: https://en.wikipedia.org/wiki/Cholesky_decomposition
5 * The matrix M is positive definite if and only if there exists
6 * a unique lower triangular matrix L, with real and strictly
7 * positive diagonal elements, such that M = LL*.
8 */
9
10#include <civlc.cvh>
11#include <stdio.h>
12#define n 2
13
14$input double a1,a2,a3;
15$input double b[n];
16double x[n];
17double xcg[n];
18
19void cg(double A[n][n], double b[n], double x[n]) {
20 double r[n]; //residual
21 double p[n]; //search direction
22 double temp[n];
23 double temp2[n];
24 double rsold; //<r, r>
25 double rsnew;
26 double alpha;
27 double beta;
28
29 for (int i=0; i<n; i++) x[i] = 0;
30 for (int i=0; i<n; i++) {
31 temp[i] = 0.0;
32 for(int j=0; j<n; j++) temp[i] += A[i][j]*x[j];
33 }
34 for (int i=0; i<n; i++) r[i] = b[i] - temp[i];
35 rsold = 0.0;
36 for (int i=0; i<n; i++) rsold += r[i]*r[i];
37 for (int i=0; i<n; i++) p[i] = r[i];
38 for (int k=0; k<n; k++) {
39 for (int i=0; i<n; i++) {
40 temp[i] = 0.0;
41 for (int j=0; j<n; j++) temp[i] += A[i][j]*p[j];
42 }
43 alpha = 0.0;
44 for (int i=0; i<n; i++) alpha += p[i]*temp[i];
45 $assume(alpha != 0);
46 alpha = rsold/alpha;
47 // temp2 = r-alpha*temp
48 for (int i=0; i<n; i++) temp2[i] = r[i] -alpha*temp[i];
49 // r = temp2
50 for (int i=0; i<n; i++) r[i] = temp2[i];
51 // temp2 = x+alpha*p
52 for (int i=0; i<n; i++) temp2[i] = x[i] +alpha*p[i];
53 // x = temp2
54 for (int i=0; i<n; i++) x[i] = temp2[i];
55 if (k<n-1) {
56 // rsnew = <r,r>
57 rsnew = 0.0;
58 for (int i=0; i<n; i++) rsnew += r[i]*r[i];
59 $assume(rsold !=0);
60 beta = rsnew/rsold;
61 for (int i=0; i<n; i++) temp2[i] = r[i] +beta*p[i];
62 // p = temp2
63 for (int i=0; i<n; i++) p[i] = temp2[i];
64 rsold = rsnew;
65 }
66 }
67}
68
69void main() {
70 double Ax[n]; // result of A multiply x after get solution x
71 double L[n][n]; //lower triangular matrix
72 double LT[n][n];//its transpose
73 double A[n][n];
74
75 L[0][0] = a1;
76 L[0][1] = 0;
77 L[1][0] = a2;
78 L[1][1] = a3;
79
80 LT[0][0] = a1;
81 LT[0][1] = a2;
82 LT[1][0] = 0;
83 LT[1][1] = a3;
84
85 for (int i=0; i<n; i++) {
86 for (int j=0; j<n; j++) {
87 A[i][j] = 0.0;
88 for (int k=0; k<n; k++) {
89 A[i][j] += L[i][k] * LT[k][j]; //form the input matrix A
90 } // to ensure A is Positive Definite
91 }
92 }
93
94 $assume(b[0]!=0 || b[1]!=0);
95
96 cg(A,b,xcg);
97 for (int i=0; i<n; i++) {
98 printf("x[%d] = %f\n\n",i, xcg[i]);
99 }
100 for (int i=0; i<n; i++) {
101 Ax[i] = 0;
102 for (int j=0; j<n; j++)
103 Ax[i] += A[i][j]*xcg[j];
104 $assert(Ax[i] == b[i]);
105 }
106}
107
Note: See TracBrowser for help on using the repository browser.