From 0c2ee28e04797a00b45cb3245fffea71657147f6 Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Mon, 24 Feb 2025 12:02:55 +0530 Subject: [PATCH 1/2] feat: add C ndarray interface and refactor implementation --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - 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 --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../@stdlib/stats/base/dnanstdevwd/README.md | 177 +++++++++++++++--- .../base/dnanstdevwd/benchmark/benchmark.js | 30 +-- .../dnanstdevwd/benchmark/benchmark.native.js | 30 +-- .../benchmark/benchmark.ndarray.js | 30 +-- .../benchmark/benchmark.ndarray.native.js | 30 +-- .../benchmark/c/benchmark.length.c | 60 +++++- .../stats/base/dnanstdevwd/docs/repl.txt | 36 ++-- .../base/dnanstdevwd/docs/types/index.d.ts | 12 +- .../base/dnanstdevwd/examples/c/example.c | 9 +- .../stats/base/dnanstdevwd/examples/index.js | 21 +-- .../stats/base/dnanstdevwd/include.gypi | 2 +- .../include/stdlib/stats/base/dnanstdevwd.h | 9 +- .../stats/base/dnanstdevwd/lib/dnanstdevwd.js | 13 +- .../dnanstdevwd/lib/dnanstdevwd.native.js | 9 +- .../stats/base/dnanstdevwd/lib/index.js | 7 +- .../stats/base/dnanstdevwd/lib/ndarray.js | 12 +- .../base/dnanstdevwd/lib/ndarray.native.js | 20 +- .../stats/base/dnanstdevwd/manifest.json | 74 +++++++- .../stats/base/dnanstdevwd/src/addon.c | 64 +++++++ .../stats/base/dnanstdevwd/src/addon.cpp | 130 ------------- .../stats/base/dnanstdevwd/src/dnanstdevwd.c | 35 ---- .../@stdlib/stats/base/dnanstdevwd/src/main.c | 52 +++++ .../base/dnanstdevwd/test/test.dnanstdevwd.js | 13 +- .../test/test.dnanstdevwd.native.js | 13 +- .../base/dnanstdevwd/test/test.ndarray.js | 13 +- .../dnanstdevwd/test/test.ndarray.native.js | 13 +- 26 files changed, 536 insertions(+), 378 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/addon.c delete mode 100644 lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/addon.cpp delete mode 100644 lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/dnanstdevwd.c create mode 100644 lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/main.c diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/README.md b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/README.md index 7a2d1b8b66eb..54cbd21bc6c6 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/README.md +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/README.md @@ -98,9 +98,9 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var dnanstdevwd = require( '@stdlib/stats/base/dnanstdevwd' ); ``` -#### dnanstdevwd( N, correction, x, stride ) +#### dnanstdevwd( N, correction, x, strideX ) -Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array `x` ignoring `NaN` values and using Welford's algorithm. +Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array, ignoring `NaN` values and using Welford's algorithm. ```javascript var Float64Array = require( '@stdlib/array/float64' ); @@ -116,39 +116,36 @@ The function has the following parameters: - **N**: number of indexed elements. - **correction**: degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation], 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). - **x**: input [`Float64Array`][@stdlib/array/float64]. -- **stride**: index increment for `x`. +- **strideX**: stride length for `x`. -The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`, +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`, + + ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); -var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN ] ); -var N = floor( x.length / 2 ); +var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ] ); -var v = dnanstdevwd( N, 1, x, 2 ); +var v = dnanstdevwd( 5, 1, x, 2 ); // returns 2.5 ``` Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. - + ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); -var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] ); +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); - -var v = dnanstdevwd( N, 1, x1, 2 ); +var v = dnanstdevwd( 5, 1, x1, 2 ); // returns 2.5 ``` -#### dnanstdevwd.ndarray( N, correction, x, stride, offset ) +#### dnanstdevwd.ndarray( N, correction, x, strideX, offsetX ) Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics. @@ -163,18 +160,18 @@ var v = dnanstdevwd.ndarray( x.length, 1, x, 1, 0 ); The function has the following additional parameters: -- **offset**: starting index for `x`. +- **offsetX**: starting index for `x`. + +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [standard deviation][standard-deviation] for every other value in `x` starting from the second value -While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [standard deviation][standard-deviation] for every other value in `x` starting from the second value + ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); -var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -var N = floor( x.length / 2 ); +var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); -var v = dnanstdevwd.ndarray( N, 1, x, 2, 1 ); +var v = dnanstdevwd.ndarray( 5, 1, x, 2, 1 ); // returns 2.5 ``` @@ -200,18 +197,19 @@ var v = dnanstdevwd.ndarray( N, 1, x, 2, 1 ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var dnanstdevwd = require( '@stdlib/stats/base/dnanstdevwd' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); } + +var x = filledarrayBy( 10, 'float64', rand ); console.log( x ); var v = dnanstdevwd( x.length, 1, x, 1 ); @@ -222,6 +220,125 @@ console.log( v ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/base/dnanstdevwd.h" +``` + +#### stdlib_strided_dnanstdevwd( N, correction, \*X, strideX ) + +Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm. + +```c +const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 }; + +double v = stdlib_strided_dnanstdevwd( 4, 1.0, x, 1 ); +// returns ~4.3333 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **correction**: `[in] double` degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation], 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). +- **X**: `[in] double*` input array. +- **strideX**: `[in] CBLAS_INT` stride length for `X`. + +```c +double stdlib_strided_dnanstdevwd( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX ); +``` + +#### stdlib_strided_dnanstdevwd_ndarray( N, correction, \*X, strideX, offsetX ) + +Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics. + +```c +const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 }; + +double v = stdlib_strided_dnanstdevwd_ndarray( 4, 1.0, x, 1, 0 ); +// returns ~4.3333 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **correction**: `[in] double` degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation], 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). +- **X**: `[in] double*` input array. +- **strideX**: `[in] CBLAS_INT` stride length for `X`. +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. + +```c +double stdlib_strided_dnanstdevwd_ndarray( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/stats/base/dnanstdevwd.h" +#include + +int main( void ) { + // Create a strided array: + const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 }; + + // Specify the number of elements: + const int N = 6; + + // Specify the stride length: + const int strideX = 2; + + // Compute the variance: + double v = stdlib_strided_dnanstdevwd( N, 1.0, x, strideX ); + + // Print the result: + printf( "sample standard deviation: %lf\n", v ); +} +``` + +
+ + + +
+ + + * * *
diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.js index a937c9d1d86c..e976a86f31ce 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.js @@ -21,16 +21,30 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); -var Float64Array = require( '@stdlib/array/float64' ); var pkg = require( './../package.json' ).name; var dnanstdevwd = require( './../lib/dnanstdevwd.js' ); // FUNCTIONS // +/** +* Returns a random value or `NaN`. +* +* @private +* @returns {number} random number or `NaN` +*/ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -10.0, 10.0 ); +} + /** * Creates a benchmark function. * @@ -39,17 +53,7 @@ var dnanstdevwd = require( './../lib/dnanstdevwd.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = new Float64Array( len ); - for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - x[ i ] = NaN; - } else { - x[ i ] = ( randu()*20.0 ) - 10.0; - } - } + var x = filledarrayBy( len, 'float64', rand ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.native.js index a6a6b1a46a54..eed95dd7034c 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.native.js @@ -22,10 +22,11 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); -var Float64Array = require( '@stdlib/array/float64' ); var tryRequire = require( '@stdlib/utils/try-require' ); var pkg = require( './../package.json' ).name; @@ -40,6 +41,19 @@ var opts = { // FUNCTIONS // +/** +* Returns a random value or `NaN`. +* +* @private +* @returns {number} random number or `NaN` +*/ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -10.0, 10.0 ); +} + /** * Creates a benchmark function. * @@ -48,17 +62,7 @@ var opts = { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = new Float64Array( len ); - for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - x[ i ] = NaN; - } else { - x[ i ] = ( randu()*20.0 ) - 10.0; - } - } + var x = filledarrayBy( len, 'float64', rand ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.ndarray.js index 0ae41df185b5..88edb746bee8 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.ndarray.js @@ -21,16 +21,30 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); -var Float64Array = require( '@stdlib/array/float64' ); var pkg = require( './../package.json' ).name; var dnanstdevwd = require( './../lib/ndarray.js' ); // FUNCTIONS // +/** +* Returns a random value or `NaN`. +* +* @private +* @returns {number} random number or `NaN` +*/ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -10.0, 10.0 ); +} + /** * Creates a benchmark function. * @@ -39,17 +53,7 @@ var dnanstdevwd = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = new Float64Array( len ); - for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - x[ i ] = NaN; - } else { - x[ i ] = ( randu()*20.0 ) - 10.0; - } - } + var x = filledarrayBy( len, 'float64', rand ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.ndarray.native.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.ndarray.native.js index 58eefd14461f..0362e4f26b3b 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.ndarray.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/benchmark.ndarray.native.js @@ -22,10 +22,11 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); -var Float64Array = require( '@stdlib/array/float64' ); var tryRequire = require( '@stdlib/utils/try-require' ); var pkg = require( './../package.json' ).name; @@ -40,6 +41,19 @@ var opts = { // FUNCTIONS // +/** +* Returns a random value or `NaN`. +* +* @private +* @returns {number} random number or `NaN` +*/ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -10.0, 10.0 ); +} + /** * Creates a benchmark function. * @@ -48,17 +62,7 @@ var opts = { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = new Float64Array( len ); - for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - x[ i ] = NaN; - } else { - x[ i ] = ( randu()*20.0 ) - 10.0; - } - } + var x = filledarrayBy( len, 'float64', rand ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/c/benchmark.length.c index 0cc06e2d3257..fcc7d212ce7f 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/benchmark/c/benchmark.length.c @@ -94,7 +94,7 @@ static double rand_double( void ) { * @param len array length * @return elapsed time in seconds */ -static double benchmark( int iterations, int len ) { +static double benchmark1( int iterations, int len ) { double elapsed; double x[ len ]; double v; @@ -102,11 +102,16 @@ static double benchmark( int iterations, int len ) { int i; for ( i = 0; i < len; i++ ) { - x[ i ] = ( rand_double() * 20000.0 ) - 10000.0; + if ( rand_double() < 0.2 ) { + x[ i ] = 0.0 / 0.0; // NaN + } else { + x[ i ] = ( rand_double() * 20000.0 ) - 10000.0; + } } v = 0.0; t = tic(); for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar v = stdlib_strided_dnanstdevwd( len, 1.0, x, 1 ); if ( v != v ) { printf( "should not return NaN\n" ); @@ -120,6 +125,44 @@ static double benchmark( int iterations, int len ) { return elapsed; } +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param len array length +* @return elapsed time in seconds +*/ +static double benchmark2( int iterations, int len ) { + double elapsed; + double x[ len ]; + double v; + double t; + int i; + + for ( i = 0; i < len; i++ ) { + if ( rand_double() < 0.2 ) { + x[ i ] = 0.0 / 0.0; // NaN + } else { + x[ i ] = ( rand_double() * 20000.0 ) - 10000.0; + } + } + v = 0.0; + t = tic(); + for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar + v = stdlib_strided_dnanstdevwd_ndarray( len, 1.0, x, 1, 0 ); + if ( v != v ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( v != v ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + /** * Main execution sequence. */ @@ -142,7 +185,18 @@ int main( void ) { for ( j = 0; j < REPEATS; j++ ) { count += 1; printf( "# c::%s:len=%d\n", NAME, len ); - elapsed = benchmark( iter, len ); + elapsed = benchmark1( iter, len ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + } + 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:ndarray:len=%d\n", NAME, len ); + elapsed = benchmark2( iter, len ); print_results( iter, elapsed ); printf( "ok %d benchmark finished\n", count ); } diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/docs/repl.txt index 45755dc95842..a80eea514a0a 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/docs/repl.txt @@ -1,10 +1,10 @@ -{{alias}}( N, correction, x, stride ) +{{alias}}( N, correction, x, strideX ) Computes the standard deviation of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use a typed array view. @@ -33,8 +33,8 @@ x: Float64Array Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -49,21 +49,18 @@ ~2.0817 // Using `N` and `stride` parameters: - > x = new {{alias:@stdlib/array/float64}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ] ); - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > var stride = 2; - > {{alias}}( N, 1, x, stride ) + > x = new {{alias:@stdlib/array/float64}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0, NaN, NaN ] ); + > {{alias}}( 4, 1, x, 2 ) ~2.0817 // Using view offsets: - > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); + > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN, NaN ] ); > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > stride = 2; - > {{alias}}( N, 1, x1, stride ) + > {{alias}}( 4, 1, x1, 2 ) ~2.0817 -{{alias}}.ndarray( N, correction, x, stride, offset ) + +{{alias}}.ndarray( N, correction, x, strideX, offsetX ) Computes the standard deviation of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics. @@ -92,10 +89,10 @@ x: Float64Array Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -111,9 +108,8 @@ ~2.0817 // Using offset parameter: - > var x = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, 1, x, 2, 1 ) + > var x = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN, NaN ] ); + > {{alias}}.ndarray( 4, 1, x, 2, 1 ) ~2.0817 See Also diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/docs/types/index.d.ts index 002b40c65361..bccdf8ea6056 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/docs/types/index.d.ts @@ -28,7 +28,7 @@ interface Routine { * @param N - number of indexed elements * @param correction - degrees of freedom adjustment * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @returns standard deviation * * @example @@ -39,7 +39,7 @@ interface Routine { * var v = dnanstdevwd( x.length, 1, x, 1 ); * // returns ~2.0817 */ - ( N: number, correction: number, x: Float64Array, stride: number ): number; + ( N: number, correction: number, x: Float64Array, strideX: number ): number; /** * Computes the standard deviation of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics. @@ -47,8 +47,8 @@ interface Routine { * @param N - number of indexed elements * @param correction - degrees of freedom adjustment * @param x - input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns standard deviation * * @example @@ -59,7 +59,7 @@ interface Routine { * var v = dnanstdevwd.ndarray( x.length, 1, x, 1, 0 ); * // returns ~2.0817 */ - ndarray( N: number, correction: number, x: Float64Array, stride: number, offset: number ): number; + ndarray( N: number, correction: number, x: Float64Array, strideX: number, offsetX: number ): number; } /** @@ -68,7 +68,7 @@ interface Routine { * @param N - number of indexed elements * @param correction - degrees of freedom adjustment * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @returns standard deviation * * @example diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/examples/c/example.c b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/examples/c/example.c index 1c2961c530aa..6dd443d0b570 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/examples/c/example.c +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/examples/c/example.c @@ -17,21 +17,20 @@ */ #include "stdlib/stats/base/dnanstdevwd.h" -#include #include int main( void ) { // Create a strided array: - double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 }; + const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 }; // Specify the number of elements: - int64_t N = 6; + const int N = 6; // Specify the stride length: - int64_t stride = 2; + const int strideX = 2; // Compute the standard deviation: - double v = stdlib_strided_dnanstdevwd( N, 1, x, stride ); + double v = stdlib_strided_dnanstdevwd( N, 1.0, x, strideX ); // Print the result: printf( "sample standard deviation: %lf\n", v ); diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/examples/index.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/examples/index.js index 7472435bef44..308ba14535dc 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/examples/index.js @@ -18,22 +18,19 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var dnanstdevwd = require( './../lib' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - x[ i ] = NaN; - } else { - x[ i ] = round( (randu()*100.0) - 50.0 ); +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; } + return uniform( -50.0, 50.0 ); } + +var x = filledarrayBy( 10, 'float64', rand ); console.log( x ); var v = dnanstdevwd( x.length, 1, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/include.gypi b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/include.gypi index 868c5c12e852..26476a8c2655 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/include.gypi +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/include.gypi @@ -36,7 +36,7 @@ # Source files: 'src_files': [ - '<(src_dir)/addon.cpp', + '<(src_dir)/addon.c', ' +#include "stdlib/blas/base/shared.h" /* * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. @@ -31,7 +31,12 @@ extern "C" { /** * Computes the standard deviation of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm. */ -double stdlib_strided_dnanstdevwd( const int64_t N, const double correction, const double *X, const int64_t stride ); +double API_SUFFIX(stdlib_strided_dnanstdevwd)( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX ); + +/** +* Computes the standard deviation of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics. +*/ +double API_SUFFIX(stdlib_strided_dnanstdevwd_ndarray)( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/dnanstdevwd.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/dnanstdevwd.js index 0c640201d426..9cc39b5c3445 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/dnanstdevwd.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/dnanstdevwd.js @@ -20,8 +20,8 @@ // MODULES // -var dnanvariancewd = require( '@stdlib/stats/base/dnanvariancewd' ); -var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -32,20 +32,19 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment * @param {Float64Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} standard deviation * * @example * var Float64Array = require( '@stdlib/array/float64' ); * * var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); -* var N = x.length; * -* var v = dnanstdevwd( N, 1, x, 1 ); +* var v = dnanstdevwd( x.length, 1, x, 1 ); * // returns ~2.0817 */ -function dnanstdevwd( N, correction, x, stride ) { - return sqrt( dnanvariancewd( N, correction, x, stride ) ); +function dnanstdevwd( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/dnanstdevwd.native.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/dnanstdevwd.native.js index 7d368070af71..aadf26938137 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/dnanstdevwd.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/dnanstdevwd.native.js @@ -31,20 +31,19 @@ var addon = require( './../src/addon.node' ); * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment * @param {Float64Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} standard deviation * * @example * var Float64Array = require( '@stdlib/array/float64' ); * * var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); -* var N = x.length; * -* var v = dnanstdevwd( N, 1, x, 1 ); +* var v = dnanstdevwd( x.length, 1, x, 1 ); * // returns ~2.0817 */ -function dnanstdevwd( N, correction, x, stride ) { - return addon( N, correction, x, stride ); +function dnanstdevwd( N, correction, x, strideX ) { + return addon( N, correction, x, strideX ); } diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/index.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/index.js index 12f726dffc28..147fef9cb02c 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/index.js @@ -28,20 +28,17 @@ * var dnanstdevwd = require( '@stdlib/stats/base/dnanstdevwd' ); * * var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); -* var N = x.length; * -* var v = dnanstdevwd( N, 1, x, 1 ); +* var v = dnanstdevwd( x.length, 1, x, 1 ); * // returns ~2.0817 * * @example * var Float64Array = require( '@stdlib/array/float64' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * var dnanstdevwd = require( '@stdlib/stats/base/dnanstdevwd' ); * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); -* var N = floor( x.length / 2 ); * -* var v = dnanstdevwd.ndarray( N, 1, x, 2, 1 ); +* var v = dnanstdevwd.ndarray( 5, 1, x, 2, 1 ); * // returns 2.5 */ diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/ndarray.js index aaa828313a34..263e4ad9fa35 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/ndarray.js @@ -32,22 +32,20 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' ); * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment * @param {Float64Array} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} standard deviation * * @example * var Float64Array = require( '@stdlib/array/float64' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); -* var N = floor( x.length / 2 ); * -* var v = dnanstdevwd( N, 1, x, 2, 1 ); +* var v = dnanstdevwd( 5, 1, x, 2, 1 ); * // returns 2.5 */ -function dnanstdevwd( N, correction, x, stride, offset ) { - return sqrt( dnanvariancewd( N, correction, x, stride, offset ) ); +function dnanstdevwd( N, correction, x, strideX, offsetX ) { + return sqrt( dnanvariancewd( N, correction, x, strideX, offsetX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/ndarray.native.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/ndarray.native.js index d72407c6491f..a74dc2f9141a 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/lib/ndarray.native.js @@ -20,8 +20,7 @@ // MODULES // -var Float64Array = require( '@stdlib/array/float64' ); -var addon = require( './dnanstdevwd.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // @@ -32,27 +31,20 @@ var addon = require( './dnanstdevwd.native.js' ); * @param {PositiveInteger} N - number of indexed elements * @param {number} correction - degrees of freedom adjustment * @param {Float64Array} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} standard deviation * * @example * var Float64Array = require( '@stdlib/array/float64' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); -* var N = floor( x.length / 2 ); * -* var v = dnanstdevwd( N, 1, x, 2, 1 ); +* var v = dnanstdevwd( 5, 1, x, 2, 1 ); * // returns 2.5 */ -function dnanstdevwd( N, correction, x, stride, offset ) { - var view; - if ( stride < 0 ) { - offset += (N-1) * stride; - } - view = new Float64Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len - return addon( N, correction, view, stride ); +function dnanstdevwd( N, correction, x, strideX, offsetX ) { + return addon.ndarray( N, correction, x, strideX, offsetX ); } diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/manifest.json b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/manifest.json index b41fcc155657..3b867836e84d 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/manifest.json @@ -1,5 +1,8 @@ { - "options": {}, + "options": { + "task": "build", + "wasm": false + }, "fields": [ { "field": "src", @@ -24,17 +27,80 @@ ], "confs": [ { + "task": "build", + "wasm": false, "src": [ - "./src/dnanstdevwd.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/napi/export", + "@stdlib/napi/argv", + "@stdlib/napi/argv-int64", + "@stdlib/napi/argv-double", + "@stdlib/napi/argv-strided-float64array", + "@stdlib/math/base/special/sqrt", + "@stdlib/stats/base/dnanvariancewd", + "@stdlib/napi/create-double" + ] + }, + { + "task": "benchmark", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/math/base/special/sqrt", + "@stdlib/stats/base/dnanvariancewd" + ] + }, + { + "task": "examples", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/math/base/special/sqrt", + "@stdlib/stats/base/dnanvariancewd" + ] + }, + { + "task": "", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" ], + "libraries": [], "libpath": [], "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/math/base/special/sqrt", "@stdlib/stats/base/dnanvariancewd" ] } diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/addon.c b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/addon.c new file mode 100644 index 000000000000..dc292f9b14df --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/addon.c @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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/dnanstdevwd.h" +#include "stdlib/blas/base/shared.h" +#include "stdlib/napi/export.h" +#include "stdlib/napi/argv.h" +#include "stdlib/napi/argv_int64.h" +#include "stdlib/napi/argv_double.h" +#include "stdlib/napi/argv_strided_float64array.h" +#include "stdlib/napi/create_double.h" +#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 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_DOUBLE( env, correction, argv, 1 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 2 ); + STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dnanstdevwd)( N, correction, X, strideX ), v ); + return v; +} + +/** +* 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_method( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 5 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_DOUBLE( env, correction, argv, 1 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 4 ); + STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dnanstdevwd_ndarray)( N, correction, X, strideX, offsetX ), v ); + return v; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ) diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/addon.cpp b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/addon.cpp deleted file mode 100644 index 33df0458524b..000000000000 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/addon.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 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/dnanstdevwd.h" -#include -#include -#include -#include -#include - -/** -* Add-on namespace. -*/ -namespace stdlib_stats_base_dnanstdevwd { - - /** - * Computes the standard deviation of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm. - * - * ## Notes - * - * - When called from JavaScript, the function expects four arguments: - * - * - `N`: number of indexed elements - * - `correction`: degrees of freedom adjustment - * - `X`: input array - * - `stride`: stride length - */ - napi_value node_dnanstdevwd( napi_env env, napi_callback_info info ) { - napi_status status; - - size_t argc = 4; - napi_value argv[ 4 ]; - status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr ); - assert( status == napi_ok ); - - if ( argc < 4 ) { - napi_throw_error( env, nullptr, "invalid invocation. Must provide 4 arguments." ); - return nullptr; - } - - napi_valuetype vtype0; - status = napi_typeof( env, argv[ 0 ], &vtype0 ); - assert( status == napi_ok ); - if ( vtype0 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." ); - return nullptr; - } - - napi_valuetype vtype1; - status = napi_typeof( env, argv[ 1 ], &vtype1 ); - assert( status == napi_ok ); - if ( vtype1 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a number." ); - return nullptr; - } - - bool res; - status = napi_is_typedarray( env, argv[ 2 ], &res ); - assert( status == napi_ok ); - if ( res == false ) { - napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a Float64Array." ); - return nullptr; - } - - napi_valuetype vtype3; - status = napi_typeof( env, argv[ 3 ], &vtype3 ); - assert( status == napi_ok ); - if ( vtype3 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a number." ); - return nullptr; - } - - int64_t N; - status = napi_get_value_int64( env, argv[ 0 ], &N ); - assert( status == napi_ok ); - - double correction; - status = napi_get_value_double( env, argv[ 1 ], &correction ); - assert( status == napi_ok ); - - int64_t stride; - status = napi_get_value_int64( env, argv[ 3 ], &stride ); - assert( status == napi_ok ); - - napi_typedarray_type vtype2; - size_t xlen; - void *X; - status = napi_get_typedarray_info( env, argv[ 2 ], &vtype2, &xlen, &X, nullptr, nullptr ); - assert( status == napi_ok ); - if ( vtype2 != napi_float64_array ) { - napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a Float64Array." ); - return nullptr; - } - if ( (N-1)*llabs(stride) >= (int64_t)xlen ) { - napi_throw_range_error( env, nullptr, "invalid argument. Third argument has insufficient elements based on the associated stride and the number of indexed elements." ); - return nullptr; - } - - napi_value v; - status = napi_create_double( env, stdlib_strided_dnanstdevwd( N, correction, (double *)X, stride ), &v ); - assert( status == napi_ok ); - - return v; - } - - napi_value Init( napi_env env, napi_value exports ) { - napi_status status; - napi_value fcn; - status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_dnanstdevwd, NULL, &fcn ); - assert( status == napi_ok ); - return fcn; - } - - NAPI_MODULE( NODE_GYP_MODULE_NAME, Init ) -} // end namespace stdlib_stats_base_dnanstdevwd diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/dnanstdevwd.c b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/dnanstdevwd.c deleted file mode 100644 index 6d0529656574..000000000000 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/dnanstdevwd.c +++ /dev/null @@ -1,35 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 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/dnanstdevwd.h" -#include "stdlib/stats/base/dnanvariancewd.h" -#include -#include - -/** -* Computes the standard deviation of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm. -* -* @param N number of indexed elements -* @param correction degrees of freedom adjustment -* @param X input array -* @param stride stride length -* @return output value -*/ -double stdlib_strided_dnanstdevwd( const int64_t N, const double correction, const double *X, const int64_t stride ) { - return sqrt( stdlib_strided_dnanvariancewd( N, correction, X, stride ) ); -} diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/main.c b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/main.c new file mode 100644 index 000000000000..52b674b38fd3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/main.c @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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/dnanstdevwd.h" +#include "stdlib/stats/base/dnanvariancewd.h" +#include "stdlib/math/base/special/sqrt.h" +#include "stdlib/blas/base/shared.h" +#include "stdlib/strided/base/stride2offset.h" +#include + +/** +* Computes the standard deviation of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm. +* +* @param N number of indexed elements +* @param correction degrees of freedom adjustment +* @param X input array +* @param strideX stride length +* @return output value +*/ +double API_SUFFIX(stdlib_strided_dnanstdevwd)( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX ) { + const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + return API_SUFFIX(stdlib_strided_dnanstdevwd_ndarray)( N, correction, X, strideX, ox ); +} + +/** +* Computes the standard deviation of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics. +* +* @param N number of indexed elements +* @param correction degrees of freedom adjustment +* @param X input array +* @param strideX stride length +* @param offsetX starting index for X +* @return output value +*/ +double API_SUFFIX(stdlib_strided_dnanstdevwd_ndarray)( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { + return stdlib_base_sqrt( API_SUFFIX(stdlib_strided_dnanvariancewd_ndarray )( N, correction, X, strideX, offsetX ) ); +} diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.dnanstdevwd.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.dnanstdevwd.js index 428201809672..5e6a5bf79634 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.dnanstdevwd.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.dnanstdevwd.js @@ -21,7 +21,6 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var Float64Array = require( '@stdlib/array/float64' ); @@ -214,7 +213,6 @@ tape( 'if provided a `correction` parameter yielding a correction term less than }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -231,15 +229,13 @@ tape( 'the function supports a `stride` parameter', function test( t ) { NaN ]); - N = floor( x.length / 2 ); - v = dnanstdevwd( N, 1, x, 2 ); + v = dnanstdevwd( 5, 1, x, 2 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; var i; @@ -256,9 +252,8 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 4.0, // 0 2.0 ]); - N = floor( x.length / 2 ); - v = dnanstdevwd( N, 1, x, -2 ); + v = dnanstdevwd( 5, 1, x, -2 ); t.strictEqual( v, 2.5, 'returns expected value' ); x = new Float64Array( 1e3 ); @@ -296,7 +291,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` p tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -314,9 +308,8 @@ tape( 'the function supports view offsets', function test( t ) { ]); x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - v = dnanstdevwd( N, 1, x1, 2 ); + v = dnanstdevwd( 5, 1, x1, 2 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.dnanstdevwd.native.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.dnanstdevwd.native.js index 9bca0c204731..d89a7295049c 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.dnanstdevwd.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.dnanstdevwd.native.js @@ -22,7 +22,6 @@ var resolve = require( 'path' ).resolve; var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var Float64Array = require( '@stdlib/array/float64' ); @@ -223,7 +222,6 @@ tape( 'if provided a `correction` parameter yielding a correction term less than }); tape( 'the function supports a `stride` parameter', opts, function test( t ) { - var N; var x; var v; @@ -240,15 +238,13 @@ tape( 'the function supports a `stride` parameter', opts, function test( t ) { NaN ]); - N = floor( x.length / 2 ); - v = dnanstdevwd( N, 1, x, 2 ); + v = dnanstdevwd( 5, 1, x, 2 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', opts, function test( t ) { - var N; var x; var v; var i; @@ -265,9 +261,8 @@ tape( 'the function supports a negative `stride` parameter', opts, function test 4.0, // 0 2.0 ]); - N = floor( x.length / 2 ); - v = dnanstdevwd( N, 1, x, -2 ); + v = dnanstdevwd( 5, 1, x, -2 ); t.strictEqual( v, 2.5, 'returns expected value' ); x = new Float64Array( 1e3 ); @@ -305,7 +300,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` p tape( 'the function supports view offsets', opts, function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -323,9 +317,8 @@ tape( 'the function supports view offsets', opts, function test( t ) { ]); x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - v = dnanstdevwd( N, 1, x1, 2 ); + v = dnanstdevwd( 5, 1, x1, 2 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.ndarray.js index fd39c6d52d70..b06d9eebe799 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.ndarray.js @@ -21,7 +21,6 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var Float64Array = require( '@stdlib/array/float64' ); @@ -214,7 +213,6 @@ tape( 'if provided a `correction` parameter yielding a correction term less than }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -231,15 +229,13 @@ tape( 'the function supports a `stride` parameter', function test( t ) { NaN ]); - N = floor( x.length / 2 ); - v = dnanstdevwd( N, 1, x, 2, 0 ); + v = dnanstdevwd( 5, 1, x, 2, 0 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; var i; @@ -256,9 +252,8 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 4.0, // 0 2.0 ]); - N = floor( x.length / 2 ); - v = dnanstdevwd( N, 1, x, -2, 8 ); + v = dnanstdevwd( 5, 1, x, -2, 8 ); t.strictEqual( v, 2.5, 'returns expected value' ); x = new Float64Array( 1e3 ); @@ -294,7 +289,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` p }); tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -310,9 +304,8 @@ tape( 'the function supports an `offset` parameter', function test( t ) { NaN, NaN // 4 ]); - N = floor( x.length / 2 ); - v = dnanstdevwd( N, 1, x, 2, 1 ); + v = dnanstdevwd( 5, 1, x, 2, 1 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.ndarray.native.js b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.ndarray.native.js index d630574a7c6c..b19d666d6c1a 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.ndarray.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/test/test.ndarray.native.js @@ -22,7 +22,6 @@ var resolve = require( 'path' ).resolve; var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var Float64Array = require( '@stdlib/array/float64' ); @@ -223,7 +222,6 @@ tape( 'if provided a `correction` parameter yielding a correction term less than }); tape( 'the function supports a `stride` parameter', opts, function test( t ) { - var N; var x; var v; @@ -240,15 +238,13 @@ tape( 'the function supports a `stride` parameter', opts, function test( t ) { NaN ]); - N = floor( x.length / 2 ); - v = dnanstdevwd( N, 1, x, 2, 0 ); + v = dnanstdevwd( 5, 1, x, 2, 0 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', opts, function test( t ) { - var N; var x; var v; var i; @@ -265,9 +261,8 @@ tape( 'the function supports a negative `stride` parameter', opts, function test 4.0, // 0 2.0 ]); - N = floor( x.length / 2 ); - v = dnanstdevwd( N, 1, x, -2, 8 ); + v = dnanstdevwd( 5, 1, x, -2, 8 ); t.strictEqual( v, 2.5, 'returns expected value' ); x = new Float64Array( 1e3 ); @@ -303,7 +298,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` p }); tape( 'the function supports an `offset` parameter', opts, function test( t ) { - var N; var x; var v; @@ -319,9 +313,8 @@ tape( 'the function supports an `offset` parameter', opts, function test( t ) { NaN, NaN // 4 ]); - N = floor( x.length / 2 ); - v = dnanstdevwd( N, 1, x, 2, 1 ); + v = dnanstdevwd( 5, 1, x, 2, 1 ); t.strictEqual( v, 2.5, 'returns expected value' ); t.end(); From 6f0bd62a65a9364c89c1fd98c0447ffd95cd81e3 Mon Sep 17 00:00:00 2001 From: Aayush Khanna <96649223+aayush0325@users.noreply.github.com> Date: Tue, 11 Mar 2025 09:32:36 +0530 Subject: [PATCH 2/2] chore: suggestion from code review --- lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/main.c b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/main.c index 52b674b38fd3..dcf704051d5e 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dnanstdevwd/src/main.c @@ -21,7 +21,6 @@ #include "stdlib/math/base/special/sqrt.h" #include "stdlib/blas/base/shared.h" #include "stdlib/strided/base/stride2offset.h" -#include /** * Computes the standard deviation of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm.