In class I sometimes go through the steps in mathematical derivations using Mathematica. Some of the steps involve substitutions and assumptions that come conceptually outside a strict mathematical derivation itself, but must be incorporated. One clear example is when deriving the equations governing Fraunhofer optical diffraction we assume that the distance to the screen is much larger than the size of the optical slit, i.e., $D \gg a$, or that we take only the first two terms in a Taylor series (so that an integral can be calculated).

Implementing most of these are fairly straightforward, for instance using /. to perform a substitution, or limiting the number of terms in a Series. Functions such as MultiplySides and DivideSides prove valuable as well.

I'd also like to process an integral so that all the constant terms (more specifically, the terms that do not depend upon the integration variable) are factored out and placed in front of the integral.

Here's a minimal example: Convert

$\int \frac{(\sin (\theta) + 2)\sqrt{x + a}}{2 x}\ dx \Rightarrow \frac{\sin (\theta) + 2}{2} \int \frac{\sqrt{x+a}}{x}\ dx$.

How do I do that algorithmically? The method should automatically know what the integration variable is, of course... I don't want to "hand enter" that information each time.

Such a function might be of the form:

ExtractConstants[ Integrate[...]]

(It should, of course, work on both definite and indefinite integrals.)

As far as I see it, the similar linked problems do not solve my problem fully but the below do.


2 Answers 2


This is a slight variation on the solution proposed in How to simplify symbolic integration:

moveconst[x_] := (x /. 
   Integrate[factor_ expr_, {var_, min_, max_}] /; 
     FreeQ[factor, var] :> factor Integrate[expr, {var, min, max}])
moveconst[x_] := (x /. 
   Integrate[factor_ expr_, var_] /; FreeQ[factor, var] :> 
    factor Integrate[expr, var])

moveconst[Integrate[(Sin[theta] + 2) f[x], x]]

(* Out: Integrate[f[x], x]*(2 + Sin[theta]) *)
  • $\begingroup$ Both this and the below work well... so for the first time in my many years here I can't decide which to accept. (Both get $+1$...) $\endgroup$ Jun 6, 2023 at 17:36
  • $\begingroup$ I am having hard time understanding how this works. The Integrate[(Sin[theta] + 2) f[x], x] gets evaluated right away, generating Integrate[f[x], x]*(2 + Sin[theta]) before the call into moveconst is even made. So moveconst gets an input Integrate[f[x], x]*(2 + Sin[theta]) *). So why use moveconst at all? Just doing Integrate[(Sin[theta] + 2) f[x], x] gives same result automatically. Screen shot !Mathematica graphics Should you not have used Inactivate on the integral to prevent its evaluation first? I must be overlooking something. $\endgroup$
    – Nasser
    Jun 6, 2023 at 23:34
  • $\begingroup$ The way I understood the question, is that the constants multiplier(s) that do not depend on $x$, should be removed out of the integrand before doing the integration not after (so they look the same and not gets mixed up with terms of the antiderivative that could result). But may be I I just misunderstood the question. $\endgroup$
    – Nasser
    Jun 6, 2023 at 23:40

linearExpand[integral] from How to do algebra on unevaluated integrals?, works, too:

linearExpand[e_] := 
  e //. {int : Inactive[Integrate][_Plus, _] :> Distribute[int], 
    Verbatim[Integrate][integrand_Times, dom : {x_, _, _} | x_] :> 
     With[{dependencies = 
        Internal`DependsOnQ[#, x] & /@ List @@ integrand}, 
      Pick[integrand, dependencies, False]*
       Integrate[Pick[integrand, dependencies, True], 

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