Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# library for algebra simplification and solving [closed]

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

like image 977
Dan W Avatar asked Jul 17 '11 03:07

Dan W


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

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"));
    
like image 63
Damon Achey Avatar answered Sep 21 '22 11:09

Damon Achey


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)
like image 44
dharmatech Avatar answered Sep 17 '22 11:09

dharmatech