← All NMath Code Examples
using System;
using System.Globalization;
using System.Threading;
using CenterSpace.NMath.Core;
namespace CenterSpace.NMath.Examples.CSharp
{
/// <summary>
/// A .NET example in C# showing some of the functionality of the NMath class.
/// </summary>
class NMathExample
{
static void Main( string[] args )
{
// Most common mathematical functions have been overloaded to accept
// vector and matrix types. These functions are provided as static
// methods in the class CenterSpace.NMathFunctions.Core.NMathFunctions. The
// result of invoking one of these methods with a vector or matrix
// argument is a new vector or matrix object of the same size whose
// values are the result of applying the function to each element
// of its argument.
Console.WriteLine();
CultureInfo original = Thread.CurrentThread.CurrentCulture;
// This example uses strings representing numbers in the US locale
// so change the current culture info. For example, "-.5"
Thread.CurrentThread.CurrentCulture = new CultureInfo( "en-US" );
var v = new FloatVector( 1.2F, -4.5F, 9.1F, -10.01F );
// Create a vector whose values are the absolute values of v.
FloatVector absV = NMathFunctions.Abs( v );
var A = new DoubleComplexMatrix( "2x2 [(1,-1) (2,-.5) (2.2,1.1) (7,9)]" );
// Back to your original culture.
Thread.CurrentThread.CurrentCulture = original;
Console.WriteLine( "absV = {0}", absV.ToString() ); // absV = [1.2 4.5 9.1 10.01]
Console.WriteLine();
// Use the NMath Conj method to obtain a matrix whose elements are
// the complex conjugates of the complex matrix A.
DoubleComplexMatrix AConj = NMathFunctions.Conj( A );
// AConj = 2x2 [(1,1) (2,0.5) (2.2,-1.1) (7,-9)]
Console.WriteLine( "AConj..." );
Console.WriteLine( AConj );
Console.WriteLine();
// Now use the Imag method to create a real matrix containing
// the imaginary parts of AConj.
DoubleMatrix AConjImag = NMathFunctions.Imag( AConj );
// AConjImag = 2x2 [1 0.5 -1.1 -9]
Console.WriteLine( "AConjImag..." );
Console.WriteLine( AConjImag );
Console.WriteLine();
// The NMath class also provides static methods for solving
// linear systems, computing determinants, matrix inverses, and
// matrix condition numbers. See the LU factorization example
// to see how re-use the LU factorization of a matrix to compute
// these quantities.
double detAConjImag = NMathFunctions.Determinant( AConjImag );
Console.WriteLine( "The determinant of AConjImag = {0}", detAConjImag );
Console.WriteLine();
// If the determinant is non-zero, the matrix is invertible. So we
// can compute inverses and solve linear systems.
if ( detAConjImag != 0 )
{
DoubleMatrix inv = NMathFunctions.Inverse( AConjImag );
Console.WriteLine( "The inverse of AConjImag..." );
Console.WriteLine( inv.ToString( "F3" ) );
Console.WriteLine();
var b = new DoubleVector( "[0 -1]" );
DoubleVector x = NMathFunctions.Solve( AConjImag, b );
Console.WriteLine( "The solution, x to AConjImag*x=b is..." );
Console.WriteLine( x.ToString( "F3" ) );
Console.WriteLine();
double conditionNumber = NMathFunctions.EstimateConditionNumber( AConjImag, NormType.OneNorm );
Console.Write( "The condition number of AConjImag in the 1-norm is... " );
Console.WriteLine( conditionNumber.ToString( "F5" ) );
}
else
{
Console.WriteLine( "Sorry, matrix is singular" );
}
Console.WriteLine();
Console.WriteLine( "Press Enter Key" );
Console.Read();
} // Main
}// class
}// namespace
← All NMath Code Examples