# Simultaneous use of Set AND SetDelayed [duplicate]

Tha Mathematica documentation gives a good example of the simultaneous use of Set and SetDelayed in dynamic programming for the Fibonacci sequence under "neat examples" for SetDelayed

fib = fib = 1; fib[n_] := fib[n] = fib[n - 1] + fib[n - 2]


However, in the notebooks that accompany Hartle's Gravity, An Introduction to Einsteins General Relativity there are uses of Set and SetDelayed that seem significantly different, for example:

geodesic := geodesic =
Simplify[Table[
-Sum[affine[[i, j, k]] u[ coord[[j]] ] u[ coord[[k]] ], {j, 1, n}, {k, 1, n}],
{i, 1, n}]]


Since no parameters are specified in association with geodesic (and without worrying about the detailed content of the Simplify) what was the author (Leonard Parker) achieving by this construction?

• The effect is to delay computing geodesic until it is actually used for the first time (SetDelayed) but also avoid computing it more than once (Set). I am not sure why he does this. Does he define affine and coord after geodesic? – Szabolcs Jan 21 '15 at 16:05
• For example, x := x = a^2; a=2; x has pretty much the same effect as a = 2; x = a^2, but the former allows for exchanging the order of definitions of a and x. This is just an uncertain guess at the author's motivation though. – Szabolcs Jan 21 '15 at 16:10
• It's likely that geodesic might never be called, so it isn't calculated unless required. – Chris Degnen Jan 21 '15 at 18:37
• Thanks for the comments. affine and coord are defined before geodesic geodesic is called subsequently in listgeodesic := Table[{"d/d\[Tau]" ToString[u[coord[[i]]]], "=", geodesic[[i]]}, {i, 1, n}] Uses of "not sure" and "uncertain" noted! – Julian Moore Jan 22 '15 at 14:21

This syntax is called memoization.

f[x_] := f[x] = RandomInteger[x]


See the documentation: Functions That Remember Values They Have Found

The first result is remembered to save time on subsequent calls. Rather than re-run a possibly time-consuming calculation, if the input parameter has been used before the previous result is returned.

For example, RandomInteger is only called once here:

f


29

f


29

A new parameter causes RandomInteger to be run again.

f


80

f


80

The stored results can be seen in downvalues:

DownValues[f]


{HoldPattern[f] :> 29, HoldPattern[f] :> 80, HoldPattern[f[x_]] :> (f[x] = RandomInteger[x])}

Memoization should be used with some caution when variables are likely to change, e.g.

y = 10;

f[z_] := f[z] = RandomInteger[z + y]

f


4

y = 10000;

f


4

The stored value is returned despite y having changed.

f


1075

A new parameter causes the function to run, picking up the new value of y.

• It might be helpful to the OP if you give an example of memoization on a function with no parameters, like the second example in the question. – 2012rcampion Jan 21 '15 at 17:30
• Followed-up in comments to the question. – Chris Degnen Jan 21 '15 at 18:39
• @chris-degnen Thanks. It would seem however that without parameters there is only on value that will be remembered. Perhaps the advantage is that the evaluation time can be incurred at the time of the user's choosing rather than later when the function is finally called? The example showing where and how care is required is appreciated. – Julian Moore Jan 22 '15 at 14:25