Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Mathematica, how do I compile the function Outer[] for an arbitrary number of arguments?
    primarykey
    data
    text
    <p>If I want to find all possible sums from two lists <code>list1</code> and <code>list2</code>, I use the <code>Outer[]</code> function with the specification of <code>Plus</code> as the combining operator:</p> <p><code>In[1]= list1 = {a, b}; list2 = {c, d}; Outer[Plus, list1, list2]</code></p> <p><code>Out[1]= {{a + c, a + d}, {b + c, b + d}}</code></p> <p>If I want to be able to handle an arbitrary number of lists, say a list of lists,</p> <p><code>In[2]= listOfLists={list1, list2};</code></p> <p>then the only way I know how to find all possible sums is to use the <code>Apply[]</code> function (which has the short hand <code>@@</code>) along with <code>Join</code>:</p> <p><code>In[3]= argumentsToPass=Join[{Plus},listOfLists]</code></p> <p><code>Out[3]= {Plus, {a, b}, {c, d}}</code></p> <p><code>In[4]= Outer @@ argumentsToPass</code></p> <p><code>Out[4]= {{a + c, a + d}, {b + c, b + d}}</code></p> <p>or simply</p> <p><code>In[5]= Outer @@ Join[{Plus},listOfLists]</code></p> <p><code>Out[5]= {{a + c, a + d}, {b + c, b + d}}</code></p> <p>The problem comes when I try to compile:</p> <p><code>In[6]= Compile[ ..... Outer @@ Join[{Plus},listOfLists] .... ]</code></p> <p><code>Compile::cpapot: "Compilation of Outer@@Join[{Plus},listOfLists]] is not supported for the function argument Outer. The only function arguments supported are Times, Plus, or List. Evaluation will use the uncompiled function. "</code></p> <p>The thing is, I <em>am</em> using a supported function, namely <code>Plus</code>. The problem seems to be solely with the <code>Apply[]</code> function. Because if I give it a fixed number of lists to outer-plus together, it works fine</p> <p><code>In[7]= Compile[{{bob, _Integer, 1}, {joe, _Integer, 1}}, Outer[Plus, bob, joe]]</code></p> <p><code>Out[7]= CompiledFunction[{bob, joe}, Outer[Plus, bob, joe],-CompiledCode-]</code></p> <p>but as soon as I use <code>Apply</code>, it breaks</p> <p><code>In[8]= Compile[{{bob, _Integer, 1}, {joe, _Integer, 1}}, Outer @@ Join[{Plus}, {bob, joe}]]</code></p> <p><code>Out[8]= Compile::cpapot: "Compilation of Outer@@Join[{Plus},{bob,joe}] is not supported for the function argument Outer. The only function arguments supported are Times, Plus, or List. Evaluation will use the uncompiled function."</code></p> <p><strong>So my questions is</strong>: Is there a way to circumvent this error or, alternatively, a way to compute all possible sums of elements pulled from an arbitrary number of lists in a compiled function?</p> <p>(Also, I'm not sure if "compilation" is an appropriate tag. Please advise.)</p> <p>Thanks so much.</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