1
$\begingroup$

I want to write a function in Mathematica which has two inputs 'n' and 'd', and the output should be (in Latex notation)

$$F(n,d) = \sum_{i[1],...,i[n] =0; i[1]+...+i[n]\leq d}^{d} C[i[1],...,i[n]] f[i[1],x[1]] f[i[2],x[2]] ...f[i[n],x[n]]$$

n and d are integers.

The problem is that I can hard code F for a given n and d alright. But when I want to write a generic function which take in any n and d and spits out the required right hand side, it becomes difficult as I have to keep changing the array size along the way depending on the values of n and d. Probably, there is a simpler way to do this in Mathematica?

$\endgroup$

1 Answer 1

3
$\begingroup$

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]]
*)
$\endgroup$
2
  • $\begingroup$ ,Thanks a lot for that! I probably did a silly mistake in the function. The functions f's are not independent of i[j]'s. Thus, makesum[2,5] should give (C[1, 1] f[i[1], x[1]] f[i[1], x[2]] + C[2, 1] f[i[2], x[1]] f[i[1], x[2]] + C[2, 2] f[i[2], x[1]] f[i[2], x[2]] + C[3, 1] f[i[3], x[1]] f[i[1], x[2]] + C[3, 2] f[i[3], x[1]] f[i[2], x[2]] + C[4, 1]) f[i[1], x[4]] f[ i[1], x[2]]. Moreover, for some reason, the loop over d doesn't start from 0. Howe can I change this? Thanks a lot again. dbm368 $\endgroup$
    – dbm
    Commented Mar 27, 2013 at 21:57
  • $\begingroup$ Right, I overlooked the lower bound. It's corrected now. Also, I think I added what you meant in terms of the f[i,x], but I'm not sure if you wanted them to remain symbolic or see their values as it appears in my answer. $\endgroup$
    – Jens
    Commented Mar 27, 2013 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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