Extreme Optimization > QuickStart Samples > Function Delegates QuickStart Sample (VB.NET)

Extreme Optimization QuickStart Samples

Function Delegates QuickStart Sample (VB.NET)

Illustrates the basic use of function delegates (Extreme.Mathematics namespace) in Visual Basic .NET.

C# code Back to QuickStart Samples

' The function delegates reside in the Extreme.Mathematics
' namespace.
Imports Extreme.Mathematics
' We will also use classes from the Extreme.Mathematics.SpecialFunctions 
' namespace.
Imports Extreme.Mathematics.SpecialFunctions

Namespace Extreme.Mathematics.QuickStart.VB
    ' Illustrates the use of function delegates in the 
    ' Extreme.Mathematics namespace of the Extreme Optimization 
    ' Mathematics Library for .NET.
    Module FunctionDelegates

        Sub Main()
            ' Delegates are used throughout the Extreme 
            ' Optimization Mathematics Library for .NET
            ' in places where a mathematical function must be 
            ' passed as an argument.

            '
            ' The RealFunction delegate
            '

            ' This delegate represents a function of one real
            ' variable that returns a real number. Many static 
            ' methods in the System.Math class fall in this
            ' category, such as the cosine function:
            Dim cos As RealFunction = _
                New RealFunction(AddressOf Math.Cos)
            ' Many static methods in the Extreme.Mathematics.SpecialFunctions
            ' classes are also of this form:
            Dim gamma As RealFunction = _
                New RealFunction( _
                    AddressOf GammaFunctions.Gamma)

            ' You can call delegates just like they were
            ' methods:
            Console.WriteLine("Cos(1) = {0}", cos(1))
            Console.WriteLine("Gamma(1.5) = {0}", gamma(1.5))

            '
            ' Other function delegates
            '

            ' A BivariateRealFunction represents a
            ' function that takes two real arguments and returns
            ' a real number. An example is the Atan2 method
            ' of System.Math:
            Dim atan2 As BivariateRealFunction = _
                New BivariateRealFunction( _
                    AddressOf Math.Atan2)
            Console.WriteLine("Atan2(1, 3) = {0}", atan2(1, 3))
            ' Or the Hypot method from SpecialFunctions.Elementary:
            Dim hypot As BivariateRealFunction = _
                New BivariateRealFunction( _
                    AddressOf ElementaryFunctions.Hypot)
            Console.WriteLine("Hypot(3, 4) = {0}", hypot(3, 4))

            ' A TrivariateRealFunction represents a
            ' function that takes two real arguments and returns
            ' a real number. An example is the 
            ' Incomplete Beta function, implemented by the
            ' of the IncompleteBeta method of the GammaFunctions
            ' class in Extreme.Mathematics.SpecialFunctions:
            Dim incompleteBeta As TrivariateRealFunction = _
                New TrivariateRealFunction( _
                    AddressOf GammaFunctions.IncompleteBeta)
            Console.WriteLine("Incomplete Beta(1, 3, 0.3) = {0}", _
                incompleteBeta(1, 3, 0.3))

            ' A ComplexFunction represents a function
            ' taking a complex argument and returning a 
            ' complex number.
            Dim complexCosh As ComplexFunction = _
                New ComplexFunction( _
                    AddressOf DoubleComplex.Cosh)
            Dim z As DoubleComplex = New DoubleComplex(1, 1)
            Console.WriteLine("cosh({0}) = {1}", z, complexCosh(z))

            ' A ParameterizedRealFunction represents 
            ' a function taking one integer and one real 
            ' argument that returns a real argument. An example
            ' is the Elementary.Pow method in the
            ' Extreme.Mathematics.SpecialFunctions namespace. However,
            ' the arguments are in the wrong order, so we
            ' need a helper function for this one. The 
            ' IntegerPower method is defined below.
            Dim power As ParameterizedRealFunction = _
                New ParameterizedRealFunction( _
                    AddressOf IntegerPower)
            Console.WriteLine("2^5 = {0}", power(5, 2))

            ' Finally, there are two more function delegates.
            ' Their operation is entirely analogous to the
            ' examples above.
            ' 
            ' MultivariateRealFunction returns a real
            ' number and takes a Vector (Extreme.Mathematics.LinearAlgebra
            ' namespace) as its argument.
            ' 
            ' MultivariateVectorFunction returns a 
            ' Vector and takes a Vector as its argument.

            '
            ' The FunctionFactory class
            '

            ' Sometimes, we need a delegate that is a special
            ' case of another delegate. We can call static methods
            ' of the FunctionFactory class to accomplish
            ' this.
            ' We can fix one of the arguments of a
            ' BivariateRealFunction function delegate.
            ' The second argument is the value of the fixed
            ' argument. The last parameter is the zero-based
            ' index of the argument that will be fixed:
            Dim hypotWithX4 As RealFunction = _
                FunctionFactory.RealFromBivariateRealFunction( _
                    hypot, 4, 0)
            Console.WriteLine("hypot(4, 3) = {0}", hypotWithX4(3))

            ' We can also fix the integer argument of a
            ' ParameterizedRealFunction:
            Dim seventhPower As RealFunction = _
                FunctionFactory.RealFromParameterizedRealFunction( _
                    power, 7)
            Console.WriteLine("3^7 = {0}", seventhPower(3))

            Dim factors As Int64() = _
                NumberTheory.Factorize(4292017459171704241)
            Dim i As Int32
            For i = 0 To factors.Length - 1
                Console.WriteLine(factors(i))
            Next
            Console.WriteLine()

            Console.Write("Press Enter key to exit...")
            Console.ReadLine()
        End Sub

        Function IntegerPower(ByVal exponent As Int32, _
            ByVal x As Double) As Double

            Return ElementaryFunctions.Pow(x, exponent)
        End Function

    End Module

End Namespace
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army