C language Gaussian-ruordang Elimination Method

Source: Internet
Author: User

Gauss ruorang facilitates the solution of the N-element equation:

# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>

Float a [3] [4] = {,-}, {-3,-,-11}, {-, 2,-3 }};
Int rows = 3, cols = 4;


Void print_matrix ()
{// Print the Matrix
Int I, J;
Int M = rows, n = Cols;
For (I = 0; I <m; I ++)
{
For (j = 0; j <n; j ++ ){
Printf ("% 10.3f \ t", a [I] [J]);
}
Printf ("\ n ");
}
}
Void swap_row (row1, row2)
{// Exchange rows and columns
Int J = 0, n = Cols;
Float temp;
For (; j <n; j ++ ){
Temp = A [row1] [J];
A [row1] [J] = A [row2] [J];
A [row2] [J] = temp;
}
}
Void xiao_zhuyuan (INT row, int curj, float Val)
{// Deprecated to 1
Printf ("row % d divided by % F \ n", row, Val );
Int J = 0, n = Cols;
For (; j <n; j ++)
{
A [row] [J] = A [row] [J]/val;
}
}

Void Gauss (){
Int I = 0, j = 0; // rows and columns
Int M = rows, n = Cols;
While (I <M & J <n)
{
Int K, Maxi;
Float zhuyuan;
K = I + 1;

If (FABS (A [k] [J])> FABS (A [I] [J]) {

Swap_row (K, I); // sort by absolute value
Printf ("switching between row % d and row % d \ n", K, I );
Print_matrix ();
}


// Remove the non-1 principal component;
Zhuyuan = A [I] [J];
If (zhuyuan! = 1 ){

Xiao_zhuyuan (I, j, zhuyuan );
Print_matrix ();
} // If



If (A [k] [J]! = 0 ){
Int temp = 0;
Int U, V;

For (V = I; v <m-1; V ++ ){
Float per = A [V + 1] [J]/A [I] [J]; // Coefficient

Printf ("row % D minus (% F times) Row % d \ n", V + 1, Per, I );
For (u = 0; U <n; U ++ ){
A [V + 1] [u] = A [V + 1] [u]-Per * A [I] [u];

}
Print_matrix ();

}
J ++;
}
I ++;


} // While
} // Gauss

Int main ()
{
// The algorithm complexity of Gaussian elimination method is O (N3). That is to say, if the coefficient matrix is n × N, the calculation required by Gaussian elimination method is approximately proportional to N3.

/* 2x + Y-z = 8
*-3x-y + 2Z =-11
*-2x + Y + 2Z =-3
* Therefore, the augmented proof is:
* 2 1-1 8
*-3-1 2-11
*-2 1 2-3
*/

Int I, J;
Printf ("original Augmented Matrix \ n ");
Print_matrix ();
Gauss ();

// Printf ("matrix after elimination \ n ");
// Print_matrix ();

}

However, only the matrix Echelon is available currently.

Continue .....

------------------------------------------------------------------

In the previous example, some problems exist:

1. When switching rows, there is no full row comparison, only two adjacent rows are compared.

2. When the rows are displayed, the subscript of the array is used, so 1 is missing.

3. The display of floating point numbers is not optimized. Currently, "%. 3G" is used to reduce the length of the decimal part.

4. The Jordan method is not used for direct solution.

The following is a complete example, but it is not the best, because the variables in it seem messy.

# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>

Float a [3] [4] = {,-}, {-3,-,-11}, {-, 2,-3 }};
// Float a [4] [5] = {,-, 1}, {-3,-,-11,1}, {-, 2, -3, 1 },{ 5, 7, 8, 0, 1 }};
Int rows = 3, cols = 4;


Void print_matrix ()
{// Print the Matrix
Int I, J;
Int M = rows, n = Cols;
For (I = 0; I <m; I ++)
{
For (j = 0; j <n; j ++ ){
Printf ("% 10.3g", a [I] [J]);
}
Printf ("\ n ");
}
Printf ("\ n ");
}
Void swap_row (row1, row2)
{// Exchange rows and columns
Int J = 0, n = Cols;
Float temp;
For (; j <n; j ++ ){
Temp = A [row1] [J];
A [row1] [J] = A [row2] [J];
A [row2] [J] = temp;
}
}
Void xiao_zhuyuan (INT row, int curj, float Val)
{// Deprecated to 1
Printf ("row % 2D elimination %. 3g \ n", row + 1, Val );
Int J = 0, n = Cols;
For (; j <n; j ++)
{
A [row] [J] = A [row] [J]/val;
}
}

Void Gauss (){
Printf ("\ n Gaussian element elimination start: \ n ");
Int I = 0, j = 0; // rows and columns
Int M = rows, n = Cols;
While (I <M & J <n)
{
Int K, D, Maxi = I, maxval;
Float zhuyuan;
K = I + 1;
Maxval = FABS (A [I] [J]);
For (D = K; D <n; D ++ ){
If (FABS (A [d] [J])> maxval ){
Maxval = FABS (A [d] [J]);
Maxi = D;
}
}
If (Maxi! = I ){
Swap_row (Maxi, I); // sort by absolute value
Printf ("line % 2D and line % 2D interchange position \ n", Maxi + 1, I + 1 );
Print_matrix ();
}


// Remove the non-1 principal component;
Zhuyuan = A [I] [J];
If (zhuyuan! = 1 ){
Xiao_zhuyuan (I, j, zhuyuan );
Print_matrix ();
} // If



If (A [k] [J]! = 0 ){
Int temp = 0;
Int U, V;

For (V = I; v <m-1; V ++ ){
Float per = A [V + 1] [J]/A [I] [J]; // Coefficient

Printf ("row % 2D = row % 2D minus (%. 3G) times of rows % 2D \ n ", V + 2, V + 2, Per, I + 1 );
// Printf ("Subtract (%. 3f × row % d) from row % d \ n", Per, I + 1, V + 2 );
For (u = 0; U <n; U ++ ){
A [V + 1] [u] = A [V + 1] [u]-Per * A [I] [u];
}
Print_matrix ();
} //
J ++;
} // If
I ++;
} // While
} // Gauss

Void Jordan ()
{
Printf ("\ n if the consumer starts: \ n ");
Int I = 0, j = 0, K; // rows and columns
Int M = rows, n = Cols;
Float Xs;
For (I = s-1; I> = 0; I --){
For (k = 0; k <I; k ++ ){
Xs = A [k] [I];
If (I! = K ){
Printf ("row % 2D = row % 2D minus (%. 3G) times of rows % 2D \ n ", k + 1, k + 1, xs, I + 1 );
// Printf ("Subtract (%. 3f × row % d) from row % d \ n", xs, I + 1, k + 1 );
For (j = 0; j <n; j ++ ){
A [k] [J] = A [k] [J]-xs * A [I] [J];
}
Print_matrix ();
} // If
}
} // For I

} // Jordan
Int main ()
{

Int I, J;
Printf ("original Augmented Matrix \ n ");
Print_matrix ();
Gauss (); // use Gaussian elimination Element
Jordan (); // use a role as a consumer
}

 

When testing this program, we found that, if it is a square matrix, if the elements are eliminated, they will become a matrix similar to the following:

1 0 0 0 0 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 1

Appendix:

Gaussian elimination method Encyclopedia: http://zh.wikipedia.org/wiki/%E9% AB %98%E6%96%AF%E6%B6%88%E5%8E%BB%E6%B3%95
Gauss when the elimination method Encyclopedia: http://zh.wikipedia.org/wiki/%E9% AB %98%E6%96%AF-%E8%8B%A5%E7%88%BE%E7%95%B6%E6%B6%88%E5%85%83%E6%B3%95
Online Gaussian-ruoerdangdeyuan calculation: http://www.idomaths.com/gauss_jordan.php

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.