如何将函数传递给函数? VB2010 中有函子/函数对象吗?
我想制定一种数值积分方法,采用分析函数并将其在特定间隔内积分。对于数值积分过程,我想使用 nr.com 中的一些过程。问题是这些是用 C++ 编程的,并且它们使用函子将函数传递给积分方法。我怎样才能在 VB 2010 中做到这一点?
我想初始化该函数(即为函数 y(x)=a*x+b 设置 a=1,b=0),然后将该函数传递给积分方法。然后,当集成方法调用该函数时,它仅调用带有一个参数的函数(即 x,因为 a、b 已设置)
在 VB2010 中执行此操作的最佳方法是什么? 我想制定一种通用的积分方法,可以传递任何单值函数和积分限制。
我刚刚开始使用 VB,从目前为止我所发现的来看,您拥有的工具似乎是 - 给我们一个代表该功能的人 - 对函数使用 lambda 表达式 - 发送一个指针/地址 - 创建一个函数类/结构并将其提交给函数
目前我最倾向于创建一个函数类。但我不太确定如何。 F. 前。我为每个要集成的“uniqe 函数”创建不同的类,但是当我需要在集成函数调用中指定参数类型时,如何将它们传递给集成函数?
这似乎是一个适用于许多数学运算的基本问题,所以我认为澄清这一点非常有用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉代码块较长,但我想通过 lambda 表达式和匿名函数演示可用的不同选项。
首先,我们将创建一些基本函数来使用......
以及一个接受函数的函数。
最后,我们将使用它!
但 AddressOf 有一些限制......
EvalEquationForX 需要一个只接受一个参数的函数,因此我不能简单地使用 AddressOf,因为我无法传递额外的参数。但是,我可以动态创建一个可以为我做到这一点的函数。
我应该注意,您可以定义一个 Func(Of T1, T2, T3, T4,... TResult),这样您就可以创建一个可以接受两个参数的函数并使用它。
并像这样使用它:
希望有帮助!
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...
And a function that takes a function.
And finally, we'll use it!
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.
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.And use it like this:
Hope that helps!
查看
代表
。您应该使用要调用的函数的签名来定义委托。 “接受另一个函数”的函数应该有一个您定义的委托类型的参数。然后,您可以创建委托实例,传递实际函数的地址,并通过参数将委托实例传递给函数。
一个快速而肮脏的例子。
您还应该查看
Lambdas
或匿名函数
作为定义函数调用的替代方法,而无需专用的命名函数。Check out
delegates
.You should define a delegate with the signature of the function you want to call. The function "that takes another function" should have a parameter of the type of delegate you defined. You can then create an instance of the delegate, passing
addressof
the actual function, and pass the delegate instance in to the function via the parameter.A quick and dirty example.
You should also check out
Lambdas
orAnonymous functions
as an alternate way of defining the function call without having a dedicated named function.