[转] 动态地生成用户输入的函数表达式

银河原文动态地生成用户输入的函数表达式(C#)画函数图形的C#程序(改进版)

版本1

这个画函数图形的C#程序有一个严重的缺点,就是函数表达式是直接写的源程序中的,不能象SciLab和Matlab那样交互式地输入。不知道用 System.Reflection.Emit.ILGenerator 类能不能动态地生成用户输入的函数表达式?关于动态地生成用户输入的函数表达式, 看看下面这个帖子说不定有帮助: LINK

经研究,作者写了一个动态地生成用户输入的函数表达式的类(class Expression),表达式使用 C# 语法,可带一个的自变量(x),其自变量和值均为“double”类型。下面是测试程序的运行结果:

C> ExpressionTest
Usage: ExpressionTest expression [ parameters ... ]

C> ExpressionTest Math.PI*Math.E 0
f(x): Math.PI*Math.E
f(0) = 8.53973422267357

C> ExpressionTest Math.Pow(2,x) 0 10 49 50 1024 -1 -1024
f(x): Math.Pow(2,x)
f(0) = 1
f(10) = 1024
f(49) = 562949953421312
f(50) = 1.12589990684262E+15
f(1024) = 正无穷大
f(-1) = 0.5
f(-1024) = 5.562684646268E-309

C> ExpressionTest "double u = Math.PI - x; double pi2 = Math.PI * Math.PI; return 3 * x * x + Math.Log(u * u) / pi2 / pi2 + 1;" 3.13 3.14 3.15 3.16 3.1416
f(x): double u = Math.PI - x; double pi2 = Math.PI * Math.PI; return 3 * x * x + Math.Log(u * u) / pi2 / pi2 + 1;
f(3.13) = 30.2991811562164
f(3.14) = 30.44652582187
f(3.15) = 30.6693849404716
f(3.16) = 30.8747746902426
f(3.1416) = 30.3662371931734

其中最后一个例子就是我在随笔“画函数图形的C#程序,兼论一个病态函数”的下列函数的计算结果:

实际上这个病态函数是《C数值算法(第二版)》第三章“内插法和外推法”中提到的:

---------------------------------------------------------------------------
可以很容易地构造一些病态函数使内插法失败。例如,考虑函数 
f(x) = 3 * x2 + π-4 * ln[(π-x)2] + 1
它除了 x = π 之外都有定义,而 x = π 时无定义,其它情况,值有正有负。而这函数在任何基于数值 x = 3.13, 3.14, 3.15, 3.16 的插值法,都肯定在 x = 3.1416 处得到一个错误的解,尽管通过这五个点所画的曲线确实相当平滑!(用计算器试试看。)
---------------------------------------------------------------------------

可以看出,而这函数在任何基于数值 x = 3.13, 3.14, 3.15, 3.16 的插值法,在 x = 3.1416 处得到的解肯定在 30.44652582187 和 30.6693849404716 之间,但实际的解应该是 30.3662371931734,所以说作者断言在该处肯定会得到一个错误的解。
下面就是源程序:

// ExpressionTest.cs - 动态生成数学表达式并计算其值的测试程序
// 编译方法: csc ExpressionTest.cs Expression.cs

using System;
using Skyiv.Util;

namespace Skyiv.Test
{
  class ExpressionTest
  {
    static void Main(string [] args)
    {
      try
      {
        if (args.Length > 0)
        {
          Console.WriteLine("f(x): {0}", args[0]);
          Expression expression = new Expression(args[0]);
          for (int i = 1; i < args.Length; i++)
          {
            double x = double.Parse(args[i]);
            Console.WriteLine("f({0}) = {1}", x, expression.Compute(x));
          }
        }
        else Console.WriteLine("Usage: ExpressionTest expression [ parameters  ]");
      }
      catch (Exception ex)
      {
        Console.WriteLine("错误: " + ex.Message);
      }
    }
  }
} 
// Expression.cs - 动态生成数学表达式并计算其值
// 表达式使用 C# 语法,可带一个的自变量(x)。
// 表达式的自变量和值均为(double)类型。
// 使用举例:
//   Expression expression = new Expression("Math.Sin(x)");
//   Console.WriteLine(expression.Compute(Math.PI / 2));
//   expression = new Expression("double u = Math.PI - x;" +
//     "double pi2 = Math.PI * Math.PI;" +
//     "return 3 * x * x + Math.Log(u * u) / pi2 / pi2 + 1;");
//   Console.WriteLine(expression.Compute(0));

using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.Text;

namespace Skyiv.Util
{
  sealed class Expression
  {
    object instance;
    MethodInfo method;
    
    public Expression(string expression)
    {  
      if (expression.IndexOf("return") < 0) expression = "return " + expression + ";";
      string className = "Expression";
      string methodName = "Compute";
      CompilerParameters p = new CompilerParameters();
      p.GenerateInMemory = true;
      CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(p, string.
        Format("using System;sealed class {0}{{public double {1}(double x){{{2}}}}}",
        className, methodName, expression));
      if(cr.Errors.Count > 0)
      {
        string msg = "Expression("" + expression + ""): 
";
        foreach (CompilerError err in cr.Errors) msg += err.ToString() + "
";
        throw new Exception(msg);
      }
      instance = cr.CompiledAssembly.CreateInstance(className);
      method = instance.GetType().GetMethod(methodName);
    }
    
    public double Compute(double x)
    {
      return (double)method.Invoke(instance, new object [] { x });
    }
  }
}
Expression.cs

作者的更新(改进版)

后来,根据“空间/IV”的评论,我写了个动态生成用户输入的函数表达式的类,用以改进这个画函数图形的C#程序。下面是该程序的运行效果:


可以看到,不但要画的函数的表达式可以由用户动态地输入,而且函数自变量的范围也可以是常量表达式。 下面就是源程序:

// plot.cs: 画函数图形, 编译方法: csc /t:winexe plot.cs Expression.cs
using System;
using System.Drawing;
using System.Windows.Forms;
using Skyiv.Util;

namespace Skyiv.Ben.Plot
{
  sealed class PlotForm : Form
  {
    const int yBase = 24; // 屏幕保留区域的高度

    TextBox tbxX0, tbxX1;  // 函数自变量的取值范围
    TextBox tbxExpression; // 函数的表达式
    
    PlotForm()
    {
      SuspendLayout();
      
      Button btnSubmit = new Button();
      btnSubmit.Text = "刷新";
      btnSubmit.Location = new Point(0, 0);
      btnSubmit.Size = new Size(48, 24);
      btnSubmit.Click += new EventHandler(BtnSubmit_Click);

      tbxX0 = new TextBox();
      tbxX0.Text = "-Math.PI";
      tbxX0.Location = new Point(55, 3);
      tbxX0.Size = new Size(100, 20);

      tbxX1 = new TextBox();
      tbxX1.Text = "Math.PI";
      tbxX1.Location = new Point(160, 3);
      tbxX1.Size = new Size(100, 20);

      tbxExpression = new TextBox();
      tbxExpression.Text = "Math.Sin(x)";
      tbxExpression.Location = new Point(265, 3);
      tbxExpression.Size = new Size(335, 20);
      tbxExpression.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);

      Controls.AddRange(new Control[]{btnSubmit, tbxX0, tbxX1, tbxExpression});
      Text = "Plot";
      BackColor = Color.White;
      ClientSize = new Size(600, 600 + yBase);
      // WindowState = FormWindowState.Maximized;

      ResumeLayout(false);
    }

    // 点击“刷新”按钮时重绘程序主窗口
    void BtnSubmit_Click(object sender, EventArgs e)
    {
      Invalidate();
    }
    
    /*
    // 因为本程序使用 C# 的反射功能动态生成数学表达式并计算其值
    // 所以重画时有点慢,如果你的计算机的速度不是非常快的,
    // 就不要在窗口改变大小时强制重绘,而是通过点击发“刷新”按钮重绘。
    protected override void OnSizeChanged(EventArgs e)
    {
      Invalidate();
      base.OnSizeChanged(e);
    }
    */
    
    protected override void OnPaint(PaintEventArgs e)
    {
      Graphics gc = e.Graphics;
      try
      {
        double x0 = new Expression(tbxX0.Text).Compute(0);
        double x1 = new Expression(tbxX1.Text).Compute(0);
        Size size = ClientSize;
        int i0 = 0;
        int i1 = size.Width - 1;
        int j0 = yBase;
        int j1 = size.Height - 1;
        Pen pen = new Pen(Color.Black, 1);
        gc.DrawLine(pen, i0, j0, i1, j0); // 画图区和保留区的分界线
        double rx = (x1 - x0) / (i1 - i0);
        double y0, y1;
        Expression fx = new Expression(tbxExpression.Text);
        GetFunctionValueRange(fx, x0, rx, i0, i1, out y0, out y1);
        double ry = (y1 - y0) / (j1 - j0);
        Out(gc, 0, "ClientSize: {0}x{1}", i1 - i0 + 1, j1 - j0 + 1);
        Out(gc, 1, "f(x): " + tbxExpression.Text);
        Out(gc, 2, "x:[{0}, {1}] range:{2}", x0, x1, x1 - x0);
        Out(gc, 3, "y:[{0}, {1}] range:{2}", y0, y1, y1 - y0);
        Out(gc, 4, "rx:{0}", 1 / rx);  // 函数自变量每单位值用多少个象素表示
        Out(gc, 5, "ry:{0}", 1 / ry);  // 函数的值每单位值用多少个象素表示
        Out(gc, 6, "r :{0}", rx / ry); // 该值如果小于1表示图形纵向被压扁,反之则被拉伸
        pen.Color = Color.Green;
        int j = j1 + (int)(y0 / ry);
        if (j >= j0 && j <= j1) gc.DrawLine(pen, i0, j, i1, j); // x坐标轴
        int i = i0 - (int)(x0 / rx);
        if (i >= i0 && i <= i1) gc.DrawLine(pen, i, j0, i, j1); // y坐标轴
        pen.Color = Color.Red;
        for (i = i0; i <= i1; i++)
        {
          double x = x0 + (i - i0) * rx;
          double y = fx.Compute(x);
          if (double.IsInfinity(y) || double.IsNaN(y)) continue;
          j = j1 - (int)((y - y0) / ry);
          if (j > j1 || j < j0) continue;
          gc.DrawLine(pen, i, j, i + 1, j); // 画函数的图形
        }
      }
      catch (Exception ex)
      {
        Out(gc, 0, ex.Message);
      }
      base.OnPaint(e);
    }
    
    // 函数值的取值范围
    void GetFunctionValueRange(Expression fx, double x0, double rx, int i0, int i1, out double y0, out double y1)
    {
      y0 = double.MaxValue;
      y1 = double.MinValue;
      for (int i = i0; i <= i1; i++)
      {
        double x = x0 + (i - i0) * rx;
        double y = fx.Compute(x);
        if (double.IsInfinity(y) || double.IsNaN(y)) continue;
        if (y0 > y) y0 = y;
        if (y1 < y) y1 = y;
      }
    }
    
    // 在指定的位置写字符串
    void Out(Graphics gc, int line, string fmt, params object [] args)
    {
      gc.DrawString(string.Format(fmt, args), new Font("Courier New", 10), Brushes.Blue, new PointF(5, yBase + 15 * line));
    }

    static void Main()
    {
      Application.Run(new PlotForm());
    }
  }
}
View Code
原文地址:https://www.cnblogs.com/arxive/p/5810879.html