Gaussian elimination element method
First, we import several concepts.
definition 1: A matrix is called a ladder shape (row ladder), if it has the following three properties:
1. Each non-0 line is above every 10 lines;
2. The column where the leading element of a row is located behind the previous row of the pilot element;
3. The element below the column that contains the leading element is zero.
Like what
Definition 2: If a ladder-shaped matrix satisfies the following properties, it is called a simplified ladder shape (simplifying the row ladder shape):
1. The pilot element for each non-0 line is 1;
2. Each pilot element 1 is the only non-0 element of the column that contains the element.
Like what
theorem 1: A simplified ladder-shaped matrix for each matrix row equivalent Yu Wei. That is, simplifying the ladder-shaped matrix is unique.
below, we use a concrete example to illustrate the main steps of the Gaussian elimination method.
Original matrix:
The first step, starting with the leftmost non-0 column, is a primary column. The primary location is at the top of the column.
In the second step, select a non-0-element as the principal in the main tuple column. If necessary, shift the element to the main element position.
The third step is to transform the elements below the main element into 0 with the doubly-line transformation.
The fourth step, for the moment, regardless of the row containing the primary location and the rows above it, use the three steps above for the remaining sub-matrices until no 0 rows are required to be processed.
Repeat the above steps for each row.
The fifth step, starting with the rightmost main element, turns each element above each principal into 0. If one of the main elements is not 1, it will be changed to 1 by multiplying the transform.
Finally, we get the simplified ladder shape of the original matrix.
The 1th to 4th step, which is called the forward step of the line simplification algorithm, produces the only 5th step of simplifying the ladder shape, called the backward step.
C + + Implement
We try to implement the above steps in C + +. Here is a simple implementation, that is, the code describes the above steps, do not consider too many problems. You are welcome to point out the questions in the comments and make better suggestions for future improvement.
The approximate idea is to implement the forward step first:
First, we find the first non-zero element for each row, and set the line to 1 * * * *, with this line multiplied by a multiplier to each row after it.
To implement the backward steps again:
Then we start with the last line, select the main element, and add it to each previous row so that the column's elements are zero.
Finally, we completed the simplification and got a simplified ladder shape.
The above algorithm is only a rough implementation, mainly reflected in:
1. The selection of the main element is not optimal;
2. The problem of accuracy will occur;
3. Cannot be processed for certain situations.
Post the code temporarily and then have time to optimize it.
1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 intMain ()7 {8 Doublemartix[ -][ -];9 intN, M;//n rows M columnTen Onescanf"%d%d", &n, &m); A - //input - for(inti =0; I < n; i++) the for(intj =0; J < M; J + +) -scanf"%LF", &martix[i][j]); - - //forward Step + for(inti =0; I < n-1; i++) - { + //Brahma Yuan A intpos =0; at for(intj =0; J < M; J + +) - if(Martix[i][j]) - { -pos =J; - Break; - } in - if(Martix[i][pos]! =1&& Martix[i][pos]! =0) to { + DoubleTMP =Martix[i][pos]; - for(intj = Pos; J < M; J + +) the { *MARTIX[I][J] = Martix[i][j]/tmp; $ }Panax Notoginseng } - for(intj = i +1; J < N; J + +) the { + if(!Martix[j][pos]) A Continue; the DoubleTMP =Martix[j][pos]; + for(intK = pos; K < M; k++) - { $MARTIX[J][K] = martix[j][k]-martix[i][k] *tmp; $ } - } - } the - //Step BackwardWuyi for(inti = n-1; i >0; i--) the { - intpos =0; Wu for(intj =0; J < M; J + +) - if(Martix[i][j]) About { $pos =J; - Break; - } - A if(Martix[i][pos]! =1&& Martix[i][pos]! =0) + { the DoubleTMP =Martix[i][pos]; - for(intj = Pos; J < M; J + +) $ { theMARTIX[I][J] = Martix[i][j]/tmp; the } the } the - for(intj =0; J < I; J + +) in { the if(!Martix[j][pos]) the Continue; About DoubleTMP =Martix[j][pos]; the for(intK = pos; K < M; k++) the { theMARTIX[J][K] = martix[j][k]-martix[i][k] *tmp; + } - } the }Bayi the //Output the for(inti =0; I < n; i++) - { - for(intj =0; J < M; J + +) theprintf"%-10.2f", Martix[i][j]); theprintf"\ n"); the } the return 0; -}
Simple implementation of C + + with Gaussian elimination method