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[1] = fib[2] = 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?
geodesicuntil 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 defineaffineandcoordaftergeodesic? – Szabolcs Jan 21 '15 at 16:05x := x = a^2; a=2; xhas pretty much the same effect asa = 2; x = a^2, but the former allows for exchanging the order of definitions ofaandx. This is just an uncertain guess at the author's motivation though. – Szabolcs Jan 21 '15 at 16:10geodesicmight never be called, so it isn't calculated unless required. – Chris Degnen Jan 21 '15 at 18:37affineandcoordare defined beforegeodesicgeodesicis called subsequently inlistgeodesic := 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