3
$\begingroup$

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.

$\endgroup$

2 Answers 2

4
$\begingroup$

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]
{HoldAll, Protected, ReadProtected}

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

 Sum[a[Sequence @@ indicelist], Evaluate[Sequence @@ iteratorlist]]
$\endgroup$
2
  • 2
    $\begingroup$ The use of Sequence[] is not too hard to bypass: Sum[a @@ indicelist, ##] & @@ iteratorlist. $\endgroup$ Oct 15, 2012 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, 2012 at 12:36
2
$\begingroup$

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

$\endgroup$
3
  • 1
    $\begingroup$ ah yes, blame it on me if it's wrong :) (joking) $\endgroup$
    – acl
    Oct 15, 2012 at 23:41
  • $\begingroup$ @acl hardly; I make no claim that I understood you either. ;-) $\endgroup$
    – Mr.Wizard
    Oct 15, 2012 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, 2012 at 23:48

Your Answer

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

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