9
$\begingroup$

I define a very simple sphere in 3D as follows:

ℛ = ImplicitRegion[(x - 2)^2 + (y - 3)^2 + (z - 8)^2 == 1, {x, y, z}];

Now when I try to sample some 500 points from it using RandomPoint nothing happens:

points = RandomPoint[ℛ, 500]
(* RandomPoint[
 ImplicitRegion[(-2 + x)^2 + (-3 + y)^2 + (-8 + z)^2 == 1, {x, y, 
   z}], 500] *)

What's the problem?

$\endgroup$
5
  • 7
    $\begingroup$ For a sphere one could go with RandomPoint[Sphere[{2, 3, 8}, 1], 500] $\endgroup$
    – corey979
    Commented Apr 24, 2018 at 15:09
  • $\begingroup$ @corey979 Your suggestion seems more straightforward than any of the current answers, and should probably be an answer itself. Perhaps you could also compare its speed with other approaches? $\endgroup$
    – MarcoB
    Commented Apr 24, 2018 at 16:07
  • 1
    $\begingroup$ Please, be advised that the answer by @corey979 is the appropriate one if you want to sample the sphere with points distributed uniformly $\endgroup$ Commented Apr 24, 2018 at 17:14
  • $\begingroup$ @MarcoB I suspect that the OP used this simple example to illustrate the problem, but in fact asks for something more general (Henrik's answer seems to be general enough), and because it's so straightforward, I will refrain from making it an answer. $\endgroup$
    – corey979
    Commented Apr 24, 2018 at 19:23
  • 2
    $\begingroup$ Interestingly, RandomPoint[ImplicitRegion[x^2 + y^2 == 1, {x, y}]] works, but in 3D it doesn't. Nothing in the documentation (except for a lack of working examples...) indicates it shouldn't work. $\endgroup$
    – corey979
    Commented Apr 24, 2018 at 19:31

3 Answers 3

6
$\begingroup$

It works if you discretize the region first:

ℛ = DiscretizeRegion[
  ImplicitRegion[(x - 2)^2 + (y - 3)^2 + (z - 8)^2 == 1, {x, y, z}],
  MaxCellMeasure -> 0.00001
  ];
points = RandomPoint[ℛ, 500];

The trade-off is that the points do not lie exactly on the sphere; in this example, the is error about 0.01 percent which should be fine for statistical experiments.

$\endgroup$
1
  • $\begingroup$ You could then project onto the original region with RegionNearest. Though this won’t necessarily be a uniform sampling, but probably close enough. $\endgroup$
    – Greg Hurst
    Commented Apr 24, 2018 at 16:15
5
$\begingroup$

I see this behaviour rather erroneous:

reg = ImplicitRegion[(x - 2)^2 + (y - 3)^2 + (z - 8)^2 == 1, {x, y, z}];
RegionPlot3D[reg]

leads to an empty plot, and

RandomPoint[reg]

RandomPoint[ ImplicitRegion[(-2 + x)^2 + (-3 + y)^2 + (-8 + z)^2 == 1, {x, y, z}]]

On the other hand, a similar construct in 2D works:

reg2 = ImplicitRegion[x^2 + y^2 == 1, {x, y}];
RandomPoint[reg2]

{0.961994, 0.27307}

while

RegionPlot[reg2]

gives an empty plot, too.

The behaviour of RegionPlot3D is explained in the docs:

RegionPlot3D[pred,....]

The predicate pred can be any logical combination of inequalities.

Moreover:

RegionPlot3D initially evaluates pred at a 3D grid of equally spaced sample points specified by PlotPoints. Then it uses an adaptive algorithm to subdivide at most MaxRecursion times, attempting to find the boundaries of all regions in which pred is True.

The same can be found in the docs of RegionPlot. Ok, so we shouldn't expect the plots to work.

BUT:

Recall that RandomPoint[reg] didn't work; consider instead

reg3 = ImplicitRegion[(x - 2)^2 + (y - 3)^2 - (z - 8)^2 == 1, {x, y, z}];

where I only changed the sign before $(z-8)^2$. Then:

RandomPoint[reg3]

{6.81984, 4.05919, 3.16754}

with an error:

RandomPoint::unbndreg: The specified region appears to be unbounded. Appropriate bounds will be automatically computed. Explicit bounds may be specified as a third argument.

which can be easily dealt with by providing the third argument:

RandomPoint[reg3, 1, {{-10, 10}, {-10, 10}, {-10, 10}}]

{{-2.33781, 2.75421, 3.77188}}

Hence, I see no reason why RandomPoint, working for reg3, shouldn't work for reg.

$\endgroup$
2
  • 3
    $\begingroup$ The behaviour is the same in v10.4 and v11.3. I see it as a bug. If no one proves me wrong, I will add the tag and report to Wolfram. $\endgroup$
    – corey979
    Commented Apr 24, 2018 at 20:07
  • $\begingroup$ Yeah, that's actually a weird behavior. $\endgroup$ Commented Apr 24, 2018 at 22:35
4
$\begingroup$

It works if we define a finite-thickness shell:

eps = .001;
p = RandomPoint[
   ImplicitRegion[
    1 - eps <= (x - 2)^2 + (y - 3)^2 + (z - 8)^2 <= 1 + eps, {x, y, 
     z}], 1000];

If you make eps smaller it seems to work but becomes extremely slow.

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