The following is the code and output of a Mathematica command
How do I get Mathematica to remove $g(y)$ outside the integral?
Answer
This is some code I adapted from a package I wrote to manipulate Sum
s, instead of integrals. But it should work in bringing out expression that do not depend on the integration variable too. After all, integrals are sums pushed to the limit. EDIT: cleaned up code, there is no need to separate rules here.
outrules = {
Integrate[f_ + g_, it:{x_Symbol, __}] :> Integrate[f, it] + Integrate[g, it],
Integrate[c_ f_, it:{x_Symbol, __}] :> c Integrate[f, it] /; FreeQ[c, x],
Integrate[c_, it:{x_Symbol, __}] :> c Integrate[1, it] /; FreeQ[c, x]
};
(You could apply the rules directly as in expr //. outrules
, but I like to define an ordinary procedure like BringOut
- or PullOut
if you prefer)
BringOut[s_] := s //. outrules
It works in your simple example
g[y] Integrate[f[x1, y]/g[y], {x1, 0, x}] // BringOut
(* Integrate[f[x1, y], {x1, 0, x}] *)
and it also works with more complex integrands (basically, it exploits the linear property of the integral)
Integrate[y(f[x1, y]/(1 + g[y]) - h[y]), {x1, 0, x}] // BringOut
(* y (Integrate[f[x1, y], {x1,0,x}]/(1 + g[y]) - x h[y]) *)
Sometimes you might want to wrap and Expand
, or an Apart
or some other expression manipulation procedure to the integrand. Since the action usually depends on the expression, I found no use into incorporating this into BringOut
. It's cleaner to apply the desired simplification where you need it and only when you need it, usually after BringOut
has done its work:
BringOut[Integrate[y(f[x1, y]/(1 + g[y]) - h[y]), {x1, 0, x}]] // Factor
(* (Integrate[f[x1, y], {x1,0,x}] - x h[y] + x g[y] h[y]) y / (1 + g[y]) *)
Caveat emptor: the original code worked on a user defined and unevaluated version of Sum
, and I have not tested it thoroughly with a built-in procedure like Integrate
. But I do not see why it shouldn't work.
Comments
Post a Comment