Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

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

Search the Archive

RE: = versus := and trying to speed up calculations

  • To: mathgroup at smc.vnet.net
  • Subject: [mg35438] RE: [mg35410] = versus := and trying to speed up calculations
  • From: "DrBob" <majort at cox-internet.com>
  • Date: Fri, 12 Jul 2002 04:29:03 -0400 (EDT)
  • Reply-to: <drbob at bigfoot.com>
  • Sender: owner-wri-mathgroup at wolfram.com

Here's a recursive definition for n! that saves answers so they don't
have to be computed again:

ClearAll[f]
f[0] = 1;
f[n_] := f[n] = n f[n - 1]
f[1];
?? f

Global`f
f[0] = 1
f[1] = 1
f[n_] := f[n] = n*f[n - 1]

The rule defined by SetDelayed (":=") is only invoked once for each n;
next time the same n is used, the rule defined by Set ("=") takes
precedence.

The downside is that you're storing a rule for each value of n
encountered.  You also have to compute the low-level values ahead of
time.  If the above code is followed immediately by

f[2000]

$RecursionLimit::"reclim": "Recursion depth of 256 exceeded."

Instead you have to compute from the bottom up (the first time you want
to reach 2000):

f /@ Range[1, 2000, 256]; f[2000]

In your problem, the memory requirements of this method may be
prohibitive.  If so, you might experiment with saving SOME intermediate
results but not others.

Bobby Treat

-----Original Message-----
From: Geoff Tims [mailto:Geoff at swt.edu] 
To: mathgroup at smc.vnet.net
Subject: [mg35438] [mg35410] = versus := and trying to speed up calculations

I have the following function and it is called many many times in a
program
I have written.  This is a bracket operator for a certain type of Lie
Algebra I am looking at and it must be called over and over to test
whether
or not the Jacobi is actually 0 as it should be.  With very low numbers
for
n, my program runs in a second or less.  If n is around 20+ or if the
coefficients are large, the program takes nearer a minute or two.
That's
not a long time, but I have a feeling that it's having to calculate the
same
Bracket many times in the program and I'm hoping to get rid of that
somehow.
However, I don't understand the differences between := and = enough.
I've
read the help files, but I don't understand the subtle differences such
as I
can call

Bracket[stuff, something]:= somethingelse

more than once, but it doesn't seem as if I can use = more than once.

Bracket[a_.*e[i_], b_.*e[j_]] :=
    Which[
      i + j > n, 0,
      i == j, 0,
      i == 1, a*b*e[j + 1],
      i == 2 && j == 3, 14a*b*e[7],
      i == 3 && j == 4, 0,
      i < j, -a*b*Bracket[e[i + 1], e[j - 1]] +
        Bracket[e[1], Bracket[e[i], e[j - 1]]],
      i > j, -a*b*Bracket[e[j], e[i]]
      ];
(* bilinear function *)
Bracket[mul_, expr_Plus] := Map[Bracket[mul, #] &, expr];
Bracket[expr_Plus, mul_] := Map[Bracket[#, mul] &, expr];
Bracket[0, x_] := 0
Bracket[x_, 0] := 0


Any help would be much appreciated.
Geoff Tims







  • Prev by Date: Re: Memory conserving Input of big Ascii tables ?
  • Next by Date: Re: = versus := and trying to speed up calculations
  • Previous by thread: = versus := and trying to speed up calculations
  • Next by thread: Re: = versus := and trying to speed up calculations