speed up symbolic summation

I have the following summation

L=24;
sind=Range[-Pi,Pi,2*Pi/L];
Sum[f, {x, sind}, {y, sind}, {x1, sind}, {y1, sind}, {x2, sind}, {y2, sind}]

where f has been evaluated at a previous step and is a function of x,y,x1,x2,y1,y2. However f contains symbols, e.g f= Cos[x]*Sin[y]*Sin[x+x1]*Cos[y+y1]*a[x,y]+g[x,y]*Cos[x2-y2]*Sin[x+x1+x2] where a, g are abstract functions that don't take explicit real values or symbolic values. They will stay as a[-Pi,-Pi] etc at the end of the calculation.

Is there a way to speed up such sums? They are very slow...

• Your specific example contains a typo (Sin[x+x1+x2,y+y1+y2]) and an unknown function (g[x,y]). You're more likely to get answers with a complete specific example.
– JimB
Jan 9 '21 at 20:05
• @JimB thanks for the comment. Typo corrected. a, g are symbolic functions. Their explicit form is unimportant. They will stay as a[-Pi,-Pi], g[-Pi,Pi],... at the end of the summation.
– geom
Jan 9 '21 at 22:23
• Maybe you can write a sum of products as a product of sums? $a_1b_1+a_1b_2+a_2b_1+a_2b_2=(a_1+a_2)(b_1+b_2)$ Jan 9 '21 at 22:25

Instead of evaluating a single sum, you could break up the sum into two or more sums.

L = 24;
sind = Range[-Pi, Pi, 2*Pi/L];
a[x_, y_] := x y
g[x_, y_] := x + y
f = Cos[x]*Sin[y]*Sin[x + x1]*Cos[y + y1]*a[x, y] + g[x, y]*Cos[x2 - y2]*Sin[x + x1 + x2];
AbsoluteTiming[sx = Sum[f, {x, sind}, {x1, sind}, {x2, sind}] // Simplify;
sxy = Sum[sx, {y, sind}, {y1, sind}, {y2, sind}] // Simplify]
(* {1.23668, 625/4 π (52 (2 + Sqrt + Sqrt + Sqrt) + (7 + 4 Sqrt) π)} *)

If a[x,y] and g[x,y] are unknown indexed functions, then still breaking into multiple sums seems feasible:

L = 24;
sind = Range[-Pi, Pi, 2*Pi/L];

fa = Cos[x]*Sin[y]*Sin[x + x1]*Cos[y + y1]*a[x, y];
AbsoluteTiming[sax1x2 = Sum[fa, {x1, sind}, {x2, sind}] // Simplify;
sax1x2y1y2 = Sum[sax1x2, {y1, sind}, {y2, sind}] // Simplify;
sax1x2y1y2y = Sum[sax1x2y1y2, {y, sind}] // Simplify;
sa = Sum[sax1x2y1y2y, {x, sind}];]
(* {0.131486, Null} *)

fg = g[x, y]*Cos[x2 - y2]*Sin[x + x1 + x2];
AbsoluteTiming[
sgx1x2 = Sum[fg, {x1, sind}, {x2, sind}] // Simplify;
sgx1x2y1y2 = Sum[sgx1x2, {y1, sind}, {y2, sind}] // Simplify;
sgx1x2y1y2y = Sum[sgx1x2y1y2, {y, sind}] // Simplify;
sg = Sum[sgx1x2y1y2y, {x, sind}];]
(* {0.13079, Null} *)

And one can certainly speed things up a bit more with fa and fg by noticing that x2 and y2 are not in fa and y1 is not in fg.

• If a, g had explicit forms that would work (along with other solutions). I am mentioning that a, g are symbolic functions that will stay as a[-Pi,Pi] etc
– geom
Jan 9 '21 at 23:10
• I might be wrong but I think you mean "indexed function" rather than "symbolic function" as g[x_,y_]:=a+b y+c x is also a symbolic function.
– JimB
Jan 9 '21 at 23:18