Skip to content

Commit 17b5854

Browse files
feat: add C implementation for stats/base/dists/triangular/cdf
PR-URL: #4669 Closes: #4668 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]>
1 parent 59e3497 commit 17b5854

File tree

19 files changed

+1447
-10
lines changed

19 files changed

+1447
-10
lines changed

lib/node_modules/@stdlib/stats/base/dists/triangular/cdf/README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,110 @@ for ( i = 0; i < 25; i++ ) {
148148

149149
<!-- /.examples -->
150150

151+
152+
<!-- C interface documentation. -->
153+
154+
* * *
155+
156+
<section class="c">
157+
158+
## C APIs
159+
160+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
161+
162+
<section class="intro">
163+
164+
</section>
165+
166+
<!-- /.intro -->
167+
168+
<!-- C usage documentation. -->
169+
170+
<section class="usage">
171+
172+
### Usage
173+
174+
```c
175+
#include "stdlib/stats/base/dists/triangular/cdf.h"
176+
```
177+
178+
#### stdlib_base_dists_triangular_cdf( x, a, b, c )
179+
180+
Evaluates the [cumulative distribution function][cdf] (CDF) for a [triangular][triangular-distribution] distribution with parameters `a` (lower limit), `b` (upper limit) and `c` (mode).
181+
182+
```c
183+
double y = stdlib_base_dists_triangular_cdf( 0.5, -1.0, 1.0, 0.0 );
184+
// returns 0.875
185+
```
186+
187+
The function accepts the following arguments:
188+
189+
- **x**: `[in] double` input value.
190+
- **a**: `[in] double` lower limit.
191+
- **b**: `[in] double` upper limit.
192+
- **c**: `[in] double` mode.
193+
194+
```c
195+
double stdlib_base_dists_triangular_cdf( const double x, const double a, const double b, const double c );
196+
```
197+
198+
</section>
199+
200+
<!-- /.usage -->
201+
202+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
203+
204+
<section class="notes">
205+
206+
</section>
207+
208+
<!-- /.notes -->
209+
210+
<!-- C API usage examples. -->
211+
212+
<section class="examples">
213+
214+
### Examples
215+
216+
```c
217+
#include "stdlib/stats/base/dists/triangular/cdf.h"
218+
#include "stdlib/constants/float64/eps.h"
219+
#include <stdlib.h>
220+
#include <stdio.h>
221+
#include <math.h>
222+
223+
static double random_uniform( const double min, const double max ) {
224+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
225+
return min + ( v*(max-min) );
226+
}
227+
228+
int main( void ) {
229+
double a;
230+
double b;
231+
double c;
232+
double x;
233+
double y;
234+
int i;
235+
236+
for ( i = 0; i < 25; i++ ) {
237+
x = random_uniform( 0.0, 30.0 );
238+
a = random_uniform( 0.0, 10.0 );
239+
b = random_uniform( a, 40.0 ) + STDLIB_CONSTANT_FLOAT64_EPS;
240+
c = a + random_uniform( 0.0, b - a );
241+
y = stdlib_base_dists_triangular_cdf( x, a, b, c );
242+
printf( "x: %lf, a: %lf, b: %lf, c: %lf, F(x;a,b,c): %lf\n", x, a, b, c, y );
243+
}
244+
}
245+
```
246+
247+
</section>
248+
249+
<!-- /.examples -->
250+
251+
</section>
252+
253+
<!-- /.c -->
254+
151255
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
152256

153257
<section class="related">

lib/node_modules/@stdlib/stats/base/dists/triangular/cdf/benchmark/benchmark.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var Float64Array = require( '@stdlib/array/float64' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
2526
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2627
var EPS = require( '@stdlib/constants/float64/eps' );
2728
var pkg = require( './../package.json' ).name;
@@ -34,17 +35,26 @@ bench( pkg, function benchmark( b ) {
3435
var mode;
3536
var min;
3637
var max;
38+
var len;
3739
var x;
3840
var y;
3941
var i;
4042

43+
len = 100;
44+
x = new Float64Array( len );
45+
min = new Float64Array( len );
46+
max = new Float64Array( len );
47+
mode = new Float64Array( len );
48+
for ( i = 0; i < len; i++ ) {
49+
x[ i ] = uniform( 0.0, 30.0 );
50+
min[ i ] = uniform( 0.0, 10.0 );
51+
max[ i ] = uniform( min[ i ] + EPS, min[ i ] + 40.0 );
52+
mode[ i ] = uniform( min[ i ], max[ i ] );
53+
}
54+
4155
b.tic();
4256
for ( i = 0; i < b.iterations; i++ ) {
43-
x = randu() * 30.0;
44-
min = randu() * 10.0;
45-
max = min + ( randu() * 40.0 ) + EPS;
46-
mode = min + ( ( max - min ) * randu() );
47-
y = cdf( x, min, max, mode );
57+
y = cdf( x[ i % len ], min[ i % len ], max[ i % len ], mode[ i % len ] );
4858
if ( isnan( y ) ) {
4959
b.fail( 'should not return NaN' );
5060
}
@@ -62,6 +72,7 @@ bench( pkg+':factory', function benchmark( b ) {
6272
var mode;
6373
var min;
6474
var max;
75+
var len;
6576
var x;
6677
var y;
6778
var i;
@@ -70,11 +81,15 @@ bench( pkg+':factory', function benchmark( b ) {
7081
max = 1.5;
7182
mode = 0.5;
7283
mycdf = cdf.factory( min, max, mode );
84+
len = 100;
85+
x = new Float64Array( len );
86+
for ( i = 0; i < len; i++ ) {
87+
x[ i ] = uniform( -2.0, 2.0 );
88+
}
7389

7490
b.tic();
7591
for ( i = 0; i < b.iterations; i++ ) {
76-
x = ( randu()*2.0 ) - 2.0;
77-
y = mycdf( x );
92+
y = mycdf( x[ i % len ] );
7893
if ( isnan( y ) ) {
7994
b.fail( 'should not return NaN' );
8095
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var uniform = require( '@stdlib/random/base/uniform' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var EPS = require( '@stdlib/constants/float64/eps' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var cdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( cdf instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( pkg+'::native', opts, function benchmark( b ) {
44+
var mode;
45+
var min;
46+
var max;
47+
var len;
48+
var x;
49+
var y;
50+
var i;
51+
52+
len = 100;
53+
x = new Float64Array( len );
54+
min = new Float64Array( len );
55+
max = new Float64Array( len );
56+
mode = new Float64Array( len );
57+
for ( i = 0; i < len; i++ ) {
58+
x[ i ] = uniform( 0.0, 30.0 );
59+
min[ i ] = uniform( 0.0, 10.0 );
60+
max[ i ] = uniform( min[ i ] + EPS, min[ i ] + 40.0 );
61+
mode[ i ] = uniform( min[ i ], max[ i ] );
62+
}
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
y = cdf( x[ i%len ], min[ i%len ], max[ i%len ], mode[ i%len ] );
67+
if ( isnan( y ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( y ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});

0 commit comments

Comments
 (0)