2
$\begingroup$

I am working on computing a challenging integral. I am working with someone else who wrote some code in Mathematica to compute it. I do not have mathematica so I am trying to do the same thing in C++. The problem is, he uses Mathematica's NIntegrate function which I cannot figure out how to re-write.

How can I re-write this function in C++(or any other language)?

Thanks. If you vote down, please tell me why so I can improve this question.

$\endgroup$
  • 2
    $\begingroup$ We could be of greater assistance if state what exactly you want to integrate. "A challenging integral" might mean singularities, or a family with multiple parameters, or one where no closed form is available. $\endgroup$ – hardmath Feb 25 '15 at 18:18
  • $\begingroup$ @hardmath I only need to get the integral of one equation. The only thing would change would be the $x_\max$ $\endgroup$ – Progo Feb 26 '15 at 3:27
  • $\begingroup$ I'm suggesting you spell out that I ntegral in order to get more specific advice on how best to write numerical integration routines. $\endgroup$ – hardmath Feb 26 '15 at 6:12
4
$\begingroup$

First of all, it is impossible to completely rewrite NIntegrate because usually, it evaluates (parts of) the integrand symbolically to check for certain properties. This means, you probably have to re-implement Mathematica completely.

Nevertheless, if you are tackling one specific integral, I assume chances are very good that you can get similar numerical results by re-implementing some parts in C++ or using a library.

I would start by analysing (in Mathematica) what algorithm is used for your integral. In this tutorial you can find details about how Mathematica chooses the algorithm when NIntegrate uses Method->Automatic.

After this, you should study the help-page of NIntegrate carefully to understand how it works. Then you should go on by

  • fixing the Method option of NIntegrate
  • using EvaluationMonitor to see where your integrand is sampled
  • using specific MaxPoints, MaxRecursion, etc settings

until you finally get equally good results. If you have achieved this, then you know what algorithm you have to implement or find in C++ library.


Let me say that there is another way: Understand the integral you want to solve! You as human should see what type of integrand you have and you can check the literature, how those types are solved numerically. This is probably faster than trying to understand what Mathematica does.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for the answer! You said, " it is impossible to completely rewrite NIntegrate..." How is it impossible? If it is 'impossible', then how does Mathematica compute it? According to [this page][en.wikipedia.org/wiki/Mathematica], Mathematica is written in Wolfram Language, C/C++, Java and Mathematica. Since Mathematica is written in the languages, it can't be impossible to re-write the NIntegrate function. +1 for your answer and I'll keep looking for an implemantation in another language. Thanks again! $\endgroup$ – Progo Feb 24 '15 at 22:57
  • 2
    $\begingroup$ Let me try to explain: When NIntegrate examines your integrand symbolically, it needs the power of the Mathematica kernel. The final integration is only a numerical scheme which can be implemented, but for this very first step, you would need to re-implement the Mathematica Kernel. As Mathematica is closed-source, you will have to break the law to look at its implementation. This is why I wrote it is "impossible" because many parts of Mathematica are black boxes that cannot be examined. $\endgroup$ – halirutan Feb 24 '15 at 23:04
  • $\begingroup$ @Progo For the final numerical integration, you have much more information: You know what algorithms are used; you know what parameters are used; you can even inspect the running integration with EvaluationMonitor. This is why it is easier than re-implementing the Math Kernel. But to be exact: It is not impossible to do this. It's only far to much work to even think about it. $\endgroup$ – halirutan Feb 24 '15 at 23:08
  • $\begingroup$ Ahh... Well then you have answered my question well. To find a solution from here on, I will use this question for a starting spot. Matlab also has an integral function which I will look very closely at. Thanks your the help! $\endgroup$ – Progo Feb 24 '15 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.