gpt4 book ai didi

c# - 使用表达式树构建 Func 多项式

转载 作者:行者123 更新时间:2023-11-30 13:21:53 27 4
gpt4 key购买 nike

长话短说

如何使用系数数组构建表达式并将其转换为 Func<int, double> ?有没有比表达式树更好的方法?


我有一个使用 Func<int, double> formula 构造的不可变序列类型用于为序列 A 生成术语 An。我开始构建一个辅助类来构建具有一些简单参数的通用数学公式:

public static Sequence CreateLinearSequence (double m, double b)
{ return new Sequence (n => m * n + b); }

我为常数序列、对数和简单多项式(线性、二次、三次和四次)构建了标准方法,但我想使用 params 扩展它以支持任意数量的项关键字。

这是我的方法:

 public static Sequence CreatePolynomialSequence (params double[] coeff)
{
Expression<Func<int, double>> e = x => 0;
double pow = 0;

for (int i = coeff.Length - 1; i >= 0; i--)
{
double c = coeff[i];
var p = Expression.Parameter (typeof (int), "x");
e = Expression.Lambda<Func<int, double>> (
Expression.Add (
e,
(Expression<Func<int, double>>)(x => c * Math.Pow (x, pow))
),
p);
pow++;
}
return new Sequence (e.Compile ());
}

你们可能很明显我做错了什么;我弄乱了一些东西,直到我觉得应该可以工作,但事实并非如此。

目标是让 Sequence 像这样为数组工作 double[] coeff = {a,b,c,d,e,f,g,h}

x => h + gx + fx^2 + ex^3 + dx^4 + cx^5 + bx^6 + ax^7使用适当的 Math.Pow(x, exponent)电话。

运行

var s2 = SequenceHelper.CreatePolynomialSequence (new[] { 1d, 2 });
Console.WriteLine ("s2: " + s2);

结果

Unhandled Exception: System.InvalidOperationException: The binary operator Add is not defined for the types 'System.Func2[System.Int32,System.Double]' and
'System.Func
2[System.Int32,System.Double]'. at System.Linq.Expressions.Expression.GetUserDefinedBinaryOperatorOrThrow (ExpressionType binaryType, System.String name, System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, Boolean liftToNull) [0x0004a] in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/BinaryExpression.cs:658 at System.Linq.Expressions.Expression.Add (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Reflection.MethodInfo method) [0x00057] in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/BinaryExpression.cs:1409 at System.Linq.Expressions.Expression.Add (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right) [0x00000] in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/BinaryExpression.cs:1390 at Sequence.SequenceHelper.CreatePolynomialSequence (System.Double[] coeff) [0x00110] in /Users/Knoble/MonoProjects/Sequences/Sequence/SequenceHelper.cs:88
at Sequence.Test.Main () [0x0001f] in /Users/Knoble/MonoProjects/Sequences/Sequence/Test.cs:53 [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: The binary operator Add is not defined for the types 'System.Func2[System.Int32,System.Double]' and
'System.Func
2[System.Int32,System.Double]'. at System.Linq.Expressions.Expression.GetUserDefinedBinaryOperatorOrThrow (ExpressionType binaryType, System.String name, System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, Boolean liftToNull) [0x0004a] in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/BinaryExpression.cs:658 at System.Linq.Expressions.Expression.Add (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Reflection.MethodInfo method) [0x00057] in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/BinaryExpression.cs:1409 at System.Linq.Expressions.Expression.Add (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right) [0x00000] in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/BinaryExpression.cs:1390 at Sequence.SequenceHelper.CreatePolynomialSequence (System.Double[] coeff) [0x00110] in /Users/Knoble/MonoProjects/Sequences/Sequence/SequenceHelper.cs:88
at Sequence.Test.Main () [0x0001f] in /Users/Knoble/MonoProjects/Sequences/Sequence/Test.cs:53 The application was terminated by a signal: SIGHUP

最佳答案

我对这个问题和所有三个答案感到困惑;如果您打算做的只是将它们编译成委托(delegate),为什么还要搞乱表达式树?直接返回委托(delegate)即可!

public static Func<double, double> CreatePolynomialFunction (params double[] coeff)
{
if (coeff == null) throw new ArgumentNullException("coeff");
return x =>
{
double sum = 0.0;
double xPower = 1;
for (int power = 0; power < coeff.Length; power += 1)
{
sum += xPower * coeff[power];
xPower *= x;
}
return sum;
};
}

完成。不需要弄乱表达式树。

(我注意到我假设数组中的第 n 项是第 n 个系数;显然你在数组中向后列出了你的系数。这似乎容易出错,但如果那是你想要的那么不难修改这个答案运行从 Length-1 到零的循环 down。)

关于c# - 使用表达式树构建 Func<int, double> 多项式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34744721/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com