17

I need to do analytical integration in C++. For example, I should integrate expressions like this: exp[I(x-y)], I is an imaginary number.

How can I do this in C++?

I tried GiNaC but it can just integrate polynomials. I also tried SymbolicC++. It can integrate functions like sine, cosine or exp(x) and ln(x), but it is not very powerful. For example, it can not integrate x*ln(x) which can be easily obtained by use of Mathematica or by integration by parts.

Are there any other tools or libraries which are able to do symbolic computation like analytical integration in C++?

reuben
  • 3,360
  • 23
  • 28
MOON
  • 2,516
  • 4
  • 31
  • 49
  • Did you already try [Maxima](http://maxima.sourceforge.net/)? – jxh Jul 04 '12 at 08:57
  • is using Matlab an option? Matlab's symbolic toolbox should be able to do these things. Using the C Matlab Engine you can call it from your C++ code? – Philipp Jul 04 '12 at 08:59
  • I do not have Matlab's licence so i can not use it. Does Maxima use c++ syntax? I prefer some library in c++. if i wanted to use other programs like maxima or Matlab i would use Mathematica instead. actually i have wrote my program in mathematica. It is a simple program to silve Integro-defferential equations but the problem is that Mathematica is not efficient in terms of time required for computation. So i decided to write my program in c++. – MOON Jul 04 '12 at 09:50
  • Despite the fact that probably there is nothing like you are asking for as a native C++ library, your question, even in principle has many subtleties. To begin with, what do you want to integrate in the first place, you have to be able to define such an *expression*, is that a runtime expression (e.g. an expression tree) or a compile time expression (e.g. a la Boost.Phoenix)? What kind of result do you want a (runbtime/compiletime) callable function? Your question is very valid, the problem is that a solution will have to resolve this subtleties first. – alfC Dec 17 '13 at 09:36
  • @alfC Is there any library, that allows to perform symbolic computations at runtime? – Tomilov Anatoliy Feb 15 '14 at 19:07
  • @Dukales, GiNaC, SymbolicC++, and libmatheval http://www.gnu.org/software/libmatheval/ . In my opinion none of them makes the cut. – alfC Feb 17 '14 at 17:02
  • **GiNaC** can integrate `sine`, `cosine`, etc on newer versions – Isaac Pascual Aug 10 '16 at 09:43

3 Answers3

7

If you need to do symbolic integration, then you're probably not going to get anything faster than running it in mathematica or maxima - they're already highly optimised. So unless your equations have a very specific formulae that you can exploit in a way that Mathematica or Maxima can not then you're probably out of luck -- and at very least you're not going to get that kind of custom manipulation from an off-the-shelf library.

You may be justified in writing your own code to get a speed boost if you needed to do numerical solutions. ( I know that I did for generating numerical solutions to PDEs).

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • My code written in Mathematica is in this link : http://www.mediafire.com/?cw9s3ne7jz8fq7s This code is about a recuursion relation. I give it the 0th term and it obtains the first term then it uses the first term to obtain the second one and so on. For the first 4 term it doesn't take so much time but for further terms it takes a lot lot time and acually i have never obtained those higher terms. this code is supposed to solve an integro-differential equation so i need a lot of terms to be obtained. At least i think 50 terms is necessary. – MOON Jul 05 '12 at 08:44
  • So beacuse it takes alot of time in Mathematica i decided to write it by c++ – MOON Jul 05 '12 at 08:44
  • Can you add a picture of the equation, I can't open the mathematica notebook (no mathematica available to me these days...) – Michael Anderson Jul 05 '12 at 08:58
  • MAxima is not written in C++ but in Lisp – Damien Mattei Oct 05 '22 at 09:29
  • @DamienMattei agreed, I've removed the comment that suggested it was. – Michael Anderson Oct 05 '22 at 09:45
5

The other C++ libraries I am aware of that do symbolic computation are

If I am not mistaken, SymEngine does not yet support integration; however, Piranha does. The documentation for Piranha is somewhat limited at the moment and is under development, but you can see the integration function here. Note that the second link uses the syntax for the Python wrapper Piranha. However, Piranha "is a computer-algebra library for the symbolic manipulation of sparse multivariate polynomials and other closely-related symbolic objects (such as Poisson series)", so I do not think it can integrate the particular functions in which you may be interested.

Though it is not C++, you may also be interested in SymPy for Python, which can perform some of the more complicated symbolic integration you may be interested in. The documentation for SymPy's integrate is here.

Grayscale
  • 1,462
  • 1
  • 13
  • 20
4

A couple of days ago, I was searching for a symbolic math library like SymPy for C++, because I bedazzled by its speed comparing to Python or most of the other programming languages.

I found Vienna Math Library, an awesome library with very modern syntax, and SymPy's features to the best of my knowledge. This library also has an integral function that can be used for your problem.

It was good enough for solving IK (Inverse Kinematics) of 3 degrees of freedom articulated manipulator.

  • It looks like Vienna only supports analytic integration of very simple functions. "Analytical integration is in ViennaMath 1.0.0 available for compiletime types and polynomials as integrands only. " (compiletime types look like they're only binary ops, constants and independent variables) – Michael Anderson Oct 05 '22 at 09:50