Extreme Optimization > User's Guide > Mathematics Library > General Classes > Function Delegates

Extreme Optimization User's Guide

User's Guide

Up: General Classes Next: Numerical Algorithms Support Classes Previous: Elementary Functions Contents

Function Delegates

Many calculations involve the repeated evaluation of one or more user-supplied functions. Numerical integration and differentiation and finding zeroes of functions are common examples. In the .NET framework, this functionality is implemented using delegates.

The Extreme Optimization Mathematics Library for .NET provides delegate types for the most common function signatures. In addition, the FunctionFactory class provides methods that aid in deriving new delegates from existing ones.

Delegate types

Five delegate types represent functions returning a Double value. The simplest and most widely used is the RealFunction delegate. It takes one Double parameter. Most of the static methods of the System.Math class, as well as the classes in the Extreme.Mathematics.SpecialFunctions namespace can be encapsulated with this type of delegate. The following creates a RealFunction delegate for the cosine function and for the Gamma function:

C# CopyCode imageCopy Code
using Extreme.Mathematics;
using Extreme.Mathematics.SpecialFunctions;
RealFunction cos = new RealFunction(Math.Cos);
RealFunction gamma = new RealFunction(GammaFunctions.Gamma);
Visual Basic CopyCode imageCopy Code
Imports Extreme.Mathematics
Imports Extreme.Mathematics.SpecialFunctions
Dim fCos As RealFunction = _
     New RealFunction(AddressOf Math.Cos)
Dim fGamma As RealFunction = _
     New RealFunction(AddressOf GammaFunctions.Gamma)

You can call delegates as if they were methods:

C# CopyCode imageCopy Code
Console.WriteLine("Cos(1) = {0}", cos(1));
Console.WriteLine("Gamma(1.5) = {0}", gamma(1.5));
Visual Basic CopyCode imageCopy Code
Console.WriteLine("Cos(1) = {0}", fCos(1))
Console.WriteLine("Gamma(1.5) = {0}", fGamma(1.5))

The BivariateRealFunction delegate takes two Double parameters. Examples of methods are the System.Math.Atan2 and the Beta function:

C# CopyCode imageCopy Code
BivariateRealFunction atan2 =    new BivariateRealFunction (Math.Atan2);
Console.WriteLine("Atan2(1, 3) = {0}", atan2(1, 3));
BivariateRealFunction beta =    new BivariateRealFunction(GammaFunctions.Beta);
Console.WriteLine("Beta(1.5, 4) = {0}", beta(1.5, 4));
Visual Basic CopyCode imageCopy Code
Dim fAtan2 As BivariateRealFunction = _
    New BivariateRealFunction (AddressOf Math.Atan2)
Console.WriteLine("Atan2(1, 3) = {0}", fAtan2(1, 3))
Dim fBeta As BivariateRealFunction = _
    New BivariateRealFunction(AddressOf GammaFunctions.Beta)
Console.WriteLine("Beta(1.5, 4) = {0}", fBeta(1.5, 4))

A TrivariateRealFunction represents a function that takes three real Double arguments and returns a real number. An example is the incomplete Beta function, implemented by the IncompleteBeta method of the GammaFunctions class:

C# CopyCode imageCopy Code
TrivariateRealFunction incBeta =   new TrivariateRealFunction(GammaFunctions.IncompleteBeta);
Console.WriteLine("IB(1, 3, 0.3) = {0}", incBeta(1, 3, 0.3));
Visual Basic CopyCode imageCopy Code
TrivariateRealFunction incBeta = _
   New TrivariateRealFunction(AddressOf GammaFunctions.IncompleteBeta)
Console.WriteLine("IB(1, 3, 0.3) = {0}", incBeta(1, 3, 0.3))

A ParameterizedRealFunction represents a function taking one Integer and one Double argument that returns a real number. This can be used to encapsulate members of a class of functions that are indexed by an integer. The monomials, xn, with n an integer are an example of this. The Elementary.Pow method implements this function. However, the arguments are in the wrong order, so we need a helper function for this one.

C# CopyCode imageCopy Code
static double Power(int exponent, double x)
{
    return ElementaryFunctions.Pow(x, exponent);
}
...
ParameterizedRealFunction power = 
    new ParameterizedRealFunction(Power);
Console.WriteLine("2.7^5 = {0}", power(5, 2.7));
Visual Basic CopyCode imageCopy Code
Shared Function Power(exponent As Integer, x  As Double) As Double
    Return ElementaryFunctions.Pow(x, exponent)
End Function
...
ParameterizedRealFunction power = 
    New ParameterizedRealFunction(AddressOf Power)
Console.WriteLine("2.7^5 = {0}", power(5, 2.7))

A ComplexFunction represents a function that takes a DoubleComplex argument and also returns a complex number. The DoubleComplex structure defines many static methods that fall into this category. Here is the complex hyperbolic cosine:

C# CopyCode imageCopy Code
ComplexFunction complexCosh = 
    new ComplexFunction(DoubleComplex.Cosh);
DoubleComplex z = new DoubleComplex(1, 1);
Console.WriteLine("cosh({0}) = {1}", z, complexCosh(z));
Visual Basic CopyCode imageCopy Code
Dim complexCosh As ComplexFunction  = _
    New ComplexFunction(AddressOf DoubleComplex.Cosh)
DoubleComplex z = New DoubleComplex(1, 1)
Console.WriteLine("cosh({0}) = {1}", z, complexCosh(z))

A MultivariateRealFunction represents a function that takes a Vector argument and returns a real number. Vectors reside in the Extreme.Mathematics.LinearAlgebra namespace. The example below implements the Rosenbrock function, a function often used to test optimization algorithms.

C# CopyCode imageCopy Code
double Rosenbrock(Vector x)
{
    double a = 1 - x[0];
    double b =  (x[1] - x[0]*x[0]);
    return a*a + 105*b*b;
}
MultivariateRealFunction rosenbrock = 
    new MultivariateRealFunction(Rosenbrock);
Visual Basic CopyCode imageCopy Code
Function Rosenbrock(x As Vector) As Double
    Dim a As Double = 1 - x(0)
    Dim b As Double = (x(1) - x(0)*x(0))
    Return a*a + 105*b*b
End Function

Dim rosenbrock As MultivariateRealFunction  = _
    New MultivariateRealFunction(AddressOf Rosenbrock)

A MultivariateVectorFunction represents a function that takes a Vector argument and returns a Vector. An example of this type of function is a function that represents a system of nonlinear equations. The following example defines the system of equations that that is the basis for the Rosenbrock function mentioned above:

C# CopyCode imageCopy Code
Vector RosenbrockSystem(Vector x)
{
    Vector f = new GeneralVector(2);
    f[0] = 1 - x[0];
    f[1] = x[1] - x[0]*x[0];
    return f;
}
MultivariateVectorFunction rosenbrockSystem = 
    new MultivariateVectorFunction(RosenbrockSystem);
Visual Basic CopyCode imageCopy Code
Function RosenbrockSystem(x As Vector) As Vector
    Dim f As Vector = New GeneralVector(2)
    f(0) = 1 - x(0)
    f(1) = x(1) - x(0)*x(0)
    Return f
End Function

Dim rosenbrockSystem As MultivariateVectorFunction = _
    New MultivariateVectorFunction(AddressOf RosenbrockSystem)

A FastMultivariateVectorFunction is a variation of this delegate that takes a second Vector parameter that contains the output vector. On exit, this vector contains the value of the function. The return value is also a reference to this same vector. The same example as above using this fast variant gives:

C# CopyCode imageCopy Code
Vector FastRosenbrockSystem(Vector x, Vector f)
{
    if (f == null)
        f = new GeneralVector(2);
    f[0] = 1 - x[0];
    f[1] = x[1] - x[0]*x[0];
    return f;
}
FastMultivariateVectorFunction fastRosenbrockSystem = 
    new FastMultivariateVectorFunction(FastRosenbrockSystem);
Visual Basic CopyCode imageCopy Code
Function RosenbrockSystem(x As Vector, f As Vector) As Vector
    If f Is Nothing Then
        f = New GeneralVector(2)
    End If
    f(0) = 1 - x(0)
    f(1) = x(1) - x(0)*x(0)
    Return f
End Function
Dim rosenbrockSystem As MultivariateVectorFunction = _
    New MultivariateVectorFunction(AddressOf RosenbrockSystem)

Finally, FastMultivariateMatrixFunction represents a multivariate function that returns a matrix. The first parameter is a a Vector that contains the input vector. The second parameter is a a Matrix that contains the output matrix. On exit, this matrix contains the value of the function. The return value is also a reference to this same matrix.

Examples of this type of function includes the Jacobian matrix of a system of equations, and the Hessian of a multivariate function. Here, we show the evaluation of the Jacobian of the Rosenbrock system defined earlier.

C# CopyCode imageCopy Code
Matrix RosenbrockJacobian(Vector x, Matrix J)
{
    if (J == null)
        J = new GeneralMatrix(2, 2);
    J[0,0] = -1;
    J[1,0] = -2*x[0];
    J[0,1] = 0;
    J[1,1] = 1;
    return J;
}
FastMultivariateMatrixFunction jacobian = 
    new FastMultivariateMatrixFunction(RosenbrockJacobian);
Visual Basic CopyCode imageCopy Code
Function RosenbrockJacobian(x As Vector, J As Matrix) As Matrix
    If J Is Nothing Then
        J = New GeneralMatrix(2, 2)
    End If
    J(0,0) = -1
    J(1,0) = -2*x(0)
    J(0,1) = 0
    J(1,1) = 1
    Return J
End Function
Dim jacobian As FastMultivariateMatrixFunction = _
    New FastMultivariateMatrixFunction(AddressOf RosenbrockJacobian)

The FunctionFactory class

In mathematics, it is not uncommon to 'fix' one of the arguments of a function that takes two arguments, and treat this as a new function of a single argument. There is no direct way to do this programmatically.

The FunctionFactory class contains a couple of static methods that transform BivariateRealFunction or a ParameterizedRealFunction into a RealFunction delegate by fixing one of the arguments.

We can fix one of the arguments of a BivariateRealFunction delegate using the RealFromBivariateRealFunction method. The first argument is a BivariateRealFunction 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:

C# CopyCode imageCopy Code
RealFunction hypotWithX4 =    FunctionFactory.RealFromBivariateRealFunction(hypot, 4, 0);
Console.WriteLine("hypot(4, 3) = {0}", hypotWithX4(3));
Visual Basic CopyCode imageCopy Code
RealFunction hypotWithX4 = _
    FunctionFactory.RealFromBivariateRealFunction(hypot, 4, 0)
Console.WriteLine("hypot(4, 3) = {0}", hypotWithX4(3))

We can also fix the integer argument of a ParameterizedRealFunction. This example uses the Power variable defined earlier:

C# CopyCode imageCopy Code
RealFunction seventhPower =    FunctionFactory.RealFromParameterizedRealFunction(power, 7);
Console.WriteLine("3^7 = {0}", seventhPower(3));
Visual Basic CopyCode imageCopy Code
RealFunction seventhPower = _
    FunctionFactory.RealFromParameterizedRealFunction(power, 7)
Console.WriteLine("3^7 = {0}", seventhPower(3))

Another group of methods combines an array of delegates returning a real number or a vector into vector or matrix functions.

We can combine one or more MultivariateRealFunction delegates to form either a MultivariateVectorFunction or a FastMultivariateVectorFunction. The RealFunctionsToVectorFunction and RealFunctionsToFastVectorFunction methods perform this task. These methods take one parameter: an array of MultivariateRealFunction delegates. The resulting vector function returns a vector whose components are the function values of the delegates in the array. These methods can be used, for example, to combine the left-hand sides of a system of non-linear equations into a single multivariate vector function.

C# CopyCode imageCopy Code
double Rosenbrock1(Vector x)
{
    return 1 - x[0];
}
double Rosenbrock2(Vector x)
{
    return x[1] - x[0]*x[0];
}
MultivariateRealFunction rosenbrock1 =    new MultivariateRealFunction(Rosenbrock1);
MultivariateRealFunction rosenbrock2 =    new MultivariateRealFunction(Rosenbrock2);
MultivariateVectorFunction rosenbrock =    FunctionFactory.RealFunctionsToVectorFunction(rosenbrock1, rosenbrock2);
Visual Basic CopyCode imageCopy Code
Function Rosenbrock1(x As Vector) As Double 
    Return 1 - x(0)
End Function 
Function Rosenbrock2(x As Vector) As Double 
    Return x(1) - x(0)*x(0)
End Function 
Dim MultivariateRealFunction As rosenbrock1 = _
    New MultivariateRealFunction(AddressOf Rosenbrock1)
Dim MultivariateRealFunction As rosenbrock2 = _
    New MultivariateRealFunction(AddressOf Rosenbrock2)
Dim rosenbrock As MultivariateVectorFunction = _
    FunctionFactory.RealFunctionsToVectorFunction(rosenbrock1, rosenbrock2)

One or more MultivariateVectorFunction or FastMultivariateVectorFunction delegates can be combined to create a FastMultivariateMatrixFunction delegate using the VectorFunctionsToFastMatrixFunction and FastVectorFunctionsToFastMatrixFunction methods. One application of these methods is to combine the gradients of a series of individual multivariate vector functions into the Jacobian of the corresponding system of nonlinear equations.

Up: General Classes Next: Numerical Algorithms Support Classes Previous: Elementary Functions Contents

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