Extreme Optimization >
Mathematics Library for .NET >
QuickStart Samples >
FunctionDelegates QuickStart Sample (C#)
Extreme Optimization Mathematics Library for .NET
FunctionDelegates QuickStart Sample (C#)
Illustrates the use of function delegates (Extreme.Mathematics namespace) in
C#.
VB.NET code
Back to QuickStart Samples
using System;
namespace Extreme.Mathematics.QuickStart.CSharp
{
// The DoubleComplex structure resides in the Extreme namespace.
using Extreme;
// The delegate classes reside in the Extreme.Mathematics
// namespace.
using Extreme.Mathematics;
// We also use the Extreme.Mathematics.SpecialFunctions namespace.
using Extreme.Mathematics.SpecialFunctions;
/// <summary>
/// Illustrates the use of function delegates in the
/// Extreme.Mathematics namespace of the Extreme Optimization
/// Mathematics Library for .NET.
/// </summary>
class FunctionDelegates
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// 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:
RealFunction cos =
new RealFunction(Math.Cos);
// Many static methods in the Extreme.Mathematics.SpecialFunctions
// classes are also of this form:
RealFunction gamma =
new RealFunction(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:
BivariateRealFunction atan2 =
new BivariateRealFunction(Math.Atan2);
Console.WriteLine("Atan2(1, 3) = {0}", atan2(1, 3));
// Or the Hypot method from SpecialFunctions.Elementary:
BivariateRealFunction hypot =
new BivariateRealFunction(
ElementaryFunctions.Hypot);
Console.WriteLine("Hypot(3, 4) = {0}", hypot(3, 4));
// A TrivariateRealFunction represents a
// function that takes three 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:
TrivariateRealFunction incompleteBeta =
new TrivariateRealFunction(
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.
ComplexFunction complexCosh =
new ComplexFunction(DoubleComplex.Cosh);
DoubleComplex z = 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 Power
// method is defined below.
ParameterizedRealFunction power =
new ParameterizedRealFunction(Power);
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:
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:
RealFunction seventhPower =
FunctionFactory.RealFromParameterizedRealFunction(
power, 7);
Console.WriteLine("3^7 = {0}", seventhPower(3));
Console.Write("Press Enter key to exit...");
Console.ReadLine();
}
static double Power(int exponent, double x)
{
return ElementaryFunctions.Pow(x, exponent);
}
}
}
Copyright 2004-2008,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual
Studio.NET, and the Visual Studio Logo are registered trademarks of Microsoft Corporation