I wrote the following Mathematica module:

`QNlalternative[NN_, l_, f_] := Module[{s, wz, w, z, j, lvec}, s = 0; Do[ wz = Table[weightsNodesQ1l@lvec@i, {i, NN}]; w = Table[wz[[i]][[1, All]], {i, NN}]; z = Table[wz[[i]][[2, All]], {i, NN}]; s = s + Function[Sum[(f @@ (Table[z[[i]][[j[i]]], {i, NN}]))*(Times @@ (Table[ w[[i]][[j[i]]], {i, NN}])), ##]] @@ Table[{j[k], 2^lvec[k] + 1}, {k, NN}], ## ] & @@ Table[{lvec[i], l + NN - 1 - Total@Table[lvec[k], {k, i - 1}]}, {i, NN}]; Return[s] ]; `

This module calls another module:

`sumPrime[v_List] := First[v]/2 + Total[Delete[v, 1]] weightsNodes[NN_] := Module[{w, z}, w = Table[4/NN*sumPrime[Table[1/(1 - n^2)*Cos[n*k*Pi/NN], {n, 0., NN, 2}]], {k, 0., NN}]; z = Table[Cos[k*Pi/NN], {k, 0., NN}]; Return[{w, z}] ]; weightsNodesQ1l[l_] := weightsNodes[2^l] `

This code is related to a mathematical problem I am solving (it is a modification). When I first was thinking about how to write the module `QNlalternative`

, I wrote the particular case of `NN=5`

in a sloppy manner, using repeated statements, as follows:

`Q5l[l_, f_] := Module[{s, wzl1, wzl2, wzl3, wzl4, wzl5, wl1, zl1, wl2, zl2, wl3, zl3, wl4, zl4, wl5, zl5}, s = 0; Do[ wzl1 = weightsNodesQ1l[l1]; wzl2 = weightsNodesQ1l[l2]; wzl3 = weightsNodesQ1l[l3]; wzl4 = weightsNodesQ1l[l4]; wzl5 = weightsNodesQ1l[l5]; wl1 = wzl1[[1, All]]; zl1 = wzl1[[2, All]]; wl2 = wzl2[[1, All]]; zl2 = wzl2[[2, All]]; wl3 = wzl3[[1, All]]; zl3 = wzl3[[2, All]]; wl4 = wzl4[[1, All]]; zl4 = wzl4[[2, All]]; wl5 = wzl5[[1, All]]; zl5 = wzl5[[2, All]]; s = s + Sum[f[zl1[[i1]], zl2[[i2]], zl3[[i3]], zl4[[i4]], zl5[[i5]]]* wl1[[i1]]*wl2[[i2]]*wl3[[i3]]*wl4[[i4]]*wl5[[i5]], {i1, 1, 2^l1 + 1}, {i2, 1, 2^l2 + 1}, {i3, 1, 2^l3 + 1}, {i4, 1, 2^l4 + 1}, {i5, 1, 2^l5 + 1}], {l1, 1, l + 5 - 1}, {l2, 1, l + 5 - 1 - l1}, {l3, 1, l + 5 - 1 - l1 - l2}, {l4, 1, l + 5 - 1 - l1 - l2 - l3}, {l5, 1, l + 5 - 1 - l1 - l2 - l3 - l4} ]; Return[s] ]; `

The module `Q5l`

is much faster than `QNlalternative`

:

`AbsoluteTiming[QNlalternative3[5, 6, Sin[Plus[##]]^2 &]] (* {19.4634, 6213.02} *) AbsoluteTiming[Q5l[6, Sin[Plus[##]]^2 &]] (* {6.64357, 6213.02} *) `

Why is `QNlalternative`

slower? Which step of the generalization of `Q5l`

to an arbitrary `NN`

is too slow?