Skip to content

Commit 2d05be5

Browse files
committed
feat: add the C src files
1 parent aaf7299 commit 2d05be5

File tree

4 files changed

+216
-0
lines changed
  • lib/node_modules/@stdlib/stats/base/dists/hypergeometric/pmf

4 files changed

+216
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
#ifndef STDLIB_STATS_BASE_DISTS_HYPERGEOMETRIC_PMF_H
20+
#define STDLIB_STATS_BASE_DISTS_HYPERGEOMETRIC_PMF_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 probability mass function (PMF) for a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n`.
31+
*
32+
* @param x input value
33+
* @param N population size
34+
* @param K subpopulation size
35+
* @param n number of draws
36+
* @return evaluated PMF
37+
*/
38+
double stdlib_base_dists_hypergeometric_pmf( const double x, const double N, const double K, const double n );
39+
40+
#ifdef __cplusplus
41+
}
42+
#endif
43+
44+
#endif // !STDLIB_STATS_BASE_DISTS_HYPERGEOMETRIC_PMF_H
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) 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 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) 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/math/base/napi/quaternary.h"
20+
#include "stdlib/stats/base/dists/hypergeometric/pmf.h"
21+
22+
// cppcheck-suppress shadowFunction
23+
STDLIB_MATH_BASE_NAPI_MODULE_DDDD_D( stdlib_base_dists_hypergeometric_pmf )
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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/math/base/assert/is_nonnegative_integer.h"
20+
#include "stdlib/math/base/assert/is_nan.h"
21+
#include "stdlib/math/base/special/exp.h"
22+
#include "stdlib/math/base/special/factorialln.h"
23+
#include "stdlib/math/base/special/max.h"
24+
#include "stdlib/math/base/special/min.h"
25+
#include "stdlib/constants/float64/pinf.h"
26+
27+
/**
28+
* Evaluates the probability mass function (PMF) for a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n`.
29+
*
30+
* @param x input value
31+
* @param N population size
32+
* @param K subpopulation size
33+
* @param n number of draws
34+
* @return evaluated PMF, or NaN if inputs are invalid
35+
*
36+
* @example
37+
* double sd = stdlib_base_dists_hypergeometric_pmf( 1.0, 8, 4, 2 );
38+
* // returns ~0.571
39+
*
40+
* @example
41+
* double sd = stdlib_base_dists_hypergeometric_pmf( 0.0, 8, 4, 2 );
42+
* // returns ~0.214
43+
*
44+
* @example
45+
* double sd = stdlib_base_dists_hypergeometric_pmf( NaN, 10, 5, 2 );
46+
* // returns NaN
47+
*/
48+
double stdlib_base_dists_hypergeometric_pmf ( const double x, const double N, const double K, const double n ) {
49+
if (
50+
stdlib_base_is_nan(x) ||
51+
stdlib_base_is_nan(N) ||
52+
stdlib_base_is_nan(K) ||
53+
stdlib_base_is_nan(n) ||
54+
!stdlib_base_is_nonnegative_integer(N) ||
55+
!stdlib_base_is_nonnegative_integer(K) ||
56+
!stdlib_base_is_nonnegative_integer(n) ||
57+
N == STDLIB_CONSTANT_FLOAT64_PINF ||
58+
K == STDLIB_CONSTANT_FLOAT64_PINF ||
59+
K > N ||
60+
n > N
61+
) {
62+
return NAN;
63+
}
64+
65+
double mins = stdlib_base_max(0, n + K - N);
66+
double maxs = stdlib_base_min(K, n);
67+
68+
if (stdlib_base_is_nonnegative_integer(x) && mins <= x && x <= maxs) {
69+
double lnum = stdlib_base_factorialln(n) + stdlib_base_factorialln(K) +
70+
stdlib_base_factorialln(N - n) + stdlib_base_factorialln(N - K);
71+
double ldenom = stdlib_base_factorialln(N) + stdlib_base_factorialln(x) +
72+
stdlib_base_factorialln(n - x) +
73+
stdlib_base_factorialln(K - x) +
74+
stdlib_base_factorialln(N - K + x - n);
75+
double lpmf = lnum - ldenom;
76+
return stdlib_base_exp(lpmf);
77+
}
78+
return 0.0;
79+
}

0 commit comments

Comments
 (0)