diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/README.md b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/README.md index 47c31c680c2..2f1ad11e788 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/README.md @@ -36,7 +36,7 @@ limitations under the License. var dnansumkbn2 = require( '@stdlib/blas/ext/base/dnansumkbn2' ); ``` -#### dnansumkbn2( N, x, stride ) +#### dnansumkbn2( N, x, strideX ) Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm. @@ -53,7 +53,7 @@ The function has the following parameters: - **N**: number of indexed elements. - **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 the strided arrays are accessed at runtime. For example, to compute the sum of every other element in `x`, @@ -80,7 +80,7 @@ var v = dnansumkbn2( 4, x1, 2 ); // returns 5.0 ``` -#### dnansumkbn2.ndarray( N, x, stride, offset ) +#### dnansumkbn2.ndarray( N, x, strideX, offsetX ) Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics. @@ -95,9 +95,9 @@ var v = dnansumkbn2.ndarray( 4, 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 sum of 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 sum of every other value in `x` starting from the second value: ```javascript var Float64Array = require( '@stdlib/array/float64' ); @@ -129,11 +129,19 @@ var v = dnansumkbn2.ndarray( 4, x, 2, 1 ); ```javascript -var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var filledarrayBy = require( '@stdlib/array/filled-by' ); var dnansumkbn2 = require( '@stdlib/blas/ext/base/dnansumkbn2' ); -var x = filledarrayBy( 10, 'float64', discreteUniform( 0, 100 ) ); +function clbk() { + if ( bernoulli( 0.7 ) > 0 ) { + return discreteUniform( 0, 100 ); + } + return NaN; +} + +var x = filledarrayBy( 10, 'float64', clbk ); console.log( x ); var v = dnansumkbn2( x.length, x, 1 ); @@ -144,8 +152,123 @@ console.log( v ); + + * * * +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/dnansumkbn2.h" +``` + +#### stdlib_strided_dnansumkbn2( N, \*X, strideX ) + +Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm. + +```c +const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 }; + +double v = stdlib_strided_dnansumkbn2( 4, x, 1 ); +// returns 7.0 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **X**: `[in] double*` input array. +- **strideX**: `[in] CBLAS_INT` stride length for `X`. + +```c +double stdlib_strided_dnansumkbn2( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ); +``` + +#### stdlib_strided_dnansumkbn2_ndarray( N, \*X, strideX, offsetX ) + +Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics. + +```c +const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 }; + +double v = stdlib_strided_dnansumkbn2_ndarray( 4, x, 1, 0 ); +// returns 7.0 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **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_dnansumkbn2_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/dnansumkbn2.h" +#include + +int main( void ) { + // Create a strided array: + const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 0.0/0.0, 0.0/0.0 }; + + // Specify the number of elements: + const int N = 5; + + // Specify the stride length: + const int strideX = 2; + + // Compute the sum: + double v = stdlib_strided_dnansumkbn2( N, x, strideX ); + + // Print the result: + printf( "sum: %lf\n", v ); +} +``` + +
+ + + +
+ + +
## References diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/benchmark/c/benchmark.length.c index e5f2093bf99..903e5489dc6 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/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; @@ -124,6 +124,43 @@ 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++ ) { + v = stdlib_strided_dnansumkbn2_ndarray( len, 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. */ @@ -146,7 +183,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/blas/ext/base/dnansumkbn2/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/docs/repl.txt index 938d719aafd..bc5623e2718 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/docs/repl.txt @@ -1,10 +1,10 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm. - The `N` and stride parameters determine which elements in the strided + 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 @@ -20,8 +20,8 @@ x: Float64Array Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -47,13 +47,13 @@ -1.0 -{{alias}}.ndarray( N, x, stride, offset ) +{{alias}}.ndarray( N, x, strideX, offsetX ) Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics. While typed array views mandate a view offset based on the underlying - buffer, the `offset` parameter supports indexing semantics based on a + buffer, the offset parameter supports indexing semantics based on a starting index. Parameters @@ -64,10 +64,10 @@ x: Float64Array Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/docs/types/index.d.ts index a019b1ff6d9..a305caa59a1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/docs/types/index.d.ts @@ -27,7 +27,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @returns sum * * @example @@ -38,15 +38,15 @@ interface Routine { * var v = dnansumkbn2( x.length, x, 1 ); * // returns 1.0 */ - ( N: number, x: Float64Array, stride: number ): number; + ( N: number, x: Float64Array, strideX: number ): number; /** * Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics. * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns sum * * @example @@ -57,7 +57,7 @@ interface Routine { * var v = dnansumkbn2.ndarray( x.length, x, 1, 0 ); * // returns 1.0 */ - ndarray( N: number, x: Float64Array, stride: number, offset: number ): number; + ndarray( N: number, x: Float64Array, strideX: number, offsetX: number ): number; } /** @@ -65,7 +65,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @returns sum * * @example diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/examples/c/example.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/examples/c/example.c index 903bf37968d..27acad838c5 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/examples/c/example.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/examples/c/example.c @@ -17,7 +17,6 @@ */ #include "stdlib/blas/ext/base/dnansumkbn2.h" -#include #include int main( void ) { @@ -25,13 +24,13 @@ int main( void ) { const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 0.0/0.0, 0.0/0.0 }; // Specify the number of elements: - const int64_t N = 5; + const int N = 5; // Specify the stride length: - const int64_t stride = 2; + const int strideX = 2; // Compute the sum: - double v = stdlib_strided_dnansumkbn2( N, x, stride ); + double v = stdlib_strided_dnansumkbn2( N, x, strideX ); // Print the result: printf( "sum: %lf\n", v ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/include/stdlib/blas/ext/base/dnansumkbn2.h b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/include/stdlib/blas/ext/base/dnansumkbn2.h index 4a9ca3659e9..ce51759775c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/include/stdlib/blas/ext/base/dnansumkbn2.h +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/include/stdlib/blas/ext/base/dnansumkbn2.h @@ -19,7 +19,7 @@ #ifndef STDLIB_BLAS_EXT_BASE_DNANSUMKBN2_H #define STDLIB_BLAS_EXT_BASE_DNANSUMKBN2_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 sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm. */ -double stdlib_strided_dnansumkbn2( const int64_t N, const double *X, const int64_t stride ); +double API_SUFFIX(stdlib_strided_dnansumkbn2)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ); + +/** +* Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order Kahan–Babuška algorithm and alternative indexing semantics. +*/ +double API_SUFFIX(stdlib_strided_dnansumkbn2_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/dnansumkbn2.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/dnansumkbn2.js index bb24ab6b0bc..69cb6e3accb 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/dnansumkbn2.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/dnansumkbn2.js @@ -20,8 +20,8 @@ // MODULES // -var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var abs = require( '@stdlib/math/base/special/abs' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -39,7 +39,7 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} sum * * @example @@ -51,56 +51,8 @@ var abs = require( '@stdlib/math/base/special/abs' ); * var v = dnansumkbn2( N, x, 1 ); * // returns 1.0 */ -function dnansumkbn2( N, x, stride ) { - var sum; - var ccs; - var ix; - var cs; - var cc; - var v; - var t; - var c; - var i; - - if ( N <= 0 ) { - return 0.0; - } - if ( N === 1 || stride === 0 ) { - if ( isnan( x[ 0 ] ) ) { - return 0.0; - } - return x[ 0 ]; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - sum = 0.0; - ccs = 0.0; // second order correction term for lost low order bits - cs = 0.0; // first order correction term for lost low order bits - for ( i = 0; i < N; i++ ) { - v = x[ ix ]; - if ( isnan( v ) === false ) { - t = sum + v; - if ( abs( sum ) >= abs( v ) ) { - c = (sum-t) + v; - } else { - c = (v-t) + sum; - } - sum = t; - t = cs + c; - if ( abs( cs ) >= abs( c ) ) { - cc = (cs-t) + c; - } else { - cc = (c-t) + cs; - } - cs = t; - ccs += cc; - } - ix += stride; - } - return sum + cs + ccs; +function dnansumkbn2( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/dnansumkbn2.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/dnansumkbn2.native.js index 3c63a7a7214..0e63351f9a2 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/dnansumkbn2.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/dnansumkbn2.native.js @@ -30,7 +30,7 @@ var addon = require( './../src/addon.node' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} sum * * @example @@ -42,8 +42,8 @@ var addon = require( './../src/addon.node' ); * var v = dnansumkbn2( N, x, 1 ); * // returns 1.0 */ -function dnansumkbn2( N, x, stride ) { - return addon( N, x, stride ); +function dnansumkbn2( N, x, strideX ) { + return addon( N, x, strideX ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/ndarray.js index 1e2ede0922f..ba4ea97223c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/ndarray.js @@ -39,8 +39,8 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @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} sum * * @example @@ -51,7 +51,7 @@ var abs = require( '@stdlib/math/base/special/abs' ); * var v = dnansumkbn2( 5, x, 2, 1 ); * // returns 5.0 */ -function dnansumkbn2( N, x, stride, offset ) { +function dnansumkbn2( N, x, strideX, offsetX ) { var sum; var ccs; var ix; @@ -62,17 +62,18 @@ function dnansumkbn2( N, x, stride, offset ) { var c; var i; + sum = 0.0; if ( N <= 0 ) { return 0.0; } - if ( N === 1 || stride === 0 ) { - if ( isnan( x[ offset ] ) ) { - return 0.0; + ix = offsetX; + if ( strideX === 0 ) { + if ( isnan( x[ ix ] ) ) { + return sum; } - return x[ offset ]; + sum = x[ ix ] * N; + return sum; } - ix = offset; - sum = 0.0; ccs = 0.0; // second order correction term for lost low order bits cs = 0.0; // first order correction term for lost low order bits for ( i = 0; i < N; i++ ) { @@ -94,7 +95,7 @@ function dnansumkbn2( N, x, stride, offset ) { cs = t; ccs += cc; } - ix += stride; + ix += strideX; } return sum + cs + ccs; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/ndarray.native.js index b0c724a4563..4f8670eed2d 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/lib/ndarray.native.js @@ -20,8 +20,7 @@ // MODULES // -var Float64Array = require( '@stdlib/array/float64' ); -var addon = require( './dnansumkbn2.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // @@ -31,8 +30,8 @@ var addon = require( './dnansumkbn2.native.js' ); * * @param {PositiveInteger} N - number of indexed elements * @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} sum * * @example @@ -43,13 +42,8 @@ var addon = require( './dnansumkbn2.native.js' ); * var v = dnansumkbn2( 5, x, 2, 1 ); * // returns 5.0 */ -function dnansumkbn2( N, 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, view, stride ); +function dnansumkbn2( N, x, strideX, offsetX ) { + return addon.ndarray( N, x, strideX, offsetX ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/manifest.json b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/manifest.json index 01e40f8cc35..57e9af5c51e 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/manifest.json +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/manifest.json @@ -28,53 +28,57 @@ { "task": "build", "src": [ - "./src/dnansumkbn2.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], "dependencies": [ "@stdlib/math/base/assert/is-nan", "@stdlib/napi/export", "@stdlib/napi/argv", "@stdlib/napi/argv-int64", - "@stdlib/napi/argv-strided-float64array" + "@stdlib/napi/argv-strided-float64array", + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/base/shared", + "@stdlib/math/base/special/abs", + "@stdlib/napi/create-double" ] }, { "task": "benchmark", "src": [ - "./src/dnansumkbn2.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], "dependencies": [ - "@stdlib/math/base/assert/is-nan" + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/special/abs", + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset" ] }, { "task": "examples", "src": [ - "./src/dnansumkbn2.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], "dependencies": [ - "@stdlib/math/base/assert/is-nan" + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/special/abs", + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset" ] } ] diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/src/addon.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/src/addon.c index dca633b53ed..8acdd40ee5b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/src/addon.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/src/addon.c @@ -17,12 +17,13 @@ */ #include "stdlib/blas/ext/base/dnansumkbn2.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_strided_float64array.h" +#include "stdlib/napi/create_double.h" #include -#include /** * Receives JavaScript callback invocation data. @@ -32,16 +33,29 @@ * @return Node-API value */ static napi_value addon( napi_env env, napi_callback_info info ) { - STDLIB_NAPI_ARGV( env, info, argv, argc, 3 ); - STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); - STDLIB_NAPI_ARGV_INT64( env, stride, argv, 2 ); - STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, stride, argv, 1 ); - - napi_value v; - napi_status status = napi_create_double( env, stdlib_strided_dnansumkbn2( N, X, stride ), &v ); - assert( status == napi_ok ); + STDLIB_NAPI_ARGV( env, info, argv, argc, 3 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 ); + STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dnansumkbn2)( N, X, strideX ), v ); + return 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, 4 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 3 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 ); + STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dnansumkbn2_ndarray)( N, 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/blas/ext/base/dnansumkbn2/src/dnansumkbn2.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/src/main.c similarity index 51% rename from lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/src/dnansumkbn2.c rename to lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/src/main.c index c0b278ab19a..488831d012b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/src/dnansumkbn2.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/src/main.c @@ -17,9 +17,10 @@ */ #include "stdlib/blas/ext/base/dnansumkbn2.h" +#include "stdlib/strided/base/stride2offset.h" #include "stdlib/math/base/assert/is_nan.h" -#include -#include +#include "stdlib/math/base/special/abs.h" +#include "stdlib/blas/base/shared.h" /** * Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm. @@ -32,51 +33,70 @@ * * - Klein, Andreas. 2005. "A Generalized Kahan-Babuška-Summation-Algorithm." _Computing_ 76 (3): 279–93. doi:[10.1007/s00607-005-0139-x](https://doi.org/10.1007/s00607-005-0139-x). * -* @param N number of indexed elements -* @param X input array -* @param stride stride length -* @return output value +* @param N number of indexed elements +* @param X input array +* @param strideX stride length +* @return output value */ -double stdlib_strided_dnansumkbn2( const int64_t N, const double *X, const int64_t stride ) { +double API_SUFFIX(stdlib_strided_dnansumkbn2)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ) { + CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + return API_SUFFIX(stdlib_strided_dnansumkbn2_ndarray)( N, X, strideX, ox ); +} + +/** +* Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics. +* +* ## Method +* +* - This implementation uses a second-order iterative Kahan–Babuška algorithm, as described by Klein (2005). +* +* ## References +* +* - Klein, Andreas. 2005. "A Generalized Kahan-Babuška-Summation-Algorithm." _Computing_ 76 (3): 279–93. doi:[10.1007/s00607-005-0139-x](https://doi.org/10.1007/s00607-005-0139-x). +* +* @param N number of indexed elements +* @param X input array +* @param strideX stride length +* @param offsetX starting index +* @return output value +*/ +double API_SUFFIX(stdlib_strided_dnansumkbn2_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { double sum; double ccs; - int64_t ix; - int64_t i; + CBLAS_INT ix; + CBLAS_INT i; double cs; double cc; double v; double t; double c; + sum = 0.0; if ( N <= 0 ) { - return 0.0; + return sum; } - if ( N == 1 || stride == 0 ) { - if ( stdlib_base_is_nan( X[ 0 ] ) ) { - return 0.0; + ix = offsetX; + if ( strideX == 0 ) { + if ( stdlib_base_is_nan( X[ ix ] ) ) { + return sum; } - return X[ 0 ]; + sum = X[ ix ] * N; + return sum; } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - sum = 0.0; ccs = 0.0; // second order correction term for lost lower order bits cs = 0.0; // first order correction term for lost low order bits for ( i = 0; i < N; i++ ) { v = X[ ix ]; if ( !stdlib_base_is_nan( v ) ) { t = sum + v; - if ( fabs( sum ) >= fabs( v ) ) { + if ( stdlib_base_abs( sum ) >= stdlib_base_abs( v ) ) { c = (sum-t) + v; } else { c = (v-t) + sum; } sum = t; t = cs + c; - if ( fabs( cs ) >= fabs( c ) ) { + if ( stdlib_base_abs( cs ) >= stdlib_base_abs( c ) ) { cc = (cs-t) + c; } else { cc = (c-t) + cs; @@ -84,7 +104,7 @@ double stdlib_strided_dnansumkbn2( const int64_t N, const double *X, const int64 cs = t; ccs += cc; } - ix += stride; + ix += strideX; } return sum + cs + ccs; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.dnansumkbn2.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.dnansumkbn2.js index 25571e885b9..74b611671bf 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.dnansumkbn2.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.dnansumkbn2.js @@ -150,14 +150,14 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element', function test( t ) { +tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', function test( t ) { var x; var v; x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); v = dnansumkbn2( x.length, x, 0 ); - t.strictEqual( v, 1.0, 'returns expected value' ); + t.strictEqual( v, 5.0, 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.dnansumkbn2.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.dnansumkbn2.native.js index 48453c89aae..3ac27f39c02 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.dnansumkbn2.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.dnansumkbn2.native.js @@ -241,14 +241,14 @@ tape( 'the function supports a negative `stride` parameter', opts, function test t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element', opts, function test( t ) { +tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', opts, function test( t ) { var x; var v; x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); v = dnansumkbn2( x.length, x, 0 ); - t.strictEqual( v, 1.0, 'returns expected value' ); + t.strictEqual( v, 5.0, 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.ndarray.js index baab443c589..cd0ffaf0e2f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.ndarray.js @@ -150,14 +150,14 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns the first indexed element', function test( t ) { +tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', function test( t ) { var x; var v; x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); v = dnansumkbn2( x.length, x, 0, 0 ); - t.strictEqual( v, 1.0, 'returns expected value' ); + t.strictEqual( v, 5.0, 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.ndarray.native.js index 1f1db138798..4d6f82d79de 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/test/test.ndarray.native.js @@ -159,14 +159,14 @@ tape( 'the function supports a negative `stride` parameter', opts, function test t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns the first indexed element', opts, function test( t ) { +tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', opts, function test( t ) { var x; var v; x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); v = dnansumkbn2( x.length, x, 0, 0 ); - t.strictEqual( v, 1.0, 'returns expected value' ); + t.strictEqual( v, 5.0, 'returns expected value' ); t.end(); });