0
$\begingroup$

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...

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ – JimB Jan 9 at 20:05
  • $\begingroup$ @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. $\endgroup$ – geom Jan 9 at 22:23
  • $\begingroup$ 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)$ $\endgroup$ – yarchik Jan 9 at 22:25
1
$\begingroup$

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[2] + Sqrt[3] + Sqrt[6]) + (7 + 4 Sqrt[3]) π)} *)

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.

$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$ – geom Jan 9 at 23:10
  • 1
    $\begingroup$ 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. $\endgroup$ – JimB Jan 9 at 23:18

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.