The f[x[i]]
factor out of the problem, so the only problem is to determine the range of the sum. Here is one way (naming the function makeSum
instead of F
).
Edit:
The summation range initially didn't include 0
, but this can be specified as a third argument to IntegerPartitions
:
makeSum[n_, d_] :=
Sum[C @@ i Product[f[i[[j]], x[j]], {j, n}], {i,
Join @@ Table[IntegerPartitions[i, {n}, Range[0, d]], {i, 0, d}]}]
makeSum[2, 5]
(* ==>
C[0, 0] f[0, x[1]] f[0, x[2]] + C[1, 0] f[0, x[2]] f[1, x[1]] +
C[1, 1] f[1, x[1]] f[1, x[2]] + C[2, 0] f[0, x[2]] f[2, x[1]] +
C[2, 1] f[1, x[2]] f[2, x[1]] + C[2, 2] f[2, x[1]] f[2, x[2]] +
C[3, 0] f[0, x[2]] f[3, x[1]] + C[3, 1] f[1, x[2]] f[3, x[1]] +
C[3, 2] f[2, x[2]] f[3, x[1]] + C[4, 0] f[0, x[2]] f[4, x[1]] +
C[4, 1] f[1, x[2]] f[4, x[1]] + C[5, 0] f[0, x[2]] f[5, x[1]] *)
In using IntegerPartitions
, I assumed that you want to avoid double-counting. This may have to be modified if the condition includes permutations:
makeSum[n_, d_] :=
Sum[C @@ i Product[f[i[[j]], x[j]], {j, n}], {i,
Flatten[Permutations /@
Join @@ Table[IntegerPartitions[i, {n}, Range[0, d]], {i, 0, d}],
1]}]
makeSum[2, 5]
(*
==>
C[0, 0] f[0, x[1]] f[0, x[2]] + C[1, 0] f[0, x[2]] f[1, x[1]] +
C[0, 1] f[0, x[1]] f[1, x[2]] + C[1, 1] f[1, x[1]] f[1, x[2]] +
C[2, 0] f[0, x[2]] f[2, x[1]] + C[2, 1] f[1, x[2]] f[2, x[1]] +
C[0, 2] f[0, x[1]] f[2, x[2]] + C[1, 2] f[1, x[1]] f[2, x[2]] +
C[2, 2] f[2, x[1]] f[2, x[2]] + C[3, 0] f[0, x[2]] f[3, x[1]] +
C[3, 1] f[1, x[2]] f[3, x[1]] + C[3, 2] f[2, x[2]] f[3, x[1]] +
C[0, 3] f[0, x[1]] f[3, x[2]] + C[1, 3] f[1, x[1]] f[3, x[2]] +
C[2, 3] f[2, x[1]] f[3, x[2]] + C[4, 0] f[0, x[2]] f[4, x[1]] +
C[4, 1] f[1, x[2]] f[4, x[1]] + C[0, 4] f[0, x[1]] f[4, x[2]] +
C[1, 4] f[1, x[1]] f[4, x[2]] + C[5, 0] f[0, x[2]] f[5, x[1]] +
C[0, 5] f[0, x[1]] f[5, x[2]]
*)