This is inspired by this answer which features code that I cannot get to work at all, and I tried to figure out why.
It boils down to using the function RandomPoint
, which is new and a little bit tricky to use (see here and here), in combination with ImplicitRegion
.
Here are two and three dimensional regions,
twoDRegion =
ImplicitRegion[{0 <= a <= 1, 0 <= b <= 1, a + b == 1}, {a, b}];
threeDRegion =
ImplicitRegion[{0 <= a <= 1, 0 <= b <= 1, 0 <= c <= 1,
a + b + c == 1}, {a, b, c}];
These regions are the set of all positive tuples that sum to 1,
RandomPoint
works on the 2D version, but not the 3D version,
RandomPoint /@ {twoDRegion, threeDRegion}
(* {{0.146978, 0.853022},
RandomPoint[
ImplicitRegion[
0 <= a <= 1 && 0 <= b <= 1 && 0 <= c <= 1 && a + b + c == 1, {a, b,
c}]]} *)
Interestingly, RegionPlot
also has trouble with the 3D version,
RegionPlot /@ {twoDRegion, threeDRegion}
but DiscretizeRegion
can handle both
DiscretizeRegion /@ {twoDRegion, threeDRegion}
This led me to think that I just need to discretize before selecting the random point, and this in fact works for the 3D case
RandomPoint@DiscretizeRegion@# & /@ {twoDRegion, threeDRegion}
(* {{0.579605, 0.420395}, {0.154819, 0.0404491, 0.804732}} *)
But it won't work for higher dimensions since DiscretizeRegion
is limited to 3 dimensions or lower.
Is this a bug?
BooleanRegion
s of withn>3
. $\endgroup$