Pretty simple question: how do I sum over multiple indices that can only take specific values all together?

To clarify: let's say I have the following sum:

$\sum p_{a,b,c} f(a,b,c)$

where $f$ is a function and $p$ is a coefficient (number) still in symbolic form, both dependent on indices $a,b,c$. Here {$a$, $b$, $c$} can take values specified by a list of three numbers, like {1,2,3}, {3,6,7} and so on. My idea is that I have a list of triplets, and I would like the sum to run on these triplets by assigning $a$ to the first number, $b$ to the second and $c$ to the third. So let's say that for a list of triplets


The outcome should be


I was able to do it with Apply if there was only a function, but the coefficients $p$ need to carry the indices so it becomes tricky.

  • $\begingroup$ I'm not understanding what you mean by p_{1,2,3} as that translates to {p_,2 p_, 3 p_}. But if p_ is really a function that takes a list as an argument, then you might want to try Sum[p[list[[i]]] f[list[[i, 1]], list[[i, 2]], list[[i, 3]]], {i, Length[list]}] which gets you f[1, 2, 3] p[{1, 2, 3}] + f[2, 6, 9] p[{2, 6, 9}] + f[3, 6, 7] p[{3, 6, 7}]. $\endgroup$
    – JimB
    Jun 9 '15 at 13:47
  • $\begingroup$ So $p$ is actually also a function of $a,b,c$? $\endgroup$ Jun 9 '15 at 13:51
  • $\begingroup$ no, $p$ carries indices $a,b,c$ but they are just labels. i edited the question. $\endgroup$
    – user50473
    Jun 9 '15 at 13:55
  • $\begingroup$ So $p$ is symbolic and not numerical? $\endgroup$ Jun 9 '15 at 14:00
  • $\begingroup$ If p is subscripted (and using subscripts can have definite but sometimes unexpected consequences), then the following might be what you want: Sum[Subscript[p, list[[i, 1]], list[[i, 2]], list[[i, 3]]] f[list[[i, 1]], list[[i, 2]], list[[i, 3]]], {i, Length[list]}] with output f[1, 2, 3] Subscript[p, 1, 2, 3] + f[2, 6, 9] Subscript[p, 2, 6, 9] + f[3, 6, 7] Subscript[p, 3, 6, 7]. $\endgroup$
    – JimB
    Jun 9 '15 at 14:10

Here's one way:

Total[Subscript[p, #] & /@ lst f @@@ lst]


Total[Subscript[p, #] & /@ lst f /@ lst]

But you're probably making a mistake defining your p's this way, especially if you later need to do something with them. If you are willing to define p as a function, then

Total[p @@@ lst f @@@ lst]

would work.

  • $\begingroup$ (p @@@ lst).(f @@@ lst) will also work. $\endgroup$ Jun 9 '15 at 22:41

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.