Questions tagged [memoization]

For questions related to user defined caching mechanisms, memoization, in particular.

For questions related to user defined caching mechanisms, memoization, in particular. Questions dealing with the structure x := x = expr should use this tag.

For questions about the internal caches in Mathematica, see .

Useful links:

Example questions:

85 questions
20
votes
3 answers

How to memoize a function with Options?

Consider the following modified fibonacci function: tempHist = $History $History = 0 ClearAll[fibon] Options[fibon] = {k -> 1} fibon[0] = 0; fibon[1] = 1; fibon[n_, OptionsPattern[]]:= fibon[n-1] + OptionValue[k] * fibon[n-2] fibon[30] // Timing …
Ryogi
  • 1,076
  • 6
  • 16
10
votes
1 answer

Can I memoize retroactively?

I have a function defined something like this: f[x_] := Module[...]; I can easily write a memoized version of it like this: f2[x_] := f2[x] = f[x]; And then replace every instance of f[...] with f2[...] in my code. However, I have code that uses f…
user1722
8
votes
2 answers

Correct way to use memoization for recurrence relation having an Integral

I am trying to construct a way to make a function for the following recurrence relation. $$ Q(N,L) = \int_{0}^{L-(N-1)a} Q(N-1, L-a-z) dz $$, with the initial condition $Q(2,L) = \frac{1}{2} (a-L)^2$ for positive real numbers $a$ and $L$. Since…
Sungmin
  • 2,285
  • 15
  • 23
7
votes
0 answers

Time complexity to choose the right overload from the memoized function

If we want to save some outputs of the function, we often use the memoization pattern as follows, f[x_]:=f[x]=x f/@Range[10^5] If I call this function many times for different arguments, Mathematica has to first find the best pattern defined so far…
Sungmin
  • 2,285
  • 15
  • 23
5
votes
2 answers

Problems with DumpSave and memoization

I defined a function that takes some time to compute using memoization: y[a_, b_, c_] := y[a, b, c] = First[$y /. NDSolve[.........] so that if I call y[a,b,c] a second time it doesn't do the NDSolve computation all over again if it has already…
fpghost
  • 2,125
  • 2
  • 21
  • 25
4
votes
0 answers

Memoization of built in function

What is the best way to let mathematica memoize built-in function like e.g. SphericalHarmonicY[l,m,th,ph] One possibility would be for sure: sphericalHarmonicY[l_,m_,th_,ph_]:= sphericalHarmonicY[l,m,th,ph]=SphericalHarmonicY[l, m, th, ph] But…
Mr Puh
  • 1,017
  • 6
  • 12
2
votes
1 answer

Memoization only under certain conditions

I have a function that takes in sequences of values (as a list), and recursively removes the largest element from that sequence as part of its computation. Some of these sequences can be quite large, but I suspect that they often have common…
1
vote
0 answers

Prevent Mathematica using previous result

How can I prevent mathematica from using the previous computation in new one? How do I make the code below to compute same amount of time without memorizing previous computation. In[25]:= AbsoluteTiming@Sum[i^3 + 4. i^2, {i, 1000000}] Out[25]=…
Al Guy
  • 1,610
  • 1
  • 10
  • 15
0
votes
2 answers

memoization based on function expression?

I want to create a function $f$ which takes as a parameter a function $h$, and calculates its integral between $a$ and $b$. But I want to apply memoization to this, by only calculating the integral the first time it is called for a particular…
user56834
  • 545
  • 3
  • 10
0
votes
0 answers

Reuse symbolic polynomial

Suppose I have a function that returns a polynomial in x, GetPoly[params_,x_]:=GetPoly[params,x]... but it is quite slow, so I memoize this. However, doing GetPoly[params,2y] or similar do not utilize the memoized data, it does the same…
Per Alexandersson
  • 2,469
  • 15
  • 19