source: CIVL/examples/experimental/gaussElim.cvl@ 6360a31

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 6360a31 was 6360a31, checked in by Tim Zirkel <zirkeltk@…>, 13 years ago

Added comment on running example to gaussElim

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

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