C# Eig Decomp 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 eigenvalue decomposition classes.
  /// </summary>
  class EigDecompExample
  {

    static void Main( string[] args )
    {
      // A general m x n system with random entries.
      var rng = new RandGenUniform( -1, 1 );
      rng.Reset( 0x75 );
      int rows = 4;
      int cols = 4;
      var A = new FloatMatrix( rows, cols, rng );

      // Construct an eigenvalue decomposition of A.
      var decomp = new FloatEigDecomp( A );

      Console.WriteLine();

      // Is it good?
      Console.WriteLine( "Good? " + decomp.IsGood );

      // Look at the eigenvalues
      Console.WriteLine();
      Console.WriteLine( "Eigenvalues = " + decomp.EigenValues );

      // Look at the left eigenvectors
      Console.WriteLine();
      Console.WriteLine( "Left eigenvectors = " );
      Console.WriteLine( decomp.LeftEigenVectors.ToTabDelimited( "G3" ) );

      // Look at the right eigenvectors
      Console.WriteLine( "Right eigenvectors = " );
      Console.WriteLine( decomp.RightEigenVectors.ToTabDelimited( "G3" ) );

      // The class FloatEigDecompServer allows more control over the computation. 
      // Suppose that you are only interested in the singular values, not the 
      // vectors. You can configure a DoubleComplexSVDecompServer object to 
      // compute just the singular values.

      var eigServer = new FloatEigDecompServer();
      eigServer.ComputeLeftVectors = false;
      eigServer.ComputeRightVectors = false;
      eigServer.Balance = BalanceOption.Permute;
      decomp = eigServer.Factor( A );

      Console.WriteLine();
      Console.WriteLine( "Number of left vectors computed: {0}", decomp.NumberOfLeftEigenVectors ); // 0

      Console.WriteLine();
      Console.WriteLine( "Number of right vectors computed: {0}", decomp.NumberOfRightEigenVectors ); // 0

      // Is it good?
      Console.WriteLine();
      Console.WriteLine( "Good? " + decomp.IsGood );

      // Look at the eigenvalues
      Console.WriteLine();
      Console.WriteLine( "Eigenvalues = " + decomp.EigenValues );

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

← All NMath Code Examples
Top