How to pass a function to a function? - functors / functions available in VB2010?

I want to make a method of numerical integration with taking an analytic function and integrate it over a certain interval. For the numerical integration procedure, I want to use some procedures at nr.com . The problem is that they are programmed in C ++, and they use functors to pass functions to the integration method. How to do it in VB 2010?

I want to initialize a function (i.e. set a = 1, b = 0 for the function y (x) = a * x + b), and then pass the function to the integration method. Then, when the integration method calls the function, it calls the function with only one parameter (i.e., X, since a, b are 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 values ​​and limits of integration.

I just started using VB, and from what I have found so far, it looks like you have tools - a function delegate to us - use a lambda expression for the function - send a pointer / addressOf - create a class / structure for the function and pass it the functions

At the moment, I tend to create a class of functions. But I'm not quite sure how to do this. F.ex. I make different classes for each "uniqe function" that I want to integrate, but how do I pass it to the integration function when I need to specify the type of the argument in the function-function-call?

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

+6
source share
2 answers

Sorry for the longer snippets of code, but I wanted to demonstrate the various options available to you using lambdas and anonymous functions.

First we will 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 (ie 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 will 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 only one parameter, so I cannot just use AddressOf, since I cannot pass additional parameters. However, I can dynamically create a function that can do this 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 Func(Of T1, T2, T3, T4,... TResult) so that you can create a function that could take two parameters and use them 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 as follows:

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

Hope this helps!

+9
source

Check out delegates .

You must define a delegate with the signature of the function you want to call. The function "that takes another function" must have a parameter of the delegate type you define. You can then instantiate the delegate by passing addressof actual function and pass the delegate instance to the function through a parameter.

A quick and dirty example.

 Public Class Test Public Delegate Function MyDelegate(Param1 As Integer) As Integer Public Function DoSomethingWithParam1(Param1 As Integer) As Integer Return Param1 + 1 End Function Public Sub ThisFunctionTakesADelegate(f As MyDelegate) Dim result As Integer = f.Invoke(2) End Sub Public Sub main() Dim f As New MyDelegate(AddressOf DoSomethingWithParam1) ThisFunctionTakesADelegate(f)'pass the function "DoSomethingWithParam1" as a parameter to "ThisFunctionTakesADelegate" End Sub End Class 

You should also check out Lambdas or Anonymous functions as an alternative way to define a function call without using an assigned named function.

+4
source

Source: https://habr.com/ru/post/899142/


All Articles