19

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

And 's' would simplify down to: "1082.532/x+5*x+2.866*x^2"

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

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).

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

dharmatech
  • 8,979
  • 8
  • 42
  • 88
Dan W
  • 3,520
  • 7
  • 42
  • 69
  • Your simplification example is a bit inconsistent. On the left you keep the exact value `sqrt` and on the right you round it to a 4 digit decimal. – CodesInChaos Jul 17 '11 at 09:33
  • You want to do arbitrary algebra, and you think standard algebra systems are overkill? Unless you limit the kind the algrebra you are willing to handle, you'll need a lot of what these system offer. A simple desire to be 200Kb is not likely to lead you to good end here. – Ira Baxter Jul 17 '11 at 20:24
  • @dan-w: Did you manage to find a solution for this. – Naresh Jois Apr 07 '14 at 15:08
  • @Naresh Jois: Nothing seemed ideal at the time. Not sure if something recently has cropped up though. – Dan W Apr 08 '14 at 00:26

4 Answers4

11

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"));
    
Damon Achey
  • 182
  • 4
  • 9
  • 1
    I try to duplicate your action, but it failed as it cannot load the sympy module? Any help will be appreciated. – BO KANG Mar 21 '15 at 02:58
4

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)
dharmatech
  • 8,979
  • 8
  • 42
  • 88
4

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.

Community
  • 1
  • 1
Francois G
  • 11,957
  • 54
  • 59
  • Unfortunately, the link to the "related SO question" has been "removed from Stack Overflow for reasons of moderation". – Morris Jul 01 '22 at 15:19
0

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?

Ryan Fisch
  • 2,614
  • 5
  • 36
  • 57
  • 1
    I don't think the parsing part is the problem. There are many math parsers available. Doing the algebra is the problem. – CodesInChaos Jul 19 '11 at 21:03