Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a function to a function? is functors/function objects avaiable in VB2010?

Tags:

vb.net

functor

I want to make an numerical integration method with takes in an analytic function and integrate it over a specific interval. For the numerical integration procedure I want to use some procedures in nr.com. The problem is that these are programmed in C++ and they uses functors to pass a function to the integration method. How can I do this in VB 2010?

I want to initialize the function (i.e. set a=1,b=0 for function y(x)=a*x+b) and then pass the function to the integration method. Then when the integration method call the function it only calls the function with one parameter (i.e. x since a,b is already set)

What is the best way to do this in VB2010? I want to make a general integration method where I can pass any single valued function and integration limits.

I have just started using VB, and from what I have found so far it seems like the tools you have is - to us a delegate for the function - to use a lambda expression for the function - send a pointer/adressOf - to create a function class/structure and submit this to the function

As for now I am most inclined to create a function-class. But I am not really sure how. F.ex. I make different classes for each "uniqe function" I want to integrate, but how can I pass them to the integration function when I need to specify the argument type in the integration-function-call?

This seems like a basic problem which applies to many Math operations, so I think it would be very useful to clarify this.

like image 263
drT Avatar asked Oct 12 '11 15:10

drT


1 Answers

Sorry for the longer code chunks, but I wanted to demonstrate the different options available to you with lambdas and anonymous functions.

First we'll create some basic functions to play with...

'Solves a basic linear equation y(x) = ax + b, given a, b, and x.
Function Linear(a As Double, b As Double, x As Double) As Double
    Return a * x + b
End Function

'Return the inverse of a number (i.e. y(x) = -x)
Function Inverse(x As Double) As Double
    Return -x
End Function

And a function that takes a function.

'To help differentiate the type of the parameter from the return type,
'I'm being generic with the return type. This function takes any function 
'that takes a double and returns some generic type, T.
Public Function EvalEquation(Of T)(x As Double, equation As Func(Of Double, T)) As T
    Return equation(x)
End Function

And finally, we'll use it!

'The closest thing to a functor is probably the AddressOf keyword.
For x = 0 To 10
    Dim answer = EvalEquation(x, AddressOf Inverse)
    'Do something
Next

But AddressOf has some limitations... EvalEquationForX expects a function that takes just one parameter, therefore I can't simply use AddressOf, since I can't pass the extra parameters. However, I can dynamically create a function which can do that for me.

For x = 0 To 10
  Dim answer = EvalEquation(x, Function(x)
                                   Dim a = 1
                                   Dim b = 0
                                   Return Linear(a, b, x)
                               End Function)
  'Do something
Next

I should note that you can define a Func(Of T1, T2, T3, T4,... TResult), so you could create a function that could take two parameters and use that instead.

Public Function EvalEquationWithTwoParameters(Of T)(
    a As Double, b As Double, x As Double,
    equation As Func(Of Double, Double, Double, T)) As T

    Return equation(a, b, x)
End Function

And use it like this:

For x = 0 To 10
    Dim answer = EvalEquationWithTwoParameters(1, 0, x, AddressOf Linear)
    'Do something
Next

Hope that helps!

like image 84
Jeff B Avatar answered Sep 18 '22 11:09

Jeff B