C# BrentSearch从字符串输入中查找根、最小值和最大值

C# BrentSearch从字符串输入中查找根、最小值和最大值,c#,search,accord.net,C#,Search,Accord.net,我正在为学校的项目建造科学计算器,必须为[a,b]中x,y的函数实现二次方程 因此,我正在使用Accord.Net,并且已经成功地使用了它,但是我想自动化这个过程 我有以下代码: Func<double, double> function = x => x * x * x + 2 * x * x - 10 * x; Accord.Math.Optimization.BrentSearch search = new Accord.Math.Optimization

我正在为学校的项目建造科学计算器,必须为[a,b]中x,y的函数实现二次方程 因此,我正在使用Accord.Net,并且已经成功地使用了它,但是我想自动化这个过程

我有以下代码:

Func<double, double> function = x => x * x * x + 2 * x * x - 10 * x;
        Accord.Math.Optimization.BrentSearch search = new Accord.Math.Optimization.BrentSearch(function, -4, 3);
        double max = search.Maximize();
        double min = search.Minimize();
        double root = search.FindRoot();
Func function=x=>x*x*x+2*x*x-10*x;
Accord.Math.Optimization.BrentSearch search=新的Accord.Math.Optimization.BrentSearch(函数-4,3);
double max=search.Maximize();
double min=search.Minimize();
双根=search.FindRoot();
但我需要这样做:

string temp = Input.Text.ToString();
        Func<double, double> function = x => temp; //doesn't want string, and BrentSearch wants func<double,double>
        Accord.Math.Optimization.BrentSearch search = new Accord.Math.Optimization.BrentSearch(function, -4, 3);
        double max = search.Maximize();
        double min = search.Minimize();
        double root = search.FindRoot();
string input = "x * x";  // Or however you get the input
Func<double, double> myFunction = CompileFunction(input);
string temp=Input.Text.ToString();
Func function=x=>temp//不需要字符串,BrentSearch需要func
Accord.Math.Optimization.BrentSearch search=新的Accord.Math.Optimization.BrentSearch(函数-4,3);
double max=search.Maximize();
double min=search.Minimize();
双根=search.FindRoot();
问题是Func不接受字符串,而BrentSearch想要Func

此外,输入来自文本框,用户手动输入功能


谢谢你

这不是一项简单的任务。实际上,您正在尝试将用户输入转换为C代码——这与C编译器的功能基本相同

如果您想尝试一下,可以利用C#编译器为您进行解析

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Text;
using Microsoft.CSharp;

private static Func<double, double> CompileFunction(string expression)
{
    StringBuilder sourceBuilder = new StringBuilder();
    sourceBuilder.AppendLine("using System;");
    sourceBuilder.AppendLine("using System.Linq.Expressions;");
    sourceBuilder.AppendLine("class ExpressionGenerator{");
    sourceBuilder.AppendLine("public static Func<double, double> Generate(){");
    sourceBuilder.AppendLine("return x => " + expression + ";");
    sourceBuilder.AppendLine("}}");


    Dictionary<string, string> providerOptions = new Dictionary<string, string>();
    providerOptions.Add("CompilerVersion", "v3.5");
    CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
    CompilerParameters parameters = new CompilerParameters(new[] { "System.Core.dll" });
    CompilerResults results = provider.CompileAssemblyFromSource(parameters, sourceBuilder.ToString());
    return (Func<double, double>)results.CompiledAssembly.GetType("ExpressionGenerator").GetMethod("Generate").Invoke(null, null);
}
使用系统;
使用System.CodeDom.Compiler;
使用System.Collections.Generic;
使用系统文本;
使用Microsoft.CSharp;
私有静态Func编译函数(字符串表达式)
{
StringBuilder sourceBuilder=新的StringBuilder();
AppendLine(“使用系统;”);
AppendLine(“使用System.Linq.Expressions;”);
AppendLine(“类表达式生成器{”);
AppendLine(“publicstaticfunc Generate(){”);
AppendLine(“返回x=>”+表达式+“;”);
AppendLine(“}}”);
Dictionary providerOptions=新建字典();
添加(“CompilerVersion”、“v3.5”);
CSharpCodeProvider provider=新的CSharpCodeProvider(providerOptions);
CompilerParameters参数=新的CompilerParameters(新[]{“System.Core.dll”});
CompilerResults results=provider.compileasemblyFromSource(参数,sourceBuilder.ToString());
return(Func)results.CompiledAssembly.GetType(“ExpressionGenerator”).GetMethod(“Generate”).Invoke(null,null);
}
您可以这样调用此函数:

string temp = Input.Text.ToString();
        Func<double, double> function = x => temp; //doesn't want string, and BrentSearch wants func<double,double>
        Accord.Math.Optimization.BrentSearch search = new Accord.Math.Optimization.BrentSearch(function, -4, 3);
        double max = search.Maximize();
        double min = search.Minimize();
        double root = search.FindRoot();
string input = "x * x";  // Or however you get the input
Func<double, double> myFunction = CompileFunction(input);
string input=“x*x”//或者你如何得到输入
Func myFunction=编译函数(输入);

您仍然需要处理所提供字符串中的语法错误等问题。

是的,这很有效。。最后,我将我的函数放入字符串中,在您的帮助下,现在可以正确地获得根了!谢谢