Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

This is inspired by this answerthis 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 herehere and herehere), 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}

enter image description here

but DiscretizeRegion can handle both

DiscretizeRegion /@ {twoDRegion, threeDRegion}

enter image description here

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?

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}

enter image description here

but DiscretizeRegion can handle both

DiscretizeRegion /@ {twoDRegion, threeDRegion}

enter image description here

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?

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}

enter image description here

but DiscretizeRegion can handle both

DiscretizeRegion /@ {twoDRegion, threeDRegion}

enter image description here

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?

Tweeted twitter.com/StackMma/status/696654041552699392
Source Link
Jason B.
  • 70.1k
  • 3
  • 144
  • 297

RandomPoint works with a 2D ImplicitRegion but fails for any higher dimension

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}

enter image description here

but DiscretizeRegion can handle both

DiscretizeRegion /@ {twoDRegion, threeDRegion}

enter image description here

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?