protected override Expression ApplyTransform(Expression x, MatchContext Matched) { return(result.Substitute(Matched, true).Evaluate()); }
/// <summary> /// Evaluate an expression. /// </summary> /// <param name="f">Expression to evaluate.</param> /// <param name="x0">List of variable, value pairs to evaluate the function at.</param> /// <returns>The evaluated expression.</returns> public static Expression Evaluate(this Expression f, IDictionary <Expression, Expression> x0) { return(f.Substitute(x0).Evaluate()); }
protected override Expression VisitBinary(Binary B) { Expression L = Visit(B.Left); Expression R = Visit(B.Right); // Evaluate substitution operators. if (B is Substitute) { return(Visit(L.Substitute(Set.MembersOf(R).Cast <Arrow>()))); } Real?LR = AsReal(L); Real?RR = AsReal(R); // Evaluate relational operators on constants. if (LR != null && RR != null) { switch (B.Operator) { case Operator.Equal: return(Constant.New(LR.Value == RR.Value)); case Operator.NotEqual: return(Constant.New(LR.Value != RR.Value)); case Operator.Less: return(Constant.New(LR.Value < RR.Value)); case Operator.Greater: return(Constant.New(LR.Value <= RR.Value)); case Operator.LessEqual: return(Constant.New(LR.Value > RR.Value)); case Operator.GreaterEqual: return(Constant.New(LR.Value >= RR.Value)); case Operator.ApproxEqual: return(Constant.New( LR.Value == RR.Value || Real.Abs(LR.Value - RR.Value) < 1e-12 * Real.Max(Real.Abs(LR.Value), Real.Abs(RR.Value)))); } } // Evaluate boolean operators if possible. switch (B.Operator) { case Operator.And: if (IsFalse(LR) || IsFalse(RR)) { return(Constant.New(false)); } else if (IsTrue(LR) && IsTrue(RR)) { return(Constant.New(true)); } break; case Operator.Or: if (IsTrue(LR) || IsTrue(RR)) { return(Constant.New(true)); } else if (IsFalse(LR) && IsFalse(RR)) { return(Constant.New(false)); } break; case Operator.Equal: case Operator.ApproxEqual: if (L.Equals(R)) { return(Constant.New(true)); } break; case Operator.NotEqual: if (L.Equals(R)) { return(Constant.New(false)); } break; } return(Binary.New(B.Operator, L, R)); }
/// <summary> /// Evaluate an expression at x = x0. /// </summary> /// <param name="f">Expression to evaluate.</param> /// <param name="x">Variable to evaluate at.</param> /// <param name="x0">Value to evaluate for.</param> /// <returns>The evaluated expression.</returns> public static Expression Substitute(this Expression f, Expression x, Expression x0) { return(f.Substitute(new Dictionary <Expression, Expression> { { x, x0 } })); }
public static Expression Substitute(this Expression f, params Arrow[] x) { return(f.Substitute(x.AsEnumerable())); }
/// <summary> /// Evaluate an expression at x = x0. /// </summary> /// <param name="f">Expression to evaluate.</param> /// <param name="x">Arrow expressions representing substitutions to evaluate.</param> /// <returns>The evaluated expression.</returns> public static Expression Substitute(this Expression f, IEnumerable <Arrow> x) { return(f.Substitute(x.ToDictionary(i => i.Left, i => i.Right))); }