Note that there are some explanatory texts on larger screens.

plurals
  1. POselectively picking pattern free down values in the most efficient way: speed is an issue
    primarykey
    data
    text
    <p>I have the following problem, the code speaks for itself:</p> <pre><code>In[1]:= f[1][{1, 2}] = 112 Out[1]= 112 In[2]:= f[1][{3, 4}] = 114 Out[2]= 114 In[3]:= f[2][{1, 6}] = 216 Out[3]= 216 In[4]:= f[2][{2, 7}] = 227 Out[4]= 227 In[5]:= DownValues[f] Out[5]= {} In[6]:= SubValues[f] Out[6]= {HoldPattern[f[1][{1, 2}]] :&gt; 112, HoldPattern[f[1][{3, 4}]] :&gt; 114, HoldPattern[f[2][{1, 6}]] :&gt; 216, HoldPattern[f[2][{2, 7}]] :&gt; 227} In[7]:= SubValues[f[1]] During evaluation of In[7]:= SubValues::sym: Argument f[1] at position 1 is expected to be a symbol. &gt;&gt; Out[7]= SubValues[f[1]] </code></pre> <p>EDIT: the values above are not hard coded. They are constructed incrementally at run time. They are constructed using following algorithm:</p> <pre><code>f[_Integer][{_Integer..}] :=0 ... someplace later in the code, e.g., index = get index; c = get random configuration (i.e. a pair of integers) = {n1, n2}; f[index][c] = f[index][c] + 1; which tags that configuration c has occured once more in the simulation at time instance index </code></pre> <p>Please note that what happens in the last line is that the left hand side is not evaluated, the right hand side is, first the value for f[index][c] is looked up, if it is not found, then the defult rule is used which gives 0 + 1, if found the old value is incremented by one and stored. All this should be constant time. If one uses arrays, then it will be quadratic complexity (since whole array needs to be copied when one element is added).</p> <p>The problem is that that, later on, I would like SubValues[f<a href="https://stackoverflow.com/questions/7256563/the-most-efficient-way-for-picking-pattern-free-downvalues-from-a-sparse-definiti">1</a>] to give a list of definitions associated with f<a href="https://stackoverflow.com/questions/7256563/the-most-efficient-way-for-picking-pattern-free-downvalues-from-a-sparse-definiti">1</a> that are pattern free but the syntax of SubValues forces me to retrieve all of them. Of course, this has later impact on the speed since one has to extract f<a href="https://stackoverflow.com/questions/7256563/the-most-efficient-way-for-picking-pattern-free-downvalues-from-a-sparse-definiti">1</a>s of interest (e.g. in this example f<a href="https://stackoverflow.com/questions/7256563/the-most-efficient-way-for-picking-pattern-free-downvalues-from-a-sparse-definiti">1</a>[{1, 2}] = 112, and f<a href="https://stackoverflow.com/questions/7256563/the-most-efficient-way-for-picking-pattern-free-downvalues-from-a-sparse-definiti">1</a>[{3, 4}] = 114) from possibly a very long list.</p> <p>Ultimatelly the problem is to harvest f<a href="https://stackoverflow.com/questions/7256563/the-most-efficient-way-for-picking-pattern-free-downvalues-from-a-sparse-definiti">1</a> data so that the following structure is returned:</p> <pre><code>{list1, list2} </code></pre> <p>where</p> <pre><code>list1 = {{1,2}, {3,4}} list2 = {112, 134} </code></pre> <p>I know that one can use Cases[SubValues[f], suitablePattern] and work on the result to get the desired outcome, but I would like to do this more directly and in the most efficient way (since the procedure is repeated many times at runtime).</p> <p>Regards Zoran</p> <p>EDIT: Seems that the problem was not well formulated. The better version of the the same problem can be found here:</p> <p><a href="https://stackoverflow.com/questions/7256563/the-most-efficient-way-for-picking-pattern-free-downvalues-from-a-sparse-definiti">better formulated problem</a></p> <p>so please "abandon the ship" and apologies for not being clear from scratch.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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