Gaussian elimination of linear equations

A form of

\(\begin{cases} A_{11}X_{1} + A_{12}X_{2} + ......+A_{1n}X_{n}=B_{1} \\ A_{21}X_{1} + A_{22}X_{2} + ......+A_{2n}X_{n}=B_{2}\\ ...... A_{n1}X_{1} + A_{n2}X_{2} + ......+A_{nn}X_{n}=B_{n} \end{cases}\)

The above equations:

\(A=\)\(\begin{bmatrix} A_{11} & \cdots & A_{1n} \\ \vdots & \vdots & \vdots \\ A_{n1} & \cdots & A_{nn} \end{bmatrix}\)

\(B=\)\(\begin{bmatrix} B_{1} \\ \vdots \\ B_{n} \end{bmatrix}\)

\(X=\)\(\begin{bmatrix} X_{1} \\ \vdots \\ X_{n} \end{bmatrix}\)

Can be seen as: \ (AX = B \)

Second, elementary transformation (lines)

1. The same or by a line with a number of other (non-zero)
2. a row to another row
3. The exchange of any two rows

Values ​​are the same (we all know)

Third, step

We can find solutions of linear equations is the equivalent elementary equation, so we step is equivalent to the equation method to simulate elementary school

1. Elimination

For the first we consider \ (I \) equations, we eliminate the elimination \ (X_ {i} \)

So we need to find each equation found for the \ (X_i \) largest coefficient \ (MAXN \) and its corresponding equation, it first \ (I \) equations swap, this can ensure maximum coefficient \ (X_i \) minimum number of treatments, the minimum time.

After finding the biggest factor, we need to determine what \ (maxn \) is equal to 0, if equal to 0 represents no solution. However, because the use of \ (Double \) type, to consider accuracy problems, the condition is determined to be smaller than \ (EPS \) ,

for(int i=1;i<=n;i++)
{
    int now=i;
    for(int j=i+1;j<=n;j++)
    {
        if(fabs(a[now][i])<fabs(a[j][i]))now=j;//寻找x[i]的最大系数
    }
    if(fabs(a[now][i])<eps)//判断,如果x[i]的系数小于eps,说明x[i]=0,判断为无解
    {
        puts("No Solution");
        return 0;
    }
    if(i!=now)swap(a[i],a[now]);//将最大系数x[i]所在的方程与第i方程交换 

We madeElementary School Mathematical OlympiadAvailability, if we are to make \ (X_i \) disappear, the most convenient way is to put all equations \ (X_i \) of coefficients into \ (1 \) , then you can add and subtract elimination, in Note that other process variables in the equation will change accordingly

    dl div=a[i][i];//这就是maxn
    for(int j=i;j<=n+1;j++)a[i][j]/=div;//先处理以maxn为系数的x[i]所在的方程
    for(int j=i+1;j<=n;j++)//将其他方程都进行处理
    {
        div=a[j][i];//其他方程的x[i]的系数
        for(int k=i;k<=n+1;k++)
        {
            a[j][k]-=div*a[i][k];
        }       
    }
}

So that we can keep doing this loop, the final processing of \ (X_n \)

ans[n]=a[n][n+1]/a[n][n];

2. The back substitution

Back-substitution is also well understood, each time from the \ (n-\) back into analog substituting equation, the processing of each \ (X_i \)

for(int i=n-1;i>=1;i--)
{
    ans[i]=a[i][n+1];
    for(int j=i+1;j<=n;j++)ans[i]-=ans[j]*a[i][j];
}

Overall Code

#include<bits/stdc++.h>
using namespace std;
typedef double dl;
const int N=110;
const dl eps=1e-9;
dl a[N][N],ans[N],x[N][N];
int n;

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n+1;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%lf",&x[i][j]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            a[i][j]=2*(x[1][j]-x[i+1][j]);
            a[i][n+1]+=x[1][j]*x[1][j]-x[i+1][j]*x[i+1][j];
        }
    }
    for(int i=1;i<=n;i++)
    {
        int now=i;
        for(int j=i+1;j<=n;j++)
        {
            if(fabs(a[now][i])<fabs(a[j][i]))now=j;
        }
        if(fabs(a[now][i])<eps)
        {
            puts("No Solution");
            return 0;
        }
        if(i!=now)swap(a[i],a[now]);
        dl div=a[i][i];
        for(int j=i;j<=n+1;j++)a[i][j]/=div;
        for(int j=i+1;j<=n;j++)
        {
            div=a[j][i];
            for(int k=i;k<=n+1;k++)
            {
                a[j][k]-=div*a[i][k];
            }       
        }
    }
    ans[n]=a[n][n+1]/a[n][n];
    for(int i=n-1;i>=1;i--)
    {
        ans[i]=a[i][n+1];
        for(int j=i+1;j<=n;j++)ans[i]-=ans[j]*a[i][j];
    }
    for(int i=1;i<=n;i++)printf("%.3lf ",ans[i]);
    return 0;
}

Fourth, determine the solution

\ (A, has a unique solution: \) last processed out \ (X_i \) coefficient is not 0 and the process is not derived coefficient 0

\ (B, no solution: \) found that for the process \ (X_i \) a \ (maxn = 0 \)

\ (C, infinite solutions: \) last processed out \ (X_i \) coefficient is 0 and the right side of the equation is also 0

Title:
P3389 [template] Gaussian elimination
P2455 [SDOI2006] Linear Equations

V. Summary

In fact, you can find Gaussian elimination is well understood, the main difficulty is in front of the process of how to derive linear equations, derived directly after the sets of templates like

Guess you like

Origin www.cnblogs.com/ShuraEye/p/11354533.html