2
$\begingroup$

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

Integrate[u[y]^2,y] 

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

$\endgroup$
7
  • $\begingroup$ 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]... $\endgroup$ May 17, 2012 at 15:04
  • $\begingroup$ Your question is not very clear... can you show what you tried with /.? $\endgroup$
    – rm -rf
    May 17, 2012 at 15:05
  • $\begingroup$ 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! $\endgroup$
    – adm
    May 17, 2012 at 15:06
  • $\begingroup$ @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. $\endgroup$
    – rcollyer
    May 17, 2012 at 15:07
  • $\begingroup$ @rcollyer: Integrate[] is very cautious that way... $\endgroup$ May 17, 2012 at 15:08

1 Answer 1

4
$\begingroup$

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]
*)
$\endgroup$
7
  • 1
    $\begingroup$ 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. $\endgroup$
    – Jens
    May 17, 2012 at 16:08
  • $\begingroup$ @Jens, absolutely, and as such my method above needs to be used with caution. $\endgroup$
    – rcollyer
    May 17, 2012 at 16:13
  • $\begingroup$ +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. $\endgroup$ May 18, 2012 at 11:46
  • $\begingroup$ @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. $\endgroup$
    – rcollyer
    May 18, 2012 at 12:38
  • $\begingroup$ 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. $\endgroup$ May 18, 2012 at 12:44

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .