3
$\begingroup$

I have a double lattice sum and I was wondering how I could calculate this with Mathematica. In particular, I have a function $F:\mathbb{R}^2 \times \mathbb{R}^2 \to \mathbb{R}$ which takes as arguments a pair of points $x,y\in\mathbb{R}^2$, and I want to evaluate the sum

$$ \sum_{x\in L}\sum_{y\in L} F(x,y) $$

where $L\subset\mathbb{R}^2$ is some finite lattice, i.e. a collection of points in $\mathbb{R}^2$.

Now, I have tried using Outer for this via

Total[Flatten@Outer[F[#1,#2] &, L, L]]

but with this, I obtain a sum of $F$ evaluated at real pairs $s,t\in\mathbb{R}$

F[s,t] + ...

which doesn't make sense, instead of $F$ evaluated at real vector pairs $x=(x_1,x_2),y=(y_1,y_2)\in\mathbb{R}^2$

F[{x_1,x_2},{y_1,y_2}] + ...

as it should be. I'm not sure how to remedy this, but any help with this would be much appreciated.

I am also open to recommendations for any alternative/better ways to perform such a double sum.

Extra Information My function F is defined as a conditional, i.e.

F[{x_,y_},{s_,t_}] = If[Abs[g[{x, y}]] < eps || Abs[g[{s, t}]] < eps, 0, W[{x - s, y - t}] * g[{x, y}] * g[{s, y}]]

for some other lattice functions g and W.

Thanks to answer below, I am able to evaluate the summation, but I obtain as an answer

If[Abs[\[Piecewise] 1 {-(27/2),(7 Sqrt[3])/2}==0&&0<={-27,7 Sqrt[3]}<=2 Sqrt[3]&&{-(27/2)+8 Sqrt[3],Sqrt[3]/2}==0 0 True ]<2.22045*10^-14,0,W[{{-(27/2),(7 Sqrt[3])/2},{-(27/2),(7 Sqrt[3])/2}}] g[{{-(27/2),(7 Sqrt[3])/2},{-(27/2),(7 Sqrt[3])/2}}]] +...

It seems that the double summation has not been executed entirely.

Edit

Thank you for all the solutions and comments, they have been helpful and I shall surely use those in the future. Sadly, due to the nature of my functions, they didn't work perfectly, but that was just because of how my functions were defined.

Although, I have found a solution that seems to work: To evaluate the double sum, I used the Map function twice

Total[Flatten@Map[Function[y, Map[Function[x, F[x,y]], L, L]]]]

I found this solution in another post given by @halirutan

$\endgroup$
2
  • 1
    $\begingroup$ Change the definition of F to F[{x_, y_}, {s_, t_}] = If[Abs[g[{x, y}]] < eps || Abs[g[{s, t}]] < eps, 0, W[{x - s, y - t}]*g[{x, y}]*g[{s, y}]] // PiecewiseExpand However, without the definitions of your functions and eps the result gets unwieldly. $\endgroup$
    – Bob Hanlon
    Commented Sep 20, 2021 at 16:56
  • $\begingroup$ @BobHanlon I have tried that, sadly I get the same problem. Must be the way I defined my other functions. I have found a solution now though. I have updated my answer with it $\endgroup$
    – spaceman
    Commented Sep 20, 2021 at 17:31

2 Answers 2

6
$\begingroup$

You can use Sum to iterate twice through the set of lattice points.

lattice = Catenate@Table[{x, y}, {x, 0, 3}, {y, 0, 3}]

(* {{0, 0}, {0, 1}, {0, 2}, {0, 3}, {1, 0}, {1, 1}, {1, 2}, {1, 3}, {2, 
  0}, {2, 1}, {2, 2}, {2, 3}, {3, 0}, {3, 1}, {3, 2}, {3, 3}} *)

Sum[F[p1, p2], {p1, lattice}, {p2, lattice}]]

(* F[{0, 0}, {0, 0}] + F[{0, 0}, {0, 1}] + F[{0, 0}, {0, 2}] + 
 F[{0, 0}, {0, 3}] + ... *)

Edit: The following old answer can be significantly less memory efficient when performed on a large set of lattice points, as kindly warned by Henrik.

Total[Flatten@Table[F[p1, p2], {p1, lattice}, {p2, lattice}]]
$\endgroup$
3
  • $\begingroup$ Thank you very much for your answer. This makes sense. I have a problem though. My function $F$ is defined via a conditional, and for some reason, my summation does not complete. I will update the question briefly with more details. Would you be able to give insight on this (I understand if not, I will mark the question as complete either way) $\endgroup$
    – spaceman
    Commented Sep 20, 2021 at 15:43
  • 1
    $\begingroup$ Using Sum instead of Total+Table might save a considerable amount of memory... $\endgroup$ Commented Sep 20, 2021 at 19:35
  • $\begingroup$ @Domen Note one may also use ParallelSum, which may be faster, to speed things up. $\endgroup$
    – Hans Olo
    Commented Sep 21, 2021 at 11:24
4
$\begingroup$
sum1 = Total[F @@@ Tuples[Range[0, 3], {2, 2}]]

sum1 // Short
F[{0, 0}, {0, 0}] + F[{0, 0}, {0, 1}] +  F[{0, 0}, {0, 2}] + << 250 >> +
 F[{3, 3}, {3, 1}] + F[{3, 3}, {3, 2}] + F[{3, 3}, {3, 3}]
sum2 = Array[F[{#, #2}, {##3}] &, {4, 4, 4, 4}, 0, Plus];

sum2 == sum1
True
$\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.