| [6360a31] | 1 | /* Commandline execution:
|
|---|
| 2 | * civl verify -inputN_BOUND=3 -inputM_BOUND=3 gaussElim.cvl
|
|---|
| 3 | * */
|
|---|
| [9803bc1] | 4 | #include<civlc.cvh>
|
|---|
| [53828dd] | 5 |
|
|---|
| [0baeebd] | 6 | $input int N_BOUND = 3; // bound on the number of rows
|
|---|
| 7 | $input int M_BOUND = 3; // bound on the number of columns
|
|---|
| [53828dd] | 8 | $input int N; // number of rows
|
|---|
| 9 | $input int M; // number of columns
|
|---|
| [3ff27cf] | 10 | $assume(N>0 && N<N_BOUND);
|
|---|
| 11 | $assume(M>0 && M<M_BOUND);
|
|---|
| [53828dd] | 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.
|
|---|
| 17 | void 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.
|
|---|
| 102 | void checkOutput() {
|
|---|
| 103 |
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void main() {
|
|---|
| 107 | $heap h;
|
|---|
| [73a0934] | 108 | double* matrix;
|
|---|
| [53828dd] | 109 |
|
|---|
| [73a0934] | 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));
|
|---|
| [53828dd] | 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 | }
|
|---|