QGXZ [Linear Algebra xOI / ACM] decomposition of the coefficient matrix

Unimportant Q & A

Q: how do you think this fancy algorithm ah?
And honor a few days ago to learn Linear Algebra: A Magolor Gangster algorithm discussed $ LU $ decomposition when multiple solutions for the time complexity of the problem, so yy out of this strange (?).

Q: Why is it called $ QGXZ $ decompose it? You are not loaded to force ah?
A: The name is Magolor big brother played, I can only obey unconditionally slightly ~ ~ any similarity is by no means academic misconduct

Q: Magolor Gangster too strong friends ~
A: Congratulations to our consensus -

Outline

$ QGXZ $ decomposition, is an algorithm to solve linear equations and more general solution for the problem. Specifically:

Gives $ n \ times m $ $ A $ matrix of coefficients, each seeking $ Ax = b_1, Ax = b_2 , ..., Ax = b_q $ of the general solution , which is $ B_i $ $ n \ times 1 $ column vector. The following assumptions $ n, m, q $ the same order.

If $ b_i $ mandatory online, then the time complexity of the algorithm is simple $ O (n ^ 4) $. If $ QGXZ $ matrix decomposition, the complexity is reduced to $ O (n ^ 3) $.

Pre-skills

$ QGXZ $ $ LU $ decomposition decomposition is essentially an extension, so the first to introduce $ LU $ decomposition.

$ The LU $ decomposition for a $ n \ times m $ matrix, which is decomposed into a $ n \ times n $ triangular matrix $ under L $ and a $ n \ times m $ upper trapezoidal matrix $ the U-$ of the results of the product, i.e., $ a = L \ times U $.

Seeking: for matrix $ A $, from top to bottom matrix row transformation process (just to consider a third line transformation: multiplying a row on a row number is added to zero). We know that, once the line change becomes $ A $ $ B $ process can be used $ A = K \ times B $ form described, where $ K $ is a transformation matrix. Since under the premise consumption spend $ K $ is a lower triangular matrix, the product of the lower triangular matrix is ​​a lower triangular matrix, the product of each transformation matrix is ​​what we are asking the triangular matrix L $ $, $ and a final result is an upper trapezoidal matrix of $ $ U $.

E.g:

$$
\begin{pmatrix}
1&1&0&0\\\
1&0&1&1\\\
2&1&0&0\end{pmatrix}=
\begin{pmatrix}
1&0&0\\\
1&1&0\\\
2&0&1
\end{pmatrix}
\begin{pmatrix}
1&1&0&0\\\
0&-1&1&1\\\
0&-1&0&0
\end{pmatrix}=
\begin{pmatrix}
1&0&0\\\
1&1&0\\\
2&-1&1
\end{pmatrix}
\begin{pmatrix}
1&1&0&0\\\
0&-1&1&1\\\
0&0&-1&-1
\end{pmatrix}
$$

$ LU $ decomposition what's the use?

If there are equations $ Ax = b $, it is equivalent to $ LUx = b $. We can put $ Ux $ as a whole $ y $, the first solution of the equation $ Ly = b $, then de-$ Ux = y $. Obviously these two equations are relatively "easy" solved.

limitation

$ LU $ decomposition there are two limitations:

  1. Since the transformation process line must be used on top of the row below the row consumption, for some matrix therefore may not be directly $ $ the LU decomposition;

  2. Even if the LU $ $ matrix can be decomposed, in the solution of the equation when Y $ $ Ux =, if multiple solutions of equations, consisting of the main element required to represent the element. In the process of substituting solved, there are $ O (n) $ equations, each equation to be substituted into $ O (n) $ main element, each main element use $ O (n) $ represents a free element, Thus even if one knows the LU coefficient matrix $ $ exploded form, a complexity is substituted into $ O (n ^ 3) $, and no difference and violence.

Here we introduce the $ GXZ $ $ QGXZ $ decomposition and decomposition to solve these two limitations.

$ GXZ $ decomposition

$ GXZ $ decomposition for a $ n \ times m $ matrix, which is decomposed into a $ n \ times n $ lower triangular matrix $ G $, a $ n \ times n $ upper triangular matrix $ X-$ and a $ n \ times m $ matrix in reduced row echelon (other pivot position where each column is $ 0 $ stepped row matrix) results the Z $ $ product, i.e., $ a = G \ times X \ times Z $.

This evaluation method is very simple: the LU decomposition using a row translating spur cancellation obtained after transformation matrix $ L $ and row echelon matrix $ the U-$, we then anti extinction wave, with a corresponding position of the primary element rows above row elimination to $ 0 $, and using the recording method of the transformation matrix with the LU $ $ decomposition. As each arc above the following are used, so the transform matrix is ​​an upper triangular matrix bound ($ and $ the LU decomposition similar).

There are $ A = GXZ $ in substitution after wave of variable names.

E.g:

$$
\begin{pmatrix}
1&1&0&0\\\
1&0&1&1\\\
2&1&0&0
\end{pmatrix}=
\begin{pmatrix}
1&0&0\\\
1&1&0\\\
2&-1&1
\end{pmatrix}
\begin{pmatrix}
1&0&0\\\
0&1&-1\\\
0&0&1
\end{pmatrix}
\begin{pmatrix}
1&1&0&0\\\
0&-1&0&0\\\
0&0&-1&-1
\end{pmatrix}
$$

In this case, only solutions of the equation $ Gd = b $, $ Xe = d $ and $ Zx = e $ can. The first two equations clearly $ O (n ^ 2) $, and the third equation only a front element and not substituting process is $ O (n ^ 2) $ a.

So we get a $ O (n ^ 3) $ pretreatment, $ O (n ^ 2) $ single interrogation algorithm.

$ QGXZ $ decomposition

$ GXZ $ decomposing the second point of limitations, the limitations of the first point by $ QGXZ $ decomposition to solve.

$ QGXZ $ exploded soon $ n \ times m $ permutation matrix exploded form into the product matrix and $ Q $ $ $ GXZ decomposed.

Specific methods: When the first step (LU decomposition) $ $ GXZ decomposition, assumed that the current consumption has become a $ A = L_0U_0 $ form conversion was found to be exchanged for a further two rows $ $ U_0 when elimination, i.e. $ U_0 = T_0U_1 $, where $ T_0 $ is a permutation matrix. What we need now is to become a $ L_0T_0U_1 $ $ T_1L_1U_1 $, that is $ L_0T_0 $ becomes $ T_1L_1 $.

We know, $ L_0T_0 $ equivalent of two exchange a $ L_0 $, and $ T_1L_1 $ equivalent exchange $ L_1 $ of a two lines. Since our elimination process is carried out from top to bottom, so $ $ L_0 to be exchanged must be only two main diagonal is $ 1 $, $ 0 $ rest position.

Thus, we only need to manually exchange the main L_0 $ $ foregoing two corresponding diagonal lines as part of $ L_1 $, and T_0 $ $ get directly in front, as intact to $ $ T_l.

For example: We want to exchange $ L_0 $ $ 2 $ th and $ 3 $ row, the manual exchange $ L_0 $ front $ $ 2 $ rows and $ 3 $ line \ text {min} (2,3) -1 $ th as the number of $ L_1 $, the $ T_0 $ get $ L_0 $ $ L_1 $ in front as you can. That is:

$$
\begin{pmatrix}
1&0&0\\\
x&1&0\\\
y&0&1
\end{pmatrix}
\begin{pmatrix}
1&0&0\\\
0&0&1\\\
0&1&0
\end{pmatrix}=
\begin{pmatrix}
1&0&0\\\
0&0&1\\\
0&1&0
\end{pmatrix}
\begin{pmatrix}
1&0&0\\\
y&1&0\\\
x&0&1
\end{pmatrix}
$$

Each exchange have carried out such a process, so we put a permutation matrix and permutation matrix put together, the lower triangular matrix and the lower triangular matrix put together. Because of their special nature of the product does not change the matrix, so the final $ Q $ inevitable also permutation matrix, $ G $ must also be lower triangular matrix.

This, $ Ax = b $ solution becomes: Decomposition $ A = Q \ times G \ times X \ times Z $, then the solutions were $ Qc = b $, $ Gd = c $, $ Xe = d $, to $ Zx = e $.

Asked a single time complexity is $ O (n ^ 2) $ unchanged.

Code

#include <bits/stdc++.h>
#define N 510
#define eps 1e-6
using namespace std;
int pos[N];
double Q[N][N] , G[N][N] , X[N][N] , Z[N][N] , b[N] , c[N] , d[N] , e[N];
int main()
{
    int n , m , q , i , j , k , p = 0 , t;
    double mx;
    scanf("%d%d%d" , &n , &m , &q);
    for(i = 1 ; i <= n ; i ++ )
        for(j = 1 ; j <= m ; j ++ )
            scanf("%lf" , &Z[i][j]);
    for(i = 1 ; i <= n ; i ++ ) Q[i][i] = G[i][i] = X[i][i] = 1;
    for(i = 1 ; i <= m ; i ++ )
    {
        t = 0 , mx = eps;
        for(j = p + 1 ; j <= n ; j ++ )
            if(abs(Z[j][i]) > mx)
                t = j , mx = abs(Z[j][i]);
        if(!t) continue;
        pos[ ++ p] = i;
        for(k = i ; k <= m ; k ++ ) swap(Z[p][k] , Z[t][k]);
        for(k = 1 ; k <= n ; k ++ ) swap(Q[p][k] , Q[t][k]);
        for(k = 1 ; k < p ; k ++ ) swap(G[p][k] , G[t][k]);
        for(j = p + 1 ; j <= n ; j ++ )
        {
            G[j][p] = Z[j][i] / Z[p][i];
            for(k = i ; k <= m ; k ++ )
                Z[j][k] -= Z[p][k] * G[j][p];
        }
    }
    for(i = p ; i ; i -- )
    {
        for(j = i - 1 ; j ; j -- )
        {
            X[j][i] = Z[j][pos[i]] / Z[i][pos[i]];
            for(k = pos[i] ; k <= m ; k ++ )
                Z[j][k] -= Z[i][k] * X[j][i];
        }
    }
    while(q -- )
    {
        for(i = 1 ; i <= n ; i ++ ) scanf("%lf" , &b[i]);
        for(i = 1 ; i <= n ; i ++ )
            for(j = 1 ; j <= n ; j ++ )
                if(Q[i][j] == 1)
                    c[j] = b[i];
        for(i = 1 ; i <= n ; i ++ )
        {
            d[i] = c[i];
            for(j = 1 ; j < i ; j ++ )
                d[i] -= G[i][j] * d[j];
        }
        for(i = n ; i ; i -- )
        {
            e[i] = d[i];
            for(j = n ; j > i ; j -- )
                e[i] -= X[i][j] * e[j];
        }
        for(i = p + 1 ; i <= n ; i ++ )
            if(abs(e[i]) > eps)
                break;
        if(i <= n) puts("No solution!");
        else
        {
            for(i = 1 ; i <= p ; i ++ )
            {
                printf("x[%d]=%lf" , pos[i] , e[i] / Z[i][pos[i]]);
                for(j = pos[i] + 1 ; j <= m ; j ++ )
                    if(abs(Z[i][j]) > eps)
                        printf("%+lfx[%d]" , -Z[i][j] / Z[i][pos[i]] , j);
                puts("");
            }
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/GXZlegend/p/11735752.html