source: CIVL/examples/cg/cg2_sylvester.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.5 KB
Line 
1/*
2 * CG 2x2 case with positive definite assumption
3 * based on Sylvester's criteria.
4 * From wiki: https://en.wikipedia.org/wiki/Sylvester%27s_criterion
5 * Sylvester's criterion states that a Hermitian matrix M
6 * is positive-definite if and only if all the following
7 * matrices have a positive determinant:
8 * the upper left 1-by-1 corner of M,
9 * the upper left 2-by-2 corner of M,
10 * the upper left 3-by-3 corner of M,
11 * M itself.
12 * In other words, all of the leading principal minors must be positive.
13 */
14
15#include <civlc.cvh>
16#include <stdio.h>
17#define n 2
18
19$input double a1,a2,a3;
20$input double b[n];
21double x[n];
22double xcg[n];
23
24void cg(double A[n][n], double b[n], double x[n]) {
25 double r[n]; //residual
26 double p[n]; //search direction
27 double temp[n];
28 double temp2[n];
29 double rsold; //<r, r>
30 double rsnew;
31 double alpha;
32 double beta;
33
34 for (int i=0; i<n; i++) x[i] = 0;
35 for (int i=0; i<n; i++) {
36 temp[i] = 0.0;
37 for(int j=0; j<n; j++) temp[i] += A[i][j]*x[j];
38 }
39 for (int i=0; i<n; i++) r[i] = b[i] - temp[i];
40 rsold = 0.0;
41 for (int i=0; i<n; i++) rsold += r[i]*r[i];
42 for (int i=0; i<n; i++) p[i] = r[i];
43 for (int k=0; k<n; k++) {
44 for (int i=0; i<n; i++) {
45 temp[i] = 0.0;
46 for (int j=0; j<n; j++) temp[i] += A[i][j]*p[j];
47 }
48 alpha = 0.0;
49 for (int i=0; i<n; i++) alpha += p[i]*temp[i];
50 $assume(alpha != 0);
51 alpha = rsold/alpha;
52 // temp2 = r-alpha*temp
53 for (int i=0; i<n; i++) temp2[i] = r[i] -alpha*temp[i];
54 // r = temp2
55 for (int i=0; i<n; i++) r[i] = temp2[i];
56 // temp2 = x+alpha*p
57 for (int i=0; i<n; i++) temp2[i] = x[i] +alpha*p[i];
58 // x = temp2
59 for (int i=0; i<n; i++) x[i] = temp2[i];
60 if (k<n-1) {
61 // rsnew = <r,r>
62 rsnew = 0.0;
63 for (int i=0; i<n; i++) rsnew += r[i]*r[i];
64 $assume(rsold !=0);
65 beta = rsnew/rsold;
66 for (int i=0; i<n; i++) temp2[i] = r[i] +beta*p[i];
67 // p = temp2
68 for (int i=0; i<n; i++) p[i] = temp2[i];
69 rsold = rsnew;
70 }
71 }
72}
73
74void main() {
75 double Ax[n];
76 double A[n][n];
77
78 A[0][0] = a1;
79 A[0][1] = a2;
80 A[1][0] = a2;
81 A[1][1] = a3;
82 $assume(b[0]!=0 || b[1]!=0);
83 $assume(a1 > 0); //assumption of Sylvester's criterion
84 $assume( (a1*a3 - a2*a2) > 0);//all principal minors are positive.
85 cg(A,b,xcg);
86 for(int i=0; i<n; i++) {
87 printf("x[%d] = %f\n\n",i, xcg[i]);
88 }
89 for(int i=0; i<n; i++) {
90 Ax[i] = 0;
91 for(int j=0; j<n; j++)
92 Ax[i] += A[i][j]*xcg[j];
93 $assert(Ax[i] == b[i]);
94 }
95}
96
Note: See TracBrowser for help on using the repository browser.