/// <summary>
 /// The function returns the reduced echelon form of a given matrix
 /// </summary>
 public static MATRIX ReducedEchelonForm(MATRIX matrix)
 {
     try
     {
         MATRIX ReducedEchelonMatrix = matrix.Duplicate();
         for (int i = 0; i < matrix.Rows; i++)
         {
             if (ReducedEchelonMatrix[i, i] == 0)                        // if diagonal entry is zero,
             {
                 for (int j = i + 1; j < ReducedEchelonMatrix.Rows; j++)
                 {
                     if (ReducedEchelonMatrix[j, i] != 0)                                 //check if some below entry is non-zero
                     {
                         ReducedEchelonMatrix.InterchangeRow(i, j);                       // then interchange the two rows
                     }
                 }
             }
             if (ReducedEchelonMatrix[i, i] == 0)        // if not found any non-zero diagonal entry
             {
                 continue;                               // increment i;
             }
             if (ReducedEchelonMatrix[i, i] != 1)        // if diagonal entry is not 1 ,
             {
                 for (int j = i + 1; j < ReducedEchelonMatrix.Rows; j++)
                 {
                     if (ReducedEchelonMatrix[j, i] == 1)                                 //check if some below entry is 1
                     {
                         ReducedEchelonMatrix.InterchangeRow(i, j);                       // then interchange the two rows
                     }
                 }
             }
             ReducedEchelonMatrix.MultiplyRow(i, Fraction.Inverse(ReducedEchelonMatrix[i, i]));
             for (int j = i + 1; j < ReducedEchelonMatrix.Rows; j++)
             {
                 ReducedEchelonMatrix.AddRow(j, i, -ReducedEchelonMatrix[j, i]);
             }
             for (int j = i - 1; j >= 0; j--)
             {
                 ReducedEchelonMatrix.AddRow(j, i, -ReducedEchelonMatrix[j, i]);
             }
         }
         return(ReducedEchelonMatrix);
     }
     catch (Exception)
     {
         throw new MatrixException("Matrix can not be reduced to Echelon form");
     }
 }