       Re: fourier transform time

• To: mathgroup at smc.vnet.net
• Subject: [mg36446] Re: fourier transform time
• From: Tom Burton <tburton at brahea.com>
• Date: Sat, 7 Sep 2002 02:54:00 -0400 (EDT)
• References: <al9ldd\$b2j\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```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 answer,
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 about
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: Solve of inverse quadratic finite element problem - resend
• Next by Date: RE: RE: Generating Two Unit Orthogonal Vectors
• Previous by thread: Re: fourier transform time
• Next by thread: RE: Re: fourier transform time