Skip to main content
21 votes
Accepted

Throwaway memoization makes built-ins faster?

Cause of speed up This is definitely not memoization. The reason for the observed speed up is that for large arrays (e.g. 10^8 elements), the memory clean up operations may take noticeable time. If ...
Ray Shadow's user avatar
  • 7,846
16 votes
Accepted

When to use Once for memoization?

If your goal is maximum performance in a kernel session, Once is never the answer. It is just too heavyweight. It does, however, provide a real memoization method-...
Itai Seggev's user avatar
  • 14.2k
15 votes

Parallelize evaluation of function with memoization

The problem with a normal shared memoized function definition f[x_] := f[x] = (Pause[3]; N[Sin[x]]) is indeed that any evaluation of f[n] on a parallel subkernel ...
Roman's user avatar
  • 516
12 votes

Throwaway memoization makes built-ins faster?

You are absolutely correct that this memoization is completely unnecessary. What seems to happens is that from the second run onwards on the same data, the builtin functions become faster. I do not ...
Szabolcs's user avatar
  • 236k
9 votes

Memoization of a function defined by a recurrence relation

Here is something that might be more elaborated than really necessary for your task (for which I think Marius answer might work well enough) but shows some helpful techniques. It basically does the ...
Albert Retey's user avatar
  • 23.6k
9 votes
Accepted

Recursive solution to the extended Josephus problem

Your problem arrises because you are recursing downward. With large n such as 50000, this mean a huge recursive structure must be built all the way down to ...
m_goldberg's user avatar
  • 108k
9 votes
Accepted

Why is a function of two functions that use memoization still slow?

Switch off adaptive sampling: setting MaxRecursion to zero, ...
Roman's user avatar
  • 48.3k
8 votes

How to remember evaluation across sessions?

Strategy: Use memoization and then save the relevant definition in a file at the end of your session. Next session you can recover the "memoized" definition. Memoization Look at memoization ...
rhermans's user avatar
  • 36.7k
7 votes
Accepted

Clever caching of a recursively defined function

A long time ago, I wrote a note about how to do this with DownValues. Since then, we got Association, which is a much better data structure for caching. MaTeX uses ...
Szabolcs's user avatar
  • 236k
7 votes

Only perform a symbolic differentiation once

Just for fun, here is an explicit formula for the derivative: ...
Carl Woll's user avatar
  • 131k
6 votes

Memoization of a function defined by a recurrence relation

You mean something like this? ...
Marius Ladegård Meyer's user avatar
6 votes
Accepted

Memoization of functions with optional arguments

One way is to use multiple dispatch instead of optional arguments: ...
Roman's user avatar
  • 48.3k
6 votes
Accepted

Memoize a compiled function

You can use a separate CompiledFunction cf as a fallback when the argument does not appear in the ...
Henrik Schumacher's user avatar
5 votes
Accepted

Memoization (or otherwise) for a recurrent definition of a (double) sequence of functions

Memoize p properly (you had a typo), and make sure you use Evaluate inside of function definitions: ...
Roman's user avatar
  • 48.3k
5 votes
Accepted

Dynamic programming and Module

Outline of sections below (See summary section below for a shorter answer) Feel free to skip around as the sections are somewhat independent 1) Experiment to see what Mathematica is doing. Content: ...
userrandrand's user avatar
  • 5,937
5 votes
Accepted

Recursive function Catalan triangle

If you look at the error message given, you'll see that your algorithm eventually tries to evaluate something like catalan[-1019-1,2]. That's a clue to what's ...
lericr's user avatar
  • 30.1k
4 votes

Throwaway memoization makes built-ins faster?

I can't reproduce claimed speedup on "11.0.1 for Linux x86 (64-bit) (September 21, 2016)". In my tests, custom function wrappers without memoization (as suggested ...
jkuczm's user avatar
  • 15.1k
4 votes

Timing of three different strategies for fibonacci numbers

Memoization means almost optimal number of unique calculations. The first guy is the worst: keeps repeating same calculation again and again and again and doesn't have the benefit of aggregating sums....
John Joseph M. Carrasco's user avatar
4 votes
Accepted

Memoization Involving More than one Variable

You can for example memoize a function B[k] for each value of $k$, so that we can call B[k][n] with any argument $n$: ...
Roman's user avatar
  • 48.3k
3 votes

memoization based on function expression?

As a pure function: f1[h_, a_, b_] := f1[h, a, b] = Integrate[h[x], {x, a, b}] f1[#^2 &, 1, 2] As a function of x: ...
corey979's user avatar
  • 24k
3 votes
Accepted

Memoization and finding already calculated DownValue

This is just a silly example to show how you can use Cases to grab the previously input values of a function and apply some test to them. Here the function is ...
Jason B.'s user avatar
  • 68.9k
3 votes

Memoization of a function defined by a recurrence relation

Original post This is not an answer to the question, but an extended comment. I was curious if the recursion problem (without memorization) would admit an explicit solution in Mathematica. I can ...
Dr. Wolfgang Hintze's user avatar
3 votes

How to write crash-robust code?

If you can keep a cell on screen, Dynamic can do it. Perhaps not the prettiest of methods, but here's one possible approach: ...
eyorble's user avatar
  • 9,468
3 votes

Recurrence Table Differentiation

Perhaps f[x_] := Exp[7 x] FoldList[D[#, x] &, f[x], Range[10]] {E^(7 x), 7 E^(7 x), 49 E^(7 x), 343 E^(7 x), 2401 E^(7 x), 16807 E^(7 x), 117649 E^(7 x)...
corey979's user avatar
  • 24k
3 votes
Accepted

Memoization only under certain conditions

Define the function twice, once without the memoization, and once with the memoization and your condition. Consider this simple example: ...
march's user avatar
  • 23.9k
3 votes
Accepted

Pattern matching in Association fails in `Set` assignment

I think you're running into the issue described here. To work around it, you can try something like the following: ...
Lukas Lang's user avatar
  • 34.1k
3 votes

Efficient primorial memoization?

Using the usual recursive memoizaton scheme is not practical when evaluating for large numbers, as one easily reach the recursion and memory limits. Also, storing each solution for all integers ...
rhermans's user avatar
  • 36.7k
2 votes
Accepted

Which data tabulation concept is best suited for discrete dynamic programming problems (e.g. for optimum series of account withdrawals)?

If you are interested in increasing performance I would recommend using FindMaximum. If you go through five steps you will discover at the end the original account ...
Jack LaVigne's user avatar
  • 14.5k
2 votes

Saving memoization to disk

Here is another approach with LocalSymbol: ...
tueda's user avatar
  • 793
2 votes
Accepted

Debugging and decision support tools for Dynamic Programming (cached definitions) in Mathematica

A brute-force approach would be to access the private package function used to reset the values of unitaryQ, by executing ...
MarcoB's user avatar
  • 67.4k

Only top scored, non community-wiki answers of a minimum length are eligible