Search code examples
c++algebra

Algebra Library for C++


I need a C++ algebra library in order to use in my project. At the beginning I thought I can write one, but then I realized I was unsuccessfully trying to reinvent the wheel and wasting my precious time.

For arithmetic issues I found GMP Library (you know, for unlimited arithmetic calculations), and tools for other kind of tasks (standard C++ library seemed quite enough for pseudo-random number generation). However, I couldn't find a suitable one for algebraic works.

There are linear algebra libraries (such as Armadillo) but I'm not sure I need such a library. I want to summarize my needs.

#include <string>
#include <somelibrary.h>

int main(){
std::string str = "3*x^3+2*x^2+x+sqrt(x)*x^(1/3)";
algebraic_expression* exp = new algebraic_expression(str);
}


I want to have a tree from such an expression. Lets say it will return a std::vector or C style array with some information. For example (considering the example above) exp[0] will be "3*x^3", or maybe exp[0]["base"]="x".

And why do I need this? Actually I can do the similar things by means of using RegEx, but sometimes I cannot handle it for example 3*x^0 is simply 3, I cannot print 3*x^0 because it is meaningless I want to have 3 (just like 3*x^1 is 3*x). Or (3-3)*5*2 will return 0, etc...

Thank you for your help.


Solution

  • You should look for 'CAS' (Computer Algebra System) . I can suggest you two:

    Ginac http://www.ginac.de/

    Giac: http://www-fourier.ujf-grenoble.fr/~parisse/giac.html

    An example program with Giac: http://www-fourier.ujf-grenoble.fr/~parisse/giac_us.html#First%20example

    Giac also comes with a GUI application called XCAS. It's a very powerful tool you should give it a try.