source: CIVL/examples/cg/cg2_cholesky.cvl@ 897c8d7

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 897c8d7 was 2d6a470, checked in by Stephen Siegel <siegel@…>, 10 years ago

Helping Si clean these up

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@2976 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 * CG 2x2 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#include <civlc.cvh>
9#include <stdio.h>
10#define n 2
11
12$input double a1,a2,a3;
13$assume(a1>0 && a3>0); //dignoal enties are positive
14$input double b[n];
15double x[n];
16double xcg[n];
17
18void 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 for(int i=0; i<n; i++) x[i] = 0;
29 for(int i=0; i<n; i++) {
30 temp[i] = 0.0;
31 for(int j=0; j<n; j++) {
32 temp[i] += A[i][j]*x[j];
33 }
34 }
35 for(int i=0; i<n; i++) {
36 r[i] = b[i] -temp[i];
37 }
38 rsold = 0.0;
39 for(int i=0; i<n; i++) {
40 rsold += r[i]*r[i];
41 }
42 for(int i=0; i<n; i++) {
43 p[i] = r[i];
44 }
45 for(int i=0; i<steps; i++) {
46 for(int i=0; i<n; i++) {
47 temp[i] = 0.0;
48 for(int j=0; j<n; j++) {
49 temp[i] += A[i][j]*p[j];
50 }
51 }
52 alpha = 0.0;
53 for(int i=0; i<n; i++) {
54 alpha += p[i]*temp[i];
55 }
56
57 $assume(alpha !=0);
58
59 alpha = rsold/alpha;
60
61 // tempp = r-alpha*temp
62 for(int i=0; i<n; i++) {
63 tempp[i] = r[i] -alpha*temp[i];
64 }
65
66 // r = tempp
67 for(int i=0; i<n; i++) {
68 r[i] = tempp[i];
69 }
70
71 // tempp = x+alpha*p
72 for(int i=0; i<n; i++) {
73 tempp[i] = x[i] +alpha*p[i];
74 }
75
76 // x = tempp
77 for(int i=0; i<n; i++) {
78 x[i] = tempp[i];
79 }
80
81 if(i<steps-1) {
82 // rsnew = <r,r>
83 rsnew = 0.0;
84 for(int i=0; i<n; i++) {
85 rsnew += r[i]*r[i];
86 }
87
88 $assume(rsold !=0);
89
90 rsfrac = rsnew/rsold;
91 for(int i=0; i<n; i++) {
92 tempp[i] = r[i] +rsfrac*p[i];
93 }
94
95 // p = tempp
96 for(int i=0; i<n; i++) {
97 p[i] = tempp[i];
98 }
99 rsold = rsnew;
100 }
101 }
102}
103
104void main() {
105 double bncg[n];
106 double L[n][n]; //lower triangular matrix
107 double LT[n][n];//its transpose
108 double A[n][n];
109
110 L[0][0] = a1;
111 L[0][1] = 0;
112 L[1][0] = a2;
113 L[1][1] = a3;
114
115 LT[0][0] = a1;
116 LT[0][1] = a2;
117 LT[1][0] = 0;
118 LT[1][1] = a3;
119
120 for(int i=0; i<n; i++) {
121 for(int j=0; j<n;j++) {
122 A[i][j] = 0.0;
123 for(int k=0;k<n;k++) {
124 A[i][j] += L[i][k] * LT[k][j]; //form the input matrix A
125 } // to ensure A is Positive Definite
126 }
127 }
128
129 $assume(b[0]!=0 || b[1]!=0);
130
131 cg(A,b,xcg,n);
132 printf("\n================Solution x:================\n");
133 for(int i=0; i<n; i++) {
134 printf("x[%d] = %f\n\n",i, xcg[i]);
135 }
136 for(int i=0; i<n; i++) {
137 bncg[i] = 0;
138 for(int j=0; j<n; j++) {
139 bncg[i] += A[i][j]*xcg[j];
140 }
141 $assert(bncg[i] == b[i]);
142 }
143}
144
Note: See TracBrowser for help on using the repository browser.