You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alex Herbert (Jira)" <ji...@apache.org> on 2021/05/09 15:45:00 UTC

[jira] [Commented] (RNG-135) TetrahedronSampler: Sample uniformly from a tetrahedron

    [ https://issues.apache.org/jira/browse/RNG-135?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17341540#comment-17341540 ] 

Alex Herbert commented on RNG-135:
----------------------------------

Many of the shape samplers have a performance benefit from using primitives rather than small arrays to store the coordinates. I tested the tetrahedron sampler with array based or non array based coordinates. The sampling method has 3 main branches to create the uniform deviates s, t, u which together sum to less than 1. These can be used to create the sample:
{code:java}
// generate s, t, u
if (...) {    
    if (...) {
        // update s, t, u
        return createSample(s, t, u);
    }
    // update s, t, u
    return createSample(s, t, u);
}
return createSample(s, t, u);
{code}
This can be rewritten without a method call to create the sample by falling through and creating the sample at the end of the method:
{code:java}
// generate s, t, u
if (...) {    
    if (...) {
        // update s, t, u
    } else {
        // update s, t, u
    }
}
return createSample(s, t, u);
{code}
I added a JMH benchmark for the following:
||Method||Array coordinates||createSample method||
|Array|Y|Y|
|NonArray|N|Y|
|ArrayFallThrough|Y|N|
|NonArrayFallThrough|N|N|

Results:
||Size||Method||Score||Error||Relative Score||
|1|Baseline|15.83|1.09|1.00|
|1|Array|41.60|1.85|2.63|
|1|NonArray|39.61|4.99|2.50|
|1|ArrayFallThrough|44.54|2.42|2.81|
|1|NonArrayFallThrough|43.64|1.47|2.76|
|10|Baseline|147.27|4.49|1.00|
|10|Array|394.20|9.45|2.68|
|10|NonArray|400.87|9.24|2.72|
|10|ArrayFallThrough|423.88|14.23|2.88|
|10|NonArrayFallThrough|436.69|18.36|2.97|
|100|Baseline|1463.39|42.56|1.00|
|100|Array|3744.30|233.21|2.56|
|100|NonArray|3937.12|70.56|2.69|
|100|ArrayFallThrough|3854.06|41.60|2.63|
|100|NonArrayFallThrough|4116.94|63.63|2.81|
|1000|Baseline|14729.29|563.14|1.00|
|1000|Array|37375.66|672.31|2.54|
|1000|NonArray|37844.20|957.48|2.57|
|1000|ArrayFallThrough|40076.72|3939.63|2.72|
|1000|NonArrayFallThrough|42093.05|919.01|2.86|
|10000|Baseline|134170.53|4274.78|1.00|
|10000|Array|380554.77|15832.90|2.84|
|10000|NonArray|407982.62|6280.78|3.04|
|10000|ArrayFallThrough|381376.47|13830.11|2.84|
|10000|NonArrayFallThrough|402414.90|7756.01|3.00|

For size 1 the non-array based coordinates are faster. For all other sizes the array-based coordinates are faster. This is a reversal of the JMH results for other shape samplers where the array coordinates had a performance penalty.

In almost all cases the method call to create the sample is faster than the fall-through to create the sample.

The code in the PR uses the array based coordinates with the method call.

> TetrahedronSampler: Sample uniformly from a tetrahedron
> -------------------------------------------------------
>
>                 Key: RNG-135
>                 URL: https://issues.apache.org/jira/browse/RNG-135
>             Project: Commons RNG
>          Issue Type: New Feature
>          Components: sampling
>    Affects Versions: 1.4
>            Reporter: Alex Herbert
>            Assignee: Alex Herbert
>            Priority: Minor
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> Create a sampler to sample uniformly within a [tetrahedron|https://en.wikipedia.org/wiki/Tetrahedron].
>  
> {code:java}
> public abstract class TetrahedronSampler implements 
>         SharedStateSampler<TetrahedronSampler> {
>     public static TetrahedronSampler of(double[] a,
>                                         double[] b,
>                                         double[] c,
>                                         double[] d,
>                                         UniformRandomProvider rng);
> }
> {code}
> Inputs {{a,b,c,d}} are the vertices.
> Sampling can be performed using the algorithm of:
> {noformat}
> Rocchini, C & Cignoni, P (2001)
> Generating Random Points in a Tetrahedron.
> Journal of Graphics Tools 5(4), pp 9-12.
> {noformat}
> [DOI: 10.1080/10867651.2000.10487528|https://dx.doi.org/10.1080/10867651.2000.10487528]



--
This message was sent by Atlassian Jira
(v8.3.4#803005)