Extreme Optimization >
Mathematics Library for .NET >
QuickStart Samples >
StructuredLinearEquations QuickStart Sample (C#)
Extreme Optimization Mathematics Library for .NET
StructuredLinearEquations QuickStart Sample (C#)
Illustrates how to solve symmetrical and triangular systems of
simultaneous linear equations using classes from the
Extreme.Mathematics.LinearAlgebra namespace in C#.
VB.NET code
Back to QuickStart Samples
using System;
namespace Extreme.Mathematics.QuickStart.CSharp
{
// The structured matrix classes reside in the
// Extreme.Mathematics.LinearAlgebra namespace.
using Extreme.Mathematics.LinearAlgebra;
/// <summary>
/// Illustrates solving symmetrical and triangular systems
/// of simultaneous linear equations using classes
/// in the Extreme.Mathematics.LinearAlgebra namespace of the Extreme
/// Optimization Mathematics Library for .NET.
/// </summary>
class StructuredLinearEquations
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// To learn more about solving general systems of
// simultaneous linear equations, see the
// LinearEquations QuickStart Sample.
//
// The methods and classes available for solving
// structured systems of equations are similar
// to those for general equations.
//
// Triangular systems and matrices
//
Console.WriteLine("Triangular matrices:");
// For the basics of working with triangular
// matrices, see the TriangularMatrices QuickStart
// Sample.
//
// Let's start with a triangular matrix. Remember
// that elements are stored in column-major order
// by default.
TriangularMatrix t = new TriangularMatrix(
MatrixTriangleMode.Upper, 4, new double[]
{
1, 0, 0, 0,
1, 2, 0, 0,
1, 4, 1, 0,
1, 3, 1, 2
});
GeneralVector b1 = new GeneralVector(1, 3, 6, 3);
GeneralMatrix b2 = new GeneralMatrix(4, 2, new double[]
{
1, 3, 6, 3,
2, 3, 5, 8
});
Console.WriteLine("t = {0}", t);
//
// The Solve method
//
// The following solves m x = b1. The second
// parameter specifies whether to overwrite the
// right-hand side with the result.
GeneralVector x1 = t.Solve(b1, false);
Console.WriteLine("x1 = {0}", x1);
// If the overwrite parameter is omitted, the
// right-hand-side is overwritten with the solution:
t.Solve(b1);
Console.WriteLine("b1 = {0}", b1);
// You can solve for multiple right hand side
// vectors by passing them in a GeneralMatrix:
GeneralMatrix x2 = t.Solve(b2, false);
Console.WriteLine("x2 = {0}", x2);
//
// Related Methods
//
// You can verify whether a matrix is singular
// using the IsSingular method:
Console.WriteLine("IsSingular(t) = {0}",
t.IsSingular());
// The inverse matrix is returned by the Inverse
// method:
Console.WriteLine("Inverse(t) = {0}", t.GetInverse());
// The determinant is also available:
Console.WriteLine("Det(t) = {0}", t.GetDeterminant());
// The condition number is an estimate for the
// loss of precision in solving the equations
Console.WriteLine("Cond(t) = {0}", t.EstimateConditionNumber());
Console.WriteLine();
//
// Symmetric systems and matrices
//
Console.WriteLine("Symmetric matrices:");
// For the basics of working with symmetric
// matrices, see the SymmetricMatrices QuickStart
// Sample.
//
// Let's start with a symmetric matrix. Remember
// that elements are stored in column-major order
// by default.
SymmetricMatrix s = new SymmetricMatrix(4, new double[]
{
1, 0, 0, 0,
1, 2, 0, 0,
1, 1, 2, 0,
1, 0, 1, 4
}, MatrixTriangleMode.Upper);
b1 = new GeneralVector(1, 3, 6, 3);
Console.WriteLine("s = {0}", s);
//
// The Solve method
//
// The following solves m x = b1. The second
// parameter specifies whether to overwrite the
// right-hand side with the result.
x1 = s.Solve(b1, false);
Console.WriteLine("x1 = {0}", x1);
// If the overwrite parameter is omitted, the
// right-hand-side is overwritten with the solution:
s.Solve(b1);
Console.WriteLine("b1 = {0}", b1);
// You can solve for multiple right hand side
// vectors by passing them in a GeneralMatrix:
x2 = s.Solve(b2, false);
Console.WriteLine("x2 = {0}", x2);
//
// Related Methods
//
// You can verify whether a matrix is singular
// using the IsSingular method:
Console.WriteLine("IsSingular(s) = {0}",
s.IsSingular());
// The inverse matrix is returned by the Inverse
// method:
Console.WriteLine("Inverse(s) = {0}", s.GetInverse());
// The determinant is also available:
Console.WriteLine("Det(s) = {0}", s.GetDeterminant());
// The condition number is an estimate for the
// loss of precision in solving the equations
Console.WriteLine("Cond(s) = {0}", s.EstimateConditionNumber());
Console.WriteLine();
//
// The CholeskyDecomposition class
//
// If the symmetric matrix is positive definite,
// you can use the CholeskyDecomposition class
// to optimize performance if multiple operations
// need to be performed. This class does the
// bulk of the calculations only once. This
// decomposes the matrix into G x transpose(G)
// where G is a lower triangular matrix.
//
// If the matrix is indefinite, you need to use
// the LUDecomposition class instead. See the
// LinearEquations QuickStart Sample for details.
Console.WriteLine("Using Cholesky Decomposition:");
// The constructor takes an optional second argument
// indicating whether to overwrite the original
// matrix with its decomposition:
CholeskyDecomposition cf =
new CholeskyDecomposition(s, false);
// The Factorize method performs the actual
// factorization. It is called automatically
// if needed.
cf.Decompose();
// All methods mentioned earlier are still available:
x2 = cf.Solve(b2, false);
Console.WriteLine("x2 = {0}", x2);
Console.WriteLine("IsSingular(m) = {0}",
cf.IsSingular());
Console.WriteLine("Inverse(m) = {0}", cf.GetInverse());
Console.WriteLine("Det(m) = {0}", cf.GetDeterminant());
Console.WriteLine("Cond(m) = {0}", cf.EstimateConditionNumber());
// In addition, you have access to the
// triangular matrix, G, of the composition.
Console.WriteLine(" G = {0}", cf.LowerTriangularFactor);
// Note that if the matrix is indefinite,
// the factorization will fail and throw a
// MatrixNotPositiveDefiniteException.
s[0,0] = -99;
cf = new CholeskyDecomposition(s);
try
{
cf.Decompose();
}
catch (MatrixNotPositiveDefiniteException e)
{
Console.WriteLine(e.Message);
}
// The SymmetricMatrix class take care of this
// possibility automatically, but is much less
// efficient:
Console.WriteLine("Cond s = {0}", s.EstimateConditionNumber());
Console.Write("Press Enter key to exit...");
Console.ReadLine();
}
}
}
Copyright 2004-2008,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual
Studio.NET, and the Visual Studio Logo are registered trademarks of Microsoft Corporation