This question already has an answer here:

I have a function which is a bit expensive to calculate. I want to translate and integrate. I want to translate by values on a grid, store the values of the integral and refine the grid. That is, I want to run this:

 f2[{c, d}] = NIntegrate[f1[{a - c Sqrt[3]/2, b - d}], Element[{a, b}, hex]], 
 {c, 0, 3, 1/2^n}, {d, 0, 3, 1/2^n}]

for increasing values of n, but without recalculating previously calculated values, ie don't recalculate f2[{c/2^m,d/2^m}] for m < n.

To do this I'd like to write

If[!IsDefinedQ[f2[{c,d}], f2[{c, d}] = NIntegrate[f1[{a - c Sqrt[3]/2, b -       d}], Element[{a, b}, hex]]], 
{c, 0, 3, 1/2^n}, {d, 0, 3, 1/2^n}]

Does a function as IsDefinedQ exist? Do I have a misunderstanding of the what f2[{0,2}] really means in mathematica? Can anybody see a better way to refine such evaluation without reevaluation?


marked as duplicate by Mr.Wizard Aug 14 '15 at 1:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Search for memoization. $\endgroup$ – march Aug 13 '15 at 14:07
  • $\begingroup$ What is hex? Further, you do not need to have the part f2[{c, d}] = in your table. Last, It may be a good idea to give your function (is it f1 or f2?). If it is much too complex, invent a simple one, that shares the main properties with the real one. Then, n should be somehow defined. $\endgroup$ – Alexei Boulbitch Aug 13 '15 at 14:21
  • $\begingroup$ Maybe ValueQ will do the trick? $\endgroup$ – mfvonh Aug 13 '15 at 14:42
  • $\begingroup$ Note the literal question is not a duplicate. ValueQ does not work because its argument is first evaluated. ( I suspect you need to parse the results of DownValues ) $\endgroup$ – george2079 Aug 13 '15 at 15:33
  • $\begingroup$ isDefinedQ[f_, args_] := MemberQ[(First@Cases[#[[1]], f[x__] :> {x}]) & /@ DownValues[f] , args] (Obviously you would not use this for the present problem) $\endgroup$ – george2079 Aug 13 '15 at 15:38