Skip to content

Commit 64585e7

Browse files
feat(add C implementation): add stats/base/dists/arcsine/cdf
1 parent a0ba090 commit 64585e7

File tree

7 files changed

+356
-0
lines changed

7 files changed

+356
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
#ifndef STDLIB_STATS_BASE_DISTS_ARCSINE_CDF_H
20+
#define STDLIB_STATS_BASE_DISTS_ARCSINE_CDF_H
21+
22+
/*
23+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
24+
*/
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
/**
30+
* Evaluates the cumulative distribution function (CDF) for an arcsine distribution with minimum support `a` and maximum support `b` at a value `x`.
31+
*/
32+
double stdlib_base_dists_arcsine_cdf( const double a, const double b );
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif
37+
38+
#endif // !STDLIB_STATS_BASE_DISTS_ARCSINE_CDF_H
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 addon = require( './../src/addon.node' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Evaluates the cumulative distribution function (CDF) for an arcsine distribution with minimum support `a` and maximum support `b` at a value `x`.
30+
*
31+
* @param {number} x - input value
32+
* @param {number} a - minimum support
33+
* @param {number} b - maximum support
34+
* @returns {Probability} evaluated CDF
35+
*
36+
* @example
37+
* var y = cdf( 9.0, 0.0, 10.0 );
38+
* // returns ~0.795
39+
*
40+
* @example
41+
* var y = cdf( 0.5, 0.0, 2.0 );
42+
* // returns ~0.333
43+
*
44+
* @example
45+
* var y = cdf( +Infinity, 2.0, 4.0 );
46+
* // returns 1.0
47+
*
48+
* @example
49+
* var y = cdf( -Infinity, 2.0, 4.0 );
50+
* // returns 0.0
51+
*
52+
* @example
53+
* var y = cdf( NaN, 0.0, 1.0 );
54+
* // returns NaN
55+
*
56+
* @example
57+
* var y = cdf( 0.0, NaN, 1.0 );
58+
* // returns NaN
59+
*
60+
* @example
61+
* var y = cdf( 0.0, 0.0, NaN );
62+
* // returns NaN
63+
*/
64+
function cdf( x, a, b ) {
65+
return addon( x, a, b );
66+
}
67+
68+
69+
// EXPORTS //
70+
71+
module.exports = cdf;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"options": {
3+
"task": "build",
4+
"wasm": false
5+
},
6+
"fields": [
7+
{
8+
"field": "src",
9+
"resolve": true,
10+
"relative": true
11+
},
12+
{
13+
"field": "include",
14+
"resolve": true,
15+
"relative": true
16+
},
17+
{
18+
"field": "libraries",
19+
"resolve": false,
20+
"relative": false
21+
},
22+
{
23+
"field": "libpath",
24+
"resolve": true,
25+
"relative": false
26+
}
27+
],
28+
"confs": [
29+
{
30+
"task": "build",
31+
"wasm": false,
32+
"src": [
33+
"./src/main.c"
34+
],
35+
"include": [
36+
"./include"
37+
],
38+
"libraries": [],
39+
"libpath": [],
40+
"dependencies": [
41+
"@stdlib/math/base/napi/ternary",
42+
"@stdlib/math/base/assert/is-nan",
43+
"@stdlib/math/base/special/ln",
44+
"@stdlib/constants/float64/ln-pi",
45+
"@stdlib/constants/float64/ln-two",
46+
"@stdlib/constants/float64/ninf",
47+
"@stdlib/math/base/special/asin",
48+
"@stdlib/math/base/special/sqrt"
49+
]
50+
},
51+
{
52+
"task": "benchmark",
53+
"wasm": false,
54+
"src": [
55+
"./src/main.c"
56+
],
57+
"include": [
58+
"./include"
59+
],
60+
"libraries": [],
61+
"libpath": [],
62+
"dependencies": [
63+
"@stdlib/math/base/assert/is-nan",
64+
"@stdlib/math/base/special/ln",
65+
"@stdlib/constants/float64/ln-pi",
66+
"@stdlib/constants/float64/ln-two",
67+
"@stdlib/constants/float64/ninf",
68+
"@stdlib/math/base/special/asin",
69+
"@stdlib/math/base/special/sqrt"
70+
]
71+
},
72+
{
73+
"task": "examples",
74+
"wasm": false,
75+
"src": [
76+
"./src/main.c"
77+
],
78+
"include": [
79+
"./include"
80+
],
81+
"libraries": [],
82+
"libpath": [],
83+
"dependencies": [
84+
"@stdlib/math/base/assert/is-nan",
85+
"@stdlib/math/base/special/ln",
86+
"@stdlib/constants/float64/ln-pi",
87+
"@stdlib/constants/float64/ln-two",
88+
"@stdlib/constants/float64/ninf",
89+
"@stdlib/math/base/special/asin",
90+
"@stdlib/math/base/special/sqrt"
91+
]
92+
}
93+
]
94+
}

lib/node_modules/@stdlib/stats/base/dists/arcsine/cdf/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
}
1515
],
1616
"main": "./lib",
17+
"gypfile": true,
1718
"directories": {
1819
"benchmark": "./benchmark",
1920
"doc": "./docs",
2021
"example": "./examples",
22+
"include": "./include",
2123
"lib": "./lib",
24+
"src": "./src",
2225
"test": "./test"
2326
},
2427
"types": "./docs/types",
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2024 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 for building an add-on.
53+
#
54+
# @example
55+
# make clean-addon
56+
#/
57+
clean-addon:
58+
$(QUIET) -rm -f *.o *.node
59+
60+
.PHONY: clean-addon
61+
62+
#/
63+
# Removes generated files.
64+
#
65+
# @example
66+
# make clean
67+
#/
68+
clean: clean-addon
69+
70+
.PHONY: clean
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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/arcsine/cdf.h"
20+
#include "stdlib/math/base/napi/ternary.h"
21+
22+
// cppcheck-suppress shadowFunction
23+
STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_dists_arcsine_cdf )
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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/arcsine/cdf.h"
20+
#include "stdlib/math/base/assert/is_nan.h"
21+
#include "stdlib/math/base/special/ln.h"
22+
#include "stdlib/constants/float64/pi.h"
23+
#include "stdlib/constants/float64/ln_two.h"
24+
#include "stdlib/constants/float64/ninf.h"
25+
#include "stdlib/math/base/special/asin.h"
26+
#include "stdlib/math/base/special/sqrt.h"
27+
28+
/**
29+
* Evaluates the cumulative distribution function (CDF) for an arcsine distribution with minimum support `a` and maximum support `b` at a value `x`.
30+
*
31+
* @param {number} x - input value
32+
* @param {number} a - minimum support
33+
* @param {number} b - maximum support
34+
* @returns {Probability} evaluated CDF
35+
*
36+
* @example
37+
* double y = stdlib_base_arcsine_kurtosis( 0.0, 1.0 );
38+
* // returns 1.5
39+
*/
40+
double stdlib_base_dists_arcsine_cdf( const double x, const double a, const double b ) {
41+
double TWO_OVER_PI = 2.0 / STDLIB_CONSTANT_FLOAT64_PI;
42+
if (
43+
stdlib_base_is_nan( x ) ||
44+
stdlib_base_is_nan( a ) ||
45+
stdlib_base_is_nan( b ) ||
46+
a >= b
47+
) {
48+
return 0.0/0.0;
49+
}
50+
if ( x < a ) {
51+
return 0.0;
52+
}
53+
if ( x >= b ) {
54+
return 1.0;
55+
}
56+
return TWO_OVER_PI * stdlib_base_asin( stdlib_base_sqrt( ( x-a ) / ( b-a ) ) );
57+
}

0 commit comments

Comments
 (0)