I need to calculate a complicated set of functions dpdv[j_], which depend on a set of variables m[n_]. They are time-consuming to calculate, and I don't know in advance which values I'll need. So right now, I just pre-calculate more m's than I think I will need...
m[0] = 1;
Do[
If[OddQ[n],
m[n] = 0,
m[n] = (B^(n/2))*Sum[n!*(i - 1)!!/(i!*(n - i)!), {i, 0, n, 2}]],{n, 0, 100}];
And I do the same with the dpdv's...
gau[x_, v_] = (1/Sqrt[2*Pi*v])*E^-((x^2)/(2*v));
Do[
coef = CoefficientList[
Expand[D[gau[x - z, v], {v, j}]/gau[x - z, v]], z];
t = Table[m[k - 1]*gau[x - m[k], m[k + 1] + v], {k, 1, Length[coef]}];
dpdv[j] = Total[coef*t];, {j, 1, 12}];
The values get used later on:
ord = 7;
h = Table[0, {j, 1, ord}];
h[[1]] = Log[y[v]];
Do[h[[j]] = D[h[[j - 1]], v], {j, 2, ord}];
h = h /. Derivative[n_][y][v] -> dpdv[n] /. y[v] -> gau[x - m[1], m[2] + v];
Because the dpdv[j]'s are complicated for large j, I want to calculated each value just once and not calculate extra values that I won't need.
Is there a way to create functions for dpdv[] and m[] that will calculate and return the value on the fly the first time it's called, but remember that value to return it on the next call without recalculating it?
Bonus question: Could my code be better? I'm still getting the hang of MMa coding practices.