/// <summary> /// 转换成表达式字符串,自动判断是否加入括号 /// </summary> /// <param name="v"></param> /// <returns></returns> public static string BlockToString(IExpr v) { if (v is ExprToken) { return(v.ToString()); } else { return($"({v.ToString()})"); } }
public static Func <double, double> Lambdify(IExpr expr, string variable) { //if (VariableCount(expr) != 1 || Variables(expr).Distinct().FirstOrDefault() != variable) //{ // Console.WriteLine("Variable: " + variable); // Console.WriteLine("Variable count: " + VariableCount(expr)); // Console.WriteLine("Variables: " + string.Join(", ", Variables(expr).Distinct().ToArray())); // return null; //} return(x => { var subtitution = Subtitute(expr, variable, new Constant(x)); var evaluation = Evaluate(subtitution); if (evaluation is Constant) { return evaluation.ConstantValue(); } else { Console.WriteLine("Lambdafication of the expr: " + expr.ToString() + " was not successful"); Console.WriteLine("After subtitution: " + subtitution.ToString()); Console.WriteLine("After eval: " + evaluation.ToString()); return double.NaN; } }); }
public bool Equals(IExpr other) { if (!(other is ExprValue)) { return(false); } return(other.ToString() == this.ToString()); }
public static Func<double, double> Lambdify(IExpr expr, string variable) { //if (VariableCount(expr) != 1 || Variables(expr).Distinct().FirstOrDefault() != variable) //{ // Console.WriteLine("Variable: " + variable); // Console.WriteLine("Variable count: " + VariableCount(expr)); // Console.WriteLine("Variables: " + string.Join(", ", Variables(expr).Distinct().ToArray())); // return null; //} return x => { var subtitution = Subtitute(expr, variable, new Constant(x)); var evaluation = Evaluate(subtitution); if (evaluation is Constant) return evaluation.ConstantValue(); else { Console.WriteLine("Lambdafication of the expr: " + expr.ToString() + " was not successful"); Console.WriteLine("After subtitution: " + subtitution.ToString()); Console.WriteLine("After eval: " + evaluation.ToString()); return double.NaN; } }; }