我在一篇随笔“画函数图形的C#程序,兼论一个病态函数”中提到:
“空间/IV”在该随笔的评论中指出:
经研究,我写了一个动态地生成用户输入的函数表达式的类(class Expression),表达式使用 C# 语法,可带一个的自变量(x),其自变量和值均为“double”类型。下面是测试程序的运行结果:
其中最后一个例子就是我在随笔“画函数图形的C#程序,兼论一个病态函数”的下列函数的计算结果:
可以看出,而这函数在任何基于数值 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);
}
}
}
}
// 编译方法: 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 + "\"): \n";
foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n";
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 });
}
}
}
// 表达式使用 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 + "\"): \n";
foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n";
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 });
}
}
}
在这里向 CSDN 论坛的“LoveCherry(论成败,人生豪迈;大不了,重头再来!^_^) ”表示感谢,我的程序就是在他的程序的基础上发展而来的。