Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You probably should've posted these as separate questions, but no worries!</p> <h1>Problem one</h1> <p>The problem with Alice is of course what you expect. The problem with Bob is that the inner <code>Table</code> is evaluated once per iteration of the outer <code>Table</code>. This is clearly visible with Trace:</p> <pre><code>Trace[Table[Table[i, {i, 1, 3}], {3}]] { Table[Table[i,{i,1,2}],{2}], {Table[i,{i,1,2}],{i,1},{i,2},{1,2}},{Table[i,{i,1,2}],{i,1},{i,2},{1,2}}, {{1,2},{1,2}} } </code></pre> <p>Line breaks added for emphasis, and yeah, the output of Trace on Table is a little weird, but you can see it. Clearly Mathematica could optimize this better, knowing that the outside table has no iterator, but for whatever reason, it doesn't take that into account. Only Chris does what you want, though you could modify Bob:</p> <pre><code>Transpose[Table[Evaluate[Table[V[i],{i,1,300}]],{1000}]] </code></pre> <p>This looks like it actually outperforms Chris by a factor of two or so, because it doesn't have to store the intermediate result.</p> <h1>Problem two</h1> <p>There's a simpler solution with Evaluate, though I expect it won't work with all possible functions to be compiled (i.e. ones that really should be Held):</p> <pre><code>f1 = Compile[{x}, Evaluate[x + y]]; </code></pre> <p>You could also use a <code>With</code>:</p> <pre><code>With[{y=7.3}, f1 = Compile[{x}, x + y]; ] </code></pre> <p>Or if y is defined elsewhere, use a temporary:</p> <pre><code>y = 7.3; With[{z = y}, f1 = Compile[{x}, x + z]; ] </code></pre> <p>I'm not an expert on Mathematica's scoping and evaluation mechanisms, so there could easily be a much better way, but hopefully one of those does it for you!</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