Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is my first post. I hope I get this right.</p> <p>If your inputs are lists of integers, I am skeptical of the value of compiling this function, at least in Mathematica 7.</p> <p>For example:</p> <pre><code>f = Compile[{{a, _Integer, 1}, {b, _Integer, 1}, {c, _Integer, 1}, {d, _Integer, 1}, {e, _Integer, 1}}, Outer[Plus, a, b, c, d, e] ]; a = RandomInteger[{1, 99}, #] &amp; /@ {12, 32, 19, 17, 43}; Do[f @@ a, {50}] // Timing Do[Outer[Plus, ##] &amp; @@ a, {50}] // Timing </code></pre> <p>The two Timings are not significantly different for me, but of course this is only one sample. The point is merely that Outer is already fairly fast compared to the compiled version.</p> <p>If you have reasons other than speed for compilation, you may find some use in Tuples instead of Outer, but you still have the constraint of compiled functions requiring tensor input.</p> <pre><code>f2 = Compile[{{array, _Integer, 2}}, Plus @@@ Tuples@array ]; f2[{{1, 3, 7}, {13, 25, 41}}] </code></pre> <p>If your inputs are large, then a different approach may be in order. Given a list of lists of integers, this function will return the possible sums and the number of ways to get each sum:</p> <pre><code>f3 = CoefficientRules@Product[Sum[x^i, {i, p}], {p, #}] &amp;; f3[{{1, 3, 7}, {13, 25, 41}}] </code></pre> <p>This should prove to be far more memory efficient in many cases.</p> <pre><code>a2 = RandomInteger[{1, 999}, #] &amp; /@ {50, 74, 55, 55, 90, 57, 47, 79, 87, 36}; f3[a2]; // Timing MaxMemoryUsed[] </code></pre> <p>This took 3 seconds and minimal memory, but attempting the application of Outer to a2 terminated the kernel with "No more memory available."</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. 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.
    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