# c++ – Forward and backward Gaussian motion in one cycle

## Question:

The question is more theoretical. How can one combine the forward and backward Gaussian moves when solving SLAE into one procedure, that is, one cycle, one "pass"?

Again, I don't need the whole algorithm. All I need is an idea in words with a very brief explanation of it.

Perhaps try to organize some kind of cycle from n-1 to 0 (well, if the SLAE is nxn), where the matrix coefficients will magically vanish and the solution vector will be considered?

## If suddenly someone may need a piece of code that implements these two moves, then it's here:

``````/// Прямой ход
bool gausss( tdouble ** mtx, tdouble * vect )
{
tdouble c;
for ( int j = 0; j < n; j++ ) {
tdouble temp = abs( mtx[j][j] );
int mem = j; максимум
for ( int i = j + 1; i < n; i++ ) { // от j+1
if ( temp < abs( mtx[i][j] ) ) {
temp = abs( mtx[i][j] );
mem = i;
}
}

if ( temp < eps ) return false;

changing( mtx, vect, mem, j );

for ( int i = j + 1; i < n; i++ ) {
c = mtx[i][j] / mtx[j][j];
for ( int k = j; k < n; k++ ) {
mtx[i][k] -= c * mtx[j][k];
}
vect[i] -= c * vect[j];
}
}
return true;
}

/// Обратный ход
void gausss_reverse( tdouble ** mtx, tdouble * vect, tdouble * sol )
{
for ( int i = n - 1; i >= 0; i-- ) {
tdouble temp = 0;
for ( int j = i + 1; j < n; j++ ) {
temp += mtx[i][j] * sol[j];
}
sol[i] = (vect[i] - temp) / mtx[i][i];
}
}
``````

Well look.

What is usually the Gauss method? You get zeros under the diagonal on a straight move. And on the reverse – above the diagonal. This can be easily combined.

In the combined algorithm, you first perform the direct method step, which results in zeros under the diagonal. Now you can "kill" non-zero coefficients above it with the current line. Thus, your matrix will look like this (example for the third step):

``````a11  0   0  a14
0  a22  0  a24
0   0  a33 a34
0   0   0  a44
0   0   0  a54
..............
``````

At each step, you add one column to the diagonal view of the matrix.

In principle, you can also divide by the diagonal element at each step to get one there. Then at the end you have a column of free members will become a decision column.

Scroll to Top