The following is the code and output of a Mathematica command
How do I get Mathematica to remove $g(y)$ outside the integral?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityThis 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.
outrules
list after FreeQ
.
$\endgroup$
Mar 24, 2016 at 20:18
Based on this solution (replacement rule to pull independent expression outside of Integrate) You can solve it the following way:
g[y]*Integrate[f[x1, y]/g[y], {x1, 0, x}] /.Integrate[q_/r_, {v_, l_, h_}]
/; FreeQ[r, v] :> 1/r*Integrate[q, {v, l, h}]
Integral[f[x1, y],{x1,0,x}]
Here is a stab at the problem using TransformationFunctions:
ClearAll[xx]
xx[exp_Integrate /;
Head[integrand = (First@(List @@ exp))] == Times ] := Module[{dep,indep},
indep =
Select[(List @@ integrand), (FreeQ[#, First@(Last@(List @@ exp))]) &] ;
dep = Complement[(List @@ integrand), indep];
((Times @@ indep ) Integrate[Times @@ dep, (Last@(List @@ exp))])];
xx[exp_] := exp
Simplify[g[y] Integrate[ g[x]/g[y] , {x, 0, 1}], TransformationFunctions -> {xx}]
(* Integrate[ g[x] , {x, 0, 1}] *)
Big UNTESTED warning.. I only checked that it works for the given example..
Also adding Automatic to TransformationFunctions breaks it for some reason I havent explored
g[y] Integrate[1/g[y]] -> x
. Anyone try to tackle this throughSimplify[ .. ComplexityFunction .. ]
?? $\endgroup$