3
$\begingroup$

In a multiple Sum I need to put an automatically generated summation region. But when I generate the summation region automatically I get a list whose elements are lists. Then the summation does not work.

For example:

Sum[1,{i,1,2},{j,1,2}]

works;

Sum[1,{{i,1,2},{j,1,2}}]

or

A = Table[{i[j],j+1},{j,2}];
Sum[1,A]

doesn't work.

$\endgroup$
1
  • 3
    $\begingroup$ You could use something like Sum[1,Evaluate[Sequence@@{{i,1,2},{j,1,2}}]] $\endgroup$
    – sebhofer
    Commented Jul 21, 2013 at 21:50

2 Answers 2

6
$\begingroup$

As always there are several possibilities. For example

Sum[1,Evaluate[Sequence@@{{i,1,2},{j,1,2}}]]

or

Sum[1, ##] & @@ {{i, 1, 2}, {j, 1, 2}}

both work.

$\endgroup$
3
$\begingroup$

The syntax for Sum is wrong. For multiple sums, you list each summation index as a separate argument, not enclosed in a list. For example, instead of

Sum[1,{{i,1,2},{j,1,2}}]

you need to write

Sum[1, {i, 1, 2}, {j, 1, 2}]

(* ==> 4 *)

To modify your last example,

Sum[1,A]

you should do this:

A = Table[{i, j}, {i, 1, 2}, {j, 1, 2}];
Sum[1, {tuple, Flatten[A, 1]}]

(* ==> 4 *)

This assumes that your goal was to have A be a list of tuples i, j.

$\endgroup$

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.