# Linear Equations in C# QuickStart Sample

Illustrates how to solve systems of simultaneous linear equations in C#.

View this sample in: Visual Basic F# IronPython

``````using System;

using Extreme.Mathematics;

namespace Extreme.Numerics.QuickStart.CSharp
{
/// <summary>
/// Illustrates solving systems of simultaneous linear
/// equations using the DenseMatrix and LUDecomposition classes
/// in the Extreme.Mathematics.LinearAlgebra namespace of Extreme Numerics.NET.
/// </summary>
class LinearEquations
{
static void Main(string[] args)
{
// The license is verified at runtime. We're using
// https://numerics.net/trial-key
// A system of simultaneous linear equations is
// defined by a square matrix A and a right-hand
// side B, which can be a vector or a matrix.
//
// You can use any matrix type for the matrix A.
// The optimal algorithm is automatically selected.

var m = Matrix.Create(4, 4, new double[]
{
1, 1, 1, 1,
1, 2, 3, 4,
1, 4, 9, 16,
1, 2, 1, 2
}, MatrixElementOrder.ColumnMajor);
var b1 = Vector.Create(1.0, 3.0, 6.0, 3.0);
var b2 = Matrix.Create(4, 2, new double[]
{
1, 3, 6, 3,
2, 3, 5, 8
}, MatrixElementOrder.ColumnMajor);
Console.WriteLine("m = {0:F4}", m);

//
// The Solve method
//

// The following solves m x = b1. The second
// parameter specifies whether to overwrite the
// right-hand side with the result.
var x1 = m.Solve(b1, false);
Console.WriteLine("x1 = {0:F4}", x1);
// If the overwrite parameter is omitted, the
// right-hand-side is overwritten with the solution:
m.Solve(b1);
Console.WriteLine("b1 = {0:F4}", b1);
// You can solve for multiple right hand side
// vectors by passing them in a DenseMatrix:
var x2 = m.Solve(b2, false);
Console.WriteLine("x2 = {0:F4}", x2);

//
// Related Methods
//

// You can verify whether a matrix is singular
// using the IsSingular method:
Console.WriteLine("IsSingular(m) = {0}",
m.IsSingular());
// The inverse matrix is returned by the Inverse
// method:
Console.WriteLine("Inverse(m) = {0:F4}", m.GetInverse());
// The determinant is also available:
Console.WriteLine("Det(m) = {0:F4}", m.GetDeterminant());
// The condition number is an estimate for the
// loss of precision in solving the equations
Console.WriteLine("Cond(m) = {0:F4}", m.EstimateConditionNumber());
Console.WriteLine();

//
// The LUDecomposition class
//

// If multiple operations need to be performed
// on the same matrix, it is more efficient to use
// the LUDecomposition class. This class does the
// bulk of the calculations only once.
Console.WriteLine("Using LU Decomposition:");
// The constructor takes an optional second argument
// indicating whether to overwrite the original
// matrix with its decomposition:
var lu = m.GetLUDecomposition(false);
// All methods mentioned earlier are still available:
x2 = lu.Solve(b2, false);
Console.WriteLine("x2 = {0:F4}", x2);
Console.WriteLine("IsSingular(m) = {0}",
lu.IsSingular());
Console.WriteLine("Inverse(m) = {0:F4}", lu.GetInverse());
Console.WriteLine("Det(m) = {0:F4}", lu.GetDeterminant());
Console.WriteLine("Cond(m) = {0:F4}", lu.EstimateConditionNumber());