Extreme Optimization > QuickStart Samples > Function Delegates QuickStart Sample (C#)

Extreme Optimization QuickStart Samples

Function Delegates 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);
        }
    }
}
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