C# Sym Fact Example

← 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
  /// symmetric matrices.
  /// </summary>
  class SymFactExample
  {

    static void Main( string[] args )
    {
      // Construct a symmetric matrix as the product of the transpose of a 
      // matrix with itself.
      int rows = 5;
      int cols = 5;
      var rng = new RandGenUniform( -1, 1 );
      rng.Reset( 0x124 );
      var A = new DoubleMatrix( rows, cols, rng );
      DoubleMatrix ATA = NMathFunctions.TransposeProduct( A, A );
      var S = new DoubleSymmetricMatrix( ATA );

      Console.WriteLine();

      Console.WriteLine( "S =" );
      Console.WriteLine( S.ToTabDelimited( "G3" ) );

      // S =
      // 0.791   -0.366  -0.31   0.183   0.863
      // -0.366  0.224   0.177   -0.312  -0.214
      // -0.31   0.177   0.49    -0.411  -0.48
      // 0.183   -0.312  -0.411  2.03    -0.0979
      // 0.863   -0.214  -0.48   -0.0979 2.01

      // Construct a symmetric factorization class.
      var fact = new DoubleSymFact( S );

      // Check to see if S is singular.
      string isSingularString = fact.IsSingular ? "S is singular" : "S is NOT singular";
      Console.WriteLine( isSingularString );

      // Retrieve information about the matrix S.
      double det = fact.Determinant();

      // In order to get condition number, factor with estimateCondition = true
      fact.Factor( S, true );
      double rcond = fact.ConditionNumber();

      DoubleSymmetricMatrix SInv = fact.Inverse();

      Console.WriteLine();
      Console.WriteLine( "Determinant of S = {0}", det );

      Console.WriteLine();
      Console.WriteLine( "Reciprocal condition number = {0}", rcond );

      Console.WriteLine();
      Console.WriteLine( "S inverse =" );
      Console.WriteLine( SInv.ToTabDelimited( "G3" ) );

      // Use the factorization to solve some linear systems Ax = y.
      var y0 = new DoubleVector( fact.Cols, rng );
      var y1 = new DoubleVector( fact.Cols, rng );
      DoubleVector x0 = fact.Solve( y0 );
      DoubleVector x1 = fact.Solve( y1 );

      Console.WriteLine( "Solution to Ax = y0 is {0}", x0.ToString( "G5" ) );

      Console.WriteLine();
      Console.WriteLine( "y0 - Ax0 = {0}", ( y0 - MatrixFunctions.Product( S, 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( S, x1 ) ).ToString( "G5" ) );

      // You can also solve for multiple right-hand sides.
      var Y = new DoubleMatrix( y1.Length, 2 );
      Y.Col( 0 )[Slice.All] = y0;
      Y.Col( 1 )[Slice.All] = y1;
      DoubleMatrix 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( "G3" ) );

      // Factor a different matrix.
      DoubleSymmetricMatrix B = 1.2 * S;
      fact.Factor( B );
      x0 = fact.Solve( y0 );

      Console.WriteLine( "Solution to Bx = y0 is {0}", x0.ToString( "G5" ) );

      Console.WriteLine();
      Console.WriteLine( "Press Enter Key" );
      Console.Read();
    }
  }
}

← All NMath Code Examples
Top