Note that there are some explanatory texts on larger screens.

plurals
  1. POAlgorithm for picking pattern free downvalues from a sparse definition list
    text
    copied!<p>I have the following problem.</p> <p>I am developing a stochastic simulator which samples configurations of the system at random and stores the statistics of how many times each configuration has been visited at certain time instances. Roughly the code works like this</p> <pre><code>f[_Integer][{_Integer..}] :=0 ... someplace later in the code, e.g., index = get index; c = get random configuration (i.e. a tuple of integers, say a pair {n1, n2}); f[index][c] = f[index][c] + 1; which tags that configuration c has occurred once more in the simulation at time instance index. </code></pre> <p>Once the code has finished there is a list of definitions for f that looks something like this (I typed it by hand just to emphasize the most important parts)</p> <pre><code>?f f[1][{1, 2}] = 112 f[1][{3, 4}] = 114 f[2][{1, 6}] = 216 f[2][{2, 7}] = 227 ... f[index][someconfiguration] = some value ... f[_Integer][{_Integer..}] :=0 </code></pre> <p>Please note that pattern free definitions that come first can be rather sparse. Also one cannot know which values and configurations will be picked.</p> <p>The problem is to efficiently extract down values for a desired index, for example issue something like </p> <pre><code>result = ExtractConfigurationsAndOccurences[f, 2] </code></pre> <p>which should give a list with the structure</p> <pre><code>result = {list1, list2} </code></pre> <p>where</p> <pre><code>list1 = {{1, 6}, {2, 7}} (* the list of configurations that occurred during the simulation*) list2 = {216, 227} (* how many times each of them occurred *) </code></pre> <p>The problem is that ExtractConfigurationsAndOccurences should be very fast. The only solution I could come up with was to use SubValues[f] (which gives the full list) and filter it with <code>Cases</code> statement. I realize that this procedure should be avoided at any cost since there will be exponentially many configurations (definitions) to test, which slows down the code considerably. </p> <p>Is there a natural way in Mathematica to do this in a fast way?</p> <p>I was hoping that Mathematica would see f[2] as a single head with many down values but using DownValues[f[2]] gives nothing. Also using SubValues[f[2]] results in an error.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload