source: CIVL/examples/cg/cg2_sylvester.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.6 KB
Line 
1/*
2 * CG 2x2 case with positive definite assumption based on Sylvester's criteria.
3 * From wiki:
4 * Sylvester's criterion states that a Hermitian matrix M is positive-definite
5 * if and only if all the following matrices have a positive determinant:
6 * the upper left 1-by-1 corner of M,
7 * the upper left 2-by-2 corner of M,
8 * the upper left 3-by-3 corner of M,
9 * M itself.
10 * In other words, all of the leading principal minors must be positive.
11 */
12#include <civlc.cvh>
13#include <stdio.h>
14#define n 2
15
16$input double a1,a2,a3;
17$input double b[n];
18double x[n];
19double xcg[n];
20
21void cg(double A[n][n], double b[n], double x[n], int steps) {
22 double r[n];
23 double p[n];
24 double temp[n];
25 double tempp[n];
26 double rsold;
27 double rsnew;
28 double rsfrac;
29 double alpha;
30
31 for(int i=0; i<n; i++) x[i] = 0;
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 for(int i=0; i<n; i++) {
39 r[i] = b[i] -temp[i];
40 }
41 rsold = 0.0;
42 for(int i=0; i<n; i++) {
43 rsold += r[i]*r[i];
44 }
45 for(int i=0; i<n; i++) {
46 p[i] = r[i];
47 }
48 for(int i=0; i<steps; i++) {
49 for(int i=0; i<n; i++) {
50 temp[i] = 0.0;
51 for(int j=0; j<n; j++) {
52 temp[i] += A[i][j]*p[j];
53 }
54 }
55 alpha = 0.0;
56 for(int i=0; i<n; i++) {
57 alpha += p[i]*temp[i];
58 }
59
60 $assume(alpha !=0);
61
62 alpha = rsold/alpha;
63
64 // tempp = r-alpha*temp
65 for(int i=0; i<n; i++) {
66 tempp[i] = r[i] -alpha*temp[i];
67 }
68
69 // r = tempp
70 for(int i=0; i<n; i++) {
71 r[i] = tempp[i];
72 }
73
74 // tempp = x+alpha*p
75 for(int i=0; i<n; i++) {
76 tempp[i] = x[i] +alpha*p[i];
77 }
78
79 // x = tempp
80 for(int i=0; i<n; i++) {
81 x[i] = tempp[i];
82 }
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
98 // p = tempp
99 for(int i=0; i<n; i++) {
100 p[i] = tempp[i];
101 }
102 rsold = rsnew;
103 }
104 }
105}
106
107void main() {
108 double bncg[n];
109 double A[n][n];
110 A[0][0] = a1;
111 A[0][1] = a2;
112 A[1][0] = a2;
113 A[1][1] = a3;
114 $assume(b[0]!=0 || b[1]!=0);
115 $assume(a1 > 0); //assumption of Sylvester's criterion
116 $assume( (a1*a3 - a2*a2) > 0);//all principal minors are positive.
117 cg(A,b,xcg,n);
118 printf("\n================Solution x:================\n");
119 for(int i=0; i<n; i++) {
120 printf("x[%d] = %f\n\n",i, xcg[i]);
121 }
122 for(int i=0; i<n; i++) {
123 bncg[i] = 0;
124 for(int j=0; j<n; j++) {
125 bncg[i] += A[i][j]*xcg[j];
126 }
127 $assert(bncg[i] == b[i]);
128 }
129}
130
Note: See TracBrowser for help on using the repository browser.