Skip to content

Commit 373cd28

Browse files
feat: add C implementation for stats/base/dists/discrete-uniform/logcdf
PR-URL: #4644 Closes: #3552 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: JoyBoy <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]>
1 parent 22812ea commit 373cd28

File tree

18 files changed

+1399
-37
lines changed

18 files changed

+1399
-37
lines changed

lib/node_modules/@stdlib/stats/base/dists/discrete-uniform/logcdf/README.md

Lines changed: 116 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,31 +122,136 @@ y = myLogCDF( 8.0 );
122122
<!-- eslint no-undef: "error" -->
123123

124124
```javascript
125-
var randint = require( '@stdlib/random/base/discrete-uniform' );
126-
var randu = require( '@stdlib/random/base/randu' );
125+
var uniform = require( '@stdlib/random/array/uniform' );
126+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
127127
var logcdf = require( '@stdlib/stats/base/dists/discrete-uniform/logcdf' );
128128

129-
var randa = randint.factory( 0, 10 );
130-
var randb = randint.factory();
129+
var x;
131130
var a;
132131
var b;
133-
var x;
134132
var v;
135133
var i;
136134

137-
for ( i = 0; i < 10; i++ ) {
138-
x = randu() * 15.0;
139-
a = randa();
140-
b = randb( a, a+randa() );
141-
v = logcdf( x, a, b );
142-
console.log( 'x: %d, a: %d, b: %d, ln(F(x;a,b)): %d', x.toFixed( 4 ), a.toFixed( 4 ), b.toFixed( 4 ), v.toFixed( 4 ) );
135+
x = uniform( 10, -10.0, 10.0 );
136+
a = discreteUniform( 10, -10, 0 );
137+
b = discreteUniform( 10, 0, 10 );
138+
139+
for ( i = 0; i < x.length; i++ ) {
140+
v = logcdf( x[ i ], a[ i ], b[ i ] );
141+
console.log( 'x: %d, a: %d, b: %d, ln(F(x;a,b)): %d', x[ i ].toFixed( 4 ), a[ i ], b[ i ], v.toFixed( 4 ) );
142+
}
143+
```
144+
145+
</section>
146+
147+
<!-- /.examples -->
148+
149+
<!-- C interface documentation. -->
150+
151+
<section class="c">
152+
153+
## C APIs
154+
155+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
156+
157+
<section class="intro">
158+
159+
</section>
160+
161+
<!-- /.intro -->
162+
163+
<!-- C usage documentation. -->
164+
165+
<section class="usage">
166+
167+
### Usage
168+
169+
```c
170+
#include "stdlib/stats/base/dists/discrete-uniform/logcdf.h"
171+
```
172+
173+
#### stdlib_base_dists_discrete_uniform_logcdf( x, a, b )
174+
175+
Evaluates the natural logarithm of the [cumulative distribution function][cdf] (CDF) for a [discrete uniform][discrete-uniform-distribution] distribution with parameters `a` (minimum support) and `b` (maximum support).
176+
177+
```c
178+
double out = stdlib_base_dists_discrete_uniform_logcdf( 9.0, 0, 10 );
179+
// returns ~-0.095
180+
```
181+
182+
The function accepts the following arguments:
183+
184+
- **x**: `[in] double` input value.
185+
- **a**: `[in] int32_t` minimum support.
186+
- **b**: `[in] int32_t` maximum support.
187+
188+
```c
189+
double stdlib_base_dists_discrete_uniform_logcdf( const double x, const int32_t a, const int32_t b );
190+
```
191+
192+
</section>
193+
194+
<!-- /.usage -->
195+
196+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
197+
198+
<section class="notes">
199+
200+
</section>
201+
202+
<!-- /.notes -->
203+
204+
<!-- C API usage examples. -->
205+
206+
<section class="examples">
207+
208+
### Examples
209+
210+
```c
211+
#include "stdlib/stats/base/dists/discrete-uniform/logcdf.h"
212+
#include "stdlib/math/base/special/round.h"
213+
#include <stdint.h>
214+
#include <stdlib.h>
215+
#include <stdio.h>
216+
217+
static double random_uniform( const double min, const double max ) {
218+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
219+
return min + ( v*(max-min) );
220+
}
221+
222+
int main( void ) {
223+
int32_t a;
224+
int32_t b;
225+
double x;
226+
double y;
227+
int i;
228+
229+
for ( i = 0; i < 10; i++ ) {
230+
x = random_uniform( -10.0, 10.0 );
231+
a = stdlib_base_round( random_uniform( 0.0, 5.0 ) );
232+
b = stdlib_base_round( random_uniform( a, a + 5.0 ) );
233+
y = stdlib_base_dists_discrete_uniform_logcdf( x, a, b );
234+
printf( "x:%lf, a: %d, b: %d, ln(F(X;a,b)): %lf\n", x, a, b, y );
235+
}
143236
}
144237
```
145238

146239
</section>
147240

148241
<!-- /.examples -->
149242

243+
</section>
244+
245+
<!-- /.c -->
246+
247+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
248+
249+
<section class="references">
250+
251+
</section>
252+
253+
<!-- /.references -->
254+
150255
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
151256

152257
<section class="related">

lib/node_modules/@stdlib/stats/base/dists/discrete-uniform/logcdf/benchmark/benchmark.js

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

2323
var bench = require( '@stdlib/bench' );
24-
var Float64Array = require( '@stdlib/array/float64' );
25-
var uniform = require( '@stdlib/random/base/uniform' );
26-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2726
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var pkg = require( './../package.json' ).name;
2928
var logcdf = require( './../lib' );
@@ -40,14 +39,9 @@ bench( pkg, function benchmark( b ) {
4039
var i;
4140

4241
len = 100;
43-
x = new Float64Array( len );
44-
min = new Float64Array( len );
45-
max = new Float64Array( len );
46-
for ( i = 0; i < len; i++ ) {
47-
x[ i ] = uniform( -10.0, 10.0 );
48-
min[ i ] = discreteUniform( -20, 0 );
49-
max[ i ] = discreteUniform( min[ i ], min[ i ] + 40 );
50-
}
42+
x = uniform( len, -10.0, 10.0 );
43+
min = discreteUniform( len, -10, 0 );
44+
max = discreteUniform( len, 0, 10 );
5145

5246
b.tic();
5347
for ( i = 0; i < b.iterations; i++ ) {
@@ -77,10 +71,7 @@ bench( pkg+':factory', function benchmark( b ) {
7771
max = 2;
7872
mylogcdf = logcdf.factory( min, max );
7973
len = 100;
80-
x = new Float64Array( len );
81-
for ( i = 0; i < len; i++ ) {
82-
x[ i ] = uniform( -2.0, 0.0 );
83-
}
74+
x = uniform( len, -2.0, 0.0 );
8475

8576
b.tic();
8677
for ( i = 0; i < b.iterations; i++ ) {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var logcdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( logcdf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var min;
43+
var max;
44+
var len;
45+
var x;
46+
var y;
47+
var i;
48+
49+
len = 100;
50+
x = uniform( len, -10.0, 10.0 );
51+
min = uniform( len, -10.0, 0.0 );
52+
max = uniform( len, 0.0, 10.0 );
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
y = logcdf( x[ i % len ], min[ i % len ], max[ i % len ] );
57+
if ( isnan( y ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( y ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

0 commit comments

Comments
 (0)