C ++ implements Gaussian elimination

Source: Internet
Author: User

I don't know how long I have not written a blog, so I don't want to write it ~~~ The main reason is that if you want to write something, there is really nothing to write during this period of time. If you don't want to take the exam, it's just a lot of annoying things.

I wrote some things about my mood on Douban ~~~~ Recently, I saw my friend develop a program for solving the equations, but he wrote it using Flex, which is a flash ~~ I personally think it is very interesting, so I want to write one myself. If you don't want anything else, just think about what the Gaussian elimination method is ??? Later, I found out that this method is often used when I do linear algebra. (later I found that this method was taught by my classmates, I want to convert it to C ++, So I carefully understood the Gaussian elimination method.


So let me first talk about what is Gaussian elimination?


Having a equations Ax = B, set a as a reversible matrix. The basic idea of the Gaussian elimination method is the deadlock. The true Elementary Line Transformation acts on the augmented matrix B = [a, B] of the equations, and converts a to an upper triangle matrix, then solve the triangle equations.

The above solution should be clear if you have learned linear algebra, if you really don't know, let's take a look at the matrix chapter of linear algebra.


The following describes how to solve the problem:

The key is how to replace a with an upper triangle (we suppose a is a matrix of N * n) in the addition matrix B, so the type of B is N * (n + 1)



// Depressed ~~~~~ How can I insert an image so difficult ~~~~~


Since I cannot view images, I will not introduce the mathematical form of this algorithm. below is the implementation of the algorithm I wrote using C ++ (not very well written, but it can still run)


# Include <iostream>
# Include <iomanip. h>
Using namespace STD;

Int change_num = 0;
Void display (double ** l_array, int r_size1, int r_size2) // This function is used to display the matrix (mainly used for debugging)
{
For (INT I = 0; I <r_size1; I ++)
For (Int J = 0; j <r_size2; j ++)
{

If (J! = 0)
{
Cout <setprecision (4) <setiosflags (ios_base: Left) <SETW (7) <l_array [I] [J];
}
Else
{
Cout <setprecision (4) <setiosflags (ios_base: Left) <l_array [I] [J] <"";
}

If (J + 1 = r_size2)
{
Cout <Endl;
}
}
Cout <resetiosflags (ios_base: Left );
}

// The following figure calculates the top triangle.
Bool change (double ** & array, int r_size, int c_size)
{
Int K = 0;
Double max = array [k] [k];
Int num = 0;
Bool flag = false;
While (k <r_size) // K indicates the current row
{
Max = array [k] [k];
For (INT I = K; I <r_size; I ++)
{
If (array [I] [k]> MAX)
{
Flag = true;
Max = array [I] [k];
Num = I; // num indicates the number of rows in the largest row.
}
}

If (! Flag)
{
Num = K;
}
If (max = 0) // if the maximum number in a column is 0, it indicates the singular matrix of this matrix.
{
Return false;
}
Else if (K! = Num)
{
Double temp = 0;
For (INT I = 0; I <c_size; I ++)
{
Temp = array [k] [I];
Array [k] [I] = array [num] [I];
Array [num] [I] = temp;
Change_num ++;
}
}

For (INT I = K; I <r_size-1; I ++)
{
Double temp = array [I + 1] [k]; // here temp must be saved, Because array [I + 1] [k] is used directly, the following computation will change
For (Int J = K; j <c_size; j ++)
{
Array [I + 1] [J]-= (temp/array [k] [k]) * array [k] [J]);
}
}
K ++;
Flag = false;
}
Return true;
}

Double rowlay (double ** & array, int r_size, int c_size) // calculates the determinant.


{
Double sum = 1;
For (INT I = 0; I <r_size; I ++)
{
Sum * = array [I] [I];
}
Return sum;
}

Void equation (double ** & array, int r_size, int c_size) // calculates the equations.
{
For (INT I = 0; I <r_size; I ++)
{
Double temp1 = array [I] [I];
For (Int J = I; j <c_size; j ++)
{
Array [I] [J]/= temp1;
}
}

For (INT I = 1; I <r_size; I ++)
{
For (Int J = I-1; j> = 0; j --)
{
Double temp2 = array [J] [I];
For (int h = I; H <c_size; H ++)
{
Array [J] [H]-= (temp2 * array [I] [H]);
}
}
}
}

Int main ()
{
Int COUNT = 0;
Cin> count;
Double ** array = new double * [count];
For (INT I = 0; I <count; I ++)
{
Array [I] = new double [count + 1];
}

For (INT I = 0; I <count; I ++)
For (Int J = 0; j <count + 1; j ++)
{
Cin> array [I] [J];
}
Bool flag = change (array, Count, Count + 1 );
If (FLAG)
{
Cout <Endl;
Double value = rowlay (array, Count, Count + 1 );
If (change_num % 2 = 1)
{
Value * =-1;
}
Cout <"determining factor" <value <Endl;

Equation (array, Count, Count + 1 );
Cout <"solves the equation as" <Endl;
For (INT I = 0; I <count; I ++)
{
Char CH = 'X' + I;
Cout <ch <"=" <array [I] [count] <Endl;
}
Display (array, Count, Count + 1 );

}
Else
{
Cout <"This matrix is a singular matrix" <Endl;
Cout <"there are countless solutions to this equation !!! "<Endl;
Cout <"determinant is" <0 <Endl;
}

For (Int J = 0; j <count; j ++)
{
Delete array [J];
}
Delete [] array;
}

 

 

 

This is the core algorithm, but Java is actually implemented. During the writing process, we find that Java cannot use pointers, which makes me confused for a while ~~~~ If Java is not used for the course, I don't think I will use Java. Java programs have some minor functions ~~~~~~ Upload ~~~~ Http://www.rayfile.com/zh-cn/files/18cef568-fc10-11de-b00d-0014221b798a/

 

If you are interested, please refer ~~~~~~~

 






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.