# How to speedup evaluation of a random point from an implicit region?

Consider three variables s2,t1,t2, and parameters ma,mb,m1,m2,m3,s.

The domain of definition for s2,t1,t2 is given by the region Region3Particle:

matrix\[CapitalDelta]4 = {{2*s2, s2 - t1 + mb^2, s + s2 - m1^2,
s2 - m2^2 + m3^2}, {s2 - t1 + mb^2, 2 mb^2, s - ma^2 + mb^2,
mb^2 + m3^2 - t2}, {s + s2 - m1^2, s - ma^2 + mb^2, 2 s,
s - s1 + m3^2}, {s2 - m2^2 + m3^2, mb^2 + m3^2 - t2,
s - s1 + m3^2, 2 m3^2}};
matrix\[CapitalDelta]4 // MatrixForm
\[CapitalDelta]4val[ma_, mb_, m1_, m2_, m3_, s_, s2_, t1_, t2_, s1_] =
1/16 Det[matrix\[CapitalDelta]4] // Simplify;
s1sol[ma_, mb_, m1_, m2_, m3_, s_, s2_, t1_, t2_] =
s1 /. Solve[\[CapitalDelta]4val[ma, mb, m1, m2, m3, s, s2, t1, t2,
s1] == 0, s1] // Simplify;
ConditionRegion[ma_, mb_, m1_, m2_, m3_, s_, s2_, t1_,
t2_] = (s1sol[ma, mb, m1, m2, m3, s, s2, t1, t2][[2]] -
s1sol[ma, mb, m1, m2, m3, s, s2, t1, t2][[1]] // Simplify)^2 //
Simplify // Numerator;
Region3Particle[ma_, mb_, m1_, m2_, m3_, s_] =
ImplicitRegion[
ConditionRegion[ma, mb, m1, m2, m3, s, s2, t1, t2] >= 0, {s2,
t1, t2}];

rp[ma_, mb_, m1_, m2_, m3_, s_] :=
RandomPoint[Region3Particle[ma, mb, m1, m2, m3, s]]
maval=0;
mbval=0.938;
m1val=RandomReal[{0.,13}];
m2val=0.105;
m3val=0.938;
sval=RandomReal[{(m1val+m2val+m3val)^2,1000*(m1val+m2val+m3val)^2}];

However, the evaluation turns out to be extremely slow:

rp[maval, mbval, m1val, m2val, m3val, sval]//AbsoluteTiming

{4.957888,{0.383624,0.578836,0.790515}}

Could you please tell me how to improve the performance of this evaluation?

My attempt is to compile the overall calculation:

ConditionRegionCompiled =
Compile[{{ma, _Real}, {mb, _Real}, {m1, _Real}, {m2, _Real}, {m3, \
_Real}, {s, _Real}, {s2, _Real}, {t1, _Real}, {t2, _Real}},
ConditionRegion[ma, mb, m1, m2, m3, s, s2, t1, t2]];
ConditionRegionCompiledN[ma_?NumericQ, mb_?NumericQ, m1_?NumericQ,
m2_?NumericQ, m3_?NumericQ, s_?NumericQ, s2_?NumericQ, t1_?NumericQ,
t2_?NumericQ] :=
ConditionRegionCompiled[ma, mb, m1, m2, m3, s, s2, t1, t2]
Region3ParticleCompiled[ma_, mb_, m1_, m2_, m3_, s_] :=
ImplicitRegion[
ConditionRegion[ma, mb, m1, m2, m3, s, s2, t1, t2] >= 0, {s2, t1,
t2}];
rpCompiled[ma_, mb_, m1_, m2_, m3_, s_] :=
RandomPoint[Region3ParticleCompiled[ma, mb, m1, m2, m3, s]]

But rpCompiled does not show any improvement over rp...

## 1 Answer

Edit

When we limit the range of BoundaryDiscretizeRegion, the RandomPoint work.

Clear[reg, pts];
reg = ImplicitRegion[{ConditionRegion[maval, mbval, m1val, m2val,
m3val, sval, s2, t1, t2] >= 0}, {s2, t1, t2}];
dreg = BoundaryDiscretizeRegion[
reg, {{-1000, 1000}, {-1000, 1000}, {-1000, 1000}}];
pts = RandomPoint[dreg,
1000];
Graphics3D[{FaceForm[{Opacity[.5], Cyan}], EdgeForm[], dreg,
Red, Point[pts]}, Boxed -> False]

Original

It seems that it is not easy to use DiscretizeRegion to such region to speedup the RandomPoint. We try to use FindInstance instead of RandomPoint.

Clear[reg, sols, pts, bd];
reg = ImplicitRegion[{ConditionRegion[maval, mbval, m1val, m2val,
m3val, sval, s2, t1, t2] >= 0}, {s2, t1, t2}];
sols = FindInstance[{s2, t1, t2} ∈ reg, {s2, t1, t2}, 1000];
pts = {s2, t1, t2} /. sols;
bd = MinMax[pts] // N;
Show[Region[Style[reg, Directive[Yellow, Opacity[.5]]],
PlotRange -> bd], ListPointPlot3D[pts, PlotStyle -> Red]]