The article focuses on using an algorithm for solving a system of linear equations. We will deal with the matrix of coefficients. Gaussian Elimination does not work on singular matrices (they lead to division by zero).
Input: For N unknowns, input is an augmented
matrix of size N x (N+1). One extra
column is for Right Hand Side (RHS)
mat[N][N+1] = {{3.0, 2.0,-4.0, 3.0},
{2.0, 3.0, 3.0, 15.0},
{5.0, -3, 1.0, 14.0}
};
Output: Solution to equations is:
3.000000
1.000000
2.000000
Explanation:
Given matrix represents following equations
3.0X1 + 2.0X2 - 4.0X3 = 3.0
2.0X1 + 3.0X2 + 3.0X3 = 15.0
5.0X1 - 3.0X2 + X3 = 14.0
There is a unique solution for given equations,
solutions is, X1 = 3.0, X2 = 1.0, X3 = 2.0,
Â
Row echelon form: Matrix is said to be in r.e.f. if the following conditions hold:Â
- The first non-zero element in each row, called the leading coefficient, is 1.
- Each leading coefficient is in a column to the right of the previous row leading coefficient.
- Rows with all zeros are below rows with at least one non-zero element.
Â
Reduced row echelon form: Matrix is said to be in r.r.e.f. if the following conditions hold –Â
- All the conditions for r.e.f.
- The leading coefficient in each row is the only non-zero entry in its column.
The algorithm is majorly about performing a sequence of operations on the rows of the matrix. What we would like to keep in mind while performing these operations is that we want to convert the matrix into an upper triangular matrix in row echelon form. The operations can be:Â
- Swapping two rows
- Multiplying a row by a non-zero scalar
- Adding to one row a multiple of another
The process:Â Â
- Forward elimination: reduction to row echelon form. Using it one can tell whether there are no solutions, or unique solution, or infinitely many solutions.
- Back substitution: further reduction to reduced row echelon form.
Algorithm:Â Â
- Partial pivoting: Find the kth pivot by swapping rows, to move the entry with the largest absolute value to the pivot position. This imparts computational stability to the algorithm.
- For each row below the pivot, calculate the factor f which makes the kth entry zero, and for every element in the row subtract the fth multiple of the corresponding element in the kth row.
- Repeat above steps for each unknown. We will be left with a partial r.e.f. matrix.
Below is the implementation of the above algorithm. Â
C++
// C++ program to demonstrate working of Gaussian Elimination// method#include<bits/stdc++.h>using namespace std;Â
#define N 3Â Â Â Â Â Â Â // Number of unknownsÂ
// function to reduce matrix to r.e.f. Returns a value to // indicate whether matrix is singular or notint forwardElim(double mat[N][N+1]);Â
// function to calculate the values of the unknownsvoid backSub(double mat[N][N+1]);Â
// function to get matrix contentvoid gaussianElimination(double mat[N][N+1]){Â Â Â Â /* reduction into r.e.f. */Â Â Â Â int singular_flag = forwardElim(mat);Â
    /* if matrix is singular */    if (singular_flag != -1)    {        printf("Singular Matrix.\n");Â
        /* if the RHS of equation corresponding to           zero row is 0, * system has infinitely           many solutions, else inconsistent*/        if (mat[singular_flag][N])            printf("Inconsistent System.");        else            printf("May have infinitely many "                   "solutions.");Â
        return;    }Â
    /* get solution to system and print it using       backward substitution */    backSub(mat);}Â
// function for elementary operation of swapping two rowsvoid swap_row(double mat[N][N+1], int i, int j){Â Â Â Â //printf("Swapped rows %d and %d\n", i, j);Â
    for (int k=0; k<=N; k++)    {        double temp = mat[i][k];        mat[i][k] = mat[j][k];        mat[j][k] = temp;    }}Â
// function to print matrix content at any stagevoid print(double mat[N][N+1]){Â Â Â Â for (int i=0; i<N; i++, printf("\n"))Â Â Â Â Â Â Â Â for (int j=0; j<=N; j++)Â Â Â Â Â Â Â Â Â Â Â Â printf("%lf ", mat[i][j]);Â
    printf("\n");}Â
// function to reduce matrix to r.e.f.int forwardElim(double mat[N][N+1]){    for (int k=0; k<N; k++)    {        // Initialize maximum value and index for pivot        int i_max = k;        int v_max = mat[i_max][k];Â
        /* find greater amplitude for pivot if any */        for (int i = k+1; i < N; i++)            if (abs(mat[i][k]) > v_max)                v_max = mat[i][k], i_max = i;Â
        /* if a principal diagonal element is zero,         * it denotes that matrix is singular, and         * will lead to a division-by-zero later. */        if (!mat[k][i_max])            return k; // Matrix is singularÂ
        /* Swap the greatest value row with current row */        if (i_max != k)            swap_row(mat, k, i_max);Â
Â
        for (int i=k+1; i<N; i++)        {            /* factor f to set current row kth element to 0,             * and subsequently remaining kth column to 0 */            double f = mat[i][k]/mat[k][k];Â
            /* subtract fth multiple of corresponding kth               row element*/            for (int j=k+1; j<=N; j++)                mat[i][j] -= mat[k][j]*f;Â
            /* filling lower triangular matrix with zeros*/            mat[i][k] = 0;        }Â
        //print(mat);       //for matrix state    }    //print(mat);           //for matrix state    return -1;}Â
// function to calculate the values of the unknownsvoid backSub(double mat[N][N+1]){Â Â Â Â double x[N];Â // An array to store solutionÂ
    /* Start calculating from last equation up to the       first */    for (int i = N-1; i >= 0; i--)    {        /* start with the RHS of the equation */        x[i] = mat[i][N];Â
        /* Initialize j to i+1 since matrix is upper           triangular*/        for (int j=i+1; j<N; j++)        {            /* subtract all the lhs values             * except the coefficient of the variable             * whose value is being calculated */            x[i] -= mat[i][j]*x[j];        }Â
        /* divide the RHS by the coefficient of the           unknown being calculated */        x[i] = x[i]/mat[i][i];    }Â
    printf("\nSolution for the system:\n");    for (int i=0; i<N; i++)        printf("%lf\n", x[i]);}Â
// Driver programint main(){    /* input matrix */    double mat[N][N+1] = {{3.0, 2.0,-4.0, 3.0},                          {2.0, 3.0, 3.0, 15.0},                          {5.0, -3, 1.0, 14.0}                         };Â
    gaussianElimination(mat);Â
    return 0;} |
Java
// Java program to demonstrate working of Gaussian Elimination// methodimport java.io.*;class GFG{Â
  public static int N = 3; // Number of unknownsÂ
  // function to get matrix content  static void gaussianElimination(double mat[][])  {Â
    /* reduction into r.e.f. */    int singular_flag = forwardElim(mat);Â
    /* if matrix is singular */    if (singular_flag != -1)     {      System.out.println("Singular Matrix.");Â
      /* if the RHS of equation corresponding to               zero row is 0, * system has infinitely               many solutions, else inconsistent*/      if (mat[singular_flag][N] != 0)        System.out.print("Inconsistent System.");      else        System.out.print(        "May have infinitely many solutions.");Â
      return;    }Â
    /* get solution to system and print it using           backward substitution */    backSub(mat);  }Â
  // function for elementary operation of swapping two  // rows  static void swap_row(double mat[][], int i, int j)  {    // printf("Swapped rows %d and %d\n", i, j);Â
    for (int k = 0; k <= N; k++)    {      double temp = mat[i][k];      mat[i][k] = mat[j][k];      mat[j][k] = temp;    }  }Â
  // function to print matrix content at any stage  static void print(double mat[][])  {    for (int i = 0; i < N; i++, System.out.println())      for (int j = 0; j <= N; j++)        System.out.print(mat[i][j]);    System.out.println();  }Â
  // function to reduce matrix to r.e.f.  static int forwardElim(double mat[][])  {    for (int k = 0; k < N; k++)     {Â
      // Initialize maximum value and index for pivot      int i_max = k;      int v_max = (int)mat[i_max][k];Â
      /* find greater amplitude for pivot if any */      for (int i = k + 1; i < N; i++)        if (Math.abs(mat[i][k]) > v_max)         {          v_max = (int)mat[i][k];          i_max = i;        }Â
      /* if a principal diagonal element is zero,             * it denotes that matrix is singular, and             * will lead to a division-by-zero later. */      if (mat[k][i_max] == 0)        return k; // Matrix is singularÂ
      /* Swap the greatest value row with current row             */      if (i_max != k)        swap_row(mat, k, i_max);Â
      for (int i = k + 1; i < N; i++)      {Â
        /* factor f to set current row kth element                 * to 0, and subsequently remaining kth                 * column to 0 */        double f = mat[i][k] / mat[k][k];Â
        /* subtract fth multiple of corresponding                   kth row element*/        for (int j = k + 1; j <= N; j++)          mat[i][j] -= mat[k][j] * f;Â
        /* filling lower triangular matrix with                 * zeros*/        mat[i][k] = 0;      }Â
      // print(mat);       //for matrix state    }Â
    // print(mat);           //for matrix state    return -1;  }Â
  // function to calculate the values of the unknowns  static void backSub(double mat[][])  {    double x[]      = new double[N]; // An array to store solutionÂ
    /* Start calculating from last equation up to the           first */    for (int i = N - 1; i >= 0; i--)    {Â
      /* start with the RHS of the equation */      x[i] = mat[i][N];Â
      /* Initialize j to i+1 since matrix is upper               triangular*/      for (int j = i + 1; j < N; j++)       {Â
        /* subtract all the lhs values                 * except the coefficient of the variable                 * whose value is being calculated */        x[i] -= mat[i][j] * x[j];      }Â
      /* divide the RHS by the coefficient of the               unknown being calculated */      x[i] = x[i] / mat[i][i];    }Â
    System.out.println();    System.out.println("Solution for the system:");    for (int i = 0; i < N; i++)    {      System.out.format("%.6f", x[i]);      System.out.println();    }  }Â
  // Driver program  public static void main(String[] args)  {Â
    /* input matrix */    double mat[][] = { { 3.0, 2.0, -4.0, 3.0 },                      { 2.0, 3.0, 3.0, 15.0 },                      { 5.0, -3, 1.0, 14.0 } };Â
    gaussianElimination(mat);  }}Â
// This code is contributed by Dharanendra L V. |
Python3
# Python3 program to demonstrate working of# Gaussian Elimination methodN = 3Â
# function to get matrix contentdef gaussianElimination(mat):Â
    # reduction into r.e.f.    singular_flag = forwardElim(mat)Â
    # if matrix is singular    if (singular_flag != -1):Â
        print("Singular Matrix.")Â
        # if the RHS of equation corresponding to        #  zero row is 0, * system has infinitely        #  many solutions, else inconsistent*/        if (mat[singular_flag][N]):            print("Inconsistent System.")        else:            print("May have infinitely many solutions.")Â
        returnÂ
    # get solution to system and print it using    #  backward substitution    backSub(mat)Â
# function for elementary operation of swapping two rowsdef swap_row(mat, i, j):Â
    for k in range(N + 1):Â
        temp = mat[i][k]        mat[i][k] = mat[j][k]        mat[j][k] = tempÂ
# function to reduce matrix to r.e.f.def forwardElim(mat):    for k in range(N):               # Initialize maximum value and index for pivot        i_max = k        v_max = mat[i_max][k]Â
        # find greater amplitude for pivot if any        for i in range(k + 1, N):            if (abs(mat[i][k]) > v_max):                v_max = mat[i][k]                i_max = iÂ
        # if a principal diagonal element is zero,        # it denotes that matrix is singular, and        # will lead to a division-by-zero later.        if not mat[k][i_max]:            return k   # Matrix is singularÂ
        # Swap the greatest value row with current row        if (i_max != k):            swap_row(mat, k, i_max)Â
        for i in range(k + 1, N):Â
            # factor f to set current row kth element to 0,            # and subsequently remaining kth column to 0 */            f = mat[i][k]/mat[k][k]Â
            # subtract fth multiple of corresponding kth            # row element*/            for j in range(k + 1, N + 1):                mat[i][j] -= mat[k][j]*fÂ
            # filling lower triangular matrix with zeros*/            mat[i][k] = 0Â
        # print(mat);       //for matrix stateÂ
    # print(mat);           //for matrix state    return -1Â
# function to calculate the values of the unknownsdef backSub(mat):Â
    x = [None for _ in range(N)]   # An array to store solutionÂ
    # Start calculating from last equation up to the    # first */    for i in range(N-1, -1, -1):Â
        # start with the RHS of the equation */        x[i] = mat[i][N]Â
        # Initialize j to i+1 since matrix is upper        # triangular*/        for j in range(i + 1, N):                       # subtract all the lhs values            # except the coefficient of the variable            # whose value is being calculated */            x[i] -= mat[i][j]*x[j]Â
        # divide the RHS by the coefficient of the        # unknown being calculated        x[i] = (x[i]/mat[i][i])Â
    print("\nSolution for the system:")    for i in range(N):        print("{:.8f}".format(x[i]))Â
# Driver programÂ
# input matrixmat = [[3.0, 2.0, -4.0, 3.0], [2.0, 3.0, 3.0, 15.0], [5.0, -3, 1.0, 14.0]]gaussianElimination(mat)Â
# This code is contributed by phasing17 |
C#
// C# program to demonstrate working // of Gaussian Elimination methodusing System;Â
class GFG{Â Â Â Â Â // Number of unknownspublic static int N = 3; Â
// Function to get matrix contentstatic void gaussianElimination(double [,]mat){Â
    /* reduction into r.e.f. */    int singular_flag = forwardElim(mat);         /* if matrix is singular */    if (singular_flag != -1)     {        Console.WriteLine("Singular Matrix.");                 /* if the RHS of equation corresponding to           zero row is 0, * system has infinitely           many solutions, else inconsistent*/        if (mat[singular_flag,N] != 0)            Console.Write("Inconsistent System.");        else            Console.Write("May have infinitely " +                           "many solutions.");                 return;    }         /* get solution to system and print it using    backward substitution */    backSub(mat);}Â
// Function for elementary operation of swapping two// rowsstatic void swap_row(double [,]mat, int i, int j){Â Â Â Â Â Â Â Â Â // printf("Swapped rows %d and %d\n", i, j);Â Â Â Â Â Â Â Â Â for(int k = 0; k <= N; k++)Â Â Â Â {Â Â Â Â Â Â Â Â double temp = mat[i, k];Â Â Â Â Â Â Â Â mat[i, k] = mat[j, k];Â Â Â Â Â Â Â Â mat[j, k] = temp;Â Â Â Â }}Â Â Â Â Â // Function to print matrix content at any stagestatic void print(double [,]mat){Â Â Â Â for(int i = 0; i < N; i++, Console.WriteLine())Â Â Â Â Â Â Â Â for(int j = 0; j <= N; j++)Â Â Â Â Â Â Â Â Â Â Â Â Console.Write(mat[i, j]);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Console.WriteLine();}Â
// Function to reduce matrix to r.e.f.static int forwardElim(double [,]mat){    for(int k = 0; k < N; k++)     {                 // Initialize maximum value and index for pivot        int i_max = k;        int v_max = (int)mat[i_max, k];                 /* find greater amplitude for pivot if any */        for(int i = k + 1; i < N; i++)        {            if (Math.Abs(mat[i, k]) > v_max)             {                v_max = (int)mat[i, k];                i_max = i;            }                     /* If a principal diagonal element is zero,            * it denotes that matrix is singular, and            * will lead to a division-by-zero later. */            if (mat[k, i_max] == 0)                return k; // Matrix is singular                         /* Swap the greatest value row with                current row            */            if (i_max != k)                swap_row(mat, k, i_max);                         for(int a = k + 1; a < N; a++)            {                             /* factor f to set current row kth element                * to 0, and subsequently remaining kth                * column to 0 */                double f = mat[i, k] / mat[k, k];                                 /* subtract fth multiple of corresponding                   kth row element*/                for(int j = k + 1; j <= N; j++)                    mat[i, j] -= mat[k, j] * f;                                 /* filling lower triangular matrix with                * zeros*/                mat[i, k] = 0;            }        }        // print(mat);       //for matrix state    }         // print(mat);           //for matrix state    return -1;}Â
// Function to calculate the values of the unknownsstatic void backSub(double [,]mat){         // An array to store solution    double []x = new double[N];          /* Start calculating from last equation up to the       first */    for(int i = N - 1; i >= 0; i--)    {             /* start with the RHS of the equation */        x[i] = mat[i,N];                 /* Initialize j to i+1 since matrix is upper        triangular*/        for(int j = i + 1; j < N; j++)         {                     /* subtract all the lhs values             * except the coefficient of the variable             * whose value is being calculated */            x[i] -= mat[i,j] * x[j];        }                 /* divide the RHS by the coefficient of the        unknown being calculated */        x[i] = x[i] / mat[i,i];    }         Console.WriteLine();    Console.WriteLine("Solution for the system:");    for(int i = 0; i < N; i++)    {        Console.Write("{0:F6}", x[i]);        Console.WriteLine();    }}Â
// Driver codepublic static void Main(String[] args){         /* input matrix */    double [,]mat = { { 3.0, 2.0, -4.0, 3.0 },                      { 2.0, 3.0, 3.0, 15.0 },                      { 5.0, -3, 1.0, 14.0 } };         gaussianElimination(mat);}}Â
// This code is contributed by shikhasingrajput |
Javascript
// JavaScript program to demonstrate working of Gaussian Elimination// methodÂ
let N = 3;Â
// function to get matrix contentfunction gaussianElimination(mat){Â Â Â Â /* reduction into r.e.f. */Â Â Â Â let singular_flag = forwardElim(mat);Â
    /* if matrix is singular */    if (singular_flag != -1)    {        console.log("Singular Matrix.");Â
        /* if the RHS of equation corresponding to           zero row is 0, * system has infinitely           many solutions, else inconsistent*/        if (mat[singular_flag][N])            console.log("Inconsistent System.");        else            console.log("May have infinitely many solutions.");Â
        return;    }Â
    /* get solution to system and print it using       backward substitution */    backSub(mat);}Â
// function for elementary operation of swapping two rowsfunction swap_row(mat, i, j){Â Â Â Â //printf("Swapped rows %d and %d\n", i, j);Â
    for (var k=0; k<=N; k++)    {        let temp = mat[i][k];        mat[i][k] = mat[j][k];        mat[j][k] = temp;    }}Â
// function to print matrix content at any stagefunction print(mat){Â Â Â Â for (var i=0; i<N; i++, console.log(""))Â Â Â Â Â Â Â Â for (var j=0; j<=N; j++)Â Â Â Â Â Â Â Â Â Â Â Â process.stdout.write("" + mat[i][j]);Â Â Â Â Â Â Â Â Â console.log("");}Â
// function to reduce matrix to r.e.f.function forwardElim(mat){    for (var k=0; k<N; k++)    {        // Initialize maximum value and index for pivot        var i_max = k;        var v_max = mat[i_max][k];Â
        /* find greater amplitude for pivot if any */        for (var i = k+1; i < N; i++)            if (Math.abs(mat[i][k]) > v_max)                v_max = mat[i][k], i_max = i;Â
        /* if a principal diagonal element is zero,         * it denotes that matrix is singular, and         * will lead to a division-by-zero later. */        if (!mat[k][i_max])            return k; // Matrix is singularÂ
        /* Swap the greatest value row with current row */        if (i_max != k)            swap_row(mat, k, i_max);Â
Â
        for (var i=k+1; i<N; i++)        {            /* factor f to set current row kth element to 0,             * and subsequently remaining kth column to 0 */            let f = mat[i][k]/mat[k][k];Â
            /* subtract fth multiple of corresponding kth               row element*/            for (var j=k+1; j<=N; j++)                mat[i][j] -= mat[k][j]*f;Â
            /* filling lower triangular matrix with zeros*/            mat[i][k] = 0;        }Â
        //print(mat);       //for matrix state    }    //print(mat);           //for matrix state    return -1;}Â
// function to calculate the values of the unknownsfunction backSub(mat){Â Â Â Â let x = new Array(N);Â // An array to store solutionÂ
    /* Start calculating from last equation up to the       first */    for (var i = N-1; i >= 0; i--)    {        /* start with the RHS of the equation */        x[i] = mat[i][N];Â
        /* Initialize j to i+1 since matrix is upper           triangular*/        for (var j=i+1; j<N; j++)        {            /* subtract all the lhs values             * except the coefficient of the variable             * whose value is being calculated */            x[i] -= mat[i][j]*x[j];        }Â
        /* divide the RHS by the coefficient of the           unknown being calculated */        x[i] = Math.round(x[i]/mat[i][i]);    }Â
    console.log("\nSolution for the system:");    for (var i=0; i<N; i++)        console.log(x[i].toFixed(8));}Â
// Driver programÂ
/* input matrix */let mat =  [[3.0, 2.0,-4.0, 3.0],             [2.0, 3.0, 3.0, 15.0],             [5.0, -3, 1.0, 14.0]];Â
gaussianElimination(mat);Â
Â
// This code is contributed by phasing17 |
PHP
<?php// PHP program to demonstrate working // of Gaussian Elimination methodÂ
$N = 3; // Number of unknownsÂ
Â
// function to get matrix contentfunction gaussianElimination($mat){Â Â Â Â global $N;Â Â Â Â Â Â Â Â Â /* reduction into r.e.f. */Â Â Â Â $singular_flag = forwardElim($mat);Â
    /* if matrix is singular */    if ($singular_flag != -1)    {        print("Singular Matrix.\n");Â
        /* if the RHS of equation corresponding to        zero row is 0, * system has infinitely        many solutions, else inconsistent*/        if ($mat[$singular_flag][$N])            print("Inconsistent System.");        else            print("May have infinitely many solutions.");Â
        return;    }Â
    /* get solution to system and print it using    backward substitution */    backSub($mat);}Â
// function for elementary operation// of swapping two rowsfunction swap_row(&$mat, $i, $j){Â Â Â Â global $N;Â Â Â Â //printf("Swapped rows %d and %d\n", i, j);Â
    for ($k = 0; $k <= $N; $k++)    {        $temp = $mat[$i][$k];        $mat[$i][$k] = $mat[$j][$k];        $mat[$j][$k] = $temp;    }}Â
// function to print matrix content at any stagefunction print1($mat){Â Â Â Â global $N;Â Â Â Â for ($i=0; $i<$N; $i++, print("\n"))Â Â Â Â Â Â Â Â for ($j=0; $j<=$N; $j++)Â Â Â Â Â Â Â Â Â Â Â Â print($mat[$i][$j]);Â
    print("\n");}Â
// function to reduce matrix to r.e.f.function forwardElim(&$mat){    global $N;    for ($k=0; $k<$N; $k++)    {        // Initialize maximum value and index for pivot        $i_max = $k;        $v_max = $mat[$i_max][$k];Â
        /* find greater amplitude for pivot if any */        for ($i = $k+1; $i < $N; $i++)            if (abs($mat[$i][$k]) > $v_max)                {                    $v_max = $mat[$i][$k];                    $i_max = $i;                }Â
        /* if a principal diagonal element is zero,        * it denotes that matrix is singular, and        * will lead to a division-by-zero later. */        if (!$mat[$k][$i_max])            return $k; // Matrix is singularÂ
        /* Swap the greatest value row with current row */        if ($i_max != $k)            swap_row($mat, $k, $i_max);Â
Â
        for ($i = $k + 1; $i < $N; $i++)        {            /* factor f to set current row kth element to 0,            * and subsequently remaining kth column to 0 */            $f = $mat[$i][$k]/$mat[$k][$k];Â
            /* subtract fth multiple of corresponding kth            row element*/            for ($j = $k + 1; $j <= $N; $j++)                $mat[$i][$j] -= $mat[$k][$j] * $f;Â
            /* filling lower triangular matrix with zeros*/            $mat[$i][$k] = 0;        }Â
        //print(mat); //for matrix state    }    //print(mat);    //for matrix state    return -1;}Â
// function to calculate the values of the unknownsfunction backSub(&$mat){Â Â Â Â global $N;Â Â Â Â $x = array_fill(0, $N, 0); // An array to store solutionÂ
    /* Start calculating from last equation up to the    first */    for ($i = $N - 1; $i >= 0; $i--)    {        /* start with the RHS of the equation */        $x[$i] = $mat[$i][$N];Â
        /* Initialize j to i+1 since matrix is upper        triangular*/        for ($j = $i + 1; $j < $N; $j++)        {            /* subtract all the lhs values            * except the coefficient of the variable            * whose value is being calculated */            $x[$i] -= $mat[$i][$j] * $x[$j];        }Â
        /* divide the RHS by the coefficient of the        unknown being calculated */        $x[$i] = $x[$i] / $mat[$i][$i];    }Â
    print("\nSolution for the system:\n");    for ($i = 0; $i < $N; $i++)        print(number_format(strval($x[$i]), 6)."\n");}Â
// Driver programÂ
    /* input matrix */    $mat = array(array(3.0, 2.0,-4.0, 3.0),                        array(2.0, 3.0, 3.0, 15.0),                        array(5.0, -3, 1.0, 14.0));Â
    gaussianElimination($mat);Â
// This code is contributed by mits?> |
Solution for the system: 3.000000 1.000000 2.000000
Illustration:Â
Â
Time Complexity: Since for each pivot we traverse the part to its right for each row below it, O(n)*(O(n)*O(n)) = O(n3).
We can also apply Gaussian Elimination for calculating:
- Rank of a matrix
- Determinant of a matrix
- Inverse of an invertible square matrix
Approach:
Another approach for implementing Gaussian Elimination is using Partial Pivoting. In partial pivoting, at each iteration, the algorithm searches for the row with the largest pivot element (absolute value) and swaps that row with the current row. This ensures that the largest absolute value is used as the pivot element, reducing the rounding errors that can occur when dividing by a small pivot element. The steps involved in Partial Pivoting Gaussian Elimination are:
- Start with the given augmented matrix.
- Find the row with the largest absolute value in the first column and swap that row with the first row.
- Divide the first row by the pivot element to make it equal to 1.
- Use the first row to eliminate the first column in all other rows by subtracting the appropriate multiple of the first row from each row.
- Repeat steps 2-4 for the remaining columns, using the row with the largest absolute value as the pivot row for each column.
- Back-substitute to obtain the values of the unknowns.
implementation of above approach:
C++
#include <iostream>#include <cmath>using namespace std;Â
const int MAXN = 100;Â
void partial_pivot(double A[MAXN][MAXN+1], int n) {Â Â Â Â for (int i = 0; i < n; i++) {Â Â Â Â Â Â Â Â int pivot_row = i;Â Â Â Â Â Â Â Â for (int j = i+1; j < n; j++) {Â Â Â Â Â Â Â Â Â Â Â Â if (abs(A[j][i]) > abs(A[pivot_row][i])) {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pivot_row = j;Â Â Â Â Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â if (pivot_row != i) {Â Â Â Â Â Â Â Â Â Â Â Â for (int j = i; j <= n; j++) {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â swap(A[i][j], A[pivot_row][j]);Â Â Â Â Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â for (int j = i+1; j < n; j++) {Â Â Â Â Â Â Â Â Â Â Â Â double factor = A[j][i] / A[i][i];Â Â Â Â Â Â Â Â Â Â Â Â for (int k = i; k <= n; k++) {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â A[j][k] -= factor * A[i][k];Â Â Â Â Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â }Â Â Â Â }}Â
void back_substitute(double A[MAXN][MAXN+1], int n, double x[MAXN]) {Â Â Â Â for (int i = n-1; i >= 0; i--) {Â Â Â Â Â Â Â Â double sum = 0;Â Â Â Â Â Â Â Â for (int j = i+1; j < n; j++) {Â Â Â Â Â Â Â Â Â Â Â Â sum += A[i][j] * x[j];Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â x[i] = (A[i][n] - sum) / A[i][i];Â Â Â Â }}Â
int main() {    int n = 3;    double A[MAXN][MAXN+1] = {{3.0, 2.0,-4.0, 3.0},                              {2.0, 3.0, 3.0, 15.0},                              {5.0, -3, 1.0, 14.0}                             };    double x[MAXN];         partial_pivot(A, n);    back_substitute(A, n, x);         cout << "Solution for the system:\n";    for (int i = 0; i < n; i++) {               cout << x[i] << endl;    }         return 0;} |
Java
public class Main {         private static final int MAXN = 100;         public static void partialPivot(double[][] A, int n) {        for (int i = 0; i < n; i++) {            int pivotRow = i;            for (int j = i+1; j < n; j++) {                if (Math.abs(A[j][i]) > Math.abs(A[pivotRow][i])) {                    pivotRow = j;                }            }            if (pivotRow != i) {                for (int j = i; j <= n; j++) {                    double temp = A[i][j];                    A[i][j] = A[pivotRow][j];                    A[pivotRow][j] = temp;                }            }            for (int j = i+1; j < n; j++) {                double factor = A[j][i] / A[i][i];                for (int k = i; k <= n; k++) {                    A[j][k] -= factor * A[i][k];                }            }        }    }         public static void backSubstitute(double[][] A, int n, double[] x) {        for (int i = n-1; i >= 0; i--) {            double sum = 0;            for (int j = i+1; j < n; j++) {                sum += A[i][j] * x[j];            }            x[i] = (A[i][n] - sum) / A[i][i];        }    }         public static void main(String[] args) {        int n = 3;        double[][] A = {{3.0, 2.0,-4.0, 3.0},                        {2.0, 3.0, 3.0, 15.0},                        {5.0, -3, 1.0, 14.0}                       };        double[] x = new double[MAXN];                 partialPivot(A, n);        backSubstitute(A, n, x);                 System.out.println("Solution for the system:");        for (int i = 0; i < n; i++) {            System.out.println(x[i]);        }    }} |
Python3
import numpy as npMAXN = 100Â
# Function to perform partial pivot for Gaussian eliminationdef partial_pivot(A, n):    # Iterate through each row in the matrix    for i in range(n):        pivot_row = i        # Find the row with the maximum absolute value in the current column        for j in range(i + 1, n):            if abs(A[j][i]) > abs(A[pivot_row][i]):                pivot_row = j        # Swap the current row with the row having the maximum absolute value        if pivot_row != i:            A[[i, pivot_row]] = A[[pivot_row, i]]        # Perform Gaussian elimination on the matrix        for j in range(i + 1, n):            factor = A[j][i] / A[i][i]            A[j] -= factor * A[i]Â
# Function to perform back substitution to solve the system of equationsdef back_substitute(A, n):    x = np.zeros(n)    # Iterate through each row in reverse order    for i in range(n - 1, -1, -1):        sum_val = sum(A[i][j] * x[j] for j in range(i + 1, n))        # Solve for x[i] using the previously calculated values of x        x[i] = (A[i][n] - sum_val) / A[i][i]    return x# Driver codeif __name__ == "__main__":    n = 3    # Coefficient matrix augmented with the constant terms    A = np.array([[3.0, 2.0, -4.0, 3.0],                  [2.0, 3.0, 3.0, 15.0],                  [5.0, -3, 1.0, 14.0]])         # Perform Gaussian elimination with partial pivot    partial_pivot(A, n)    x = back_substitute(A, n)         print("Solution for the system:")    for i in range(n):        print(x[i])#This code is contributed by Vikram_Shirsat |
C#
using System;Â
class Program {Â Â const int MAXN = 100;Â
  // Function to find the partial pivot  static void partial_pivot(double[, ] A, int n)  {    for (int i = 0; i < n; i++) {      int pivot_row = i;      for (int j = i + 1; j < n; j++) {        if (Math.Abs(A[j, i])            > Math.Abs(A[pivot_row, i])) {          pivot_row = j;        }      }      if (pivot_row != i) {        for (int j = i; j <= n; j++) {          double temp = A[i, j];          A[i, j] = A[pivot_row, j];          A[pivot_row, j] = temp;        }      }      for (int j = i + 1; j < n; j++) {        double factor = A[j, i] / A[i, i];        for (int k = i; k <= n; k++) {          A[j, k] -= factor * A[i, k];        }      }    }  }Â
  // Function to perform the back  // substitution  static void back_substitute(double[, ] A, int n,                              double[] x)  {    for (int i = n - 1; i >= 0; i--) {      double sum = 0;      for (int j = i + 1; j < n; j++) {        sum += A[i, j] * x[j];      }      x[i] = (A[i, n] - sum) / A[i, i];    }  }Â
  // Driver Code  static void Main(string[] args)  {    int n = 3;    double[, ] A      = new double[, ] { { 3.0, 2.0, -4.0, 3.0 },                        { 2.0, 3.0, 3.0, 15.0 },                        { 5.0, -3, 1.0, 14.0 } };    double[] x = new double[n];Â
    partial_pivot(A, n);    back_substitute(A, n, x);Â
    Console.WriteLine("Solution for the system:");    for (int i = 0; i < n; i++) {      Console.WriteLine(Math.Round(x[i]));    }  }} |
Javascript
// This program solves a system of linear equations using Gaussian Elimination with partial pivotingconst MAXN = 100;Â
// Function to perform partial pivoting on the matrix Afunction partial_pivot(A, n) {    for (let i = 0; i < n; i++) {        let pivot_row = i;        // Find the row with the largest absolute value in the ith column        for (let j = i + 1; j < n; j++) {            if (Math.abs(A[j][i]) > Math.abs(A[pivot_row][i])) {                pivot_row = j;            }        }        // Swap rows if necessary        if (pivot_row != i) {            for (let j = i; j <= n; j++) {                [A[i][j], A[pivot_row][j]] = [A[pivot_row][j], A[i][j]];            }        }        // Perform Gaussian Elimination on the matrix        for (let j = i + 1; j < n; j++) {            let factor = A[j][i] / A[i][i];            for (let k = i; k <= n; k++) {                A[j][k] -= factor * A[i][k];            }        }    }}Â
// Function to perform back substitution on the matrix A to find the solution vector xfunction back_substitute(A, n, x) {Â Â Â Â for (let i = n - 1; i >= 0; i--) {Â Â Â Â Â Â Â Â let sum = 0;Â Â Â Â Â Â Â Â for (let j = i + 1; j < n; j++) {Â Â Â Â Â Â Â Â Â Â Â Â sum += A[i][j] * x[j];Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â x[i] = (A[i][n] - sum) / A[i][i];Â Â Â Â }}Â
// Driver codelet n = 3;let A = [    [3.0, 2.0, -4.0, 3.0],    [2.0, 3.0, 3.0, 15.0],    [5.0, -3, 1.0, 14.0]];let x = Array(n);Â
partial_pivot(A, n);back_substitute(A, n, x);Â
console.log("Solution for the system:");for (let i = 0; i < n; i++) {Â Â Â Â console.log(Math.round(x[i]));} |
Solution for the system: 3 1 2
complexity
The time complexity of Partial Pivoting Gaussian Elimination is O(N^3) for an N x N matrix.
This article is contributed by Yash Varyani. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

