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> /// Solve a differential equation or system of differential equations f for functions y[t], with initial conditions y^(n)[0] = y0. /// </summary> /// <param name="f">Equation or set of equations to solve.</param> /// <param name="y">Functions to solve for.</param> /// <param name="y0">Values for y^(n)[0].</param> /// <param name="t">Independent variable.</param> /// <returns>Expression or set of expressions for y.</returns> public static Expression DSolve(Expression f, Expression y, Expression y0, Expression t) { IEnumerable <Expression> result = Set.MembersOf(f).Cast <Equal>().DSolve(Set.MembersOf(y), Set.MembersOf(y0).Cast <Arrow>(), t); return((f is Set || result.Count() != 1) ? Set.New(result) : result.Single()); }
public static bool DependsOn(this IEnumerable <Expression> f, Expression x) { return(DependsOn(f, Set.MembersOf(x))); }
public static Expression NSolve(Expression f, Expression x) { IEnumerable <Expression> result = Set.MembersOf(f).Cast <Equal>().NSolve(Set.MembersOf(x).Cast <Arrow>()); return((f is Set || result.Count() != 1) ? Set.New(result) : result.Single()); }
/// <summary> /// Check if f is a function of x. /// </summary> /// <param name="f"></param> /// <param name="x"></param> /// <returns>true if f is a function of x.</returns> public static bool DependsOn(this Expression f, Expression x) { return(DependsOn(f, Set.MembersOf(x))); }