Here''s the beginning augmented matrix. The matrix to be invertedis on the left, and the identity matrix is on the right.-2.000 0.000 3.000 4.000 1.000 0.000 0.000 0.000 1.000 0.000 1.000 -2.000 0.000 1.000 0.000 0.000 -1.000 5.000 4.000 5.000 0.000 0.000 1.000 0.000 -2.000 3.000 8.000 7.000 0.000 0.000 0.000 1.000 Step 1: First we find the pivot, which we determine by lookingat the absolute value of the first each element in the firstcolumn. In this case we don''t do anything because the firstelement in the first column has an absolute value greater thanor equal to all the other elements in the first column.-2.000 0.000 3.000 4.000 1.000 0.000 0.000 0.000 1.000 0.000 1.000 -2.000 0.000 1.000 0.000 0.000 -1.000 5.000 4.000 5.000 0.000 0.000 1.000 0.000 -2.000 3.000 8.000 7.000 0.000 0.000 0.000 1.000 Then we divide every element in the first row by the value ofthe first element in the first row.1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 1.000 0.000 1.000 -2.000 0.000 1.000 0.000 0.000 -1.000 5.000 4.000 5.000 0.000 0.000 1.000 0.000 -2.000 3.000 8.000 7.000 0.000 0.000 0.000 1.000 Now we subtract 1 times row 1 from row 2.1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 -1.000 5.000 4.000 5.000 0.000 0.000 1.000 0.000 -2.000 3.000 8.000 7.000 0.000 0.000 0.000 1.000 Now we subtract -1 times row from row 3.1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 0.000 5.000 2.500 3.000 -0.500 0.000 1.000 0.000 -2.000 3.000 8.000 7.000 0.000 0.000 0.000 1.000 Now we subtract -2 times row 1 from row 4.1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 0.000 5.000 2.500 3.000 -0.500 0.000 1.000 0.000 0.000 3.000 5.000 3.000 -1.000 0.000 0.000 1.000 Step 2: Again find the pivot, in this case we''re looking atcolumn 2. We can''t use the 0 value in row two, so we need to swaprow 2 with another row. Row 3 has the largest value for column 2so we swap rows 2 and 3.1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 0.000 5.000 2.500 3.000 -0.500 0.000 1.000 0.000 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 0.000 3.000 5.000 3.000 -1.000 0.000 0.000 1.000 Now divide row 3 by the pivot value1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 0.000 1.000 0.500 0.600 -0.100 0.000 0.200 0.000 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 0.000 3.000 5.000 3.000 -1.000 0.000 0.000 1.000 Since row 1 has 0 in column 2, we don''t need to do anything torow 1.1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 0.000 1.000 0.500 0.600 -0.100 0.000 0.200 0.000 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 0.000 3.000 5.000 3.000 -1.000 0.000 0.000 1.000 Since row 3 has 0 in column 2, we don''t need to do anything torow 3.1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 0.000 1.000 0.500 0.600 -0.100 0.000 0.200 0.000 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 0.000 3.000 5.000 3.000 -1.000 0.000 0.000 1.000 Subtract 3 times row 2 from row 4.1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 0.000 1.000 0.500 0.600 -0.100 0.000 0.200 0.000 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 0.000 0.000 3.500 1.200 -0.700 0.000 -0.600 1.000 Step 3: No we''re working on column 3. Row 4 has a larger value incolumn 3 than row 3, so we swap rows 3 and 4.1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 0.000 1.000 0.500 0.600 -0.100 0.000 0.200 0.000 0.000 0.000 3.500 1.200 -0.700 0.000 -0.600 1.000 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 Then divide row 3 by it''s value in column 3.1.000 0.000 -1.500 -2.000 -0.500 0.000 0.000 0.000 0.000 1.000 0.500 0.600 -0.100 0.000 0.200 0.000 0.000 0.000 1.000 0.343 -0.200 0.000 -0.171 0.286 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 Subtract -1.5 times row 3 from row 1.1.000 0.000 0.000 -1.486 -0.800 0.000 -0.257 0.429 0.000 1.000 0.500 0.600 -0.100 0.000 0.200 0.000 0.000 0.000 1.000 0.343 -0.200 0.000 -0.171 0.286 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 Then subtract 0.5 times row 3 from row 2.1.000 0.000 0.000 -1.486 -0.800 0.000 -0.257 0.429 0.000 1.000 0.000 0.429 -0.000 0.000 0.286 -0.143 0.000 0.000 1.000 0.343 -0.200 0.000 -0.171 0.286 0.000 0.000 2.500 0.000 0.500 1.000 0.000 0.000 Then subtract 2.5 times row 3 from row 41.000 0.000 0.000 -1.486 -0.800 0.000 -0.257 0.429 0.000 1.000 0.000 0.429 -0.000 0.000 0.286 -0.143 0.000 0.000 1.000 0.343 -0.200 0.000 -0.171 0.286 0.000 0.000 0.000 -0.857 1.000 1.000 0.429 -0.714 Step 4: Even though row 1 has a greater absolute value in thefourth column than row 4, we don''t do any swapping because row1 has already been processed. In fact, this is the last row sono swapping can be done. So we just divide row 4 by -0.8571.000 0.000 0.000 -1.486 -0.800 0.000 -0.257 0.429 0.000 1.000 0.000 0.429 -0.000 0.000 0.286 -0.143 0.000 0.000 1.000 0.343 -0.200 0.000 -0.171 0.286 0.000 0.000 0.000 1.000 -1.167 -1.167 -0.500 0.833 Then subtract -1.486 times row 4 from row 1.1.000 0.000 0.000 0.000 -2.533 -1.733 -1.000 1.667 0.000 1.000 0.000 0.429 -0.000 0.000 0.286 -0.143 0.000 0.000 1.000 0.343 -0.200 0.000 -0.171 0.286 0.000 0.000 0.000 1.000 -1.167 -1.167 -0.500 0.833 Then subtract .429 times row 4 from row 2.1.000 0.000 0.000 0.000 -2.533 -1.733 -1.000 1.667 0.000 1.000 0.000 0.000 0.500 0.500 0.500 -0.500 0.000 0.000 1.000 0.343 -0.200 0.000 -0.171 0.286 0.000 0.000 0.000 1.000 -1.167 -1.167 -0.500 0.833 Then subtract .343 times row 4 from row 3.1.000 0.000 0.000 0.000 -2.533 -1.733 -1.000 1.667 0.000 1.000 0.000 0.000 0.500 0.500 0.500 -0.500 0.000 0.000 1.000 0.000 0.200 0.400 0.000 0.000 0.000 0.000 0.000 1.000 -1.167 -1.167 -0.500 0.833 Now the matrix has been reduced so the 4x4 on the right is(roughly) equivalent to the inverse of the original matrix.
If at any point all the unprocessed rows had 0''s for elements in the current column being worked on, then the matrix would have been non-invertible, and your function should react accordingly.