From 532f7902ef88ae392fc40c05c9e6979ca4b5514c Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Mon, 7 Sep 2026 21:37:18 +0530 Subject: [PATCH 1/2] feat: add stats/base/ndarray/nanvariancepn --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../base/ndarray/nanvariancepn/README.md | 172 ++++++++++ .../benchmark/benchmark.native.js | 110 +++++++ .../nanvariancepn/benchmark/c/Makefile | 146 +++++++++ .../benchmark/c/benchmark.length.c | 198 ++++++++++++ .../base/ndarray/nanvariancepn/binding.gyp | 170 ++++++++++ .../ndarray/nanvariancepn/examples/c/Makefile | 146 +++++++++ .../nanvariancepn/examples/c/example.c | 89 +++++ .../base/ndarray/nanvariancepn/include.gypi | 53 +++ .../stdlib/stats/base/ndarray/nanvariancepn.h | 40 +++ .../base/ndarray/nanvariancepn/lib/index.js | 16 +- .../base/ndarray/nanvariancepn/lib/native.js | 91 ++++++ .../base/ndarray/nanvariancepn/manifest.json | 112 +++++++ .../base/ndarray/nanvariancepn/package.json | 4 + .../base/ndarray/nanvariancepn/src/Makefile | 70 ++++ .../base/ndarray/nanvariancepn/src/addon.c | 63 ++++ .../base/ndarray/nanvariancepn/src/main.c | 291 +++++++++++++++++ .../base/ndarray/nanvariancepn/test/test.js | 210 ++---------- .../ndarray/nanvariancepn/test/test.main.js | 231 +++++++++++++ .../ndarray/nanvariancepn/test/test.native.js | 305 ++++++++++++++++++ 19 files changed, 2334 insertions(+), 183 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/c/benchmark.length.c create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/binding.gyp create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/include.gypi create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/include/stdlib/stats/base/ndarray/nanvariancepn.h create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/lib/native.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/manifest.json create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/addon.c create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/main.c create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.main.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.native.js diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/README.md index 2ebf70300b2d..d52535c8f4e5 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/README.md +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/README.md @@ -175,6 +175,178 @@ console.log( v ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/base/ndarray/nanvariancepn.h" +``` + +#### stdlib_stats_nanvariancepn( arrays ) + +Computes the variance of a one-dimensional ndarray, ignoring `NaN` values and using a two-pass algorithm. + +```c +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include + +// Create an ndarray: +const double data[] = { 1.0, -2.0, 0.0/0.0, 2.0 }; +int64_t shape[] = { 4 }; +int64_t strides[] = { STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT }; +int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + +struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes ); + +// Create an ndarray for specifying the degrees of freedom adjustment: +const double cdata[] = { 1.0 }; +int64_t cstrides[] = { 0 }; +struct ndarray *corr = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)cdata, 0, NULL, cstrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes ); + +// Compute the result: +const struct ndarray *arrays[] = { x, corr }; +double v = stdlib_stats_nanvariancepn( arrays ); +// returns ~4.3333 + +// Free allocated memory: +stdlib_ndarray_free( x ); +stdlib_ndarray_free( corr ); +``` + +The function accepts the following arguments: + +- **arrays**: `[in] struct ndarray**` list containing the following ndarrays: + + - `[in] struct ndarray*` a one-dimensional input ndarray. + - `[in] struct ndarray*` a zero-dimensional ndarray specifying the degrees of freedom adjustment. Providing a non-zero degrees of freedom adjustment has the effect of adjusting the divisor during the calculation of the [variance][variance] according to `N-c` where `N` is the number of elements in the input ndarray and `c` corresponds to the provided degrees of freedom adjustment. When computing the [variance][variance] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the corrected sample [variance][variance], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). + +```c +double stdlib_stats_nanvariancepn( const struct ndarray *arrays[] ); +``` + +
+ + + + + +
+ +- The function supports the following input ndarray data types: `float64`, `float32`, `int32`, `uint32`, `int16`, `uint16`, `int8`, `uint8`, and `uint8c`. If provided an ndarray having an unsupported data type, the function returns `NaN`. + +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/stats/base/ndarray/nanvariancepn.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include +#include +#include + +int main( void ) { + // Create a data buffer: + const double data[] = { 1.0, -2.0, 3.0, 0.0/0.0, 5.0, -6.0, 7.0, -8.0 }; + + // Specify the number of array dimensions: + const int64_t ndims = 1; + + // Specify the array shape: + int64_t shape[] = { 4 }; + + // Specify the array strides: + int64_t strides[] = { 2*STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT }; + + // Specify the byte offset: + const int64_t offset = 0; + + // Specify the array order: + const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; + + // Specify the index mode: + const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; + + // Specify the subscript index modes: + int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + const int64_t nsubmodes = 1; + + // Create an ndarray: + struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + if ( x == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + + // Create a data buffer for an ndarray specifying the degrees of freedom adjustment: + const double cdata[] = { 1.0 }; + + // Specify the array strides: + int64_t cstrides[] = { 0 }; + + // Create an ndarray for the degrees of freedom adjustment: + struct ndarray *corr = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)cdata, 0, NULL, cstrides, 0, order, imode, nsubmodes, submodes ); + if ( corr == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + + // Define a list of ndarrays: + const struct ndarray *arrays[] = { x, corr }; + + // Compute the result: + double v = stdlib_stats_nanvariancepn( arrays ); + + // Print the result: + printf( "result: %lf\n", v ); + + // Free allocated memory: + stdlib_ndarray_free( x ); + stdlib_ndarray_free( corr ); +} +``` + +
+ + + +
+ + + * * *
diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/benchmark.native.js new file mode 100644 index 000000000000..3c52aaf4089d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/benchmark.native.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 uniform = require( '@stdlib/random/uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var nanvariancepn = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( nanvariancepn instanceof Error ) +}; +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var correction = scalar2ndarray( 1.0, options ); + var x = uniform( [ len ], -10.0, 10.0, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = nanvariancepn( [ x, correction ] ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s::native:len=%d', pkg, len ), opts, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/c/Makefile b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/c/Makefile new file mode 100644 index 000000000000..0756dc7da20a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 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.length.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 diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/c/benchmark.length.c new file mode 100644 index 000000000000..b0ead1f2abcf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/c/benchmark.length.c @@ -0,0 +1,198 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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/ndarray/nanvariancepn.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include +#include +#include +#include +#include +#include + +#define NAME "nanvariancepn" +#define ITERATIONS 1000000 +#define REPEATS 3 +#define MIN 1 +#define MAX 6 + +/** +* 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 iterations number of iterations +* @param elapsed elapsed time in seconds +*/ +static void print_results( int iterations, 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 ); +} + +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param len array length +* @return elapsed time in seconds +*/ +static double benchmark( int iterations, int len ) { + enum STDLIB_NDARRAY_INDEX_MODE imode; + const struct ndarray *arrays[ 2 ]; + enum STDLIB_NDARRAY_ORDER order; + int64_t cstrides[ 1 ]; + int8_t submodes[ 1 ]; + int64_t strides[ 1 ]; + int64_t shape[ 1 ]; + int64_t nsubmodes; + struct ndarray *x; + double cdata[ 1 ]; + int64_t offset; + double elapsed; + int64_t ndims; + double *data; + double v; + double t; + int i; + + ndims = 1; + shape[ 0 ] = len; + strides[ 0 ] = STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT; + offset = 0; + order = STDLIB_NDARRAY_ROW_MAJOR; + imode = STDLIB_NDARRAY_INDEX_ERROR; + submodes[ 0 ] = imode; + nsubmodes = 1; + + data = (double *)malloc( len * sizeof( double ) ); + for ( i = 0; i < len; i++ ) { + data[ i ] = ( rand_double() * 20000.0 ) - 10000.0; + } + // cppcheck-suppress invalidPointerCast + x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + arrays[ 0 ] = x; + + ndims = 0; + cdata[ 0 ] = 1.0; + cstrides[ 0 ] = 0; + offset = 0; + + // cppcheck-suppress invalidPointerCast + struct ndarray *corr = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)cdata, ndims, NULL, cstrides, offset, order, imode, nsubmodes, submodes ); + arrays[ 1 ] = corr; + + v = 0.0; + t = tic(); + for ( i = 0; i < iterations; i++ ) { + v = stdlib_stats_nanvariancepn( arrays ); + if ( v != v ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( v != v ) { + printf( "should not return NaN\n" ); + } + stdlib_ndarray_free( x ); + stdlib_ndarray_free( corr ); + free( data ); + arrays[ 0 ] = NULL; + arrays[ 1 ] = NULL; + + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int count; + int iter; + int len; + int i; + int j; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + count = 0; + for ( i = MIN; i <= MAX; i++ ) { + len = pow( 10, i ); + iter = ITERATIONS / pow( 10, i - 1 ); + for ( j = 0; j < REPEATS; j++ ) { + count += 1; + printf( "# c::%s:len=%d\n", NAME, len ); + elapsed = benchmark( iter, len ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + } + print_summary( count, count ); +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/binding.gyp b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/binding.gyp new file mode 100644 index 000000000000..0d6508a12e99 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/binding.gyp @@ -0,0 +1,170 @@ +# @license Apache-2.0 +# +# Copyright (c) 2026 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 +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/examples/c/Makefile b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/examples/c/Makefile new file mode 100644 index 000000000000..c8f8e9a1517b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 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 diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/examples/c/example.c b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/examples/c/example.c new file mode 100644 index 000000000000..94137bfea777 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/examples/c/example.c @@ -0,0 +1,89 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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/ndarray/nanvariancepn.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include +#include +#include + +int main( void ) { + // Create a data buffer: + const double data[] = { 1.0, -2.0, 3.0, 0.0/0.0, 5.0, -6.0, 7.0, -8.0 }; + + // Specify the number of array dimensions: + const int64_t ndims = 1; + + // Specify the array shape: + int64_t shape[] = { 4 }; + + // Specify the array strides: + int64_t strides[] = { 2 * STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT }; + + // Specify the byte offset: + const int64_t offset = 0; + + // Specify the array order: + const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; + + // Specify the index mode: + const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; + + // Specify the subscript index modes: + int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + const int64_t nsubmodes = 1; + + // Create an ndarray: + // cppcheck-suppress invalidPointerCast + struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + if ( x == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + + // Create a data buffer for an ndarray specifying the degrees of freedom adjustment: + const double cdata[] = { 1.0 }; + + // Specify the array strides: + int64_t cstrides[] = { 0 }; + + // Create an ndarray for the degrees of freedom adjustment: + // cppcheck-suppress invalidPointerCast + struct ndarray *corr = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)cdata, 0, NULL, cstrides, 0, order, imode, nsubmodes, submodes ); + if ( corr == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + + // Define a list of ndarrays: + const struct ndarray *arrays[] = { x, corr }; + + // Compute the result: + double v = stdlib_stats_nanvariancepn( arrays ); + + // Print the result: + printf( "result: %lf\n", v ); + + // Free allocated memory: + stdlib_ndarray_free( x ); + stdlib_ndarray_free( corr ); +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/include.gypi b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/include.gypi new file mode 100644 index 000000000000..bee8d41a2caf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/include.gypi @@ -0,0 +1,53 @@ +# @license Apache-2.0 +# +# Copyright (c) 2026 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': [ + '} arrays - array-like object containing ndarrays +* @returns {number} variance +* +* @example +* var vector = require( '@stdlib/ndarray/vector/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* +* var x = vector( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ], 'generic' ); +* +* var correction = scalar2ndarray( 1.0, { +* 'dtype': 'generic' +* }); +* +* var v = nanvariancepn( [ x, correction ] ); +* // returns ~4.79 +*/ +function nanvariancepn( arrays ) { + var correction = arrays[ 1 ]; + var x = arrays[ 0 ]; + if ( !isSupportedDataType( getDType( x ) ) || !isSupportedDataType( getDType( correction ) ) ) { // eslint-disable-line max-len + return fallback( arrays ); + } + return addon( getData( x ), serialize( x ), getData( correction ), serialize( correction ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = nanvariancepn; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/manifest.json b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/manifest.json new file mode 100644 index 000000000000..0fa28523c6fa --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/manifest.json @@ -0,0 +1,112 @@ +{ + "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, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/stats/strided/dnanvariancepn", + "@stdlib/ndarray/ctor", + "@stdlib/ndarray/dtypes", + "@stdlib/ndarray/base/napi/addon-arguments", + "@stdlib/napi/export", + "@stdlib/napi/argv", + "@stdlib/napi/create-double" + ] + }, + { + "task": "benchmark", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/stats/strided/dnanvariancepn", + "@stdlib/ndarray/ctor", + "@stdlib/ndarray/dtypes", + "@stdlib/ndarray/index-modes", + "@stdlib/ndarray/orders", + "@stdlib/ndarray/base/bytes-per-element" + ] + }, + { + "task": "examples", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/stats/strided/dnanvariancepn", + "@stdlib/ndarray/ctor", + "@stdlib/ndarray/dtypes", + "@stdlib/ndarray/index-modes", + "@stdlib/ndarray/orders", + "@stdlib/ndarray/base/bytes-per-element" + ] + }, + { + "task": "", + "wasm": true, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/stats/strided/dnanvariancepn", + "@stdlib/ndarray/ctor", + "@stdlib/ndarray/dtypes" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/package.json index bfa2680623e1..e060bc65271d 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/package.json +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/package.json @@ -14,11 +14,15 @@ } ], "main": "./lib", + "browser": "./lib/main.js", + "gypfile": true, "directories": { "benchmark": "./benchmark", "doc": "./docs", "example": "./examples", + "include": "./include", "lib": "./lib", + "src": "./src", "test": "./test" }, "types": "./docs/types", diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/Makefile b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/Makefile new file mode 100644 index 000000000000..2caf905cedbe --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 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 + + +# RULES # + +#/ +# Removes generated files for building an add-on. +# +# @example +# make clean-addon +#/ +clean-addon: + $(QUIET) -rm -f *.o *.node + +.PHONY: clean-addon + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: clean-addon + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/addon.c b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/addon.c new file mode 100644 index 000000000000..76702e2fd56a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/addon.c @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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/ndarray/nanvariancepn.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/base/napi/addon_arguments.h" +#include "stdlib/napi/export.h" +#include "stdlib/napi/create_double.h" +#include "stdlib/napi/argv.h" +#include +#include + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 4 ); + + // Process provided arguments: + struct ndarray *arrays[ 2 ]; + napi_value err; + napi_status status = stdlib_ndarray_napi_addon_arguments( env, argv, 4, 2, arrays, &err ); + assert( status == napi_ok ); + if ( err != NULL ) { + status = napi_throw( env, err ); + assert( status == napi_ok ); + return NULL; + } + // Create a const-qualified view of the argument pointer list: + const struct ndarray *arr[ 2 ] = { arrays[ 0 ], arrays[ 1 ] }; + + // Perform computation: + STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_stats_nanvariancepn( arr ), v ); + + // Free allocated memory: + stdlib_ndarray_free( arrays[ 0 ] ); + stdlib_ndarray_free( arrays[ 1 ] ); + arrays[ 0 ] = NULL; + arrays[ 1 ] = NULL; + + return v; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/main.c b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/main.c new file mode 100644 index 000000000000..f3ab43c1c212 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/src/main.c @@ -0,0 +1,291 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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/ndarray/nanvariancepn.h" +#include "stdlib/stats/strided/dnanvariancepn.h" +#include "stdlib/blas/base/shared.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include + +// Blocksize for pairwise summation (NOTE: decreasing the blocksize decreases rounding error as more pairs are summed, but also decreases performance. Because the inner loop is unrolled eight times, the blocksize is effectively `16`.): +#define BLOCKSIZE 128 + +/** +* Returns an ndarray element as a double-precision floating-point number. +* +* ## Notes +* +* - If provided an unsupported data type, the function returns `NaN`. +* +* @param dtype data type +* @param ptr pointer to an ndarray element +* @return element value +*/ +static double get_value( const int16_t dtype, const uint8_t *ptr ) { + switch ( dtype ) { + case STDLIB_NDARRAY_FLOAT64: + return *(const double *)ptr; + case STDLIB_NDARRAY_FLOAT32: + return (double)( *(const float *)ptr ); + case STDLIB_NDARRAY_INT32: + return (double)( *(const int32_t *)ptr ); + case STDLIB_NDARRAY_UINT32: + return (double)( *(const uint32_t *)ptr ); + case STDLIB_NDARRAY_INT16: + return (double)( *(const int16_t *)ptr ); + case STDLIB_NDARRAY_UINT16: + return (double)( *(const uint16_t *)ptr ); + case STDLIB_NDARRAY_INT8: + return (double)( *(const int8_t *)ptr ); + case STDLIB_NDARRAY_UINT8: + case STDLIB_NDARRAY_UINT8C: + return (double)( *(const uint8_t *)ptr ); + default: + return 0.0 / 0.0; // NaN + } +} + +/** +* Returns the value of a zero-dimensional ndarray as a double-precision floating-point number. +* +* @param arr input ndarray +* @return element value +*/ +static double get_scalar( const struct ndarray *arr ) { + const uint8_t *ptr = stdlib_ndarray_data( arr ) + stdlib_ndarray_offset( arr ); + return get_value( stdlib_ndarray_dtype( arr ), ptr ); +} + +/** +* Computes the sum of ndarray elements, ignoring `NaN` values and using pairwise summation. +* +* ## Method +* +* - This implementation uses pairwise summation, which has an `O(log n)` error bound (see Higham (1993)). +* +* @param N number of indexed elements +* @param dtype data type +* @param ptr pointer to the first indexed element +* @param stride stride length (in bytes) +* @param sum pointer to a running sum +* @param count pointer to a running count of non-`NaN` elements +*/ +static void nansumpw( const int64_t N, const int16_t dtype, const uint8_t *ptr, const int64_t stride, double *sum, int64_t *count ) { + int64_t M; + int64_t n; + int64_t i; + double s0; + double s1; + double s2; + double s3; + double s4; + double s5; + double s6; + double s7; + double s; + double v; + + if ( N < 8 ) { + // Use simple summation... + s = 0.0; + n = 0; + for ( i = 0; i < N; i++ ) { + v = get_value( dtype, ptr ); + if ( v == v ) { + s += v; + n += 1; + } + ptr += stride; + } + *sum += s; + *count += n; + return; + } + if ( N <= BLOCKSIZE ) { + // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... + s0 = 0.0; + s1 = 0.0; + s2 = 0.0; + s3 = 0.0; + s4 = 0.0; + s5 = 0.0; + s6 = 0.0; + s7 = 0.0; + n = 0; + + M = N % 8; + for ( i = 0; i < N-M; i += 8 ) { + v = get_value( dtype, ptr ); + if ( v == v ) { + s0 += v; + n += 1; + } + ptr += stride; + v = get_value( dtype, ptr ); + if ( v == v ) { + s1 += v; + n += 1; + } + ptr += stride; + v = get_value( dtype, ptr ); + if ( v == v ) { + s2 += v; + n += 1; + } + ptr += stride; + v = get_value( dtype, ptr ); + if ( v == v ) { + s3 += v; + n += 1; + } + ptr += stride; + v = get_value( dtype, ptr ); + if ( v == v ) { + s4 += v; + n += 1; + } + ptr += stride; + v = get_value( dtype, ptr ); + if ( v == v ) { + s5 += v; + n += 1; + } + ptr += stride; + v = get_value( dtype, ptr ); + if ( v == v ) { + s6 += v; + n += 1; + } + ptr += stride; + v = get_value( dtype, ptr ); + if ( v == v ) { + s7 += v; + n += 1; + } + ptr += stride; + } + // Pairwise sum the accumulators: + s = ( (s0+s1) + (s2+s3) ) + ( (s4+s5) + (s6+s7) ); + + // Clean-up loop... + for (; i < N; i++ ) { + v = get_value( dtype, ptr ); + if ( v == v ) { + s += v; + n += 1; + } + ptr += stride; + } + *sum += s; + *count += n; + return; + } + // Recurse by dividing by two, but avoiding non-multiples of unroll factor... + n = N / 2; + n -= n % 8; + nansumpw( n, dtype, ptr, stride, sum, count ); + nansumpw( N-n, dtype, ptr+(n*stride), stride, sum, count ); +} + +/** +* Computes the variance of a one-dimensional ndarray having an arbitrary real-valued data type, ignoring `NaN` values and using a two-pass algorithm. +* +* @param N number of indexed elements +* @param correction degrees of freedom adjustment +* @param dtype data type +* @param ptr pointer to the first indexed element +* @param stride stride length (in bytes) +* @return output value +*/ +static double compute( const int64_t N, const double correction, const int16_t dtype, const uint8_t *ptr, const int64_t stride ) { + int64_t n; + int64_t i; + double sum; + double mu; + double M2; + double nc; + double dn; + double M; + double d; + double v; + + if ( N <= 0 ) { + return 0.0 / 0.0; // NaN + } + if ( N == 1 || stride == 0 ) { + v = get_value( dtype, ptr ); + if ( v == v && (double)N-correction > 0.0 ) { + return 0.0; + } + return 0.0 / 0.0; // NaN + } + // Compute an estimate for the mean... + sum = 0.0; + n = 0; + nansumpw( N, dtype, ptr, stride, &sum, &n ); + dn = (double)n; + nc = dn - correction; + if ( nc <= 0.0 ) { + return 0.0 / 0.0; // NaN + } + mu = sum / dn; + + // Compute the variance... + M2 = 0.0; + M = 0.0; + for ( i = 0; i < N; i++ ) { + v = get_value( dtype, ptr ); + if ( v == v ) { + d = v - mu; + M2 += d * d; + M += d; + } + ptr += stride; + } + return (M2/nc) - ((M/dn)*(M/nc)); +} + +/** +* Computes the variance of a one-dimensional ndarray, ignoring `NaN` values and using a two-pass algorithm. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a zero-dimensional ndarray specifying the degrees of freedom adjustment. +* +* - The function supports the following input ndarray data types: `float64`, `float32`, `int32`, `uint32`, `int16`, `uint16`, `int8`, `uint8`, and `uint8c`. If provided an ndarray having an unsupported data type, the function returns `NaN`. +* +* @param arrays array containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying the degrees of freedom adjustment +* @return output value +*/ +double stdlib_stats_nanvariancepn( const struct ndarray *arrays[] ) { + const struct ndarray *x = arrays[ 0 ]; + const double correction = get_scalar( arrays[ 1 ] ); + const int16_t dtype = stdlib_ndarray_dtype( x ); + const int64_t N = stdlib_ndarray_dimension( x, 0 ); + + // For double-precision floating-point ndarrays, delegate to the optimized strided kernel: + if ( dtype == STDLIB_NDARRAY_FLOAT64 ) { + return API_SUFFIX(stdlib_strided_dnanvariancepn_ndarray)( N, correction, (const double *)stdlib_ndarray_data( x ), stdlib_ndarray_stride_elements( x, 0 ), stdlib_ndarray_offset_elements( x ) ); + } + // Otherwise, read each element as a double-precision floating-point number: + return compute( N, correction, dtype, stdlib_ndarray_data( x ) + stdlib_ndarray_offset( x ), stdlib_ndarray_stride( x, 0 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.js index c97d7567a568..b7107d26e868 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.js @@ -21,28 +21,16 @@ // MODULES // var tape = require( 'tape' ); -var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var Float64Array = require( '@stdlib/array/float64' ); -var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); -var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var proxyquire = require( 'proxyquire' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); var nanvariancepn = require( './../lib' ); -// FUNCTIONS // +// VARIABLES // -/** -* Returns a one-dimensional ndarray. -* -* @private -* @param {Collection} buffer - underlying data buffer -* @param {NonNegativeInteger} length - number of indexed elements -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - index offset -* @returns {ndarray} one-dimensional ndarray -*/ -function vector( buffer, length, stride, offset ) { - return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); -} +var opts = { + 'skip': IS_BROWSER +}; // TESTS // @@ -53,179 +41,37 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function has an arity of 1', function test( t ) { - t.strictEqual( nanvariancepn.length, 1, 'has expected arity' ); - t.end(); -}); - -tape( 'the function calculates the variance of a one-dimensional ndarray, ignoring NaN values', function test( t ) { - var correction; - var expected; - var opts; - var x; - var v; - - opts = { - 'dtype': 'float64' - }; - - x = new Float64Array( [ 1.0, -2.0, -4.0, NaN, 5.0, 0.0, 3.0 ] ); - correction = scalar2ndarray( 1.0, opts ); - - v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); - expected = 53.5 / 5; - t.strictEqual( v, expected, 'returns expected value' ); - - x = new Float64Array( [ -4.0, NaN, -5.0 ] ); - correction = scalar2ndarray( 1.0, opts ); - - v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); - expected = 0.5; - t.strictEqual( v, expected, 'returns expected value' ); - - x = new Float64Array( [ NaN ] ); - correction = scalar2ndarray( 1.0, opts ); - - v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - - x = new Float64Array( [ NaN, NaN ] ); - correction = scalar2ndarray( 1.0, opts ); - - v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - - t.end(); -}); - -tape( 'if provided an empty ndarray, the function returns `NaN`', function test( t ) { - var correction; - var opts; - var x; - var v; - - opts = { - 'dtype': 'float64' - }; - - x = new Float64Array( [] ); - correction = scalar2ndarray( 1.0, opts ); - - v = nanvariancepn( [ vector( x, 0, 1, 0 ), correction ] ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); +tape( 'if a native implementation is available, the main export is the native implementation', opts, function test( t ) { + var nanvariancepn = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + t.strictEqual( nanvariancepn, mock, 'returns expected value' ); t.end(); -}); - -tape( 'if provided a correction argument yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { - var correction; - var opts; - var x; - var v; - opts = { - 'dtype': 'float64' - }; + function tryRequire() { + return mock; + } - x = new Float64Array( [ 1.0 ] ); - correction = scalar2ndarray( 1.0, opts ); - - v = nanvariancepn( [ vector( x, 1, 1, 0 ), correction ] ); - t.strictEqual( isnan( v ), true, 'returns expected value' ); - - t.end(); + function mock() { + // Mock... + } }); -tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { - var correction; - var expected; - var opts; - var x; - var v; - - opts = { - 'dtype': 'float64' - }; - - x = new Float64Array([ - 1.0, // 0 - 2.0, - -2.0, // 1 - -7.0, - 2.0, // 2 - 3.0, - 4.0, // 3 - 2.0, - NaN // 4 - ]); - correction = scalar2ndarray( 1.0, opts ); - - v = nanvariancepn( [ vector( x, 5, 2, 0 ), correction ] ); - expected = 6.25; - t.strictEqual( v, expected, 'returns expected value' ); +tape( 'if a native implementation is not available, the main export is a JavaScript implementation', opts, function test( t ) { + var nanvariancepn; + var main; - t.end(); -}); + main = require( './../lib/main.js' ); -tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { - var correction; - var expected; - var opts; - var x; - var v; - - opts = { - 'dtype': 'float64' - }; - - x = new Float64Array([ - 1.0, // 4 - 2.0, - -2.0, // 3 - -7.0, - 2.0, // 2 - 3.0, - 4.0, // 1 - 2.0, - NaN // 0 - ]); - correction = scalar2ndarray( 1.0, opts ); - - v = nanvariancepn( [ vector( x, 5, -2, 8 ), correction ] ); - expected = 6.25; - t.strictEqual( v, expected, 'returns expected value' ); + nanvariancepn = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + t.strictEqual( nanvariancepn, main, 'returns expected value' ); t.end(); -}); -tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { - var correction; - var expected; - var opts; - var x; - var v; - - opts = { - 'dtype': 'float64' - }; - - x = new Float64Array([ - 2.0, - 1.0, // 0 - 2.0, - -2.0, // 1 - -2.0, - 2.0, // 2 - 3.0, - 4.0, // 3 - NaN, - NaN // 4 - ]); - correction = scalar2ndarray( 1.0, opts ); - - v = nanvariancepn( [ vector( x, 5, 2, 1 ), correction ] ); - expected = 6.25; - t.strictEqual( v, expected, 'returns expected value' ); - - t.end(); + function tryRequire() { + return new Error( 'Cannot find module' ); + } }); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.main.js b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.main.js new file mode 100644 index 000000000000..e2c42a29a43e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.main.js @@ -0,0 +1,231 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var Float64Array = require( '@stdlib/array/float64' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var nanvariancepn = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof nanvariancepn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( nanvariancepn.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the variance of a one-dimensional ndarray, ignoring NaN values', function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array( [ 1.0, -2.0, -4.0, NaN, 5.0, 0.0, 3.0 ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); + expected = 53.5 / 5; + t.strictEqual( v, expected, 'returns expected value' ); + + x = new Float64Array( [ -4.0, NaN, -5.0 ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); + expected = 0.5; + t.strictEqual( v, expected, 'returns expected value' ); + + x = new Float64Array( [ NaN ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = new Float64Array( [ NaN, NaN ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty ndarray, the function returns `NaN`', function test( t ) { + var correction; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array( [] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, 0, 1, 0 ), correction ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a correction argument yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { + var correction; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array( [ 1.0 ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, 1, 1, 0 ), correction ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + -2.0, // 1 + -7.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + 2.0, + NaN // 4 + ]); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, 5, 2, 0 ), correction ] ); + expected = 6.25; + t.strictEqual( v, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array([ + 1.0, // 4 + 2.0, + -2.0, // 3 + -7.0, + 2.0, // 2 + 3.0, + 4.0, // 1 + 2.0, + NaN // 0 + ]); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, 5, -2, 8 ), correction ] ); + expected = 6.25; + t.strictEqual( v, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + NaN, + NaN // 4 + ]); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, 5, 2, 1 ), correction ] ); + expected = 6.25; + t.strictEqual( v, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.native.js b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.native.js new file mode 100644 index 000000000000..371160455602 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/test/test.native.js @@ -0,0 +1,305 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Int32Array = require( '@stdlib/array/int32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var nanvariancepn = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( nanvariancepn instanceof Error ) +}; + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof nanvariancepn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', opts, function test( t ) { + t.strictEqual( nanvariancepn.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the variance of a one-dimensional ndarray, ignoring NaN values', opts, function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array( [ 1.0, -2.0, -4.0, NaN, 5.0, 0.0, 3.0 ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); + expected = 53.5 / 5; + t.strictEqual( v, expected, 'returns expected value' ); + + x = new Float64Array( [ -4.0, NaN, -5.0 ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); + expected = 0.5; + t.strictEqual( v, expected, 'returns expected value' ); + + x = new Float64Array( [ NaN ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = new Float64Array( [ NaN, NaN ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, x.length, 1, 0 ), correction ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty ndarray, the function returns `NaN`', opts, function test( t ) { + var correction; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array( [] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, 0, 1, 0 ), correction ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a correction argument yielding `N-correction` less than or equal to `0`, the function returns `NaN`', opts, function test( t ) { + var correction; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array( [ 1.0 ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, 1, 1, 0 ), correction ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', opts, function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + -2.0, // 1 + -7.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + 2.0, + NaN // 4 + ]); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, 5, 2, 0 ), correction ] ); + expected = 6.25; + t.strictEqual( v, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', opts, function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array([ + 1.0, // 4 + 2.0, + -2.0, // 3 + -7.0, + 2.0, // 2 + 3.0, + 4.0, // 1 + 2.0, + NaN // 0 + ]); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, 5, -2, 8 ), correction ] ); + expected = 6.25; + t.strictEqual( v, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', opts, function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + NaN, + NaN // 4 + ]); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ vector( x, 5, 2, 1 ), correction ] ); + expected = 6.25; + t.strictEqual( v, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports single-precision floating-point ndarrays', opts, function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'float32' + }; + + x = new Float32Array( [ 1.0, -2.0, -4.0, NaN, 5.0, 0.0, 3.0 ] ); + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ new ndarray( 'float32', x, [ x.length ], [ 1 ], 0, 'row-major' ), correction ] ); + expected = 53.5 / 5; + t.strictEqual( v, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports integer ndarrays', opts, function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'int32' + }; + + x = new Int32Array( [ 1, -2, -4, 5, 0, 3 ] ); + correction = scalar2ndarray( 1, opts ); + + v = nanvariancepn( [ new ndarray( 'int32', x, [ x.length ], [ 1 ], 0, 'row-major' ), correction ] ); + expected = 53.5 / 5; + t.strictEqual( v, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports ndarrays having a data type which is not supported by the native add-on (e.g., `generic`)', opts, function test( t ) { + var correction; + var expected; + var opts; + var x; + var v; + + opts = { + 'dtype': 'generic' + }; + + x = [ 1.0, -2.0, -4.0, NaN, 5.0, 0.0, 3.0 ]; + correction = scalar2ndarray( 1.0, opts ); + + v = nanvariancepn( [ new ndarray( 'generic', x, [ x.length ], [ 1 ], 0, 'row-major' ), correction ] ); + expected = 53.5 / 5; + t.strictEqual( v, expected, 'returns expected value' ); + + t.end(); +}); From f66e822160e58287b05c209314acce28965a9dec Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Mon, 7 Sep 2026 21:42:11 +0530 Subject: [PATCH 2/2] fix: update import path --- .../stats/base/ndarray/nanvariancepn/benchmark/benchmark.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/benchmark.js index fb29ae02b454..48acf37a93ee 100644 --- a/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/ndarray/nanvariancepn/benchmark/benchmark.js @@ -30,7 +30,7 @@ var pow = require( '@stdlib/math/base/special/pow' ); var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; -var nanvariancepn = require( './../lib' ); +var nanvariancepn = require( './../lib/main.js' ); // VARIABLES //