MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Re: fourier transform time

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

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))


-----Original Message-----
From: Tom Burton [mailto:tburton at] 
To: mathgroup at
Subject: [mg36464] [mg36446] Re: fourier transform time

On 9/6/02 12:23 AM, in article al9ldd$b2j$1 at, "Steve Story"
<sbstory at> 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
years. See for example the attached comparison (not sent to newsgroup)
Stephan Steinhaus ( So when Mathematica takes a very
time, you should investigate. In this case inserting Evaluate[] in two

In[91]:=b[n_] := Evaluate[(2/L)*Integrate[f[x]*Sin[n*Pi*x/L], {x, 0,
In[104]:=Timing[Plot[Evaluate[FS[120, x]], {x, 0, 2}]]
Out[104]={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
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
carrying around unevaluated mathematical expressions (usually without
asked) that could possibly be evaluated more appropriately at a later
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
care that Mathematica does not burden itself unnecessarily. That's my
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
this case that the integration can be done safely once for all n. So do

The huge difference, though, comes from pre-evaluating the argument to
Read the on-line help! You should pre-evaluate where possible. In some
cases, the most common of which involve branching within the definition
function to plot, you cannot pre-evaluate so, in keeping with the
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