← All NMath Code Examples
using System;
using CenterSpace.NMath.Core;
namespace CenterSpace.NMath.Examples.CSharp
{
/// <summary>
/// A .NET example in C# demonstrating the features of the factorization classes for
/// tridiagonal matrices.
/// </summary>
class TriDiagFactExample
{
static void Main( string[] args )
{
// Construct a tridiagonal matrix with random entries.
int rows = 5;
int cols = 5;
var rng = new RandGenUniform( -1, 1 );
rng.Reset( 0x124 );
var data1 = new FloatComplexVector( cols, rng );
var data2 = new FloatComplexVector( cols - 1, rng );
var data3 = new FloatComplexVector( cols - 1, rng );
var A = new FloatComplexTriDiagMatrix( rows, cols );
A.Diagonal()[Slice.All] = data1;
A.Diagonal( 1 )[Slice.All] = data2;
A.Diagonal( -1 )[Slice.All] = data3;
Console.WriteLine();
Console.WriteLine( "A =" );
Console.WriteLine( A.ToTabDelimited( "F3" ) );
Console.WriteLine();
// A =
// (-0.497,0.332) (0.560,0.306) (0.000,0.000) (0.000,0.000) (0.000,0.000)
// (0.773,0.358) (-0.250,0.576) (0.220,-0.077) (0.000,0.000) (0.000,0.000)
// (0.000,0.000) (-0.863,0.203) (0.196,0.182) (-0.168,-0.259) (0.000,0.000)
// (0.000,0.000) (0.000,0.000) (-0.585,0.622) (-0.044,0.074) (-0.924,0.621)
// (0.000,0.000) (0.000,0.000) (0.000,0.000) (-0.705,0.124) (-0.325,-0.280)
// Construct a tridiagonal factorization class.
var fact = new FloatComplexTriDiagFact( A );
// Check to see if A is singular.
string isSingularString = fact.IsSingular ? "A is singular" : "A is NOT singular";
Console.WriteLine( isSingularString );
// Retrieve information about the matrix A.
FloatComplex det = fact.Determinant();
// In order to get condition number, factor with estimateCondition = true
fact.Factor( A, true );
float rcond = fact.ConditionNumber();
FloatComplexMatrix AInv = fact.Inverse();
Console.WriteLine();
Console.WriteLine( "Determinant of A = {0}", det );
Console.WriteLine();
Console.WriteLine( "Reciprocal condition number = {0}", rcond );
Console.WriteLine();
Console.WriteLine( "A inverse =" );
Console.WriteLine( AInv.ToTabDelimited( "F3" ) );
// Use the factorization to solve some linear systems Ax = y.
var y0 = new FloatComplexVector( fact.Cols, rng );
var y1 = new FloatComplexVector( fact.Cols, rng );
FloatComplexVector x0 = fact.Solve( y0 );
FloatComplexVector x1 = fact.Solve( y1 );
Console.WriteLine();
Console.WriteLine( "Solution to Ax = y0 is {0}", x0.ToString( "G5" ) );
Console.WriteLine();
Console.WriteLine( "y0 - Ax0 = {0}", ( y0 - MatrixFunctions.Product( A, x0 ) ).ToString( "G5" ) );
Console.WriteLine();
Console.WriteLine( "Solution to Ax = y1 is {0}", x1.ToString( "G5" ) );
Console.WriteLine();
Console.WriteLine( "y1 - Ax1 = {0}", ( y1 - MatrixFunctions.Product( A, x1 ) ).ToString( "G5" ) );
// You can also solve for multiple right-hand sides.
var Y = new FloatComplexMatrix( y1.Length, 2 );
Y.Col( 0 )[Slice.All] = y0;
Y.Col( 1 )[Slice.All] = y1;
FloatComplexMatrix X = fact.Solve( Y );
// The first column of X should be x0; the second column should be x1.
Console.WriteLine();
Console.WriteLine( "X =" );
Console.WriteLine( X.ToTabDelimited( "G7" ) );
// Factor a different matrix.
var z = new FloatComplex( 1.23F, -.76F );
FloatComplexTriDiagMatrix B = z * A;
fact.Factor( B );
x0 = fact.Solve( y0 );
Console.WriteLine();
Console.WriteLine( "Solution to Bx = y0 is {0}", x0.ToString( "G5" ) );
Console.WriteLine();
Console.WriteLine( "Press Enter Key" );
Console.Read();
}
}
}
← All NMath Code Examples