source: CIVL/examples/experimental/gaussElim.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: 3.5 KB
Line 
1/* Commandline execution:
2 * civl verify -inputN_BOUND=3 -inputM_BOUND=3 gaussElim.cvl
3 * */
4#include<civlc.cvh>
5
6$input int N_BOUND = 3; // bound on the number of rows
7$input int M_BOUND = 3; // bound on the number of columns
8$input int N; // number of rows
9$input int M; // number of columns
10$assume(N>0 && N<N_BOUND);
11$assume(M>0 && M<M_BOUND);
12$input double inputMatrix[N*M];
13$output double outputMatrix[N*M];
14
15// Perform Guass-Jordon elimination on the matrix. Upon return, the
16// matrix will be in reduced row echelon form.
17void gaussElim(double* matrix) {
18 int top = 0; // index of current top row
19 int col = 0; // index of current left column
20 int pivotRow = 0; // index of row containing the pivot
21 double pivot = 0.0; // the value of the pivot
22 int i = 0; // loop variable over rows of matrix
23 int j = 0; // loop variable over columns of matrix
24 double tmp = 0.0; // temporary double variable
25
26 col = 0;
27 for (top=0; top<N && col< M; top++) {
28 /* At this point we know that the submatarix consisting of the
29 * first top rows of A is in reduced row-echelon form. We will
30 * now consider the submatrix B consisting of the remaining rows.
31 * We know, additionally, that the first col columns of B are all
32 * zero.
33 */
34
35 /* Step 1: Locate the leftmost column of B that does not consist
36 * entirely of zeros, if one exists. The top nonzero entry of
37 * this column is the pivot. */
38
39 pivot = 0.0;
40 for (; col < M; col++) {
41 for (pivotRow = top; pivotRow < N; pivotRow++) {
42 pivot = matrix[pivotRow*M + col];
43 if (pivot) break;
44 }
45 if (pivot) break;
46 }
47
48 if (col >= M) {
49 break;
50 }
51
52 /* At this point we are guaranteed that pivot = A[pivotRow,col] is
53 * nonzero. We also know that all the columns of B to the left of
54 * col consist entirely of zeros. */
55
56
57 /* Step 2: Interchange the top row with the pivot row, if
58 * necessary, so that the entry at the top of the column found in
59 * Step 1 is nonzero. */
60
61 if (pivotRow != top) {
62 for (j = 0; j < M; j++) {
63 tmp = matrix[top*M + j];
64 matrix[top*M + j] = matrix[pivotRow*M + j];
65 matrix[pivotRow*M + j] = tmp;
66 }
67 }
68
69 /* At this point we are guaranteed that A[top,col] = pivot is
70 * nonzero. Also, we know that (i>=top and j<col) implies
71 * A[i,j] = 0. */
72
73 /* Step 3: Divide the top row by pivot in order to introduce a
74 * leading 1. */
75
76 for (j = col; j < M; j++) {
77 matrix[top*M + j] /= pivot;
78 }
79
80 /* At this point we are guaranteed that A[top,col] is 1.0,
81 * assuming that floating point arithmetic guarantees that a/a
82 * equals 1.0 for any nonzero double a. */
83
84 /* Step 4: Add suitable multiples of the top row to all other rows
85 * so that all entries above and below the leading 1 become
86 * zero. */
87
88 for (i = 0; i < N; i++) {
89 if (i != top){
90 tmp = matrix[i*M + col];
91 for (j = col; j < M; j++) {
92 matrix[i*M + j] -= matrix[top*M + j]*tmp;
93 }
94 }
95 }
96 col++;
97 }
98
99}
100
101// Check that the output matrix is indeed in reduced row echelon form.
102void checkOutput() {
103
104}
105
106void main() {
107 $heap h;
108 double* matrix;
109
110 // Need to concretize N & M to help verification.
111 for (int i = 0; i < N; i++);
112 for (int i = 0; i < M; i++);
113 matrix = (double *) $malloc(&h, N*M*sizeof(double));
114 for (int i = 0; i < N*M; i++) {
115 matrix[i] = inputMatrix[i];
116 }
117 gaussElim(matrix);
118 for (int i = 0; i < N*M; i++) {
119 outputMatrix[i] = matrix[i];
120 }
121 checkOutput();
122}
Note: See TracBrowser for help on using the repository browser.