我有一个数值分析程序,为简单起见,它计算类似于以下的算法:
y = ax^3 + bx^2 + cx + d;
我在运行时计算 a、b、c、d 的值,并希望将以下等效项作为 Func
传递代码>.我可以在其中设置 X 的值,并获取 Y。
y = 12x^3 + 13x^2 + 14x + 15;
其中 12,13,14,15 是运行时计算的数字。
我意识到这可以通过传入一个双精度数组来完成,如下所示:Func
但我试图避免传递常量(可能有很多)。
有什么方法可以在运行时在函数中设置这些数字吗?
(最好不要计算 Func<> 本身的 a、b、c、d 部分?a、b、c、d 的计算占工作量的 80%)
例如:
a = ...
b = ...
c = ...
Func<x, double> {
((const)a) * x^3 + ((const)b) * x^2 + ((const)c) * x + 15;
}`
对于ABCD - 我将评估 10 x。
I have a numerical analysis program which for simplicity calculates an algorithm similar to:
y = ax^3 + bx^2 + cx + d;
I calculate the values of a,b,c,d at runtime and would like to pass the following equivalent as a Func<double, double>
. Where I can set a value for X, and get Y.
y = 12x^3 + 13x^2 + 14x + 15;
Where 12,13,14,15 are the numbers calculated at runtime.
I realise this can be done by passing in a double array, like so: Func<double[], double>
but I am trying to avoid passing around the constants (which can be many).
Is there any way to set these numbers in a func at runtime?
(Preferably without making the calculation of a,b,c,d part of the Func<> itself? The calculation of a,b,c,d is 80% of the work)
E.g.:
a = ...
b = ...
c = ...
Func<x, double> {
((const)a) * x^3 + ((const)b) * x^2 + ((const)c) * x + 15;
}`
For every evaluation of ABCD - I will evaluate 10 x's.
发布评论
评论(5)
我不确定我是否完全明白你在问什么,但你也许可以尝试这样的事情?
在这种情况下,您只需调用
CreateCalculationFunc()
,它将执行一次繁重的计算,并返回一个可重用的Func
来执行你的变量计算。当然,这可以扩展到任意数量的预先计算的常数和多个变量。
I'm not sure if I understand quite what you are asking, but you could try something like this, perhaps?
In this case, you can just make a call to the
CreateCalculationFunc()
, which will do the heavy calculations once, and return a reusableFunc<double,double>
for doing your variable calculations.Of course, this is extensible to any amount of pre-calculated constants and more than one variable.
您不能创建一个包含计算 (
Func
) 方法的类,该方法具有“常量”的属性。然后设置属性并使用对Calculate
方法的引用作为Func
委托:Can't you create a class that contains your calculation (
Func<double,double>
) method that has properties for your "constants". Then you set the properties and use a reference to theCalculate
method as yourFunc<double,double>
delegate:这听起来像是您想要函数式语言所谓的“柯里化”或“部分应用程序”。
给定一个
Func
,您将一一应用这些值并减少参数集。签名的函数
将产生一个带有您可以传递的
。 C# 中柯里化/部分应用程序的相关链接:
或者 - 尝试一下 F# ;)
编辑:从上面的参考站点创建的简短代码示例:
样板代码:
您的代码:
cDefined 现在是一个“知道”的新 Func预先应用的值,可以根据需要传递。我无法为具有超过 4 个参数的委托找到现成的解决方案(即不适合 Func<...> 的东西),但这也应该是可能的。
这样,您就可以像今天一样预先计算一次,但您可以将函数范围缩小到每个消费者的最后一部分。
This sounds like you'd want what the functional languages call "currying" or "partial application".
Given a
Func<a, b, c, d, x, result>
you'd apply the values one by one and reduce the parameter set.would result in a function with the signature
which you can pass on.
Relevant links for currying/partial application in C#:
Or - give F# a try ;)
Edit: A short code sample, created from the reference sites above:
Boilerplate code:
Your Code:
cDefined is now a new Func that "knows" the preapplied values and can be passed around as you like. I couldn't find a readymade solution for delegates with more than 4 parameters (i.e. things that don't fit a Func<...>), but that should be possible as well.
This way you're precalculating once and just the way you do it today, but you can narrow down the function to the last part for every consumer.
有几种不同的 Func<> 。重载,您可以使用一个带有 5 个通用参数,第一个一个是 KeyValuePair:
因此您将 a 和 b 作为 kvp 传递c、d 和 x 作为其他参数。
There are several different Func<> overloads, you can use one with 5 generic parameters, with the first one being a KeyValuePair:
So you pass a and b as the kvp and c,d and x as the other parameters.
事后看来,我实际上可以这样做;
但只是对 Func<> 有点困惑和动作>>因为这是我第一次使用它们!感谢您的帮助。
In hindsight I could actually just do this;
But was just a little confused by Func<> and Action<> as it was my first time using them! Thanks for your help.