使用表达式树

时间:2016-01-12 13:15:26

标签: c# math expression-trees func polynomials

TL; DR

如何使用系数数组建立表达式并将其转换为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 ());
 }

你们可能很明显我做错了什么;我搞砸了一下,直到我得到了一些我觉得应该工作的东西,但它没有。

目标是使序列像数组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);

结果

  

未处理的异常:System.InvalidOperationException:二进制文件   没有为类型定义operator Add   &#39; {System.Func {1}} 2 [System.Int32,System.Double]&#39 ;.在   System.Linq.Expressions.Expression.GetUserDefinedBinaryOperatorOrThrow   (ExpressionType binaryType,System.String名称,   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   在System.Linq.Expressions.Expression.Add   (System.Linq.Expressions.Expression left,   System.Linq.Expressions.Expression right,System.Reflection.MethodInfo   方法)[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   在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   在Sequence.SequenceHelper.CreatePolynomialSequence(System.Double []   coeff)[0x00110] in   /Users/Knoble/MonoProjects/Sequences/Sequence/SequenceHelper.cs:88
  在Sequence.Test.Main()[0x0001f]中   /Users/Knoble/MonoProjects/Sequences/Sequence/Test.cs:53 [ERROR]   致命的不受欢迎的异常:System.InvalidOperationException:The   二进制运算符Add没有为类型定义   &#39; {System.Func {1}} 2 [System.Int32,System.Double]&#39 ;.在   System.Linq.Expressions.Expression.GetUserDefinedBinaryOperatorOrThrow   (ExpressionType binaryType,System.String名称,   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   在System.Linq.Expressions.Expression.Add   (System.Linq.Expressions.Expression left,   System.Linq.Expressions.Expression right,System.Reflection.MethodInfo   方法)[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   在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   在Sequence.SequenceHelper.CreatePolynomialSequence(System.Double []   coeff)[0x00110] in   /Users/Knoble/MonoProjects/Sequences/Sequence/SequenceHelper.cs:88
  在Sequence.Test.Main()[0x0001f]中   /Users/Knoble/MonoProjects/Sequences/Sequence/Test.cs:53   申请被信号终止:SIGHUP

4 个答案:

答案 0 :(得分:6)

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

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运行到零。)

答案 1 :(得分:3)

您需要解决三件事:

  1. e.Body中使用e代替Add

  2. 对所有内容使用相同的参数对象。这有点棘手:x中的Expression.Parameter(typeof (int), "x");x中的e = x => 0x中的x => c * Math.Pow (x, pow) 不同参数。

  3. 在循环中创建pow的副本。否则,捕获pow并且对所有系数使用相同(最终)的pow值。

  4. 在下面的代码示例中,我通过使用内部表达式的参数调用新表达式来解决第二个问题。另一种选择是手工构建x => c * Math.Pow(x, pow)而不是使用C#lambda表达式或统一参数,如this question中所述。

    static void Main(string[] args)
    {
        var seq = CreatePolynomialSequence(1, 2, 3);
        Console.WriteLine(seq.Invoke(1)); // yields 6 = 1 + 2 + 3
        Console.WriteLine(seq.Invoke(2)); // yields 11 = 1*4 + 2*2 + 3
    }
    
    public static Func<int, double> CreatePolynomialSequence(params double[] coeff)
    {
        Expression<Func<int, double>> e = x => 0;
        double pow = 0;
    
        for (int i = coeff.Length - 1; i >= 0; i--)
        {
            var p = e.Parameters[0];
            double c = coeff[i];
            var _pow = pow; // avoid closing over the outer variable
            var next = (Expression<Func<int, double>>)(x => c * Math.Pow(x, _pow));
            var nextInvoked = Expression.Invoke(next, p);
    
            e = Expression.Lambda<Func<int, double>>(Expression.Add(e.Body, nextInvoked), p);
            pow++;
        }
        return e.Compile();
    }
    

答案 2 :(得分:3)

在@Heinzi回答的基础上,以下是如何使用CreatePolynomialExpression方法手动构建整个表达式树的方法:

public static Expression<Func<int, double>> CreatePolynomialExpression(params double[] coeff)
{
    if (coeff.Length == 0)
        return x => 0;

    double pow = 1;

    var x_param = Expression.Parameter(typeof(int), "x");

    Expression expression = Expression.Constant(coeff[coeff.Length - 1]);

    for (int i = coeff.Length - 2; i >= 0; i--)
    {
        Expression sub_expression =
            Expression.Multiply(
                Expression.Constant(coeff[i]),
                Expression.Power(
                    Expression.Convert(x_param, typeof(double)),
                    Expression.Constant(pow)));

        expression = Expression.Add(expression , sub_expression);

        pow++;
    }

    return Expression.Lambda<Func<int, double>>(expression, x_param);
}

答案 3 :(得分:1)

另一个构建器with fiddle,它不使用捕获的变量和Math.Pow,因此工作得更快

 public static Func<int, double> CreatePolynomialSequence (params double[] coeff)
 {              
     // polynom builder
     // double argument
     var y = Expression.Variable(typeof(double), "y");  

     // func result
     var res = Expression.Variable(typeof(double), "res");       

     var expr = Expression.Assign(res, Expression.Constant(coeff[0]));

     // build polynom in format: ((a*x+b)*x+c)*x+d  <=>  a*x^3 + b*x^2 + c*x + d
     for (int i = 1; i < coeff.Length; i++)
     {
         expr = Expression.Add
                (
                    Expression.Multiply(expr, y), 
                    Expression.Constant(coeff[i]) 
                );
     }       

     // function body
     var x = Expression.Variable(typeof(int), "x"); 
     var block = Expression.Block
         (
             new ParameterExpression[]{ y, res },  // local variables
             new Expression[]
             {
                 // cast int argument to double
                 Expression.Assign(y, Expression.Convert(x, typeof(double))),
                 //compute result
                 expr
             }
        );

     return Expression.Lambda<Func<int, double>>(block, x).Compile();
 }