📄 functiondelegates.vb
字号:
' 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))
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -