Skip to content

Commit 6512a7a

Browse files
nirmaljbgunjjoshiNeerajpathak07Planeshifterstdlib-bot
authored
feat: add C implementation of math/base/special/kernel-betainc
PR-URL: #10279 Ref: #649 Co-authored-by: Gunj Joshi <gunjjoshi8372@gmail.com> Co-authored-by: Neeraj Pathak <neerajrpathak710@gmail.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: stdlib-bot <noreply@stdlib.io> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Karan Anand <anandkarancompsci@gmail.com> Reviewed-by: Gunj Joshi <gunjjoshi8372@gmail.com> Reviewed-by: Neeraj Pathak <neerajrpathak710@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Neeraj Pathak <neerajrpathak710@gmail.com> Signed-off-by: Nirmal Jyoti Biswas <59522492+nirmaljb@users.noreply.github.com>
1 parent d32732a commit 6512a7a

29 files changed

Lines changed: 4865 additions & 19 deletions

File tree

lib/node_modules/@stdlib/math/base/special/kernel-betainc/README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ limitations under the License.
1818
1919
-->
2020

21-
# Kernel Betainc
21+
# kernelBetainc
2222

2323
> [Incomplete beta function][incomplete-beta-function] and its first derivative.
2424
@@ -135,6 +135,111 @@ for ( i = 0; i < 100; i++ ) {
135135

136136
<!-- /.examples -->
137137

138+
<!-- C interface documentation. -->
139+
140+
* * *
141+
142+
<section class="c">
143+
144+
## C APIs
145+
146+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
147+
148+
<section class="intro">
149+
150+
</section>
151+
152+
<!-- /.intro -->
153+
154+
<!-- C usage documentation. -->
155+
156+
<section class="usage">
157+
158+
### Usage
159+
160+
```c
161+
#include "stdlib/math/base/special/kernel_betainc.h"
162+
```
163+
164+
#### stdlib_base_kernel_betainc( x, a, b, regularized, upper, &out, &derivative )
165+
166+
Evaluates the incomplete beta function and its first derivative.
167+
168+
```c
169+
double out;
170+
double derivative;
171+
172+
stdlib_base_kernel_betainc( 0.2, 1.0, 2.0, true, false, &out, &derivative );
173+
```
174+
175+
The function accepts the following arguments:
176+
177+
- **x**: `[in] double` function input.
178+
- **a**: `[in] double` function parameter.
179+
- **b**: `[in] double` function parameter.
180+
- **regularized**: `[in] bool` indicating if the function should evaluate the regularized incomplete beta function.
181+
- **upper**: `[in] bool` indicating if the function should return the upper tail of the incomplete beta function.
182+
- **out**: `[out] double*` destination pointer to store the function value.
183+
- **derivative**: `[out] double*` destination pointer to store the first derivative.
184+
185+
```c
186+
void stdlib_base_kernel_betainc( double x, double a, double b, const bool regularized, const bool upper, double *out, double *derivative );
187+
```
188+
189+
</section>
190+
191+
<!-- /.usage -->
192+
193+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
194+
195+
<section class="notes">
196+
197+
</section>
198+
199+
<!-- /.notes -->
200+
201+
<!-- C API usage examples. -->
202+
203+
<section class="examples">
204+
205+
### Examples
206+
207+
```c
208+
#include "stdlib/math/base/special/kernel_betainc.h"
209+
#include "stdlib/random/base/randu.h"
210+
#include <stdio.h>
211+
#include <stdint.h>
212+
#include <stdbool.h>
213+
214+
int main( void ) {
215+
struct BasePRNGObject *obj = stdlib_base_random_randu_allocate( 0 );
216+
double deriv;
217+
double out;
218+
int32_t i;
219+
double x;
220+
double a;
221+
double b;
222+
223+
for ( i = 0; i < 100; i++ ) {
224+
x = stdlib_base_random_randu( obj );
225+
a = stdlib_base_random_randu( obj ) * 10.0;
226+
b = stdlib_base_random_randu( obj ) * 10.0;
227+
stdlib_base_kernel_betainc( x, a, b, true, false, &out, &deriv );
228+
printf( "x: %lf, a: %lf, b: %lf, f(x,a,b): %lf, f^1(x,a,b): %lf\n", x, a, b, out, deriv );
229+
}
230+
231+
stdlib_base_random_randu_free( obj );
232+
}
233+
```
234+
235+
</section>
236+
237+
<!-- /.examples -->
238+
239+
</section>
240+
241+
<!-- /.c -->
242+
138243
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139244
140245
<section class="related">

lib/node_modules/@stdlib/math/base/special/kernel-betainc/benchmark/benchmark.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var kernelBetainc = require( './../lib' );
3131

3232
// MAIN //
3333

34-
bench( format( '%s::regularized,upper', pkg ), function benchmark( assert ) {
34+
bench( format( '%s::regularized=true,upper=true', pkg ), function benchmark( assert ) {
3535
var out;
3636
var x;
3737
var a;
@@ -58,7 +58,7 @@ bench( format( '%s::regularized,upper', pkg ), function benchmark( assert ) {
5858
assert.end();
5959
});
6060

61-
bench( format( '%s::unregularized,upper', pkg ), function benchmark( assert ) {
61+
bench( format( '%s::regularized=false,upper=true', pkg ), function benchmark( assert ) {
6262
var out;
6363
var x;
6464
var a;
@@ -85,7 +85,7 @@ bench( format( '%s::unregularized,upper', pkg ), function benchmark( assert ) {
8585
assert.end();
8686
});
8787

88-
bench( format( '%s::regularized,lower', pkg ), function benchmark( assert ) {
88+
bench( format( '%s::regularized=true,upper=false', pkg ), function benchmark( assert ) {
8989
var out;
9090
var x;
9191
var a;
@@ -112,7 +112,7 @@ bench( format( '%s::regularized,lower', pkg ), function benchmark( assert ) {
112112
assert.end();
113113
});
114114

115-
bench( format( '%s::unregularized,lower', pkg ), function benchmark( assert ) {
115+
bench( format( '%s::regularized=false,upper=false', pkg ), function benchmark( assert ) {
116116
var out;
117117
var x;
118118
var a;
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
29+
var EPS = require( '@stdlib/constants/float64/eps' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var kernelBetainc = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( kernelBetainc instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( format( '%s::native:regularized=true,upper=true', pkg ), opts, function benchmark( assert ) {
44+
var out;
45+
var x;
46+
var a;
47+
var b;
48+
var i;
49+
50+
x = uniform( 100, 0.0, 1.0 );
51+
a = uniform( 100, EPS, 1000.0 );
52+
b = uniform( 100, EPS, 1000.0 );
53+
54+
out = [ 0.0, 0.0 ];
55+
assert.tic();
56+
for ( i = 0; i < assert.iterations; i++ ) {
57+
out = kernelBetainc( x[ i%x.length ], a[ i%a.length ], b[ i%b.length ], true, true );
58+
if ( isnan( out[ 0 ] ) || isnan( out[ 1 ] ) ) {
59+
assert.fail( 'should not return NaN' );
60+
}
61+
}
62+
assert.toc();
63+
if ( isnan( out[ 0 ] ) || isnan( out[ 1 ] ) ) {
64+
assert.fail( 'should not return NaN' );
65+
}
66+
assert.pass( 'benchmark finished' );
67+
assert.end();
68+
});
69+
70+
bench( format( '%s::native:regularized=false,upper=true', pkg ), opts, function benchmark( assert ) {
71+
var out;
72+
var x;
73+
var a;
74+
var b;
75+
var i;
76+
77+
x = uniform( 100, 0.0, 1.0 );
78+
a = uniform( 100, EPS, 1000.0 );
79+
b = uniform( 100, EPS, 1000.0 );
80+
81+
out = [ 0.0, 0.0 ];
82+
assert.tic();
83+
for ( i = 0; i < assert.iterations; i++ ) {
84+
out = kernelBetainc( x[ i%x.length ], a[ i%a.length ], b[ i%b.length ], false, true );
85+
if ( isnan( out[ 0 ] ) || isnan( out[ 1 ] ) ) {
86+
assert.fail( 'should not return NaN' );
87+
}
88+
}
89+
assert.toc();
90+
if ( isnan( out[ 0 ] ) || isnan( out[ 1 ] ) ) {
91+
assert.fail( 'should not return NaN' );
92+
}
93+
assert.pass( 'benchmark finished' );
94+
assert.end();
95+
});
96+
97+
bench( format( '%s::native:regularized=true,upper=false', pkg ), opts, function benchmark( assert ) {
98+
var out;
99+
var x;
100+
var a;
101+
var b;
102+
var i;
103+
104+
x = uniform( 100, 0.0, 1.0 );
105+
a = uniform( 100, EPS, 1000.0 );
106+
b = uniform( 100, EPS, 1000.0 );
107+
108+
out = [ 0.0, 0.0 ];
109+
assert.tic();
110+
for ( i = 0; i < assert.iterations; i++ ) {
111+
out = kernelBetainc( x[ i%x.length ], a[ i%a.length ], b[ i%b.length ], true, false );
112+
if ( isnan( out[ 0 ] ) || isnan( out[ 1 ] ) ) {
113+
assert.fail( 'should not return NaN' );
114+
}
115+
}
116+
assert.toc();
117+
if ( isnan( out[ 0 ] ) || isnan( out[ 1 ] ) ) {
118+
assert.fail( 'should not return NaN' );
119+
}
120+
assert.pass( 'benchmark finished' );
121+
assert.end();
122+
});
123+
124+
bench( format( '%s::native:regularized=false,upper=false', pkg ), opts, function benchmark( assert ) {
125+
var out;
126+
var x;
127+
var a;
128+
var b;
129+
var i;
130+
131+
x = uniform( 100, 0.0, 1.0 );
132+
a = uniform( 100, EPS, 1000.0 );
133+
b = uniform( 100, EPS, 1000.0 );
134+
135+
out = [ 0.0, 0.0 ];
136+
assert.tic();
137+
for ( i = 0; i < assert.iterations; i++ ) {
138+
out = kernelBetainc( x[ i%x.length ], a[ i%a.length ], b[ i%b.length ], false, false );
139+
if ( isnan( out[ 0 ] ) || isnan( out[ 1 ] ) ) {
140+
assert.fail( 'should not return NaN' );
141+
}
142+
}
143+
assert.toc();
144+
if ( isnan( out[ 0 ] ) || isnan( out[ 1 ] ) ) {
145+
assert.fail( 'should not return NaN' );
146+
}
147+
assert.pass( 'benchmark finished' );
148+
assert.end();
149+
});

0 commit comments

Comments
 (0)