12,445,470 members (62,821 online)
Tip/Trick
alternative version

36.7K views
12 bookmarked
Posted

# Linear Equation Solver - Gaussian Elimination (C#)

, 22 May 2012 CPOL
 Rate this:
Linear equation solver - Gaussian Elimination.

## Introduction

This code implements the Gaussian elimination algorithm in C#.

## Background

Since I was unable to find this algo in C#, I wrote it on my own.

## Using the code

Simply copy and paste the code to your project. If you prefer double precision, replace all occurances of "`float`" with "`double`".

```public static class LinearEquationSolver
{
/// <summary>Computes the solution of a linear equation system.</summary>
/// <param name="M">
/// The system of linear equations as an augmented matrix[row, col] where (rows + 1 == cols).
/// It will contain the solution in "row canonical form" if the function returns "true".
/// </param>
/// <returns>Returns whether the matrix has a unique solution or not.</returns>
public static bool Solve(float[,] M)
{
// input checks
int rowCount = M.GetUpperBound(0) + 1;
if (M == null || M.Length != rowCount * (rowCount + 1))
throw new ArgumentException("The algorithm must be provided with a (n x n+1) matrix.");
if (rowCount < 1)
throw new ArgumentException("The matrix must at least have one row.");

// pivoting
for (int col = 0; col + 1 < rowCount; col++) if (M[col, col] == 0)
// check for zero coefficients
{
// find non-zero coefficient
int swapRow = col + 1;
for (;swapRow < rowCount; swapRow++) if (M[swapRow, col] != 0) break;

if (M[swapRow, col] != 0) // found a non-zero coefficient?
{
// yes, then swap it with the above
float[] tmp = new float[rowCount + 1];
for (int i = 0; i < rowCount + 1; i++)
{ tmp[i] = M[swapRow, i]; M[swapRow, i] = M[col, i]; M[col, i] = tmp[i]; }
}
else return false; // no, then the matrix has no unique solution
}

// elimination
for (int sourceRow = 0; sourceRow + 1 < rowCount; sourceRow++)
{
for (int destRow = sourceRow + 1; destRow < rowCount; destRow++)
{
float df = M[sourceRow, sourceRow];
float sf = M[destRow, sourceRow];
for (int i = 0; i < rowCount + 1; i++)
M[destRow, i] = M[destRow, i] * df - M[sourceRow, i] * sf;
}
}

// back-insertion
for (int row = rowCount - 1; row >= 0; row--)
{
float f = M[row,row];
if (f == 0) return false;

for (int i = 0; i < rowCount + 1; i++) M[row, i] /= f;
for (int destRow = 0; destRow < row; destRow++)
{ M[destRow, rowCount] -= M[destRow, row] * M[row, rowCount]; M[destRow, row] = 0; }
}
return true;
}
}```

## Changes

• Following a Suggestion from "jarvisa", the "`rowCount`" parameter was removed.
• Minor bugfix
• Minor performance improvement

## Share

 Germany
No Biography provided

## You may also be interested in...

 First Prev Next
 My vote of 5 Member 791511423-Jan-16 14:56 Member 7915114 23-Jan-16 14:56
 Problem with search for non-zero pivot element Member 1035586924-Jan-14 17:47 Member 10355869 24-Jan-14 17:47
 Solving a System of Linear Equation WiiMaxx15-Jul-13 2:58 WiiMaxx 15-Jul-13 2:58
 Incorrect calculation elw00d12325-Sep-12 3:11 elw00d123 25-Sep-12 3:11
 Further C# packages on Gaussian Elimination and Linear Matrix Algebra in general akemper21-May-12 11:33 akemper 21-May-12 11:33
 PInvoke and ACML is a solid solution Jonathan Langdon21-May-12 7:04 Jonathan Langdon 21-May-12 7:04
 Suggestion jarvisa21-May-12 0:14 jarvisa 21-May-12 0:14
 Most of us use 3rd party libraries... Andreas Gieriet20-May-12 22:10 Andreas Gieriet 20-May-12 22:10
 Last Visit: 31-Dec-99 18:00     Last Update: 23-Aug-16 23:42 Refresh 1