       Re: Integration of a list

• To: mathgroup at yoda.physics.unc.edu
• Subject: Re: Integration of a list
• Date: Fri, 22 Jan 93 14:51:54 EST

```Hi mathgroup!
I had asked some help of you in integrating a very long list.
I got very helpful hints from Alfred M Kaufman and Dave Withoff.

___________________________________________________
The problem was :
>
> I have been trying to Integrate a moderate sized list from -Pi to Pi
> and it is taking forever.....the individual components of the expression
> are quite simple, so I cannot figure out what is taking so long...
>
> test =
> {0, ((-0.25*I*E^(-3.*I*Omgn*Pi)*x1*y1)/(Omgn^3*Pi) +
>      (0.75*I*E^(-1.*I*Omgn*Pi)*x1*y1)/(Omgn^3*Pi) +
>      (-0.75*I*E^(1.*I*Omgn*Pi)*x1*y1)/(Omgn^3*Pi) +
>      (0.25*I*E^(3.*I*Omgn*Pi)*x1*y1)/(Omgn^3*Pi) -
>      (3*E^(-I*Omgn*t)*(I + -I*E^(2*I*Omgn*t))*
>         ((E^(-I*Omgn*t)*(1 + E^(2*I*Omgn*t))*x1)/2 +
>            (E^(-I*Omgn*t)*(I + -I*E^(2*I*Omgn*t))*y1)/(2*Omgn))^2)/(2*Omgn))*
>    ((0.04166666666666666*I*E^(-3.*I*Omgn*Pi)*t*x1^3)/(Omgn*Pi) +
>      (0.375*I*E^(-1.*I*Omgn*Pi)*t*x1^3)/(Omgn*Pi) +
>      (-0.375*I*E^(1.*I*Omgn*Pi)*t*x1^3)/(Omgn*Pi) +
>      (-0.04166666666666666*I*E^(3.*I*Omgn*Pi)*t*x1^3)/(Omgn*Pi) +
>      (-0.125*I*E^(-3.*I*Omgn*Pi)*t*x1*y1^2)/(Omgn^3*Pi) +
>      (0.375*I*E^(-1.*I*Omgn*Pi)*t*x1*y1^2)/(Omgn^3*Pi) +
>      (-0.375*I*E^(1.*I*Omgn*Pi)*t*x1*y1^2)/(Omgn^3*Pi) +
>      (0.125*I*E^(3.*I*Omgn*Pi)*t*x1*y1^2)/(Omgn^3*Pi) -
>      E^(I*Omgn*t)*(((-3*I)/8*x1^3)/Omgn - (3*x1^2*y1)/(8*Omgn^2) +
>         ((-3*I)/8*x1*y1^2)/Omgn^3 - (3*y1^3)/(8*Omgn^4)) +
>      (E^(-3*I*Omgn*t)*((-I/8*x1^3)/Omgn + (3*x1^2*y1)/(8*Omgn^2) +
>           ((3*I)/8*x1*y1^2)/Omgn^3 - y1^3/(8*Omgn^4)))/3 -
>      (E^(3*I*Omgn*t)*((-I/8*x1^3)/Omgn - (3*x1^2*y1)/(8*Omgn^2) +
>           ((3*I)/8*x1*y1^2)/Omgn^3 + y1^3/(8*Omgn^4)))/3 +
>      E^(-I*Omgn*t)*(((-3*I)/8*x1^3)/Omgn + (3*x1^2*y1)/(8*Omgn^2) +
>         ((-3*I)/8*x1*y1^2)/Omgn^3 + (3*y1^3)/(8*Omgn^4)) +
>      (0.5*F*t*Sin[-1.*Omg*Pi])/(Omg*Pi) - (0.5*F*t*Sin[1.*Omg*Pi])/(Omg*Pi) +
>      (F*Sin[Omg*t])/Omg)}
>
> Tperiod = 2*Pi
>
> Mean[func_List,t_]:=
> (1.0/Tperiod)*Integrate[func,{t,-Tperiod/2.0,Tperiod/2.0}]
>
> Mean[test,t] is taking forever.
>
>
______________________________________________________________>
>
Daves Response :

This expression expands out to an integrand with 225 terms. Expanding is
a fairly reasonable way of approaching the problem, except that if
Integrate has to tackle all of the terms at once it first sets up to
try the terms in all possible combinations, since it has no way
of knowing that the terms are individually integrable.  Even though
the very first combination will succeed, just setting up to do them
all could take a very long time.

In cases like this, my first suggestion is to use something like

result = Map[Integrate[#, {t, -Pi, Pi}] &, ExpandedIntegrand]

Unfortunately, in this example some of the terms also take a very long
time by themselves.  I noticed, for example, the following, which seems
to be a bug.  It shouldn't take this long:

In:= Integrate[b t Exp[I x t] Sin[a Pi]/(x Pi), t] //Timing

I t x  b Sin[a Pi]   I b t Sin[a Pi]
Out= {5.95 Second, E      (----------- - ---------------)}
3               2
Pi x            Pi x

Since this is such a simple problem mathematically, I just went

In:= int[a_, t_] := 2 Pi a /; FreeQ[a, t]

In:= int[a_ f_, t_] := a int[f, t] /; FreeQ[a, t]

In:= Integrate[t Exp[a + b t], {t, -Pi, Pi}] //InputForm

(* compute this for copying into the subsequent rule *)

Out//InputForm=
-((-(E^a/b^2) - (E^a*Pi)/b)/E^(b*Pi)) + E^(b*Pi)*(-(E^a/b^2) + (E^a*Pi)/b)

In:= int[t_ Exp[a_. + b_. t_], t_] :=
-((-(E^a/b^2) - (E^a*Pi)/b)/E^(b*Pi)) +
E^(b*Pi)*(-(E^a/b^2) + (E^a*Pi)/b) /; FreeQ[{a, b}, t]

I then applied this to the expanded second element of test:

In:= testExpanded = Expand[test[]] ;

In:= Length[testExpanded]

Out= 225

In:= testExpanded[]

-17  3. I Omgn Pi + I Omgn t     3   2
-1.38778 10    E                        t x1  y1
Out= -------------------------------------------------
4
Omgn  Pi

In:= int[%, t]

3. I Omgn Pi      3. I Omgn Pi
-17   I Omgn Pi  E               I E             Pi
Out= (-1.38778 10    (E          (------------- - ------------------) -
2              Omgn
Omgn

3. I Omgn Pi      3. I Omgn Pi
-I Omgn Pi  E               I E             Pi     3   2
>        E           (------------- + ------------------)) x1  y1 ) /
2              Omgn
Omgn

4
>    (Omgn  Pi)

After adding one more rule, I get a result in a few seconds.

In:= int[f_ + g_, t_] := int[f, t] + int[g, t]

In:= result = int[testExpanded, t]; //Timing

Out= {17.4833 Second, Null}

Unless I've made an algebra error (which is likely) this answer is
probably correct.

You can get a result even faster using Map:

In:= Map[int[#, t] &, testExpanded]; //Timing

Out= {4.41667 Second, Null}

Dave Withoff
withoff at wri.com

_____________________________________________________

Al's response:

Bappaditya Banerjee writes that a test function:
test = {0,f1[t],f2[t],...,fn[t]}
was taking a long time to integrate.
His integration was written in the form:
Mean[func_List,t_] := 1/(2.*Pi)*Integrate[func,{t,-Pi,Pi}];
First of all the integration is not a function of t any
longer since the t in the integration is a dummy variable.

I succeeded in doing the integration very quickly by defining
the pure function:

f = Function[{t},test];

and the integration:

Mean[f_Function] := 1/(2*Pi)*Integrate[f[x],{x,-Pi,Pi}];

Then Mean[f] gives the answer which is too long to include here.
Al Kaufman <kaufman1 at llnl.gav>

I thank them for their time and interest.

regards,

bappa.

Ray W. Herrick Laboratories
Purdue University
West Lafayette, IN 47907
work : (317) 494 2132
(317) 494 2147
fax  : (317) 494 0787
home : (317) 743 3982

>
>

```

• Prev by Date: Re: interpolation of unevenly sampled points
• Next by Date: Use of remote kernels via Mac's
• Previous by thread: Integration of a list
• Next by thread: Check and Off