How to combine two usages of # into one and speed up the code? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-10-19T19:14:14Z https://mathematica.stackexchange.com/feeds/question/33520 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/33520 0 How to combine two usages of # into one and speed up the code? Eden Harder https://mathematica.stackexchange.com/users/8934 2013-10-06T06:55:50Z 2013-10-07T07:30:50Z <p>I have a function <em>num</em> with two variables, <em>matf</em> and <em>matg</em>. How can I combine the <em>last two</em> lines of code below into one? The code is aimed to find out the maxiaml count <em>bel</em> when we give each \$i3 = 1,\cdots,5\$ a value \$f[[i3]]\$ and each \$i4 = 1,\cdots,5\$ a value \$g[[i4]]\$. How can I speed up this code? Any help or suggestions will be appreciated.</p> <pre><code> FG = Tuples[{0, 1, 2, 3}, 5]; num[matf_, matg_] := Module[{f = matf, g = matg}, bel = 0; For[i3 = 1, i3 &lt;= 5, i3++, For[i4 = 1, i4 &lt;= 5, i4++, If[Mod[IntegerPart[(i3 + i4)/2 - 1], 5] == Mod[f[[i3]] + g[[i4]], 5], bel++]; ]; ]; bel ]; num2[f_] := Max[num[f, #] &amp; /@ FG] Max[num2[#] &amp; /@ FG] </code></pre> <p>Here is an example to explain what does <em>num</em> do.</p> <pre><code>Bell = {}; f = {0, 0, 1, 3, 4}; g = {1, 3, 2, 4, 1}; For[i3 = 1, i3 &lt;= 5, i3++, For[i4 = 1, i4 &lt;= 5, i4++, If[Mod[IntegerPart[(i3 + i4)/2 - 1], 5] == Mod[f[[i3]] + g[[i4]], 5], AppendTo[Bell, {f[[i3]], g[[i4]], i3, i4}]];];]; Bell Bell // Length </code></pre> <p>The output is</p> <pre><code> {{4, 3, 5, 2}, {4, 4, 5, 4}} 2 </code></pre> <p>which means that if we assign each \$i3 = 1,\cdots,5\$ the i3th element in <em>f={0, 0, 1, 3, 4}</em> and \$i4 = 1,\cdots,5\$ the i4th element in <em>g={1, 3, 2, 4, 1}</em>, then there is only \$i3=5\$ (the correspondig value is 4) \$i4=2\$ (the correspondig value is 2) or \$i3=5\$ (the correspondig value is 4) \$i4=4\$ (the correspondig value is 4) satisfy the condition <em>Mod[IntegerPart[(i3 + i4)/2 - 1], 5] == Mod[f[[i3]] + g[[i4]], 5]</em>.</p> https://mathematica.stackexchange.com/questions/33520/-/33535#33535 2 Answer by Michael E2 for How to combine two usages of # into one and speed up the code? Michael E2 https://mathematica.stackexchange.com/users/4999 2013-10-06T13:53:50Z 2013-10-06T18:26:50Z <p>There are two questions embedded in the post, one about combining two lines, and another about efficiency.</p> <h3>Composition</h3> <p>The last two lines,</p> <pre><code>num2[f_] := Max[num[f, #] &amp; /@ FG] Max[num2[#] &amp; /@ FG] </code></pre> <p>are the same as</p> <pre><code>Max[Function[f, Max[num[f, #] &amp; /@ FG]] /@ FG] </code></pre> <p>They are also equivalent to</p> <pre><code>Max[Max /@ Outer[num, FG, FG, 1]] </code></pre> <p>or simply</p> <pre><code>Max[Outer[num, FG, FG, 1]] </code></pre> <h3>Efficiency</h3> <p>Here's one improvement:</p> <pre><code>num3[f_, g_] := With[{gt = Transpose[g]}, Length[f]^2 - Total @ Unitize[ Mod[IntegerPart[(#1 + #2)/2 - 1] - (f[[#1]] + gt[[#2]]), 5] &amp; @@ Transpose @ Tuples[Range @ Length @ f, 2]]] Max[num3[#, FG] &amp; /@ FG] // AbsoluteTiming (* {0.683202, 21} *) </code></pre> <p>Almost 200 times faster than the OP's functions.</p>