Skip to content

Commit 3109ba5

Browse files
committed
feat: add C implementation for stats/base/dists/chi/cdf
Closes: #3489 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent e7b3a48 commit 3109ba5

File tree

16 files changed

+1151
-0
lines changed

16 files changed

+1151
-0
lines changed

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,96 @@ y = mycdf( 1.5 );
121121
// returns ~0.478
122122
```
123123

124+
* * *
125+
126+
### C APIs
127+
128+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
129+
130+
<section class="intro">
131+
132+
</section>
133+
134+
<!-- /.intro -->
135+
136+
<!-- C usage documentation. -->
137+
138+
<section class="usage">
139+
140+
#### Usage
141+
142+
```c
143+
#include "stdlib/stats/base/dists/chi/cdf.h"
144+
```
145+
146+
#### stdlib_base_dists_chi_cdf( x, k )
147+
148+
Evaluates the cumulative distribution function (CDF) for a chi distribution with degrees of freedom `k`.
149+
150+
```c
151+
double out = stdlib_base_dists_chi_cdf( 2.0, 1.0 );
152+
// returns ~0.954
153+
154+
out = stdlib_base_dists_chi_cdf( 2.0, 3.0 );
155+
// returns ~0.739
156+
```
157+
158+
The function accepts the following arguments:
159+
160+
- **x**: `[in] double` input value.
161+
- **k**: `[in] double` degrees of freedom.
162+
163+
```c
164+
double stdlib_base_dists_chi_cdf( const double x, const double k );
165+
```
166+
167+
</section>
168+
169+
<!-- /.usage -->
170+
171+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
172+
173+
<section class="notes">
174+
175+
</section>
176+
177+
<!-- /.notes -->
178+
179+
<!-- C API usage examples. -->
180+
181+
<section class="examples">
182+
183+
### Examples
184+
185+
```c
186+
#include "stdlib/stats/base/dists/chi/cdf.h"
187+
#include <stdlib.h>
188+
#include <stdio.h>
189+
190+
static double random_uniform( double a, double b ) {
191+
double r = ( (double)rand() / ( (double)RAND_MAX + 1.0 ) );
192+
return a + ( r * ( b - a ) );
193+
}
194+
195+
int main( void ) {
196+
double x;
197+
double k;
198+
double y;
199+
int i;
200+
201+
for ( i = 0; i < 25; i++ ) {
202+
x = random_uniform( 0.0, 2.0 );
203+
k = random_uniform( 1.0, 10.0 );
204+
y = stdlib_base_dists_chi_cdf( x, k );
205+
printf( "x: %lf, k: %lf, F(x;k): %lf\n", x, k, y );
206+
}
207+
}
208+
```
209+
210+
</section>
211+
212+
<!-- /.examples -->
213+
124214
</section>
125215

126216
<!-- /.usage -->
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 cdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( cdf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg + '::native', opts, function benchmark( b ) {
42+
var x;
43+
var k;
44+
var y;
45+
var i;
46+
47+
x = uniform( 100, 0.0, 2.0, {
48+
'dtype': 'float64'
49+
});
50+
k = uniform( 100, 1.0, 10.0, {
51+
'dtype': 'float64'
52+
});
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
y = cdf( x[ i % x.length ], k[ i % k.length ] );
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+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
# VARIABLES #
20+
21+
ifndef VERBOSE
22+
QUIET := @
23+
else
24+
QUIET :=
25+
endif
26+
27+
# Determine the OS ([1][1], [2][2]).
28+
#
29+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
30+
# [2]: http://stackoverflow.com/a/27776822/2225624
31+
OS ?= $(shell uname)
32+
ifneq (, $(findstring MINGW,$(OS)))
33+
OS := WINNT
34+
else
35+
ifneq (, $(findstring MSYS,$(OS)))
36+
OS := WINNT
37+
else
38+
ifneq (, $(findstring CYGWIN,$(OS)))
39+
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
44+
endif
45+
endif
46+
endif
47+
48+
49+
# RULES #
50+
51+
#/
52+
# Removes generated files.
53+
#
54+
# @example
55+
# make clean
56+
#/
57+
clean:
58+
$(QUIET) -rm -f *.o *.node
59+
60+
.PHONY: clean
61+
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) 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+
#include "stdlib/stats/base/dists/chi/cdf.h"
20+
#include <stdlib.h>
21+
#include <stdio.h>
22+
#include <math.h>
23+
#include <sys/time.h>
24+
25+
#define NAME "chi-cdf"
26+
#define ITERATIONS 1000000
27+
#define REPEATS 3
28+
29+
/**
30+
* Prints the TAP version.
31+
*/
32+
static void print_version( void ) {
33+
printf( "TAP version 13\n" );
34+
}
35+
36+
/**
37+
* Prints the TAP summary.
38+
*
39+
* @param total total number of tests
40+
* @param passing number of passing tests
41+
*/
42+
static void print_summary( int total, int passing ) {
43+
printf( "#\n" );
44+
printf( "1..%d\n", total ); // TAP plan
45+
printf( "# total %d\n", total );
46+
printf( "# pass %d\n", passing );
47+
printf( "#\n" );
48+
printf( "# ok\n" );
49+
}
50+
51+
/**
52+
* Prints benchmarks results.
53+
*
54+
* @param elapsed elapsed time in seconds
55+
*/
56+
static void print_results( double elapsed ) {
57+
double rate = ( (double)ITERATIONS ) / elapsed;
58+
printf( " ---\n" );
59+
printf( " iterations: %d\n", ITERATIONS );
60+
printf( " elapsed: %0.9f\n", elapsed );
61+
printf( " rate: %0.9f\n", rate );
62+
printf( " ...\n" );
63+
}
64+
65+
/**
66+
* Returns a clock time.
67+
*
68+
* @return clock time
69+
*/
70+
static double tic( void ) {
71+
struct timeval now;
72+
gettimeofday( &now, NULL );
73+
return ( (double)now.tv_sec ) + ( (double)now.tv_usec/1.0e6 );
74+
}
75+
76+
/**
77+
* Generates a pseudorandom number on the interval [0,1).
78+
*
79+
* @return pseudorandom number
80+
*/
81+
static double rand_double( void ) {
82+
int r = rand();
83+
return ( (double)r ) / ( (double)RAND_MAX + 1.0 );
84+
}
85+
86+
/**
87+
* Generates a pseudorandom number on the interval [a,b).
88+
*
89+
* @param a lower bound (inclusive)
90+
* @param b upper bound (exclusive)
91+
* @return pseudorandom number
92+
*/
93+
static double random_uniform( double a, double b ) {
94+
return a + ( rand_double() * ( b - a ) );
95+
}
96+
97+
/**
98+
* Runs a benchmark.
99+
*
100+
* @return elapsed time in seconds
101+
*/
102+
static double benchmark( void ) {
103+
double x[ 100 ];
104+
double k[ 100 ];
105+
double elapsed;
106+
double y;
107+
double t;
108+
int i;
109+
110+
for ( i = 0; i < 100; i++ ) {
111+
x[ i ] = random_uniform( 0.0, 2.0 );
112+
k[ i ] = random_uniform( 1.0, 10.0 );
113+
}
114+
115+
t = tic();
116+
for ( i = 0; i < ITERATIONS; i++ ) {
117+
y = stdlib_base_dists_chi_cdf( x[ i % 100 ], k[ i % 100 ] );
118+
if ( y != y ) {
119+
printf( "should not return NaN\n" );
120+
break;
121+
}
122+
}
123+
elapsed = tic() - t;
124+
if ( y != y ) {
125+
printf( "should not return NaN\n" );
126+
}
127+
return elapsed;
128+
}
129+
130+
/**
131+
* Main execution sequence.
132+
*/
133+
int main( void ) {
134+
double elapsed;
135+
int i;
136+
137+
// Use the current time to seed the random number generator:
138+
srand( time( NULL ) );
139+
140+
print_version();
141+
for ( i = 0; i < REPEATS; i++ ) {
142+
printf( "# c::native::%s\n", NAME );
143+
elapsed = benchmark();
144+
printf( "ok %d benchmark finished\n", i+1 );
145+
print_results( elapsed );
146+
printf( "\n" );
147+
}
148+
print_summary( REPEATS, REPEATS );
149+
}

0 commit comments

Comments
 (0)