2
$\begingroup$

I want to sum over a function of n-tuple like so:

$\sum\limits_{a\leq k_1+k_2+...+k_n\leq b}f(\{k_i\}_{i=1}^{n})$

where each $k_i$ is non-negative, i.e. $k_i\geq0 \;\forall\;i $

An Example:

Say, we want to evaluate $\sum\limits_{2\leq k_1+k_2+k_3\leq5}k_1^{k_1!}k_2^{k_2!}k_3^{k_3!}$

My attempt:

f[k_List]:=Product[Power[k[[i]],Factorial[k[[i]]]],{i,1,Length@k}];
tuplelist[x_,n_]:=Sequence @@ Permutations[Join[#, ConstantArray[0, n - Length@#]]] & /@ IntegerPartitions[x, n];
Total@Table[Total@(f[#]&/@tuplelist[i,3]),{i,2,5}]    
(*2248*)

Please suggest if there are ways to modify the existing Sum[] function or other efficient ways to do this. Looking forward to a variety of techniques!

$\endgroup$
3
$\begingroup$

Anjan Kumar's answer is good, but if you want to be able to give the function an expression (like with an ordinary Sum), here's one possibility (edited to make use of J. M.'s suggestion of FrobeniusSolve):

ClearAll[SumOverIntegerPartitions];
Attributes[SumOverIntegerPartitions] = {HoldFirst};
SumOverIntegerPartitions[
   f_, {ks_List, min_Integer?Positive, max_Integer?Positive}] := 
  With[{perms =
     Catenate[
      FrobeniusSolve[ConstantArray[1, Length@ks], #] & /@ 
       Range[min, max]]}, 
   Total[f /. (Thread[ks -> #] & /@ perms)]];

Using the same test case:

SumOverIntegerPartitions[k1^k1!*k2^k2!*k3^k3!, {{k1, k2, k3}, 2, 5}]
(* 2248 *)
$\endgroup$
6
$\begingroup$

You can also use FrobeniusSolve[] for this sum, since it automatically generates 0 in its tuples, unlike IntegerPartitions[]:

Sum[Product[k^k!, {k, m}],
    {m, Flatten[Table[FrobeniusSolve[ConstantArray[1, 3], k], {k, 2, 5}], 1]}]
   2248
$\endgroup$
3
$\begingroup$

You can modify your code to something like this:

f[k_] := Times @@ (#^#! & /@ k);
tuplelist[x_, n_] := Permutations /@ IntegerPartitions[x, {n}, Range[0, x]];
Outer[f, tuplelist[#, 3] & /@ Range[2, 5], 3] // Total[#, 3] &

2248

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.