I have a series of functions $f_d$ in $d$ variables and would like compute the sum of each one evaluated at each lattice point within the $d$-sphere of radius $R$; that is, at each point $(x_1,x_2 \dots x_d) \in \mathbb{Z}^d:\sqrt{x_1^2+x_2^2 \dots +x_d^2} \leq R$.

I have no problem evaluating the functions $f_d$ at these points; I think the best way would be to use Part within Sum on the list of lattice points. What I am unsure of is how to generate $P_R$, the list of lattice points not outside the $d$-sphere of radius $R$.

  • 3
    $\begingroup$ You could use CoordinateBoundsArray[] to generate your lattice points, and then filter them afterward with Select[] or Cases[]. $\endgroup$ Commented May 8, 2020 at 2:31

2 Answers 2


Method 1: select after building the grid

The cubic lattice with side size $2r$ in $d$-dimensions symmetric wrt origin can be defined as:


Then your points are selected as:


Example for 3-sphere:


enter image description here

Method 2: filter during building the grid

Another way of doing this is to check points not after grid is build, but during the process:


To test let's see now $2$-sphere:

Graphics[Point[sphrPTS[30, 2]]]

enter image description here

pts[dim_Integer, num_Integer] := 
 Select[RandomVariate[UniformDistribution[Table[{-1, 1}, dim]], num], 
  Norm[#] <= 1 &]



(Note that this creates 20 sample points, but some subset are within the unit ball.)

  • 1
    $\begingroup$ The OP wants lattice points (i.e. integer-valued coordinates), not random ones. $\endgroup$ Commented May 8, 2020 at 2:45

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.