C#库,用于代数简化和求解 [英] C# library for algebra simplification and solving

查看:158
本文介绍了C#库,用于代数简化和求解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网络上有很多代数求解器和简化器(例如,algebra.com上的代名词).但是,我正在寻找可以作为更大项目的一部分插入C#的东西(我正在制作自己的计算器,但显然我会征求许可等).

There are quite a few algebra solvers and simplifiers on the web (for example, the decent one at algebra.com). However, I'm looking for something I can plug into C# as part of a larger project (I'm making my own calculator, but obviously I'd ask permission etc.).

理想情况下,我会使用以下代码:

Ideally, I'd use code like:

String s = MathLib.Simplify("5x*(500/x^2*(sqrt(3)/4)+1)+2x^2+(sqrt(3)/2)*x^2");

"s"将简化为:"1082.532/x+5*x+2.866*x^2"

(3dp精度,但如有需要,可以更改).

(3dp accuracy there, but one could change that if need be).

解决一个特定的变量也很好.我还需要轻巧,快速的东西(例如上述计算,最好在5毫秒左右(包括启动延迟)之内.)

Solving for a particular variable would be nice too. I need something lightweight, and fast too (calculations such as the above would preferably be under 5ms or so including the startup latency).

经过一些研究,像Sage,Octave或Mathematica之类的程序可能会过大(我的应用程序可能只是一个小于200k的小型exe文件). Dotnumerics.com或Mathdotnet.com可能是合适的,但是前者似乎没有提到代数简化,而后者中缺少文档和示例的情况就不那么重要了.我想知道是否还有其他合适的选择.可以在这里找到一个大列表: http://en.wikipedia.org/wiki/Comparison_of_computer_algebra_systems

After some research, programs like Sage, Octave or Mathematica are probably overkill (my app will only be a small <200k exe probably). Dotnumerics.com or Mathdotnet.com may be suitable, but the former doesn't seem to mention algebraic simplification, and the lack of documentation and examples in the latter is a turn off. I'm wondering if there are any appropriate alternatives as well. A large list can be found here: http://en.wikipedia.org/wiki/Comparison_of_computer_algebra_systems

推荐答案

我成功地调用了SymPy,以从C#中完成此操作. SymPy提供了一个相对强大的简化功能,我已经获得了很好的成功.现在,我不完全确定如何将其打包好(不必安装ironpython),甚至不知道直接编写代码的难易程度.

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. 安装 IronPython
  2. 获取 SymPy
  3. 将这些资源添加到您的项目中
    • IronPython.dll
    • IronPython.Modules.dll
    • Microsoft.Dynamic.dll
    • Microsoft.Scripting.dll
  1. Install IronPython
  2. Get SymPy
  3. Add these resources to your project
    • IronPython.dll
    • IronPython.Modules.dll
    • Microsoft.Dynamic.dll
    • Microsoft.Scripting.dll

C#代码如下:

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"));

这篇关于C#库,用于代数简化和求解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆