This is related to the question (Integrate many simple terms fast) I had before, where Nasser gave me an answer which worked 'most of the time' but not always. Context: Map[Integrate[#,x]&,Expand@func[x]] works much faster for func[x] a huge sum of easy to integrate terms than just using Integrate.

There is one problem though: This cannot handle single terms, that is terms like: x^2 or E^x x^20. It needs to have at least one sum/difference in it, like x^2+1.

Is there a way to either Check if my expression is of the form 'a single term' (NOT atomic, since x^2 is not atomic)

or a work-around of this problem? (adding and subtracting e.g. 1 does not work, as Mathematica will cancel them.)

NOTE: adding {} around Expand@func[x] is NOT a viable solution, since this eliminates the whole reason for Map in the first place: Speeding up calculations. Adding {} will make it treat as one whole expression rather than each one separately, as intended.

Distribute does not speed up the calculation (as tested).


1 Answer 1


I would just check for the Head.

myIntegrate[expr_Times, var_Symbol] := Map[Integrate[#, var] &, Expand[expr]]
myIntegrate[expr_Plus, var_Symbol] := Map[Integrate[#, var] &, expr]
myIntegrate[expr_, var_Symbol] := Integrate[expr, var]

And now call myIntegrate instead of Integrate and Mathematica will figure which wrapper to call on its own.

For example

myIntegrate[x, x]
myIntegrate[3*(1 + x), x]
myIntegrate[x^2, x]


Mathematica graphics

In the above, Expand is used for Head times, to take care of cases such as a*(b+c) and if it is already + then no need to expand. For all other cases, normal Integrate is called. This should take care of all cases you want to map integrate over terms I hope.

  • $\begingroup$ Can you send me a link to where I can read about this 'wrapper' method? It seems to work, I do not understand how though. First solution seems fine. Do you know of any potential problems, like when Head does not give plus despite there being a plus? Which head takes priority? $\endgroup$ Commented Jan 18 at 22:10
  • $\begingroup$ @Confuse-ray30 try the solution given and see if it works for you. It should handle all cases now. it will only expand if the input is * otherwise, will not. For all other input, normal Integrate is called. There is nothing special about wrapper function. It just makes it easier to call Integrate from everywhere so caller does not have to check themselves and it is all done inside one function, the wrapper. $\endgroup$
    – Nasser
    Commented Jan 18 at 22:15
  • $\begingroup$ @Confuse-ray30 actually Distribute does not really work. It does the integration first, then it Distribute. Which is not what you want. You want to distribute integrate BEFORE it does the integration. So Map is what you want to use. $\endgroup$
    – Nasser
    Commented Jan 18 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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