Exemple #1
0
 /// <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()})");
     }
 }
Exemple #2
0
        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;
                }
            });
        }
Exemple #3
0
 public bool Equals(IExpr other)
 {
     if (!(other is ExprValue))
     {
         return(false);
     }
     return(other.ToString() == this.ToString());
 }
Exemple #4
0
        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;
                }

            };
        }