# List of lattice points inside $d$-dimensional sphere

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

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

## Method 1: select after building the grid

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

latt[r_,d_]:=Tuples[Range[-r,r],d]


Then your points are selected as:

pts[r_,d_]:=Select[latt[r,d],Norm[#]<=r&]


Example for 3-sphere:

Graphics3D[{Point[pts[10,3]],{Opacity[.2],Sphere[{0,0,0},10]}}]


## 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:

sphrPTS[r_,d_]:=
Flatten[Array[If[Norm[{##}]<=r,{##},Nothing]&,1+2Table[r,d],-r],d-1]


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

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


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


Then

pts[5,20]


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

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