用于代数简化和求解的 C# 库

发布于 2024-11-25 04:59:39 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流星番茄 2024-12-02 04:59:39

我已成功调用 SymPy 以从 C# 完成此操作。 SymPy 提供了一个相对强大的简化函数,我已经取得了很好的成功。现在我还不完全确定如何很好地打包它(不必安装ironpython),甚至不知道直接移植代码有多困难。

  1. 安装 IronPython
  2. 获取 SymPy
  3. 将这些资源添加到您的项目中
    • IronPython.dll
    • IronPython.Modules.dll
    • Microsoft.Dynamic.dll
    • Microsoft.Scripting.dll
  4. C#代码如下:

    var engine = Python.CreateEngine();
    var paths = engine.GetSearchPaths();
    paths.Add(@"c:\program files (x86)\ironpython 2.7\lib");
    paths.Add(@"c:\Development\sympy");
    引擎.SetSearchPaths(路径);
    
    // 简化表达式
    var expr = "0 + 1 * 1 * (x - 2) / (1 - 2) * (x - 3) / (1 - 3) * (x - 4) / (1 - 4) + 8 * 1 * (x - 1) / (2 - 1) * (x - 3) / (2 - 3) * (x - 4) / (2 - 4) + 27 * 1 * (x - 1) / (3 - 1) * (x - 2) / (3 - 2) * (x - 4) / (3 - 4) + 64 * 1 * (x - 1) / (4 - 1) * (x - 2) / (4 - 2) * (x - 3) / (4 - 3)";
    
    var 作用域 = engine.CreateScope();
    var script = engine.CreateScriptSourceFromString(@"
    从 sympy 导入 *
    导入CLR
    从系统导入字符串
    
    expr = 简化('" + expr + @"')
    结果 = clr.Convert(expr, String)
    ”);
    
    脚本.执行(范围);
    
    // 打印“x**3”
    Console.WriteLine(scope.GetVariable("结果"));
    

I've managed to successfully call into SymPy to get this done from C#. SymPy provides a relatively robust simplify function that I've had good success with. Now I'm not entirely sure how to package this nicely yet (not having to install ironpython), or even how hard a direct port of the code might be.

  1. Install IronPython
  2. Get SymPy
  3. Add these resources to your project
    • IronPython.dll
    • IronPython.Modules.dll
    • Microsoft.Dynamic.dll
    • Microsoft.Scripting.dll
  4. C# code as follows:

    var engine = Python.CreateEngine();
    var paths = engine.GetSearchPaths();
    paths.Add(@"c:\program files (x86)\ironpython 2.7\lib");
    paths.Add(@"c:\Development\sympy");
    engine.SetSearchPaths(paths);
    
    // expression to simplify
    var expr = "0 + 1 * 1 * (x - 2) / (1 - 2) * (x - 3) / (1 - 3) * (x - 4) / (1 - 4) + 8 * 1 * (x - 1) / (2 - 1) * (x - 3) / (2 - 3) * (x - 4) / (2 - 4) + 27 * 1 * (x - 1) / (3 - 1) * (x - 2) / (3 - 2) * (x - 4) / (3 - 4) + 64 * 1 * (x - 1) / (4 - 1) * (x - 2) / (4 - 2) * (x - 3) / (4 - 3)";
    
    var scope = engine.CreateScope();
    var script = engine.CreateScriptSourceFromString(@"
    from sympy import *
    import clr
    from System import String
    
    expr = simplify('" + expr + @"')
    result = clr.Convert(expr, String)
    ");
    
    script.Execute(scope);
    
    // prints "x**3"
    Console.WriteLine(scope.GetVariable("result"));
    
ゃ人海孤独症 2024-12-02 04:59:39

相关的SO问题中可以找到一系列答案。尽管没有,除了 mathdotnet 之外,都落在符号的交叉点上(您在上面要求的那种简化) )、轻量级以及 .Net 上的可访问性。

我看到您已经找到了mathdotnet 论坛。请注意,它的一些开发人员是 SO 用户:

这可能会补充您所要求的支持。

There is a flurry of answers to be found in a related SO question. Though none, other that mathdotnet, fall at the intersection of symbolics (the kind of simplificaiton you are asking for above), lightweight-ishness, and accessibility on .Net.

I see you have already found the mathdotnet forum. Note a couple of its developers are SO users:

That might supplement the support you ask for.

书间行客 2024-12-02 04:59:39

Symbolism 是一个实现代数表达式自动简化的 C# 库。

根据您的示例表达式,以下程序:

var x = new Symbol("x");

(5 * x * (500 / (x ^ 2) * (sqrt(3.0) / 4) + 1) + 2 * (x ^ 2) + (sqrt(3.0) / 2) * (x ^ 2))
    .AlgebraicExpand()
    .Disp();

在控制台上显示此内容:

1082.5317547305483 / x + 5 * x + 2.8660254037844384 * (x ^ 2)

Symbolism is a C# library which implements automatic simplification of algebraic expressions.

Going with your example expression, the following program:

var x = new Symbol("x");

(5 * x * (500 / (x ^ 2) * (sqrt(3.0) / 4) + 1) + 2 * (x ^ 2) + (sqrt(3.0) / 2) * (x ^ 2))
    .AlgebraicExpand()
    .Disp();

displays this at the console:

1082.5317547305483 / x + 5 * x + 2.8660254037844384 * (x ^ 2)
蓝天 2024-12-02 04:59:39

您是否尝试过创建一些简单的类来实现调车场算法(逆波兰表示法)使用 堆栈处理后缀表示法处理堆栈处理?

Have you tried creating a few simple classes implementing the Shunting Yard Algorithm(Reverse Polish Notation) than process the postfix notation using stack processingstack processing?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文