       RE: Re: fourier transform time

• To: mathgroup at smc.vnet.net
• Subject: [mg36464] RE: [mg36446] Re: fourier transform time
• From: "DrBob" <drbob at bigfoot.com>
• Date: Sun, 8 Sep 2002 03:30:40 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Caution: the definition

b[n_] := Evaluate[(2/L)*Integrate[f[x]*Sin[n*Pi*x/L], {x, 0, L}]];

doesn't immediately compute the integral unless f is already defined at
this point, and in that case you may as well write

b[n_] = (2/L)*Integrate[f[x]*Sin[n*Pi*x/L], {x, 0, L}];

instead.  If b will be computed more than once for the same n, its even
better to do it THIS way (if f and L do not change):

f[x_] = Cos[x] (* for example *)
Simplify[Integrate[f[x]*Sin[n*Pi*(x/L)], {x, 0, L}]]

(L*((-n)*Pi + n*Pi*Cos[L]*Cos[n*Pi] + L*Sin[L]*Sin[n*Pi]))/
((L - n*Pi)*(L + n*Pi))
b[n_] := b[n] =
(L*((-n)*Pi + n*Pi*Cos[L]*Cos[n*Pi] + L*Sin[L]*Sin[n*Pi]))/
((L - n*Pi)*(L + n*Pi))

Bobby

-----Original Message-----
From: Tom Burton [mailto:tburton at brahea.com]
To: mathgroup at smc.vnet.net
Subject: [mg36464] [mg36446] Re: fourier transform time

On 9/6/02 12:23 AM, in article al9ldd\$b2j\$1 at smc.vnet.net, "Steve Story"
<sbstory at unity.ncsu.edu> wrote:

> I don't use Mathematica
> because of the speed, but should it be this slow?

Mathematica has become more competitive in the speed department in
recent
years. See for example the attached comparison (not sent to newsgroup)
by
Stephan Steinhaus (steinhaus-net.de). So when Mathematica takes a very
long
time, you should investigate. In this case inserting Evaluate[] in two
places

In:=b[n_] := Evaluate[(2/L)*Integrate[f[x]*Sin[n*Pi*x/L], {x, 0,
L}]];
....
In:=Timing[Plot[Evaluate[FS[120, x]], {x, 0, 2}]]
Out={0.18 Second,\[SkeletonIndicator]Graphics\[SkeletonIndicator]}

speeds the process enormously (18 milliseconds to plot 120 terms on my
feeble old 500MHz PowerBook).

Why was it so slow before? When I switch from an ordinary numerical
language
to Mathematica, I enter into an implicit bargain with
Mathematica: the software will go the extra mile to get me a good
including (1) using exra precision (sometimes without being asked) and
(2)
carrying around unevaluated mathematical expressions (usually without
being
asked) that could possibly be evaluated more appropriately at a later
time.
Most tools cannot do either of these things, so I don't have to worry
it, except for the bad answers that result now and then.  But I need to
take
care that Mathematica does not burden itself unnecessarily. That's my
side
of the bargain.

Number (2) is the issue here. Your definition of b[n] is written so that
Mathematica analytically evaluates b separately for each n. But you know
in
this case that the integration can be done safely once for all n. So do
it!

The huge difference, though, comes from pre-evaluating the argument to
Plot.
Read the on-line help! You should pre-evaluate where possible. In some
cases, the most common of which involve branching within the definition
of
function to plot, you cannot pre-evaluate so, in keeping with the
bargain,
Mathematica goes the extra mile and holds back just in case. You need to
steer it into the shortcut when it's OK.

Hope this helps,

Tom Burton
--

```

• Prev by Date: Re: Plotting The Slope field of a differential equation
• Next by Date: RE: Can anyone simplify this?
• Previous by thread: Re: fourier transform time
• Next by thread: RE: Re: fourier transform time