source: CIVL/examples/experimental/gaussElim.cvl@ 53828dd

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

Added partially complete Gaussian elimination example to experimental directory.

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

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