[TOC]
Imports System
Imports System.Globalization
Imports System.Threading
Imports CenterSpace.NMath.Core
Namespace CenterSpace.NMath.Core.Examples.VisualBasic
' A .NET example in VB.NET showing some of the functionality of the NMathFunctions class.
Module NMathFunctionsExample
Sub Main()
' Most common mathematical functions have been overloaded to accept
' vector and matrix types. These functions are provided as static
' methods in the class CenterSpace.NMath.Core.NMathFunctions. The
' result of invoking one of these methoods 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()
Dim Original As CultureInfo = Thread.CurrentThread.CurrentCulture
' This example uses strings representing numbers in the US locale
' so change the current culture info. For example, "$4.30"
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
Dim v As New FloatVector(1.2F, -4.5F, 9.1F, -10.01F)
' Create a vector whose values are the absolute values of v.
Dim absV As FloatVector = NMathFunctions.Abs(v)
Dim A As New DoubleComplexMatrix("2x2 [(1,-1) (2,-.5) (2.2,1.1) (7,9)]")
' Back to 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 NMathFunctions Conj method to obtain a matrix whose elements are
' the complex cojugates of the complex matrix A.
Dim AConj As DoubleComplexMatrix = 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.
Dim AConjImag As DoubleMatrix = NMathFunctions.Imag(AConj)
' AConjImag = 2x2 [1 0.5 -1.1 -9]
Console.WriteLine("AConjImag...")
Console.WriteLine(AConjImag)
Console.WriteLine()
' The NMathFunctions 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.
Dim detAConjImag As Double = 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 (Not detAConjImag.Equals(0)) Then
Dim inv As DoubleMatrix = NMathFunctions.Inverse(AConjImag)
Console.WriteLine("The inverse of AConjImag...")
Console.WriteLine(inv.ToString("F3"))
Console.WriteLine()
Dim b As New DoubleVector("[0 -1]")
Dim x As DoubleVector = NMathFunctions.Solve(AConjImag, b)
Console.WriteLine("The solution, x to AConjImag*x=b is...")
Console.WriteLine(x.ToString("F3"))
Console.WriteLine()
Dim conditionNumber As Double = NMathFunctions.EstimateConditionNumber(AConjImag, NormType.OneNorm)
Console.WriteLine("The condition number of AConjImag in the 1-norm is...")
Console.WriteLine(conditionNumber.ToString("F5"))
Else
Console.WriteLine("Sorry, matrix is singular")
End If
Console.WriteLine()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
End Module
End Namespace
[TOC]