-3

What does you block of code shown below do? I'm confused

  if (temp != j)
        for (k = 0; k < 2 * dimension; k++) {
            temporary = augmentedmatrix[j][k];
            augmentedmatrix[j][k] = augmentedmatrix[temp][k];
            augmentedmatrix[temp][k] = temporary;
        }
user3000403
  • 7
  • 1
  • 6
  • You assume your read from the file is successful. – chris Nov 24 '13 at 00:06
  • 2
    Warning! The determinant of your example matrix is zero, thus it hasn't an inverse! :-) You will get division by zero even with a perfect code :-) – peterh Nov 24 '13 at 00:09
  • at the moment this doesn't matter, but yes, change any of the numbers to make det not zero – 4pie0 Nov 24 '13 at 00:12
  • Done this now, didn't notice that *embarrased face* – user3000403 Nov 24 '13 at 00:13
  • 1
    This is not an error, if matrix is degenerate then the process of gauss-jordan elimination will end and you will find the actual rank of the matrix.Yuo won't get division by zero because you don't use division by det in GJ procedure – 4pie0 Nov 24 '13 at 00:45
  • @user3000403 please put it into another question and restore the previous content of this one to make things in right order – 4pie0 Nov 24 '13 at 16:39

1 Answers1

3

Edit: the original question was how to compute inverse matrix with Gaussian Elimination. OP has stucked on the actual elimination part of the algorithm.


now if element 1,1 is not zero then you are going to zero element 2,1 using matrix elementary operations:

  • F_s,t - interchange rows s and t

  • F_s,t_(a) - add row t*a to s

  • F_s_(a) - multiply row s by a

You can also start by zeroing 1,2 element.

These operations correspond to elementary matrices ( and to linear transformation matrices). Because each invertible matrix can be expressed as product of some elementary matrices

A = P1,...,Pk,Ql,...,Q1

which are invertible, we can retrieve inverse of A, A_inverse by applying corresponding operations to original matrix A, and this is the same as multiplication A by P,Q:

A_inverse = Q1_inv,...,Ql_inv,Pk_inv,...,P1_inv

For each row in a matrix, if the row does not consist of only zeros, then the left-most non-zero entry is called the leading coefficient (or pivot) of that row. So if two leading coefficients are in the same column, then a row operation of type 3 (see above) could be used to make one of those coefficients zero. Then by using the row swapping operation, one can always order the rows so that for every non-zero row, the leading coefficient is to the right of the leading coefficient of the row above. If this is the case, then matrix is said to be in row echelon form. So the lower left part of the matrix contains only zeros, and all of the zero rows are below the non-zero rows. The word "echelon" is used here because one can roughly think of the rows being ranked by their size, with the largest being at the top and the smallest being at the bottom.

now basically you should store augmented matrix

float augmentedmatrix[maximum][2*maximum] ;

so you can perform operations on matrix A and on identity matrix simultaneously.

Fill identity matrix:

/* augmenting with identity matrix of similar dimensions */

     for(i=0;i<dimension; i++)
      for(j=dimension; j<2*dimension; j++)
          if(i==j%dimension)
             augmentedmatrix[i][j]=1;
          else
             augmentedmatrix[i][j]=0;

and perform Gauss-Jordan elimination on the extended matrix by:

/* finding maximum jth column element in last (dimension-j) rows */
/* swapping row which has maximum jth column element */
/* performing row operations to form required identity matrix 
   out of the input matrix */

What you are missing is:

/* using gauss-jordan elimination */
for (j = 0; j < dimension; j++) {
    temp = j;
    /* finding maximum jth column element in last (dimension-j) rows */
    for (i = j + 1; i < dimension; i++)
        if (augmentedmatrix[i][j] > augmentedmatrix[temp][j])
            temp = i;

    if (fabs(augmentedmatrix[temp][j]) < minvalue) {
        printf("\n Elements are too small to deal with !!!");
        return -1;
    }
    /* swapping row which has maximum jth column element */
    if (temp != j)
        for (k = 0; k < 2 * dimension; k++) {
            temporary = augmentedmatrix[j][k];
            augmentedmatrix[j][k] = augmentedmatrix[temp][k];
            augmentedmatrix[temp][k] = temporary;
        }
    /* performing row operations to form required identity matrix out 
       of the input matrix */
    for (i = 0; i < dimension; i++)
        if (i != j) {
            r = augmentedmatrix[i][j];
            for (k = 0; k < 2 * dimension; k++)
                 augmentedmatrix[i][k] -= augmentedmatrix[j][k] *
                                                   r / augmentedmatrix[j][j];
        } else {
            r = augmentedmatrix[i][j];
            for (k = 0; k < 2 * dimension; k++)
                augmentedmatrix[i][k] /= r;
        }
}
4pie0
  • 29,204
  • 9
  • 82
  • 118