Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am facing the following problem.

f[x_,y_] = a[x] u[y] + b[x] v[y]

Now I can ask Mathematica to calculate

g[x_] = Integrate[ Expand[f[x,y]^2],y]

which it correctly shows. In the expansion we have an expression like

Integrate[ a[x]^2 u[y]^2 + 2 a[x] b[x] u[v] v[y] + b[x]^2 v[y]^2 , y]

Now in the output I want to replace


by I11 etc. Using /. did not do the job. Can anyone please enlighten me?

share|improve this question
Huh? There's no Integrate[u[y]^2,y] in Integrate[ a[x]^2 u[y]^2 + 2 a[x] b[x] u[v] v[y] + b[x]^2 v[y]^2 , y]... – J. M. May 17 '12 at 15:04
Your question is not very clear... can you show what you tried with /.? – R. M. May 17 '12 at 15:05
OK - I will post the complete code here a I am posting from a different machine. I am also baffled why the a[x] is not being dragged out of the integral. Where I am making the error? I must confess that I have used mathematica till now as ODE solver and for calculus and plotting. This is completely a new domain for me and thus please be patient. Thanks! – adm May 17 '12 at 15:06
@J.M. There is, provided you assume that x is independent of y, then Integrate[a[x]^2 u[y]^2, y] == a[x]^2 Integrate[u[y]^2, y]. (Also, threading over the sum.) Not that Mathematica knows that. – rcollyer May 17 '12 at 15:07
@rcollyer: Integrate[] is very cautious that way... – J. M. May 17 '12 at 15:08

The main difficulty here is that Integrate does not normally thread over addition. In other words, it does not understand that

$$\int f(x) + g(x) \text{d}x = \int f(x) \text{d}x + \int g(x) \text{d}x.$$

In most cases, it is true, but as integration is a limiting process care must be taken in the order of operations. A case where that is not true does not come to mind at the moment, other than involving infinite series, but it is not out of the question that a simplification in the integrand could obscure the relationship between the LHS and RHS, above. Mathematica, however, does understand

$$\int a(x) f(y) \text{d}y = a(x) \int f(y) \text{d}y,$$

as shown by

Integrate[a[x] u[y], y]
 a[x] Integrate[u[y], y]

A method to achieve the form you are looking for is to Map Integrate across the sum, Expand[f[x,y]^2], as follows

ints = Integrate[#, y]& /@ Expand[f[x,y]^2]
  a[x]^2*Integrate[u[y]^2, y] + 2*a[x]*b[x]*Integrate[u[y]*v[y], y] 
+ b[x]^2*Integrate[v[y]^2, y]

Then, ReplaceAll (/.) works just fine,

ints /. Integrate[u[y]^2, y] -> I11
  I11*a[x]^2 + 2*a[x]*b[x]*Integrate[u[y]*v[y], y] 
+ b[x]^2*Integrate[v[y]^2, y]
share|improve this answer
One problem with expanding a sum under an integral is that if the summands depend on different parameters, there could be conditions generated - and those could be different for different terms in the sum. But that could make it hard to deduce the combined condition of validity for the entire integral. – Jens May 17 '12 at 16:08
@Jens, absolutely, and as such my method above needs to be used with caution. – rcollyer May 17 '12 at 16:13
+1. It is actually easy to construct a case when threading over addition is not correct: any integral which is finite but where the integrand is a difference of 2 terms where each term individually would lead to a divergent integral. – Leonid Shifrin May 18 '12 at 11:46
@LeonidShifrin that was actually the case that was coming to mind, but in some sense that case is reversing the order of two limiting processes with which care must be taken. – rcollyer May 18 '12 at 12:38
You can also have cancellations of divergent terms for integrals over finite intervals, if infinite limits are what you had in mind as one of the limiting processes. – Leonid Shifrin May 18 '12 at 12:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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