Introduction to Gaussian elimination

Source: Internet
Author: User
Preface

LearningGaussian deyuanPreviously, I thought it was really difficult. After learning it, I found that Gaussian yuan consumption was actually quite simple.

First, we will use an example to get a preliminary understanding of Gaussian deyuan.

Here is a set of equations:

\ [\ Begin {cases} 5x + 4y = 24, \ 4x + 3y = 19. \ end {cases} \]

How can this group of equations be solved using Gaussian elimination elements?

We first use a \ (delta \) to recordTo remove \ (x \) from formula ②, the multiple of formula ① To be expanded. It is not difficult to find that in this example, \ (delta =-\ frac45 \). Therefore, let's take the equation ① and the left and right sides of the multiplication \ (delta \), and get:

\ [-4x-\ frac {16} 5y =-\ frac {96} 5 \ tag {equality} \]

Add formula ② To cancel \ (x \)

\ [-\ Frac15y =-\ frac15 \ tag {addition and subtraction elimination method} \]

The following formula is obtained by combining formula ① With the formula ④ obtained by eliminating the RMB:

\ [\ Begin {cases} 5x + 4y = 24, \-\ frac15y =-\ frac15 \ end {cases} \]

Then, we can start from the last formula and find the answer to each unknown:

  • Formula 4:
    \ (Y =-\ frac15 audio-\ frac15 = 1 \)
    Method 1
    \ [5x = 20 \ tag {substitution elimination method} \]
  • Formula 1:
    \ (X = 20 then 5 = 4 \)

In conclusion, the solution of the original equations is
\ [\ Begin {cases} X = 4, \ y = 1. \ end {cases} \]

Train of Thought

Next, let's sort out the previous steps and ideas:

  • Step 1: first select the first element of the first line, and thenAddition and subtraction Elimination MethodRemoves this element from each row.
  • Step 2: we find that the last equation can be directly solved, and then we useSubstitution Elimination MethodThe solution obtained from the last equation is substituted into each of the preceding equations to eliminate the element.

The above steps are onlyBinary EquationFor\ (N \) Element EquationThe Gaussian elimination method should be used as follows:

  1. Create a variable \ (I \), initialize \ (I = 1 \)
  2. Find an equation with the number of rows greater than or equal to \ (I \) and the number of \ (I \) element coefficient is not 0, and move it to line \ (I \)
  3. If not, output \ (NO \) \ (solution \) and exit the program.
  4. Create variable \ (J \), initialize \ (j = I + 1 \)
  5. Use a variable \ (delta \) to record the \ (I \) and \ (J \) rows to be added to remove the \ (I \) the number of multiples of the element line \ (I \) to be expanded, that is, \ (-\ frac {coefficient of element I of Row J} {coefficient of element I of row I }\)
  6. Multiply the coefficient of each element in line \ (J \) with the coefficient of the element corresponding to line \ (I \) by the value of \ (delta \).
  7. Add \ (J \) to 1. If \ (J \) is not greater than \ (n \), return to Step 5.
  8. Add \ (I \) to 1. If \ (I \) is not greater than \ (n \), return to step 2.
  9. Assign \ (I \) to \ (n \) again \)
  10. If the coefficient of the element \ (I \) In line \ (I \) is 0, output \ (NO \) \ (solution \) and exit the program
  11. Otherwise, the coefficients of other elements in the row must be 0, so the \ (I \) the value of an unknown number is \ (\ frac {the value on the right of the equal sign of line I} {the coefficient of the element of line I }\)
  12. Place the value of the \ (I \) element into the formula of the \ (1 \ SIM i-1 \) Row to remove the \ (I \) unknown
  13. Subtract 1 from \ (I \). If \ (I \) is not less than 1, return step 10.

The next step is the implementation of the Code.

Code
# Include <bits/stdc ++. h> # define max (x, y) (x)> (y )? (X) :( y) # define min (x, y) (x) <(y )? (X) :( y) # define ABS (x) <0? -(X) :( X) # define ll long # define ull unsigned long # define Tc () (a = B & (B = (a = ff) + fread (FF, 1,100000, stdin), A = B )? EOF: * A ++) # define n 100 char FF [100000], * A = ff, * B = ff; using namespace STD; const double EPS = 1e-6; int N; double A [n + 5] [n + 5], s [n + 5]; inline int read () {int x = 0, F = 1; static char ch; while (! Isdigit (CH = tc () F = CH ^ '-'? 1:-1; while (x = (x <3) + (x <1) + ch-48, isdigit (CH = tc ())); return x * F;} inline void swap (double & X, double & Y) {double T = x; X = Y, y = T;} inline void find_line (int x) // find an equation with the number of rows greater than or equal to X and the coefficient of element x is not 0, and move it to row X {register int I = x, J; while (I <= N & FABS (A [I] [x]) <EPS) ++ I; // as long as I is not greater than N and the X-th element of the row is 0, I will add 1 if (I> N) puts ("no solution "), exit (0); // if there is no solution, output "no solution" and exit the program for (j = 1; j <= N; ++ J) swap (A [x] [J], a [I] [J]); // move row I to row x} int main () {r Egister int I, j, k; For (n = read (), I = 1; I <= N; s [I ++] = read ()) for (j = 1; j <= N; ++ J) A [I] [J] = read (); // read the coefficient of each element and the value on the right of the equal sign for (I = 1; I <= N; ++ I) {find_line (I ); for (j = I + 1; j <= N; ++ J) // deletes [I + 1 ~ N] The I element in each row {Double Delta =-A [J] [I]/A [I] [I]; // The Delta record will remove the multiple to be expanded for (k = I; k <= N; ++ k) A [J] [k] + = A [I] [k] * delta; // Add the coefficient of each element in row J to the coefficient of the element corresponding to row I multiplied by the value s [J] + = s [I] * delta ;}} for (I = N; I; -- I) {If (! A [I] [I]) return puts ("no solution"), 0; // If the coefficient of the I-th element of row I is 0, output "no solution" and exit the program for (s [I]/= A [I] [I], j = I-1; j; -- J) s [J]-= A [J] [I] * s [I]; // calculates the value of the I-th unknown, and enter the value of element I to 1st ~ The equation in the I-1 line removes the I unknown} for (I = 1; I <= N; ++ I) printf ("%. 2lf \ n ", s [I]); // output the return 0 for each unknown number ;}
Example

Example: [bzoj1013] [jsoi2008] spherical space Generator

Introduction to Gaussian elimination

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.