# Mathematica: summing over n abstract indices

This probably has a one line answer, but I'm totally stuck.

I have two tensors, i.e. two objects that depend on NN abstract indices which i've labelled i[m] (m=1,...,NN). I want to keep NN general for now. Each index i[m] ranges over the values {1,2}. I want to sum over all the i[m]s, i.e.

Sum[a[i[1],...,i[NN]] * b[i[1],...,i[NN]], {i[1],1,2},{i[2],1,2},...,{i[NN],1,2}]


The only problem I have is finding a general expression that generates the "array" {i[1],1,2},{i[2],1,2},...,{i[NN],1,2} in the summation. I tried Table but that gives me an array of the form

{{i[1],1,2},{i[2],1,2},...,{i[NN],1,2}}


and I can't get rid of the outer brackets.

What you are looking for is firstly Sequence to get rid of the outer parenthesis. It's usage is fairly simple f[Sequence[3,4]] evaluates to f[3,4], so if you apply it to a list the elements end up being the arguments. The second thing you need to do is get the transformation into a sequence to actually evaluate Sum has attribute HoldAll so if you just write it out it won't evaluate, you need to put Evaluate around it:

Attributes[Sum]

indicelist = Table[i[n], {n, 1, 3}]
iteratorlist = {#, 1, 2} & /@ indicelist;

Sum[a[Sequence @@ indicelist], Evaluate[Sequence @@ iteratorlist]]

• The use of Sequence[] is not too hard to bypass: Sum[a @@ indicelist, ##] & @@ iteratorlist. Commented Oct 15, 2012 at 12:13
• @J.M. I wanted to use sequence because the question specifically mentions "and I can't get rid of the outer brackets.", which i think Sequence solves directly, while function application seems an indirect method of doing this. I used Evaluate rather then passing to highlight the problem arising from not having evaluation of the argument. All that being said, your solution is indeed much nicer. Commented Oct 15, 2012 at 12:36

I have no confidence that I actually understand this question, but acl tried to explain it to me and based on that I think perhaps all you need is: Total[a b, -1].

{a, b} // MatrixForm


Total[a b, -1]


A I + B J + C K + D L + E M + F N + G O + H P

• ah yes, blame it on me if it's wrong :) (joking)
– acl
Commented Oct 15, 2012 at 23:41
• @acl hardly; I make no claim that I understood you either. ;-) Commented Oct 15, 2012 at 23:47
• I'm only joking. You understood me perfectly well, so it is my fault if this is wrong :)
– acl
Commented Oct 15, 2012 at 23:48