Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This has to do with evaluation order. Please see <a href="http://reference.wolfram.com/mathematica/tutorial/Evaluation.html" rel="nofollow noreferrer">Tutorial: Evaluation</a> as a reference.</p> <p><code>Sum</code> has the Attribute <code>HoldAll</code>:</p> <pre><code>Attributes[Sum] </code></pre> <blockquote> <pre><code>{HoldAll, Protected, ReadProtected} </code></pre> </blockquote> <p>Because of this only arguments with certain heads such as <code>Evaluate</code> or <code>Sequence</code> or Symbols with <em>upvalues</em> will evaluate. You may think that your argument <code>Sequence @@ iterators</code> has the head <code>Sequence</code>, but it actually has the head <code>Apply</code>:</p> <pre><code>HoldForm @ FullForm[Sequence @@ iterators] </code></pre> <blockquote> <pre><code>Apply[Sequence, iterators] </code></pre> </blockquote> <p><code>Sum</code> expects literal arguments that match its declared syntax, and thus your code fails. You can force evaluation in several different ways. Arguably the most transparent is to add <code>Evaluate</code>:</p> <pre><code>iterators = {{a, 1, 3}, {b, 5, 7}}; Sum[a^2/b, Evaluate[Sequence @@ iterators]] </code></pre> <blockquote> <pre><code>107/15 </code></pre> </blockquote> <p>More concisely you can leverage <code>Function</code>, <code>SlotSequence</code>, and <code>Apply</code>; evaluation takes place since neither <code>Apply</code>, nor <code>Function</code> by default, has <code>HoldAll</code>:</p> <pre><code>Sum[a^2/b, ##] &amp; @@ iterators </code></pre> <blockquote> <pre><code>107/15 </code></pre> </blockquote> <p>Both of these have a potential problem however: if <code>a</code> or <code>b</code> received a global value the Symbol in the definition of <code>iterators</code> will evaluate to this value causing another error:</p> <pre><code>a = 0; Sum[a^2/b, ##] &amp; @@ iterators </code></pre> <blockquote> <p>Sum::itraw: Raw object 0 cannot be used as an iterator. >></p> </blockquote> <p>Instead you can store the iterator lists in a <code>Hold</code> expression and use the "<a href="https://mathematica.stackexchange.com/a/1937/121">injector pattern</a>" to insert these values without complete evaluation:</p> <pre><code>iterators = Hold[{a, 1, 3}, {b, 5, 7}]; iterators /. _[x__] :&gt; Sum[a^2/b, x] </code></pre> <blockquote> <pre><code>107/15 </code></pre> </blockquote> <p>Alternatively you could define <code>iterators</code> as an <em>upvalue</em>:</p> <pre><code>Sum[args___, iterators] ^:= Sum[args, {a, 1, 3}, {b, 5, 7}] </code></pre> <p>Now simply:</p> <pre><code>Sum[a^2/b, iterators] </code></pre> <blockquote> <pre><code>107/15 </code></pre> </blockquote> <p>Please see my answers to <a href="https://mathematica.stackexchange.com/q/20718/121">Keep function range as a variable</a> on <a href="http://mathematica.stackexchange.com"><em>Mathematica</em>.SE</a> for more examples, as this question is closely related. Specifically see <code>setSpec</code> in my second answer which automates the <em>upvalue</em> creation.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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