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


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:

{HoldAll, Protected, ReadProtected}

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

 Sum[a[Sequence @@ indicelist], Evaluate[Sequence @@ iteratorlist]]
  • 2
    $\begingroup$ The use of Sequence[] is not too hard to bypass: Sum[a @@ indicelist, ##] & @@ iteratorlist. $\endgroup$ – J. M. will be back soon Oct 15 '12 at 12:13
  • $\begingroup$ @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. $\endgroup$ – jVincent Oct 15 '12 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

Mathematica graphics

Total[a b, -1]

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

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

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.