From 00b23b3cb5b4e72bf977a9ac170062f8e8614ef1 Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Mon, 24 Feb 2025 03:37:14 +0530 Subject: [PATCH] feat: add C ndarray interface and refactor implementation for `stats/base/dnanvariancech` PR-URL: https://github.com/stdlib-js/stdlib/pull/4803 Reviewed-by: Philipp Burckhardt Reviewed-by: Aayush Khanna --- .../stats/base/dnanvariancech/README.md | 172 +++++++++++++++--- .../dnanvariancech/benchmark/benchmark.js | 30 +-- .../benchmark/benchmark.native.js | 30 +-- .../benchmark/benchmark.ndarray.js | 30 +-- .../benchmark/benchmark.ndarray.native.js | 30 +-- .../benchmark/c/benchmark.length.c | 60 +++++- .../stats/base/dnanvariancech/docs/repl.txt | 40 ++-- .../base/dnanvariancech/docs/types/index.d.ts | 12 +- .../base/dnanvariancech/examples/c/example.c | 9 +- .../base/dnanvariancech/examples/index.js | 22 +-- .../stdlib/stats/base/dnanvariancech.h | 9 +- .../base/dnanvariancech/lib/dnanvariancech.js | 70 +------ .../lib/dnanvariancech.native.js | 9 +- .../stats/base/dnanvariancech/lib/index.js | 7 +- .../stats/base/dnanvariancech/lib/ndarray.js | 22 +-- .../base/dnanvariancech/lib/ndarray.native.js | 20 +- .../stats/base/dnanvariancech/manifest.json | 52 ++++-- .../stats/base/dnanvariancech/src/addon.c | 31 +++- .../src/{dnanvariancech.c => main.c} | 42 +++-- .../test/test.dnanvariancech.js | 13 +- .../test/test.dnanvariancech.native.js | 13 +- .../base/dnanvariancech/test/test.ndarray.js | 13 +- .../test/test.ndarray.native.js | 13 +- 23 files changed, 444 insertions(+), 305 deletions(-) rename lib/node_modules/@stdlib/stats/base/dnanvariancech/src/{dnanvariancech.c => main.c} (72%) diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/README.md b/lib/node_modules/@stdlib/stats/base/dnanvariancech/README.md index 0deb506a3836..9ed8ceea7548 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/README.md +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/README.md @@ -98,7 +98,7 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note, var dnanvariancech = require( '@stdlib/stats/base/dnanvariancech' ); ``` -#### dnanvariancech( N, correction, x, stride ) +#### dnanvariancech( N, correction, x, strideX ) Computes the [variance][variance] of a double-precision floating-point strided array `x` ignoring `NaN` values and using a one-pass trial mean algorithm. @@ -116,18 +116,16 @@ 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 [variance][variance] according to `n-c` where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements. 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 unbiased 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). - **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 [variance][variance] 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 [variance][variance] 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 = dnanvariancech( N, 1, x, 2 ); +var v = dnanvariancech( 5, 1, x, 2 ); // returns 6.25 ``` @@ -137,25 +135,22 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```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 ] ); // eslint-disable-line max-len var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); - -var v = dnanvariancech( N, 1, x1, 2 ); +var v = dnanvariancech( 5, 1, x1, 2 ); // returns 6.25 ``` -#### dnanvariancech.ndarray( N, correction, x, stride, offset ) +#### dnanvariancech.ndarray( N, correction, x, strideX, offsetX ) Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass trial mean algorithm and alternative indexing semantics. ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); +var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); var v = dnanvariancech.ndarray( x.length, 1, x, 1, 0 ); // returns ~4.33333 @@ -163,18 +158,16 @@ var v = dnanvariancech.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 [variance][variance] 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 [variance][variance] for every other element in `x` starting from the second element ```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 ] ); // eslint-disable-line max-len -var v = dnanvariancech.ndarray( N, 1, x, 2, 1 ); +var v = dnanvariancech.ndarray( 5, 1, x, 2, 1 ); // returns 6.25 ``` @@ -201,19 +194,19 @@ var v = dnanvariancech.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 dnanvariancech = require( '@stdlib/stats/base/dnanvariancech' ); -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 ); } -console.log( x ); + +var x = filledarrayBy( 10, 'float64', rand ); var v = dnanvariancech( x.length, 1, x, 1 ); console.log( v ); @@ -223,6 +216,125 @@ console.log( v ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/base/dnanvariancech.h" +``` + +#### stdlib_strided_dnanvariancech( N, correction, \*X, strideX ) + +Computes the [variance][variance] of a double-precision floating-point strided array `x` ignoring `NaN` values and using a one-pass trial mean algorithm. + +```c +const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 }; + +double v = stdlib_strided_dnanvariancech( 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 [variance][variance] according to `n-c` where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements. 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 unbiased 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). +- **X**: `[in] double*` input array. +- **strideX**: `[in] CBLAS_INT` stride length for `X`. + +```c +double stdlib_strided_dnanvariancech( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX ); +``` + +#### stdlib_strided_dnanvariancech_ndarray( N, correction, \*X, strideX, offsetX ) + +Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass trial mean algorithm and alternative indexing semantics. + +```c +const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 }; + +double v = stdlib_strided_dnanvariancech_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 [variance][variance] according to `n-c` where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements. 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 unbiased 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). +- **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_dnanvariancech_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/dnanvariancech.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_dnanvariancech( N, 1, x, strideX ); + + // Print the result: + printf( "sample variance: %lf\n", v ); +} +``` + +
+ + + +
+ + + * * *
diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/benchmark.js index a4a676c45654..aff0033dfc11 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/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 dnanvariancech = require( './../lib/dnanvariancech.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 dnanvariancech = require( './../lib/dnanvariancech.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/dnanvariancech/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/benchmark.native.js index e714be4d718a..d664bb556223 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/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/dnanvariancech/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/benchmark.ndarray.js index 93d02f9bd862..5a0905ec8619 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/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 dnanvariancech = 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 dnanvariancech = 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/dnanvariancech/benchmark/benchmark.ndarray.native.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/benchmark.ndarray.native.js index 12b1acb531ad..a46193a0ec72 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/benchmark.ndarray.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/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/dnanvariancech/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/c/benchmark.length.c index c64f38c9f76c..ce99c3966550 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/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_dnanvariancech( 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_dnanvariancech_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/dnanvariancech/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/dnanvariancech/docs/repl.txt index a726e307bd95..12c235a234ed 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/docs/repl.txt @@ -1,10 +1,10 @@ -{{alias}}( N, correction, x, stride ) +{{alias}}( N, correction, x, strideX ) Computes the variance of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass trial mean 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. @@ -34,8 +34,8 @@ x: Float64Array Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -45,26 +45,23 @@ Examples -------- // Standard Usage: - > var x = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, NaN, 2.0 ] ); + > var x = new {{alias:@stdlib/array/float64}}( [ 1.0, NaN, -2.0, 2.0 ] ); > {{alias}}( x.length, 1, x, 1 ) ~4.3333 - // 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 ) + // Using `N` and stride parameters: + > 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 ) ~4.3333 // 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 ) ~4.3333 -{{alias}}.ndarray( N, correction, x, stride, offset ) + +{{alias}}.ndarray( N, correction, x, strideX, offsetX ) Computes the variance of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass trial mean algorithm and alternative indexing semantics. @@ -94,10 +91,10 @@ x: Float64Array Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -113,9 +110,8 @@ ~4.3333 // 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 ) ~4.3333 See Also diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/dnanvariancech/docs/types/index.d.ts index 3b0c78e55e9e..3cc8eac385fd 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/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 variance * * @example @@ -39,7 +39,7 @@ interface Routine { * var v = dnanvariancech( x.length, 1, x, 1 ); * // returns ~4.3333 */ - ( N: number, correction: number, x: Float64Array, stride: number ): number; + ( N: number, correction: number, x: Float64Array, strideX: number ): number; /** * Computes the variance of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass trial mean 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 variance * * @example @@ -59,7 +59,7 @@ interface Routine { * var v = dnanvariancech.ndarray( x.length, 1, x, 1, 0 ); * // returns ~4.3333 */ - 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 variance * * @example diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/examples/c/example.c b/lib/node_modules/@stdlib/stats/base/dnanvariancech/examples/c/example.c index 09ce553fde96..fe3a788a096a 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/examples/c/example.c +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/examples/c/example.c @@ -17,21 +17,20 @@ */ #include "stdlib/stats/base/dnanvariancech.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 variance: - double v = stdlib_strided_dnanvariancech( N, 1, x, stride ); + double v = stdlib_strided_dnanvariancech( N, 1.0, x, strideX ); // Print the result: printf( "sample variance: %lf\n", v ); diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/examples/index.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/examples/index.js index 94f52c003c30..637e98094588 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/examples/index.js @@ -18,23 +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 dnanvariancech = 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 ); } -console.log( x ); + +var x = filledarrayBy( 10, 'float64', rand ); var v = dnanvariancech( x.length, 1, x, 1 ); console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/include/stdlib/stats/base/dnanvariancech.h b/lib/node_modules/@stdlib/stats/base/dnanvariancech/include/stdlib/stats/base/dnanvariancech.h index 3025a04c96c8..7a4cd14cb68d 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/include/stdlib/stats/base/dnanvariancech.h +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/include/stdlib/stats/base/dnanvariancech.h @@ -19,7 +19,7 @@ #ifndef STDLIB_STATS_BASE_DNANVARIANCECH_H #define STDLIB_STATS_BASE_DNANVARIANCECH_H -#include +#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 variance of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass trial mean algorithm. */ -double stdlib_strided_dnanvariancech( const int64_t N, const double correction, const double *X, const int64_t stride ); +double API_SUFFIX(stdlib_strided_dnanvariancech)( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX ); + +/** +* Computes the variance of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass trial mean algorithm and alternative indexing semantics. +*/ +double API_SUFFIX(stdlib_strided_dnanvariancech_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/dnanvariancech/lib/dnanvariancech.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/dnanvariancech.js index 77647cd24bc8..327caa3c272d 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/dnanvariancech.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/dnanvariancech.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); + + // MAIN // /** @@ -37,7 +43,7 @@ * @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} variance * * @example @@ -49,66 +55,8 @@ * var v = dnanvariancech( N, 1, x, 1 ); * // returns ~4.3333 */ -function dnanvariancech( N, correction, x, stride ) { - var mu; - var ix; - var M2; - var nc; - var M; - var d; - var v; - var n; - var i; - - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - v = x[ 0 ]; - if ( v === v && N-correction > 0.0 ) { - return 0.0; - } - return NaN; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - // Find an estimate for the mean... - for ( i = 0; i < N; i++ ) { - v = x[ ix ]; - if ( v === v ) { - mu = v; - break; - } - ix += stride; - } - if ( i === N ) { - return NaN; - } - ix += stride; - i += 1; - - // Compute the variance... - M2 = 0.0; - M = 0.0; - n = 1; - for ( i; i < N; i++ ) { - v = x[ ix ]; - if ( v === v ) { - d = v - mu; - M2 += d * d; - M += d; - n += 1; - } - ix += stride; - } - nc = n - correction; - if ( nc <= 0.0 ) { - return NaN; - } - return (M2/nc) - ((M/n)*(M/nc)); +function dnanvariancech( N, correction, x, strideX ) { + return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/dnanvariancech.native.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/dnanvariancech.native.js index bd41f722dd4c..dfa43c9506d0 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/dnanvariancech.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/dnanvariancech.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} variance * * @example * var Float64Array = require( '@stdlib/array/float64' ); * * var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); -* var N = x.length; * -* var v = dnanvariancech( N, 1, x, 1 ); +* var v = dnanvariancech( x.length, 1, x, 1 ); * // returns ~4.3333 */ -function dnanvariancech( N, correction, x, stride ) { - return addon( N, correction, x, stride ); +function dnanvariancech( N, correction, x, strideX ) { + return addon( N, correction, x, strideX ); } diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/index.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/index.js index 946b2cab6c61..5c0352266a8c 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/index.js @@ -28,20 +28,17 @@ * var dnanvariancech = require( '@stdlib/stats/base/dnanvariancech' ); * * var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); -* var N = x.length; * -* var v = dnanvariancech( N, 1, x, 1 ); +* var v = dnanvariancech( x.length, 1, x, 1 ); * // returns ~4.3333 * * @example * var Float64Array = require( '@stdlib/array/float64' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * var dnanvariancech = require( '@stdlib/stats/base/dnanvariancech' ); * * 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 = dnanvariancech.ndarray( N, 1, x, 2, 1 ); +* var v = dnanvariancech.ndarray( 5, 1, x, 2, 1 ); * // returns 6.25 */ diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/ndarray.js index 48e77d9eb2bd..91f86877a468 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/ndarray.js @@ -37,21 +37,19 @@ * @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} variance * * @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 = dnanvariancech( N, 1, x, 2, 1 ); +* var v = dnanvariancech( 5, 1, x, 2, 1 ); * // returns 6.25 */ -function dnanvariancech( N, correction, x, stride, offset ) { +function dnanvariancech( N, correction, x, strideX, offsetX ) { var mu; var ix; var M2; @@ -65,14 +63,14 @@ function dnanvariancech( N, correction, x, stride, offset ) { if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { - v = x[ offset ]; + if ( N === 1 || strideX === 0 ) { + v = x[ offsetX ]; if ( v === v && N-correction > 0.0 ) { return 0.0; } return NaN; } - ix = offset; + ix = offsetX; // Find an estimate for the mean... for ( i = 0; i < N; i++ ) { @@ -81,12 +79,12 @@ function dnanvariancech( N, correction, x, stride, offset ) { mu = v; break; } - ix += stride; + ix += strideX; } if ( i === N ) { return NaN; } - ix += stride; + ix += strideX; i += 1; // Compute the variance... @@ -101,7 +99,7 @@ function dnanvariancech( N, correction, x, stride, offset ) { M += d; n += 1; } - ix += stride; + ix += strideX; } nc = n - correction; if ( nc <= 0.0 ) { diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/ndarray.native.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/ndarray.native.js index 019ffb791940..974621d7be14 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/lib/ndarray.native.js @@ -20,8 +20,7 @@ // MODULES // -var Float64Array = require( '@stdlib/array/float64' ); -var addon = require( './dnanvariancech.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // @@ -32,27 +31,20 @@ var addon = require( './dnanvariancech.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} variance * * @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 = dnanvariancech( N, 1, x, 2, 1 ); +* var v = dnanvariancech( 5, 1, x, 2, 1 ); * // returns 6.25 */ -function dnanvariancech( 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 dnanvariancech( N, correction, x, strideX, offsetX ) { + return addon.ndarray( N, correction, x, strideX, offsetX ); } diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/manifest.json b/lib/node_modules/@stdlib/stats/base/dnanvariancech/manifest.json index 8d0cc91dd8c0..86308d31795d 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/manifest.json @@ -1,5 +1,8 @@ { - "options": {}, + "options": { + "task": "build", + "wasm": false + }, "fields": [ { "field": "src", @@ -25,17 +28,18 @@ "confs": [ { "task": "build", + "wasm": false, "src": [ - "./src/dnanvariancech.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", @@ -46,31 +50,51 @@ }, { "task": "benchmark", + "wasm": false, "src": [ - "./src/dnanvariancech.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], - "dependencies": [] + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset" + ] }, { "task": "examples", + "wasm": false, "src": [ - "./src/dnanvariancech.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset" + ] + }, + { + "task": "", + "wasm": true, + "src": [ + "./src/main.c" ], + "include": [ + "./include" + ], + "libraries": [], "libpath": [], - "dependencies": [] + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset" + ] } ] } diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/src/addon.c b/lib/node_modules/@stdlib/stats/base/dnanvariancech/src/addon.c index 78d3663f9fae..a87d030ae87c 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/src/addon.c +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/src/addon.c @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2024 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,12 +17,13 @@ */ #include "stdlib/stats/base/dnanvariancech.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 "stdlib/napi/argv_double.h" #include /** @@ -35,11 +36,29 @@ 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_INT64( env, stride, argv, 3 ); STDLIB_NAPI_ARGV_DOUBLE( env, correction, argv, 1 ); - STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, stride, argv, 2 ); - STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_dnanvariancech( N, correction, X, stride ), v ); + 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_dnanvariancech)( 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_dnanvariancech_ndarray)( N, correction, X, strideX, offsetX ), v ); return v; } -STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ) diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/src/dnanvariancech.c b/lib/node_modules/@stdlib/stats/base/dnanvariancech/src/main.c similarity index 72% rename from lib/node_modules/@stdlib/stats/base/dnanvariancech/src/dnanvariancech.c rename to lib/node_modules/@stdlib/stats/base/dnanvariancech/src/main.c index 18dfbe6ba632..781f36283748 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/src/dnanvariancech.c +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/src/main.c @@ -17,7 +17,8 @@ */ #include "stdlib/stats/base/dnanvariancech.h" -#include +#include "stdlib/blas/base/shared.h" +#include "stdlib/strided/base/stride2offset.h" /** * Computes the variance of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass trial mean algorithm. @@ -36,13 +37,28 @@ * @param N number of indexed elements * @param correction degrees of freedom adjustment * @param X input array -* @param stride stride length +* @param strideX stride length * @return output value */ -double stdlib_strided_dnanvariancech( const int64_t N, const double correction, const double *X, const int64_t stride ) { - int64_t ix; - int64_t n; - int64_t i; +double API_SUFFIX(stdlib_strided_dnanvariancech)( 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_dnanvariancech_ndarray)( N, correction, X, strideX, ox ); +} + +/** +* Computes the variance of a double-precision floating-point strided array ignoring `NaN` values and using a one-pass trial mean 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_dnanvariancech_ndarray)( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { + CBLAS_INT ix; + CBLAS_INT n; + CBLAS_INT i; double M2; double mu; double nc; @@ -54,18 +70,14 @@ double stdlib_strided_dnanvariancech( const int64_t N, const double correction, if ( N <= 0 ) { return 0.0 / 0.0; // NaN } - if ( N == 1 || stride == 0 ) { + if ( N == 1 || strideX == 0 ) { v = X[ 0 ]; if ( v == v && (double)N-correction > 0.0 ) { return 0.0; } return 0.0 / 0.0; // NaN } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } + ix = offsetX; // Find an estimate for the mean... for ( i = 0; i < N; i++ ) { v = X[ ix ]; @@ -73,12 +85,12 @@ double stdlib_strided_dnanvariancech( const int64_t N, const double correction, mu = v; break; } - ix += stride; + ix += strideX; } if ( i == N ) { return 0.0 / 0.0; // NaN } - ix += stride; + ix += strideX; i += 1; // Compute the variance... @@ -93,7 +105,7 @@ double stdlib_strided_dnanvariancech( const int64_t N, const double correction, M += d; n += 1; } - ix += stride; + ix += strideX; } dn = (double)n; nc = dn - correction; diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.dnanvariancech.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.dnanvariancech.js index 9e76d0079f0c..a0846cb2eecf 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.dnanvariancech.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.dnanvariancech.js @@ -21,7 +21,6 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var Float64Array = require( '@stdlib/array/float64' ); var dnanvariancech = require( './../lib/dnanvariancech.js' ); @@ -213,7 +212,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; @@ -230,15 +228,13 @@ tape( 'the function supports a `stride` parameter', function test( t ) { NaN ]); - N = floor( x.length / 2 ); - v = dnanvariancech( N, 1, x, 2 ); + v = dnanvariancech( 5, 1, x, 2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; var i; @@ -255,9 +251,8 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 4.0, // 0 2.0 ]); - N = floor( x.length / 2 ); - v = dnanvariancech( N, 1, x, -2 ); + v = dnanvariancech( 5, 1, x, -2 ); t.strictEqual( v, 6.25, 'returns expected value' ); x = new Float64Array( 1e3 ); @@ -295,7 +290,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([ @@ -313,9 +307,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 = dnanvariancech( N, 1, x1, 2 ); + v = dnanvariancech( 5, 1, x1, 2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.dnanvariancech.native.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.dnanvariancech.native.js index 7a9ba7dbe47b..a103a9877671 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.dnanvariancech.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.dnanvariancech.native.js @@ -22,7 +22,6 @@ var resolve = require( 'path' ).resolve; var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var Float64Array = require( '@stdlib/array/float64' ); var tryRequire = require( '@stdlib/utils/try-require' ); @@ -222,7 +221,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; @@ -239,15 +237,13 @@ tape( 'the function supports a `stride` parameter', opts, function test( t ) { NaN ]); - N = floor( x.length / 2 ); - v = dnanvariancech( N, 1, x, 2 ); + v = dnanvariancech( 5, 1, x, 2 ); t.strictEqual( v, 6.25, '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; @@ -264,9 +260,8 @@ tape( 'the function supports a negative `stride` parameter', opts, function test 4.0, // 0 2.0 ]); - N = floor( x.length / 2 ); - v = dnanvariancech( N, 1, x, -2 ); + v = dnanvariancech( 5, 1, x, -2 ); t.strictEqual( v, 6.25, 'returns expected value' ); x = new Float64Array( 1e3 ); @@ -304,7 +299,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([ @@ -322,9 +316,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 = dnanvariancech( N, 1, x1, 2 ); + v = dnanvariancech( 5, 1, x1, 2 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.ndarray.js index 4ac85d917c5c..2ccab39aee53 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.ndarray.js @@ -21,7 +21,6 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var Float64Array = require( '@stdlib/array/float64' ); var dnanvariancech = require( './../lib/ndarray.js' ); @@ -213,7 +212,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; @@ -230,15 +228,13 @@ tape( 'the function supports a `stride` parameter', function test( t ) { NaN ]); - N = floor( x.length / 2 ); - v = dnanvariancech( N, 1, x, 2, 0 ); + v = dnanvariancech( 5, 1, x, 2, 0 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; var i; @@ -255,9 +251,8 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 4.0, // 0 2.0 ]); - N = floor( x.length / 2 ); - v = dnanvariancech( N, 1, x, -2, 8 ); + v = dnanvariancech( 5, 1, x, -2, 8 ); t.strictEqual( v, 6.25, 'returns expected value' ); x = new Float64Array( 1e3 ); @@ -293,7 +288,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; @@ -309,9 +303,8 @@ tape( 'the function supports an `offset` parameter', function test( t ) { NaN, NaN // 4 ]); - N = floor( x.length / 2 ); - v = dnanvariancech( N, 1, x, 2, 1 ); + v = dnanvariancech( 5, 1, x, 2, 1 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.ndarray.native.js b/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.ndarray.native.js index 89b4c1fa9252..42cd423f955b 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanvariancech/test/test.ndarray.native.js +++ b/lib/node_modules/@stdlib/stats/base/dnanvariancech/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 isnan = require( '@stdlib/math/base/assert/is-nan' ); var Float64Array = require( '@stdlib/array/float64' ); var tryRequire = require( '@stdlib/utils/try-require' ); @@ -222,7 +221,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; @@ -239,15 +237,13 @@ tape( 'the function supports a `stride` parameter', opts, function test( t ) { NaN ]); - N = floor( x.length / 2 ); - v = dnanvariancech( N, 1, x, 2, 0 ); + v = dnanvariancech( 5, 1, x, 2, 0 ); t.strictEqual( v, 6.25, '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; @@ -264,9 +260,8 @@ tape( 'the function supports a negative `stride` parameter', opts, function test 4.0, // 0 2.0 ]); - N = floor( x.length / 2 ); - v = dnanvariancech( N, 1, x, -2, 8 ); + v = dnanvariancech( 5, 1, x, -2, 8 ); t.strictEqual( v, 6.25, 'returns expected value' ); x = new Float64Array( 1e3 ); @@ -302,7 +297,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; @@ -318,9 +312,8 @@ tape( 'the function supports an `offset` parameter', opts, function test( t ) { NaN, NaN // 4 ]); - N = floor( x.length / 2 ); - v = dnanvariancech( N, 1, x, 2, 1 ); + v = dnanvariancech( 5, 1, x, 2, 1 ); t.strictEqual( v, 6.25, 'returns expected value' ); t.end();