From a2e871f5767ab59fedab6fce9e297a4a332f7b96 Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:07:20 +0530 Subject: [PATCH 1/9] feat: add C implementation for binomial Median --- .../base/dists/binomial/median/README.md | 90 ++++++++++ .../binomial/median/benchmark/benchmark.js | 17 +- .../median/benchmark/benchmark.native.js | 71 ++++++++ .../binomial/median/benchmark/c/Makefile | 146 +++++++++++++++ .../binomial/median/benchmark/c/benchmark.c | 147 +++++++++++++++ .../base/dists/binomial/median/binding.gyp | 170 ++++++++++++++++++ .../dists/binomial/median/examples/c/Makefile | 146 +++++++++++++++ .../binomial/median/examples/c/example.c | 35 ++++ .../base/dists/binomial/median/include.gypi | 53 ++++++ .../stdlib/stats/base/dists/binomial/median.h | 38 ++++ .../base/dists/binomial/median/lib/native.js | 59 ++++++ .../base/dists/binomial/median/manifest.json | 76 ++++++++ .../base/dists/binomial/median/package.json | 3 + .../base/dists/binomial/median/src/Makefile | 70 ++++++++ .../base/dists/binomial/median/src/addon.c | 23 +++ .../base/dists/binomial/median/src/main.c | 48 +++++ .../dists/binomial/median/test/test.native.js | 102 +++++++++++ 17 files changed, 1290 insertions(+), 4 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/binding.gyp create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/include.gypi create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/include/stdlib/stats/base/dists/binomial/median.h create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/lib/native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/addon.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/README.md b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/README.md index f75406b8a83c..35232ffd53e9 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/README.md +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/README.md @@ -141,6 +141,96 @@ for ( i = 0; i < 10; i++ ) { + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/base/dists/binomial/median.h" +``` + +#### stdlib_base_dists_binomial_median( n, p ) + +Returns the [median][median] of a [Binomial][binomial-distribution] distribution with number of trials \( n \) and success probability \( p \). + +```c +double out = stdlib_base_dists_binomial_median( 10, 0.5 ); +// returns 5 +``` + +The function accepts the following arguments: + +- **n**: `[in] int` number of trials. +- **p**: `[in] double` success probability. + +```c +double stdlib_base_dists_binomial_median( const int n, const double p ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/stats/base/dists/binomial/median.h" +#include +#include + +int main( void ) { + int n; + double p; + double y; + int i; + + for ( i = 0; i < 25; i++ ) { + n = rand() % 20 + 1; // random number of trials between 1 and 20 + p = (double)rand() / ( (double)RAND_MAX + 1.0 ); + y = stdlib_base_dists_binomial_median( n, p ); + printf( "n: %d, p: %lf, Median(X;n,p): %lf\n", n, p, y ); + } +} +``` + +
+ + + +
+ + +
diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js index c148c663f39b..328dde2c25a3 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2024 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ var bench = require( '@stdlib/bench' ); var ceil = require( '@stdlib/math/base/special/ceil' ); var randu = require( '@stdlib/random/base/randu' ); +var Float64Array = require( '@stdlib/array/float64' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pkg = require( './../package.json' ).name; var median = require( './../lib' ); @@ -31,16 +32,24 @@ var median = require( './../lib' ); // MAIN // bench( pkg, function benchmark( b ) { + var len; var n; var p; var y; var i; + len = 100; + n = new Float64Array(len); + p = new Float64Array(len); + + for (i = 0; i < len; i++) { + n[i] = ceil( randu()*100.0 ); + p[i] = randu(); + } + b.tic(); for ( i = 0; i < b.iterations; i++ ) { - n = ceil( randu()*100.0 ); - p = randu(); - y = median( n, p ); + y = median(n[i % len], p[i % len]); if ( isnan( y ) ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.native.js new file mode 100644 index 000000000000..c2df5cb4a9e6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.native.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var bench = require( '@stdlib/bench' ); +var Float64Array = require( '@stdlib/array/float64' ); +var randint = require( '@stdlib/random/base/discrete-uniform' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var median = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( median instanceof Error ) +}; + + +// MAIN // + +bench( pkg+'::native', opts, function benchmark( b ) { + var len; + var n; + var p; + var y; + var i; + + len = 100; + n = new Float64Array( len ); + p = new Float64Array( len ); + for ( i = 0; i < len; i++ ) { + n[ i ] = randint( 1, 100 ); + p[ i ] = randu(); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = median( n[ i % len ], p[ i % len ] ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile new file mode 100644 index 000000000000..0ebf4545e1a9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2024 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c new file mode 100644 index 000000000000..c1cadc7501db --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c @@ -0,0 +1,147 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/stats/base/dists/binomial/median.h" +#include +#include +#include +#include +#include + +#define NAME "binomial-median" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static double rand_double( void ) { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Generates a random integer in the range [1, 100]. +* +* @return random integer +*/ +static int rand_int( void ) { + return (rand() % 100) + 1; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + double elapsed; + double p[ 100 ]; + int n[ 100 ]; + double y; + double t; + int i; + + for ( i = 0; i < 100; i++ ) { + p[ i ] = rand_double(); + n[ i ] = rand_int(); + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + y = stdlib_base_dists_binomial_median( n[ i%100 ], p[ i%100 ] ); + if ( y != y ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( y != y ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int i; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + printf( "# c::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", i+1 ); + } + print_summary( REPEATS, REPEATS ); +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/binding.gyp b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/binding.gyp new file mode 100644 index 000000000000..507cb00291e7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/binding.gyp @@ -0,0 +1,170 @@ +# @license Apache-2.0 +# +# Copyright (c) 2024 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# A `.gyp` file for building a Node.js native add-on. +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # List of files to include in this file: + 'includes': [ + './include.gypi', + ], + + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Target name should match the add-on export name: + 'addon_target_name%': 'addon', + + # Set variables based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + { + # Define the object file suffix: + 'obj': 'obj', + }, + { + # Define the object file suffix: + 'obj': 'o', + } + ], # end condition (OS=="win") + ], # end conditions + }, # end variables + + # Define compile targets: + 'targets': [ + + # Target to generate an add-on: + { + # The target name should match the add-on export name: + 'target_name': '<(addon_target_name)', + + # Define dependencies: + 'dependencies': [], + + # Define directories which contain relevant include headers: + 'include_dirs': [ + # Local include directory: + '<@(include_dirs)', + ], + + # List of source files: + 'sources': [ + '<@(src_files)', + ], + + # Settings which should be applied when a target's object files are used as linker input: + 'link_settings': { + # Define libraries: + 'libraries': [ + '<@(libraries)', + ], + + # Define library directories: + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + + # C/C++ compiler flags: + 'cflags': [ + # Enable commonly used warning options: + '-Wall', + + # Aggressive optimization: + '-O3', + ], + + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', + ], + + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], + + # Apply conditions based on the host OS: + 'conditions': [ + [ + 'OS=="mac"', + { + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], # end condition (OS=="mac") + [ + 'OS!="win"', + { + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], + }, + ], # end condition (OS!="win") + ], # end conditions + }, # end target <(addon_target_name) + + # Target to copy a generated add-on to a standard location: + { + 'target_name': 'copy_addon', + + # Declare that the output of this target is not linked: + 'type': 'none', + + # Define dependencies: + 'dependencies': [ + # Require that the add-on be generated before building this target: + '<(addon_target_name)', + ], + + # Define a list of actions: + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], + + # Declare the expected outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Define the command-line invocation: + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target copy_addon + ], # end targets +} \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile new file mode 100644 index 000000000000..d53ef397c77d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2024 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c new file mode 100644 index 000000000000..a184f16f974f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/stats/base/dists/binomial/median.h" +#include +#include + +int main( void ) { + double p; + double n; + double y; + int i; + + for ( i = 0; i < 25; i++ ) { + p = (double)rand() / ( (double)RAND_MAX + 1.0 ); + n = ( rand() % 100 ) + 1; + y = stdlib_base_dists_binomial_median( n, p ); + printf( "n: %lf , p: %lf , H(X;n,p): %lf\n", n, p, y ); + } +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include.gypi b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include.gypi new file mode 100644 index 000000000000..c6495fc1da3f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include.gypi @@ -0,0 +1,53 @@ +# @license Apache-2.0 +# +# Copyright (c) 2024 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# A GYP include file for building a Node.js native add-on. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + ' + +/** +* Returns the median of a Binomial distribution. +* +* @param n number of trials +* @param p success probability +* @return median +* +* @example +* double y = stdlib_base_dists_binomial_median( 10, 0.5 ); +* // returns 5 +* +* @example +* double y = stdlib_base_dists_binomial_median( 20, 0.7 ); +* // returns 14 +*/ +double stdlib_base_dists_binomial_median( const double n, const double p ) { + if ( + stdlib_base_is_nan( p ) || + p < 0.0 || + p > 1.0 || + n < 0 + ) { + return 0.0 / 0.0; // Return NaN for invalid inputs + } + return round( n * p ); +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js new file mode 100644 index 000000000000..baa975f4b840 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js @@ -0,0 +1,102 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); + + +// FIXTURES // + +var data = require( './fixtures/julia/data.json' ); + + +// VARIABLES // + +var median = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( median instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof median, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if provided `NaN` for either `n` or `p`, the function returns `NaN`', opts, function test( t ) { + var v = median( NaN, 0.5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = median( 10, NaN ); + t.equal( isnan( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'if provided `n < 0`, the function returns `NaN`', opts, function test( t ) { + var v = median( -1, 0.5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'if provided a success probability `p` outside of `[0,1]`, the function returns `NaN`', opts, function test( t ) { + var v; + + v = median( 10, -0.1 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = median( 10, 1.1 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = median( 10, NINF ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = median( 10, PINF ); + t.equal( isnan( v ), true, 'returns NaN' ); + + t.end(); +}); + +tape( 'the function returns the median of a Binomial distribution', opts, function test( t ) { + var expected; + var i; + var n; + var p; + var y; + + expected = data.expected; + n = data.n; + p = data.p; + for ( i = 0; i < expected.length; i++ ) { + y = median( n[i], p[i] ); + t.equal( y, expected[i], 'n: '+n[i]+', p: '+p[i]+', y: '+y+', expected: '+expected[i] ); + } + t.end(); +}); From bb1706fdce411895c1646134a689f0910794b0f7 Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:40:38 +0530 Subject: [PATCH 2/9] fix: resolve all the issues and added tests --- .../binomial/median/benchmark/c/Makefile | 2 +- .../dists/binomial/median/examples/c/Makefile | 2 +- .../stdlib/stats/base/dists/binomial/median.h | 2 +- .../base/dists/binomial/median/manifest.json | 146 +++++++++--------- .../base/dists/binomial/median/src/Makefile | 2 +- .../base/dists/binomial/median/src/main.c | 2 +- .../dists/binomial/median/test/test.native.js | 33 +++- 7 files changed, 103 insertions(+), 86 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile index 0ebf4545e1a9..f69e9da2b4d3 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile @@ -143,4 +143,4 @@ run: $(c_targets) clean: $(QUIET) -rm -f *.o *.out -.PHONY: clean \ No newline at end of file +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile index d53ef397c77d..6aed70daf167 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile @@ -143,4 +143,4 @@ run: $(c_targets) clean: $(QUIET) -rm -f *.o *.out -.PHONY: clean \ No newline at end of file +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include/stdlib/stats/base/dists/binomial/median.h b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include/stdlib/stats/base/dists/binomial/median.h index 5b8ed2ea7bfd..9a7d842b2c8e 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include/stdlib/stats/base/dists/binomial/median.h +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include/stdlib/stats/base/dists/binomial/median.h @@ -27,7 +27,7 @@ extern "C" { #endif /** -* Returns the median of a Binomial distribution with `n` trials and success probability `p`. +* Returns the median of a binomial distribution with `n` trials and success probability `p`. */ double stdlib_base_dists_binomial_median( const double n, const double p ); diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json index cdc17e7b5fef..db80b71a4aa4 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json @@ -1,76 +1,76 @@ { - "options": { + "options": { + "task": "build", + "wasm": false + }, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { "task": "build", - "wasm": false + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/napi/binary", + "@stdlib/math/base/assert/is-nan" + ] + }, + { + "task": "benchmark", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-nan" + ] }, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "task": "build", - "wasm": false, - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/math/base/napi/binary", - "@stdlib/math/base/assert/is-nan" - ] - }, - { - "task": "benchmark", - "wasm": false, - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/math/base/assert/is-nan" - ] - }, - { - "task": "examples", - "wasm": false, - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/math/base/assert/is-nan" - ] - } - ] -} \ No newline at end of file + { + "task": "examples", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-nan" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/Makefile index 81bb164286c3..bcf18aa46655 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/Makefile +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/Makefile @@ -67,4 +67,4 @@ clean-addon: #/ clean: clean-addon -.PHONY: clean \ No newline at end of file +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c index ef3adb8cf32d..3af905b9f913 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c @@ -42,7 +42,7 @@ double stdlib_base_dists_binomial_median( const double n, const double p ) { p > 1.0 || n < 0 ) { - return 0.0 / 0.0; // Return NaN for invalid inputs + return 0.0 / 0.0; // NaN } return round( n * p ); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js index baa975f4b840..85c15d78cd59 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js @@ -56,35 +56,52 @@ tape( 'if provided `NaN` for either `n` or `p`, the function returns `NaN`', opt v = median( 10, NaN ); t.equal( isnan( v ), true, 'returns NaN' ); + v = median( NaN, NaN ); + t.equal( isnan( v ), true, 'returns NaN' ); + t.end(); }); -tape( 'if provided `n < 0`, the function returns `NaN`', opts, function test( t ) { - var v = median( -1, 0.5 ); +tape( 'if provided an `n` which is not a nonnegative integer, the function returns `NaN`', function test( t ) { + var v; + + v = median( 1.5, 0.5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = median( -2, 0.5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = median( -1, 0.5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = median( 2.5, 0.5 ); + t.equal( isnan( v ), true, 'returns NaN' ); + + v = median( PINF, 0.5 ); t.equal( isnan( v ), true, 'returns NaN' ); t.end(); }); -tape( 'if provided a success probability `p` outside of `[0,1]`, the function returns `NaN`', opts, function test( t ) { +tape( 'if provided a success probability `p` outside of `[0,1]`, the function returns `NaN`', function test( t ) { var v; - v = median( 10, -0.1 ); + v = median( 20, -1.0 ); t.equal( isnan( v ), true, 'returns NaN' ); - v = median( 10, 1.1 ); + v = median( 20, 1.5 ); t.equal( isnan( v ), true, 'returns NaN' ); - v = median( 10, NINF ); + v = median( 20, NINF ); t.equal( isnan( v ), true, 'returns NaN' ); - v = median( 10, PINF ); + v = median( 20, PINF ); t.equal( isnan( v ), true, 'returns NaN' ); t.end(); }); -tape( 'the function returns the median of a Binomial distribution', opts, function test( t ) { +tape( 'the function returns the median of a binomial distribution', opts, function test( t ) { var expected; var i; var n; From ee16fc8fea06c62b375e868e53d1e28afc2b1cde Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:27:12 +0530 Subject: [PATCH 3/9] chore: added new test and C implementation --- .../base/dists/binomial/median/benchmark/benchmark.js | 2 +- .../stats/base/dists/binomial/median/manifest.json | 9 ++++++--- .../@stdlib/stats/base/dists/binomial/median/src/main.c | 6 +++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js index 328dde2c25a3..1fa8b5b54d7a 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2018 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json index db80b71a4aa4..f8fc97bda198 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json @@ -39,7 +39,8 @@ "libpath": [], "dependencies": [ "@stdlib/math/base/napi/binary", - "@stdlib/math/base/assert/is-nan" + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/assert/is-nonnegative-integerf" ] }, { @@ -54,7 +55,8 @@ "libraries": [], "libpath": [], "dependencies": [ - "@stdlib/math/base/assert/is-nan" + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/assert/is-nonnegative-integerf" ] }, { @@ -69,7 +71,8 @@ "libraries": [], "libpath": [], "dependencies": [ - "@stdlib/math/base/assert/is-nan" + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/assert/is-nonnegative-integerf" ] } ] diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c index 3af905b9f913..f4c6b8a7e84e 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c @@ -18,6 +18,7 @@ #include "stdlib/stats/base/dists/binomial/median.h" #include "stdlib/math/base/assert/is_nan.h" +#include "stdlib/math/base/assert/is_nonnegative_integerf.h" #include /** @@ -38,9 +39,12 @@ double stdlib_base_dists_binomial_median( const double n, const double p ) { if ( stdlib_base_is_nan( p ) || + stdlib_base_is_nan( n ) || + !stdlib_base_is_nonnegative_integerf( n ) || p < 0.0 || p > 1.0 || - n < 0 + n < 0 || + n > 1.0e6 ) { return 0.0 / 0.0; // NaN } From 5aedaeee26c3ca52d80e845a6a3d5f07b4460501 Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Thu, 19 Dec 2024 11:16:56 +0530 Subject: [PATCH 4/9] fix: added tab indentation in files --- .../binomial/median/benchmark/c/benchmark.c | 112 +++++++++--------- .../binomial/median/examples/c/example.c | 20 ++-- .../base/dists/binomial/median/src/main.c | 6 +- 3 files changed, 69 insertions(+), 69 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c index c1cadc7501db..a03f0d9479b8 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c @@ -31,22 +31,22 @@ * Prints the TAP version. */ static void print_version( void ) { - printf( "TAP version 13\n" ); + printf( "TAP version 13\n" ); } /** * Prints the TAP summary. * -* @param total total number of tests -* @param passing total number of passing tests +* @param total total number of tests +* @param passing total number of passing tests */ static void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); } /** @@ -55,12 +55,12 @@ static void print_summary( int total, int passing ) { * @param elapsed elapsed time in seconds */ static void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); } /** @@ -69,9 +69,9 @@ static void print_results( double elapsed ) { * @return clock time */ static double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; } /** @@ -80,8 +80,8 @@ static double tic( void ) { * @return random number */ static double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); } /** @@ -90,7 +90,7 @@ static double rand_double( void ) { * @return random integer */ static int rand_int( void ) { - return (rand() % 100) + 1; + return (rand() % 100) + 1; } /** @@ -99,49 +99,49 @@ static int rand_int( void ) { * @return elapsed time in seconds */ static double benchmark( void ) { - double elapsed; - double p[ 100 ]; - int n[ 100 ]; - double y; - double t; - int i; + double elapsed; + double p[ 100 ]; + int n[ 100 ]; + double y; + double t; + int i; - for ( i = 0; i < 100; i++ ) { - p[ i ] = rand_double(); - n[ i ] = rand_int(); - } + for ( i = 0; i < 100; i++ ) { + p[ i ] = rand_double(); + n[ i ] = rand_int(); + } - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - y = stdlib_base_dists_binomial_median( n[ i%100 ], p[ i%100 ] ); - if ( y != y ) { - printf( "should not return NaN\n" ); - break; - } - } - elapsed = tic() - t; - if ( y != y ) { - printf( "should not return NaN\n" ); - } - return elapsed; + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + y = stdlib_base_dists_binomial_median( n[ i%100 ], p[ i%100 ] ); + if ( y != y ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( y != y ) { + printf( "should not return NaN\n" ); + } + return elapsed; } /** * Main execution sequence. */ int main( void ) { - double elapsed; - int i; + double elapsed; + int i; - // Use the current time to seed the random number generator: - srand( time( NULL ) ); + // Use the current time to seed the random number generator: + srand( time( NULL ) ); - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - printf( "# c::%s\n", NAME ); - elapsed = benchmark(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", i+1 ); - } - print_summary( REPEATS, REPEATS ); + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + printf( "# c::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", i+1 ); + } + print_summary( REPEATS, REPEATS ); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c index a184f16f974f..a61514f3420a 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c @@ -21,15 +21,15 @@ #include int main( void ) { - double p; - double n; - double y; - int i; + double p; + double n; + double y; + int i; - for ( i = 0; i < 25; i++ ) { - p = (double)rand() / ( (double)RAND_MAX + 1.0 ); - n = ( rand() % 100 ) + 1; - y = stdlib_base_dists_binomial_median( n, p ); - printf( "n: %lf , p: %lf , H(X;n,p): %lf\n", n, p, y ); - } + for ( i = 0; i < 25; i++ ) { + p = (double)rand() / ( (double)RAND_MAX + 1.0 ); + n = ( rand() % 100 ) + 1; + y = stdlib_base_dists_binomial_median( n, p ); + printf( "n: %lf , p: %lf , H(X;n,p): %lf\n", n, p, y ); + } } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c index f4c6b8a7e84e..f432b9b0a273 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c @@ -24,9 +24,9 @@ /** * Returns the median of a Binomial distribution. * -* @param n number of trials -* @param p success probability -* @return median +* @param n number of trials +* @param p success probability +* @return median * * @example * double y = stdlib_base_dists_binomial_median( 10, 0.5 ); From fa93d9732fc93a66d954a6e31f556b5db0143eec Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Mon, 6 Jan 2025 17:42:08 +0530 Subject: [PATCH 5/9] feat: added new implementation and other minor updates --- .../base/dists/binomial/median/README.md | 25 ++++++++---- .../binomial/median/benchmark/benchmark.js | 16 ++++---- .../median/benchmark/benchmark.native.js | 11 +++--- .../binomial/median/benchmark/c/Makefile | 2 +- .../binomial/median/benchmark/c/benchmark.c | 39 ++++++++----------- .../base/dists/binomial/median/binding.gyp | 4 +- .../dists/binomial/median/examples/c/Makefile | 2 +- .../binomial/median/examples/c/example.c | 19 ++++++--- .../base/dists/binomial/median/include.gypi | 4 +- .../stdlib/stats/base/dists/binomial/median.h | 8 ++-- .../base/dists/binomial/median/lib/native.js | 16 ++++---- .../base/dists/binomial/median/manifest.json | 6 ++- .../base/dists/binomial/median/src/Makefile | 2 +- .../base/dists/binomial/median/src/addon.c | 6 +-- .../base/dists/binomial/median/src/main.c | 23 ++++------- .../dists/binomial/median/test/test.native.js | 25 +++--------- 16 files changed, 102 insertions(+), 106 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/README.md b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/README.md index 35232ffd53e9..f99437080142 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/README.md +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/README.md @@ -169,20 +169,20 @@ for ( i = 0; i < 10; i++ ) { #### stdlib_base_dists_binomial_median( n, p ) -Returns the [median][median] of a [Binomial][binomial-distribution] distribution with number of trials \( n \) and success probability \( p \). +Returns the [median][median] of a [binomial][binomial-distribution] distribution with number of trials `n` and success probability `p`. ```c -double out = stdlib_base_dists_binomial_median( 10, 0.5 ); -// returns 5 +double out = stdlib_base_dists_binomial_median( 100, 0.1 ); +// returns 10 ``` The function accepts the following arguments: -- **n**: `[in] int` number of trials. +- **n**: `[in] int32_t` number of trials. - **p**: `[in] double` success probability. ```c -double stdlib_base_dists_binomial_median( const int n, const double p ); +double stdlib_base_dists_binomial_median( const int32_t n, const double p ); ```
@@ -205,21 +205,30 @@ double stdlib_base_dists_binomial_median( const int n, const double p ); ```c #include "stdlib/stats/base/dists/binomial/median.h" +#include "stdlib/math/base/special/ceil.h" #include +#include #include +static double random_uniform( const double min, const double max ) { + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); + return min + ( v * (max - min) ); +} + int main( void ) { - int n; + int32_t n; double p; double y; int i; for ( i = 0; i < 25; i++ ) { - n = rand() % 20 + 1; // random number of trials between 1 and 20 - p = (double)rand() / ( (double)RAND_MAX + 1.0 ); + n = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) ); + p = random_uniform( 0.0, 1.0 ); y = stdlib_base_dists_binomial_median( n, p ); printf( "n: %d, p: %lf, Median(X;n,p): %lf\n", n, p, y ); } + + return 0; } ``` diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js index 1fa8b5b54d7a..80a359a5c566 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.js @@ -22,8 +22,9 @@ var bench = require( '@stdlib/bench' ); var ceil = require( '@stdlib/math/base/special/ceil' ); -var randu = require( '@stdlib/random/base/randu' ); +var Int32Array = require( '@stdlib/array/int32' ); var Float64Array = require( '@stdlib/array/float64' ); +var randu = require( '@stdlib/random/base/randu' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pkg = require( './../package.json' ).name; var median = require( './../lib' ); @@ -39,17 +40,16 @@ bench( pkg, function benchmark( b ) { var i; len = 100; - n = new Float64Array(len); - p = new Float64Array(len); - - for (i = 0; i < len; i++) { - n[i] = ceil( randu()*100.0 ); - p[i] = randu(); + n = new Int32Array( len ); + p = new Float64Array( len ); + for ( i = 0; i < len; i++ ) { + n[ i ] = ceil( randu() * 100.0 ); + p[ i ] = randu(); } b.tic(); for ( i = 0; i < b.iterations; i++ ) { - y = median(n[i % len], p[i % len]); + y = median( n[ i%len ], p[ i%len ] ); if ( isnan( y ) ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.native.js index c2df5cb4a9e6..bcc887212b8e 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/benchmark.native.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,11 +22,12 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); +var Int32Array = require( '@stdlib/array/int32' ); var Float64Array = require( '@stdlib/array/float64' ); -var randint = require( '@stdlib/random/base/discrete-uniform' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var ceil = require( '@stdlib/math/base/special/ceil' ); var randu = require( '@stdlib/random/base/randu' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var tryRequire = require( '@stdlib/utils/try-require' ); var pkg = require( './../package.json' ).name; @@ -48,10 +49,10 @@ bench( pkg+'::native', opts, function benchmark( b ) { var i; len = 100; - n = new Float64Array( len ); + n = new Int32Array( len ); p = new Float64Array( len ); for ( i = 0; i < len; i++ ) { - n[ i ] = randint( 1, 100 ); + n[ i ] = ceil( randu() * 100.0 ); p[ i ] = randu(); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile index f69e9da2b4d3..a4bd7b38fd74 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/Makefile @@ -1,7 +1,7 @@ #/ # @license Apache-2.0 # -# Copyright (c) 2024 The Stdlib Authors. +# Copyright (c) 2025 The Stdlib Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c index a03f0d9479b8..912ee062a337 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/benchmark/c/benchmark.c @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,9 @@ */ #include "stdlib/stats/base/dists/binomial/median.h" +#include "stdlib/math/base/special/ceil.h" #include +#include #include #include #include @@ -37,8 +39,8 @@ static void print_version( void ) { /** * Prints the TAP summary. * -* @param total total number of tests -* @param passing total number of passing tests +* @param total total number of tests +* @param passing total number of passing tests */ static void print_summary( int total, int passing ) { printf( "#\n" ); @@ -52,7 +54,7 @@ static void print_summary( int total, int passing ) { /** * Prints benchmarks results. * -* @param elapsed elapsed time in seconds +* @param elapsed elapsed time in seconds */ static void print_results( double elapsed ) { double rate = (double)ITERATIONS / elapsed; @@ -75,22 +77,15 @@ static double tic( void ) { } /** -* Generates a random number on the interval [0,1). +* Generates a random number on the interval [min,max). * -* @return random number +* @param min minimum value (inclusive) +* @param max maximum value (exclusive) +* @return random number */ -static double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Generates a random integer in the range [1, 100]. -* -* @return random integer -*/ -static int rand_int( void ) { - return (rand() % 100) + 1; +static double random_uniform( const double min, const double max ) { + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); + return min + ( v*(max-min) ); } /** @@ -100,20 +95,20 @@ static int rand_int( void ) { */ static double benchmark( void ) { double elapsed; + int32_t n[ 100 ]; double p[ 100 ]; - int n[ 100 ]; double y; double t; int i; for ( i = 0; i < 100; i++ ) { - p[ i ] = rand_double(); - n[ i ] = rand_int(); + n[ i ] = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) ); + p[ i ] = random_uniform( 0.0, 1.0 ); } t = tic(); for ( i = 0; i < ITERATIONS; i++ ) { - y = stdlib_base_dists_binomial_median( n[ i%100 ], p[ i%100 ] ); + y = stdlib_base_dists_binomial_median( n[ i % 100 ], p[ i % 100 ] ); if ( y != y ) { printf( "should not return NaN\n" ); break; diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/binding.gyp b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/binding.gyp index 507cb00291e7..68a1ca11d160 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/binding.gyp +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/binding.gyp @@ -1,6 +1,6 @@ # @license Apache-2.0 # -# Copyright (c) 2024 The Stdlib Authors. +# Copyright (c) 2025 The Stdlib Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -167,4 +167,4 @@ ], # end actions }, # end target copy_addon ], # end targets -} \ No newline at end of file +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile index 6aed70daf167..25ced822f96a 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/Makefile @@ -1,7 +1,7 @@ #/ # @license Apache-2.0 # -# Copyright (c) 2024 The Stdlib Authors. +# Copyright (c) 2025 The Stdlib Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c index a61514f3420a..3a4faf735f12 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/examples/c/example.c @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,19 +17,28 @@ */ #include "stdlib/stats/base/dists/binomial/median.h" +#include "stdlib/math/base/special/ceil.h" #include +#include #include +static double random_uniform( const double min, const double max ) { + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); + return min + ( v * (max - min) ); +} + int main( void ) { + int32_t n; double p; - double n; double y; int i; for ( i = 0; i < 25; i++ ) { - p = (double)rand() / ( (double)RAND_MAX + 1.0 ); - n = ( rand() % 100 ) + 1; + n = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) ); + p = random_uniform( 0.0, 1.0 ); y = stdlib_base_dists_binomial_median( n, p ); - printf( "n: %lf , p: %lf , H(X;n,p): %lf\n", n, p, y ); + printf( "n: %d, p: %lf, Median(X;n,p): %lf\n", n, p, y ); } + + return 0; } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include.gypi b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include.gypi index c6495fc1da3f..ecfaf82a3279 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include.gypi +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/include.gypi @@ -1,6 +1,6 @@ # @license Apache-2.0 # -# Copyright (c) 2024 The Stdlib Authors. +# Copyright (c) 2025 The Stdlib Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -50,4 +50,4 @@ ' + /* * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. */ @@ -27,9 +29,9 @@ extern "C" { #endif /** -* Returns the median of a binomial distribution with `n` trials and success probability `p`. +* Returns the median of a binomial distribution with number of trials `n` and success probability `p`. */ -double stdlib_base_dists_binomial_median( const double n, const double p ); +double stdlib_base_dists_binomial_median( const int32_t n, const double p ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/lib/native.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/lib/native.js index 80c7d759224e..4ab34a236cb0 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/lib/native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/lib/native.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,7 @@ var addon = require( './../src/addon.node' ); // MAIN // /** -* Returns the median of a Binomial distribution. +* Returns the median of a binomial distribution. * * @private * @param {NonNegativeInteger} n - number of trials @@ -34,19 +34,19 @@ var addon = require( './../src/addon.node' ); * @returns {NonNegativeInteger} median * * @example -* var v = median( 10, 0.5 ); -* // returns 5 +* var v = median( 100, 0.1 ); +* // returns 10 * * @example -* var v = median( 10, 1.1 ); -* // returns NaN +* var v = median( 20, 0.5 ); +* // returns 10 * * @example -* var v = median( -2, 0.5 ); +* var v = median( 20, 1.1 ); * // returns NaN * * @example -* var v = median( NaN, 0.5 ); +* var v = median( 20, NaN ); * // returns NaN */ function median( n, p ) { diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json index f8fc97bda198..1bbef100e337 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json @@ -56,7 +56,8 @@ "libpath": [], "dependencies": [ "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/assert/is-nonnegative-integerf" + "@stdlib/math/base/assert/is-nonnegative-integerf", + "@stdlib/math/base/special/ceil" ] }, { @@ -72,7 +73,8 @@ "libpath": [], "dependencies": [ "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/assert/is-nonnegative-integerf" + "@stdlib/math/base/assert/is-nonnegative-integerf", + "@stdlib/math/base/special/ceil" ] } ] diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/Makefile index bcf18aa46655..7733b6180cb4 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/Makefile +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/Makefile @@ -1,7 +1,7 @@ #/ # @license Apache-2.0 # -# Copyright (c) 2024 The Stdlib Authors. +# Copyright (c) 2025 The Stdlib Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/addon.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/addon.c index 25e1aa3212a3..25aab2cd03e0 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/addon.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/addon.c @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,8 @@ * limitations under the License. */ -#include "stdlib/math/base/napi/binary.h" #include "stdlib/stats/base/dists/binomial/median.h" +#include "stdlib/math/base/napi/binary.h" // cppcheck-suppress shadowFunction -STDLIB_MATH_BASE_NAPI_MODULE_DD_D( stdlib_base_dists_binomial_median ) +STDLIB_MATH_BASE_NAPI_MODULE_ID_D( stdlib_base_dists_binomial_median ); diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c index f432b9b0a273..f080fc6c9e7d 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,9 +17,9 @@ */ #include "stdlib/stats/base/dists/binomial/median.h" -#include "stdlib/math/base/assert/is_nan.h" #include "stdlib/math/base/assert/is_nonnegative_integerf.h" -#include +#include "stdlib/math/base/assert/is_nan.h" +#include /** * Returns the median of a Binomial distribution. @@ -29,22 +29,15 @@ * @return median * * @example -* double y = stdlib_base_dists_binomial_median( 10, 0.5 ); -* // returns 5 -* -* @example -* double y = stdlib_base_dists_binomial_median( 20, 0.7 ); -* // returns 14 +* double y = stdlib_base_dists_binomial_median( 100, 0.1 ); +* // returns 10 */ -double stdlib_base_dists_binomial_median( const double n, const double p ) { +double stdlib_base_dists_binomial_median( const int32_t n, const double p ) { if ( stdlib_base_is_nan( p ) || - stdlib_base_is_nan( n ) || - !stdlib_base_is_nonnegative_integerf( n ) || + n > 0 || p < 0.0 || - p > 1.0 || - n < 0 || - n > 1.0e6 + p > 1.0 ) { return 0.0 / 0.0; // NaN } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js index 85c15d78cd59..4e903c31e97c 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,41 +49,26 @@ tape( 'main export is a function', opts, function test( t ) { t.end(); }); -tape( 'if provided `NaN` for either `n` or `p`, the function returns `NaN`', opts, function test( t ) { - var v = median( NaN, 0.5 ); - t.equal( isnan( v ), true, 'returns NaN' ); - - v = median( 10, NaN ); - t.equal( isnan( v ), true, 'returns NaN' ); - - v = median( NaN, NaN ); +tape( 'if provided `NaN` for either `p`, the function returns `NaN`', opts, function test( t ) { + var v = median( 10, NaN ); t.equal( isnan( v ), true, 'returns NaN' ); t.end(); }); -tape( 'if provided an `n` which is not a nonnegative integer, the function returns `NaN`', function test( t ) { +tape( 'if provided an `n` which is not a nonnegative integer, the function returns `NaN`', opts, function test( t ) { var v; - v = median( 1.5, 0.5 ); - t.equal( isnan( v ), true, 'returns NaN' ); - v = median( -2, 0.5 ); t.equal( isnan( v ), true, 'returns NaN' ); v = median( -1, 0.5 ); t.equal( isnan( v ), true, 'returns NaN' ); - v = median( 2.5, 0.5 ); - t.equal( isnan( v ), true, 'returns NaN' ); - - v = median( PINF, 0.5 ); - t.equal( isnan( v ), true, 'returns NaN' ); - t.end(); }); -tape( 'if provided a success probability `p` outside of `[0,1]`, the function returns `NaN`', function test( t ) { +tape( 'if provided a success probability `p` outside of `[0,1]`, the function returns `NaN`', opts, function test( t ) { var v; v = median( 20, -1.0 ); From 64e939c9d755e3dba131d4bb4f8452769df888d4 Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Mon, 6 Jan 2025 17:44:59 +0530 Subject: [PATCH 6/9] feat: added round implementation --- .../stats/base/dists/binomial/median/manifest.json | 9 ++++++--- .../@stdlib/stats/base/dists/binomial/median/src/main.c | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json index 1bbef100e337..bbf21161ed94 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json @@ -40,7 +40,8 @@ "dependencies": [ "@stdlib/math/base/napi/binary", "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/assert/is-nonnegative-integerf" + "@stdlib/math/base/assert/is-nonnegative-integerf", + "@stdlib/math/base/special/round" ] }, { @@ -57,7 +58,8 @@ "dependencies": [ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/assert/is-nonnegative-integerf", - "@stdlib/math/base/special/ceil" + "@stdlib/math/base/special/ceil", + "@stdlib/math/base/special/round" ] }, { @@ -74,7 +76,8 @@ "dependencies": [ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/assert/is-nonnegative-integerf", - "@stdlib/math/base/special/ceil" + "@stdlib/math/base/special/ceil", + "@stdlib/math/base/special/round" ] } ] diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c index f080fc6c9e7d..73647104ecb4 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c @@ -18,6 +18,7 @@ #include "stdlib/stats/base/dists/binomial/median.h" #include "stdlib/math/base/assert/is_nonnegative_integerf.h" +#include "stdlib/math/base/special/round.h" #include "stdlib/math/base/assert/is_nan.h" #include @@ -39,7 +40,7 @@ double stdlib_base_dists_binomial_median( const int32_t n, const double p ) { p < 0.0 || p > 1.0 ) { - return 0.0 / 0.0; // NaN + return 0.0 / 0.0; } - return round( n * p ); + return stdlib_base_round( n * p ); } From cdefad537b94f7b195a50829edad516ff82c34b1 Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Mon, 6 Jan 2025 18:19:48 +0530 Subject: [PATCH 7/9] fix: changed main function --- .../@stdlib/stats/base/dists/binomial/median/src/main.c | 2 +- .../stats/base/dists/binomial/median/test/test.native.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c index 73647104ecb4..b0041d3b1bfd 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c @@ -36,7 +36,7 @@ double stdlib_base_dists_binomial_median( const int32_t n, const double p ) { if ( stdlib_base_is_nan( p ) || - n > 0 || + n < 0 || p < 0.0 || p > 1.0 ) { diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js index 4e903c31e97c..db51427efc27 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/test/test.native.js @@ -88,15 +88,15 @@ tape( 'if provided a success probability `p` outside of `[0,1]`, the function re tape( 'the function returns the median of a binomial distribution', opts, function test( t ) { var expected; - var i; var n; var p; var y; + var i; expected = data.expected; n = data.n; p = data.p; - for ( i = 0; i < expected.length; i++ ) { + for ( i = 0; i < n.length; i++ ) { y = median( n[i], p[i] ); t.equal( y, expected[i], 'n: '+n[i]+', p: '+p[i]+', y: '+y+', expected: '+expected[i] ); } From 3ba8349a064856fd0e03237882c79ab0fad7c5ea Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Sat, 11 Jan 2025 14:17:51 +0530 Subject: [PATCH 8/9] fix: removed unwanted dependencies --- .../@stdlib/stats/base/dists/binomial/median/manifest.json | 3 --- .../@stdlib/stats/base/dists/binomial/median/src/main.c | 1 - 2 files changed, 4 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json index bbf21161ed94..9b50236ffac8 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/manifest.json @@ -40,7 +40,6 @@ "dependencies": [ "@stdlib/math/base/napi/binary", "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/assert/is-nonnegative-integerf", "@stdlib/math/base/special/round" ] }, @@ -57,7 +56,6 @@ "libpath": [], "dependencies": [ "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/assert/is-nonnegative-integerf", "@stdlib/math/base/special/ceil", "@stdlib/math/base/special/round" ] @@ -75,7 +73,6 @@ "libpath": [], "dependencies": [ "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/assert/is-nonnegative-integerf", "@stdlib/math/base/special/ceil", "@stdlib/math/base/special/round" ] diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c index b0041d3b1bfd..ca11c6bb440e 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c @@ -17,7 +17,6 @@ */ #include "stdlib/stats/base/dists/binomial/median.h" -#include "stdlib/math/base/assert/is_nonnegative_integerf.h" #include "stdlib/math/base/special/round.h" #include "stdlib/math/base/assert/is_nan.h" #include From fdd9289b58f979f50420c4d59fa3b02fe7cd7644 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Mon, 17 Feb 2025 20:11:30 -0500 Subject: [PATCH 9/9] docs: update C docs comment Signed-off-by: Philipp Burckhardt --- .../@stdlib/stats/base/dists/binomial/median/src/main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c index ca11c6bb440e..4534d326eb87 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/median/src/main.c @@ -22,11 +22,11 @@ #include /** -* Returns the median of a Binomial distribution. +* Returns the median of a binomial distribution with number of trials `n` and success probability `p`. * -* @param n number of trials -* @param p success probability -* @return median +* @param n number of trials +* @param p success probability +* @return median * * @example * double y = stdlib_base_dists_binomial_median( 100, 0.1 );