I am trying to create a sequence of functions and have it properly memoize the results. The recursive operation is simply convolution, so it possible there is a better way to do this (obviously, if I could find a closed form expression for the n-th convolution, that would be ideal, but I'm not certain I can do that). Here is my current code
f[x_, c2_] := (
Exp[-c2^2/2 + c2 Sqrt[-Log[2 π] - 2 Log[x]]] +
Exp[-c2^2/2 - c2 Sqrt[-Log[2 π] - 2 Log[x]]]
) / Sqrt[(-Log[2 π] - 2 Log[x])]
LogExpect[x_, shift_] := Piecewise[{{f[Exp[x], shift] Exp[x], x < -1/2 Log[2 π]}}, 0]
LogExpectN[t_, n_] := Module[{xx},
LogExpectN[t, n] = Convolve[LogExpectN[xx, n - 1], LogExpect[xx, 0], xx, t]
]
LogExpectN[t_, 1] = LogExpect[t, 0]
In general this works, as it will properly calculate LogExpectN[t,5]
for example, and it will memorize that specific result, but if I ask it for LogExpectN[2 t,5]
or LogExpectN[x,5]
it has to redo the copmutation completely, instead of just plugging 2t
or y
into the function it already found and thus there is no speed up from the memoization. Is there a way to get the behavior I want?
Edit: By changing the the recursive definition to use a pattern, I have improved things
LogExpectN[t_, n_] := Module[{xx}, LogExpectN[t_, n] = Convolve[LogExpectN[xx, n - 1], LogExpect[xx, 0], xx, t]]
However, This only works if I evaluate consecutive values of n
in order. FOr example, after setting the base case for n=1
, I can calculate for n=2
and then n=3
, but if I try to calculate n=3
directly, it will give me an expression in terms of the internal variable xx$<numbers>$
. I am not really sure what is going on here.