Gaussian elimination element integer solution template, Gaussian elimination integer Template

Source: Internet
Author: User

Gaussian elimination element integer solution template, Gaussian elimination integer Template

# Include <iostream> # include <string. h ># include <cmath> using namespace std; const int maxn = 105; int equ, var; // There are equ equations and var variable elements. The number of rows in the augmented array is equ, ranging from 0 to equ-1, and the number of columns is var + 1, ranging from 0 to var.int a [maxn] [maxn]; int x [maxn]; // solution set. bool free_x [maxn]; // determines whether the variable is uncertain. int free_num; void Debug (void) {int I, j; for (I = 0; I <equ; I ++) {for (j = 0; j <var + 1; j ++) {cout <a [I] [j] <";}cout <endl ;} // ------------------------------------ inline int gcd (int a, int B) {int t; while (B! = 0) {t = B; B = a % B; a = t;} return a;} inline int lcm (int a, int B) {return a * B/gcd (a, B);} // Gaussian elimination method (Gauss-Jordan elimination ). (-2 indicates a floating point solution, but no integer solution.-1 indicates no solution, 0 indicates a unique solution, and greater than 0 indicates an infinite solution, and returns the number of free variables) int Gauss (void) {int I, j, k; int max_r; // the row with the largest absolute value of the current column. int col; // The column currently being processed. int ta, tb, LCM, temp, free_x_num, free_index; // converts it to a step array. col = 0; // The column currently being processed. for (k = 0; k <equ & col <var; k ++, col ++) {// enumerate the current location Line. // find the row with the largest absolute value of the col column element and swap it with row k. (to reduce the error in Division) max_r = k; for (I = k + 1; I <equ; I ++) {if (abs (a [I] [col])> abs (a [max_r] [col]) max_r = I;} if (max_r! = K) {// exchange with row k. for (j = k; j <var + 1; j ++) swap (a [k] [j], a [max_r] [j]);} if (a [k] [col] = 0) {// indicates that the column k contains all 0, and the next column of the current row is processed. k --; continue;} for (I = k + 1; I <equ; I ++) {// enumerate the rows to be deleted. if (a [I] [col]! = 0) {LCM = lcm (abs (a [I] [col]), abs (a [k] [col]); ta = LCM/abs (a [I] [col]), tb = LCM/abs (a [k] [col]); if (a [I] [col] * a [k] [col] <0) tb =-tb; // if the difference is that two numbers are added together. for (j = col; j <var + 1; j ++) {a [I] [j] = a [I] [j] * ta-a [k] [j] * tb ;}} Debug (); // 1. solution-free: the simplified augmented array exists (0, 0 ,..., a) such a row (! = 0 ). for (I = k; I <equ; I ++, record exchange. if (a [I] [col]! = 0) return-1;} // 2. infinite solution: In the augmented array of var * (var + 1), (0, 0 ,..., 0) This indicates that no strict upper triangle array is formed. // The number of rows displayed is the number of free yuan. if (k <var) {// first, there are var-k free variables, that is, there are at least var-k uncertain variables. for (I = k-1; I> = 0; I --) {// row I won't be (0, 0 ,..., 0), because such rows are from row k to row equ. // Similarly, row I will not be (0, 0 ,..., a),! If it is 0, there is no such solution. free_x_num = 0; // used to determine the number of uncertain variable elements in the row. If more than one variable exists, it cannot be solved and they are still uncertain variable elements. for (j = 0; j <var; j ++) if (a [I] [j]! = 0 & free_x [j]) free_x_num ++, free_index = j; if (free_x_num> 1) continue; // No fixed variable can be obtained. // there is only one uncertain variable free_index, so we can solve the variable and the variable is determined. temp = a [I] [var]; for (j = 0; j <var; j ++) if (a [I] [j]! = 0 & j! = Free_index) temp-= a [I] [j] * x [j]; x [free_index] = temp/a [I] [free_index]; // obtain the variable. free_x [free_index] = 0; // The variable is determined .} return var-k; // The free variable has var-k .} // 3. unique Solution: a strict upper triangle array is formed in the augmented array of var * (var + 1. // calculate the Xn-1, Xn-2... x0.for (I = var-1; I> = 0; I --) {temp = a [I] [var]; for (j = I + 1; j <var; j ++) if (a [I] [j]! = 0) temp-= a [I] [j] * x [j]; if (temp % a [I] [I]! = 0) return-2; // indicates that there is a floating point solution, but no integer solution. x [I] = temp/a [I] [I];} return 0;} int main () {int I, j; while (~ Scanf ("% d", & equ, & var) {memset (a, 0, sizeof (a); memset (x, 0, sizeof (x); memset (free_x, 1, sizeof (free_x); // At the beginning, all variables are uncertain. for (I = 0; I <equ; I ++) {for (j = 0; j <var + 1; j ++) {scanf ("% d ", & a [I] [j]) ;}// Debug (); free_num = Gauss (); if (free_num =-1) printf ("no solution! \ N "); else if (free_num =-2) printf (" floating point solution, no integer solution! \ N "); else if (free_num> 0) {printf (" infinite solution! Free variable number: % d \ n ", free_num); for (I = 0; I <var; I ++) {if (free_x [I]) printf ("x % d is uncertain \ n", I + 1); else printf ("x % d: % d \ n", I + 1, x [I]) ;}} else {for (I = 0; I <var; I ++) {printf ("x % d: % d \ n ", I + 1, x [I]) ;}} printf ("\ n") ;}return 0 ;}


What is Gaussian deyuan?

As Blue Crystal says, here is an example:
A three-element one-time equation:
A1x + b1y + c1z = d1
A2x + b2y + c2z = d2
A3x + b3y + c3z = d3
Step 1: divide the first equation by a1 and change it:
X + b1 'y + c1 'z = 1'
Multiply it by a2 and subtract from the second equation (remove x from the second equation) to change the second equation:
B2 'y + c2 'z = d2'
After performing this operation on the third equation, the entire equation is composed:
X + b1 'y + c1 'z = 1'
B2 'y + c2 'z = d2'
B3 'y + c3' z = d3'

Step 2: divide the second equation by b2'. After the preceding operation, the equations are changed:
X + c1 ''z = d1''
Y + c2 ''z = d2''
C3 ''z = d3''

Step 3: divide the third equation by c3 ''. After performing the preceding operations, the equations are changed:
X = d1 '''
Y = d2 '''
Z = d3 '''

Do you understand it? To sum up, the first step is to change the x coefficient of the first equation to 1 and remove the x coefficient of the other equations. The second step is to change the y coefficient of the second equation to 1, at the same time, remove y from other equations, step 3, change the coefficient of z in the third equation to 1, and remove z from other equations. In this way, the first line is the solution of x, the second line is the solution of y, and the third line is the solution of z. for a equations of any element, so can we.

Gaussian elimination method

In mathematics, Gaussian elimination (or Gaussian elimination) is an algorithm in linear algebra. It can be used to solve linear equations and obtain the rank of a matrix, and obtain the inverse matrix of the reversible matrix. When used in a matrix, the Gaussian elimination method generates a "Row ladder array ". Gaussian elimination method can be used in computers to solve thousands of equations and unknown numbers. However, if there are millions of equations, this algorithm will be very time-consuming. Some extremely large equations are usually solved by means of stacks. There are also some methods specifically used to solve some equations with specially arranged coefficients.

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.