From 058a920851e2704906579ff2f94cd6be5ce7db20 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 6 Oct 2024 17:04:41 +0500 Subject: [PATCH 1/9] feat: add C ndarray API and refactor --- .../blas/ext/base/dnannsumkbn2/README.md | 126 +++++++++++++++++- .../benchmark/c/benchmark.length.c | 57 +++++++- .../blas/ext/base/dnannsumkbn2/docs/repl.txt | 4 +- .../base/dnannsumkbn2/examples/c/example.c | 13 +- .../stdlib/blas/ext/base/dnannsumkbn2.h | 9 +- .../ext/base/dnannsumkbn2/lib/dnannsumkbn2.js | 64 +-------- .../blas/ext/base/dnannsumkbn2/lib/ndarray.js | 10 +- .../base/dnannsumkbn2/lib/ndarray.native.js | 20 +-- .../blas/ext/base/dnannsumkbn2/manifest.json | 33 ++--- .../blas/ext/base/dnannsumkbn2/src/addon.c | 38 +++++- .../src/{dnannsumkbn2.c => main.c} | 62 ++++++--- .../dnannsumkbn2/test/test.dnannsumkbn2.js | 6 +- .../test/test.dnannsumkbn2.native.js | 6 +- .../base/dnannsumkbn2/test/test.ndarray.js | 6 +- .../dnannsumkbn2/test/test.ndarray.native.js | 6 +- 15 files changed, 306 insertions(+), 154 deletions(-) rename lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/{dnannsumkbn2.c => main.c} (51%) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md index a1c77797ced7..2d3fd2a84774 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md @@ -106,7 +106,7 @@ The function has the following additional parameters: - **offsetX**: starting index for `x`. - **offsetOut**: starting index for `out`. -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, offset parameters support indexing semantics based on starting indices. For example, to calculate the sum of every other value in `x` starting from the second value ```javascript var Float64Array = require( '@stdlib/array/float64' ); @@ -164,8 +164,132 @@ console.log( out ); + + * * * +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/dnannsumkbn2.h" +``` + +#### stdlib_strided_dnannsumkbn2( N, \*X, strideX, \*n ) + +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 }; +CBLAS_INT n = 0; + +double v = stdlib_strided_dnannsumkbn2( 4, x, 1, &n ); +// 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` index increment for `X`. +- **n**: `[out] CBLAS_INT*` number of non-NaN elements. + +```c +double stdlib_strided_dnannsumkbn2( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, CBLAS_INT *n ); +``` + +#### stdlib_strided_dnannsumkbn2_ndarray( N, \*X, strideX, offsetX, \*n ) + +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 }; +CBLAS_INT n = 0; + +double v = stdlib_strided_dnannsumkbn2_ndarray( 4, x, 1, 0, &n ); +// 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` index increment for `X`. +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. +- **n**: `[out] CBLAS_INT*` number of non-NaN elements. + +```c +double stdlib_strided_dnannsumkbn2_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, CBLAS_INT *n ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/dnannsumkbn2.h" +#include "stdlib/blase/base/shared.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; + + // Initialize a variable for storing the number of non-NaN elements: + CBLAS_INT n = 0; + + // Compute the sum: + double v = stdlib_strided_dnannsumkbn2( N, x, strideX, &n ); + + // Print the result: + printf( "sum: %lf\n", v ); + printf( "n: %"CBLAS_IFMT"\n", n ); +} +``` + +
+ + + +
+ + +
## References diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/c/benchmark.length.c index 9740a7dbb011..fc896504c3ea 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/c/benchmark.length.c @@ -17,6 +17,7 @@ */ #include "stdlib/blas/ext/base/dnannsumkbn2.h" +#include "stdlib/blas/base/shared.h" #include #include #include @@ -94,10 +95,10 @@ 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 ]; - int64_t n; + CBLAS_INT n; double v; double t; int i; @@ -126,6 +127,45 @@ 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 ]; + CBLAS_INT n; + 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; + n = 0; + t = tic(); + for ( i = 0; i < iterations; i++ ) { + v = stdlib_strided_dnannsumkbn2_ndarray( len, x, 1, 0, &n ); + if ( v != v || n < 0 ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( v != v || n < 0 ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + /** * Main execution sequence. */ @@ -148,7 +188,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/dnannsumkbn2/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt index 820186daae2c..34564dbc476b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt @@ -63,8 +63,8 @@ algorithm and alternative indexing semantics. While typed array views mandate a view offset based on the underlying - buffer, the offset parameters supports indexing semantics - based on a starting index. + buffer, offset parameters support indexing semantics based on starting + index. Parameters ---------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/examples/c/example.c b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/examples/c/example.c index 5a38f94421a8..9c72a6ade4be 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/examples/c/example.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/examples/c/example.c @@ -17,27 +17,26 @@ */ #include "stdlib/blas/ext/base/dnannsumkbn2.h" -#include +#include "stdlib/blas/base/shared.h" #include -#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 int64_t N = 5; + const int N = 5; // Specify the stride length: - const int64_t stride = 2; + const int strideX = 2; // Initialize a variable for storing the number of non-NaN elements: - int64_t n = 0; + CBLAS_INT n = 0; // Compute the sum: - double v = stdlib_strided_dnannsumkbn2( N, x, stride, &n ); + double v = stdlib_strided_dnannsumkbn2( N, x, strideX, &n ); // Print the result: printf( "sum: %lf\n", v ); - printf( "n: %"PRId64"\n", n ); + printf( "n: %"CBLAS_IFMT"\n", n ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/include/stdlib/blas/ext/base/dnannsumkbn2.h b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/include/stdlib/blas/ext/base/dnannsumkbn2.h index 9fde30116af8..0e713bf5bce5 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/include/stdlib/blas/ext/base/dnannsumkbn2.h +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/include/stdlib/blas/ext/base/dnannsumkbn2.h @@ -19,7 +19,7 @@ #ifndef STDLIB_BLAS_EXT_BASE_DNANNSUMKBN2_H #define STDLIB_BLAS_EXT_BASE_DNANNSUMKBN2_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_dnannsumkbn2( const int64_t N, const double *X, const int64_t stride, int64_t *n ); +double API_SUFFIX(stdlib_strided_dnannsumkbn2)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, CBLAS_INT *n ); + +/** +* 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 using alternative indexing semantics. +*/ +double API_SUFFIX(stdlib_strided_dnannsumkbn2_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, CBLAS_INT *n ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/dnannsumkbn2.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/dnannsumkbn2.js index 3771b46a32bd..979e874c0411 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/dnannsumkbn2.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/dnannsumkbn2.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 // @@ -54,72 +54,16 @@ var abs = require( '@stdlib/math/base/special/abs' ); * // returns [ 1.0, 3 ] */ function dnannsumkbn2( N, x, strideX, out, strideOut ) { - var sum; - var ccs; - var cs; - var cc; var ix; var io; - var v; - var t; - var c; - var n; - var i; - if ( strideX < 0 ) { - ix = (1-N) * strideX; - } else { - ix = 0; - } + ix = stride2offset( N, strideX ); if ( strideOut < 0 ) { io = -strideOut; } else { io = 0; } - sum = 0.0; - if ( N <= 0 ) { - out[ io ] = sum; - out[ io+strideOut ] = 0; - return out; - } - if ( N === 1 || strideX === 0 ) { - if ( isnan( x[ ix ] ) ) { - out[ io ] = sum; - out[ io+strideOut ] = 0; - return out; - } - out[ io ] = x[ ix ]; - out[ io+strideOut ] = 1; - return out; - } - ccs = 0.0; // second order correction term for lost low order bits - cs = 0.0; // first order correction term for lost low order bits - n = 0; - 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; - n += 1; - } - ix += strideX; - } - out[ io ] = sum + cs + ccs; - out[ io+strideOut ] = n; - return out; + return ndarray( N, x, strideX, ix, out, strideOut, io ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js index accc50d2e995..d8a737109db9 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js @@ -39,10 +39,10 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} strideX - `x` stride length +* @param {integer} strideX - `x` index increment * @param {NonNegativeInteger} offsetX - `x` starting index * @param {Float64Array} out - output array -* @param {integer} strideOut - `out` stride length +* @param {integer} strideOut - `out` index increment * @param {NonNegativeInteger} offsetOut - `out` starting index * @returns {Float64Array} output array * @@ -77,14 +77,14 @@ function dnannsumkbn2( N, x, strideX, offsetX, out, strideOut, offsetOut ) { out[ io+strideOut ] = 0; return out; } - if ( N === 1 || strideX === 0 ) { + if ( strideX === 0 ) { if ( isnan( x[ ix ] ) ) { out[ io ] = sum; out[ io+strideOut ] = 0; return out; } - out[ io ] = x[ ix ]; - out[ io+strideOut ] = 1; + out[ io ] = abs( x[ ix ] ) * N; + out[ io+strideOut ] = N; return out; } ccs = 0.0; // second order correction term for lost low order bits diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.native.js index eac7cba19a90..958b68d8a63c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.native.js @@ -20,9 +20,7 @@ // MODULES // -var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' ); -var offsetView = require( '@stdlib/strided/base/offset-view' ); -var addon = require( './dnannsumkbn2.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // @@ -32,10 +30,10 @@ var addon = require( './dnannsumkbn2.native.js' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} strideX - `x` stride length +* @param {integer} strideX - `x` index increment * @param {NonNegativeInteger} offsetX - `x` starting index * @param {Float64Array} out - output array -* @param {integer} strideOut - `out` stride length +* @param {integer} strideOut - `out` index increment * @param {NonNegativeInteger} offsetOut - `out` starting index * @returns {Float64Array} output array * @@ -49,17 +47,7 @@ var addon = require( './dnannsumkbn2.native.js' ); * // returns [ 5.0, 4 ] */ function dnannsumkbn2( N, x, strideX, offsetX, out, strideOut, offsetOut ) { - var viewOut; - var viewX; - - offsetX = minViewBufferIndex( N, strideX, offsetX ); - if ( strideOut < 0 ) { - offsetOut += strideOut; - } - - viewX = offsetView( x, offsetX ); - viewOut = offsetView( out, offsetOut ); - addon( N, viewX, strideX, viewOut, strideOut ); + addon.ndarray( N, x, strideX, offsetX, out, strideOut, offsetOut ); return out; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/manifest.json b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/manifest.json index 719ea837d79d..b6a210cd9846 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/manifest.json +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/manifest.json @@ -28,53 +28,56 @@ { "task": "build", "src": [ - "./src/dnannsumkbn2.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], "dependencies": [ "@stdlib/napi/export", "@stdlib/napi/argv", "@stdlib/napi/argv-int64", "@stdlib/napi/argv-strided-float64array", - "@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": "benchmark", "src": [ - "./src/dnannsumkbn2.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/dnannsumkbn2.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/dnannsumkbn2/src/addon.c b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/addon.c index 4ea2ad1e0844..fd853ce40aeb 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/addon.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/addon.c @@ -17,12 +17,12 @@ */ #include "stdlib/blas/ext/base/dnannsumkbn2.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 -#include /** * Receives JavaScript callback invocation data. @@ -39,7 +39,7 @@ static napi_value addon( napi_env env, napi_callback_info info ) { STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 ); STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, Out, 2, strideOut, argv, 3 ); - int64_t io; + int io; if ( strideOut < 0 ) { io = -strideOut; } else { @@ -47,11 +47,37 @@ static napi_value addon( napi_env env, napi_callback_info info ) { } double *out = Out; - int64_t n; - out[ io ] = stdlib_strided_dnannsumkbn2( N, X, strideX, &n ); - out[ io + strideOut ] = (double)n; + CBLAS_INT n; + out[ io ] = API_SUFFIX(stdlib_strided_dnannsumkbn2)( N, X, strideX, &n ); + out[ io+strideOut ] = (double)n; return NULL; } -STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) +/** +* 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, 7 ); + 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_INT64( env, strideOut, argv, 5 ); + STDLIB_NAPI_ARGV_INT64( env, offsetOut, argv, 6 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, Out, 2, strideOut, argv, 4 ); + + int io = offsetOut; + double *out = Out; + CBLAS_INT n; + out[ io ] = API_SUFFIX(stdlib_strided_dnannsumkbn2_ndarray)( N, X, strideX, offsetX, &n ); + out[ io+strideOut ] = (double)n; + + return NULL; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/dnannsumkbn2.c b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c similarity index 51% rename from lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/dnannsumkbn2.c rename to lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c index aa1f85ed2150..5668073f5ccb 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/dnannsumkbn2.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c @@ -16,10 +16,11 @@ * limitations under the License. */ +#include "stdlib/strided/base/stride2offset.h" #include "stdlib/blas/ext/base/dnannsumkbn2.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,19 +33,42 @@ * * - 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 -* @param n pointer for storing the number of non-NaN elements -* @return output value +* @param N number of indexed elements +* @param X input array +* @param strideX stride length +* @param n pointer for storing the number of non-NaN elements +* @return output value */ -double stdlib_strided_dnannsumkbn2( const int64_t N, const double *X, const int64_t stride, int64_t *n ) { +double API_SUFFIX(stdlib_strided_dnannsumkbn2)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, CBLAS_INT *n ) { + CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + return API_SUFFIX(stdlib_strided_dnannsumkbn2_ndarray)( N, X, strideX, ox, n ); +} + +/** +* 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 index increment +* @param offsetX starting index +* @param n number of non-NaN elements +* @return output value +*/ +double API_SUFFIX(stdlib_strided_dnannsumkbn2_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, CBLAS_INT *n ) { double sum; double ccs; - int64_t ix; - int64_t i; double cs; double cc; + CBLAS_INT ix; + CBLAS_INT i; double v; double t; double c; @@ -54,32 +78,28 @@ double stdlib_strided_dnannsumkbn2( const int64_t N, const double *X, const int6 if ( N <= 0 ) { return sum; } - if ( N == 1 || stride == 0 ) { + if ( strideX == 0 ) { if ( stdlib_base_is_nan( X[ 0 ] ) ) { return sum; } - *n += 1; - return X[ 0 ]; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; + *n += N; + return stdlib_base_abs( X[ 0 ] ) * N; } + ix = offsetX; 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; @@ -88,7 +108,7 @@ double stdlib_strided_dnannsumkbn2( const int64_t N, const double *X, const int6 ccs += cc; *n += 1; } - ix += stride; + ix += strideX; } return sum + cs + ccs; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.dnannsumkbn2.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.dnannsumkbn2.js index 9a963077eef1..99369e7acf4f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.dnannsumkbn2.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.dnannsumkbn2.js @@ -215,18 +215,16 @@ tape( 'the function supports negative `stride` parameters', function test( t ) { t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns a sum equal to 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 expected; var out; var x; var v; - x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); - out = new Float64Array( 2 ); v = dnannsumkbn2( x.length, x, 0, out, 1 ); - expected = new Float64Array( [ 1.0, 1.0 ] ); + expected = new Float64Array( [ 5.0, 5.0 ] ); t.deepEqual( v, expected, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.dnannsumkbn2.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.dnannsumkbn2.native.js index 459e19a8cd9b..900cf84a8dfa 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.dnannsumkbn2.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.dnannsumkbn2.native.js @@ -224,18 +224,16 @@ tape( 'the function supports negative `stride` parameters', opts, function test( t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns a sum equal to 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 expected; var out; var x; var v; - x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); - out = new Float64Array( 2 ); v = dnannsumkbn2( x.length, x, 0, out, 1 ); - expected = new Float64Array( [ 1.0, 1.0 ] ); + expected = new Float64Array( [ 5.0, 5.0 ] ); t.deepEqual( v, expected, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.ndarray.js index 3cfec050596c..42a3d9b40f80 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.ndarray.js @@ -215,18 +215,16 @@ 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 expected; var out; var x; var v; - x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); - out = new Float64Array( 2 ); v = dnannsumkbn2( x.length, x, 0, 0, out, 1, 0 ); - expected = new Float64Array( [ 1.0, 1.0 ] ); + expected = new Float64Array( [ 5.0, 5.0 ] ); t.deepEqual( v, expected, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.ndarray.native.js index 871e1527553c..e8e121e298a9 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/test/test.ndarray.native.js @@ -224,18 +224,16 @@ 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 expected; var out; var x; var v; - x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); - out = new Float64Array( 2 ); v = dnannsumkbn2( x.length, x, 0, 0, out, 1, 0 ); - expected = new Float64Array( [ 1.0, 1.0 ] ); + expected = new Float64Array( [ 5.0, 5.0 ] ); t.deepEqual( v, expected, 'returns expected value' ); t.end(); From 72f5f6c713bd617baf57addcaf841e298ef83c52 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 6 Oct 2024 16:14:31 +0000 Subject: [PATCH 2/9] docs: fix comments and descriptions --- .../@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt | 2 +- .../dnannsumkbn2/include/stdlib/blas/ext/base/dnannsumkbn2.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt index 34564dbc476b..0674de9936ec 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt @@ -64,7 +64,7 @@ While typed array views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting - index. + indices. Parameters ---------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/include/stdlib/blas/ext/base/dnannsumkbn2.h b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/include/stdlib/blas/ext/base/dnannsumkbn2.h index 0e713bf5bce5..9b4b18cc88c4 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/include/stdlib/blas/ext/base/dnannsumkbn2.h +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/include/stdlib/blas/ext/base/dnannsumkbn2.h @@ -34,7 +34,7 @@ extern "C" { double API_SUFFIX(stdlib_strided_dnannsumkbn2)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, CBLAS_INT *n ); /** -* 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 using alternative indexing semantics. +* 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. */ double API_SUFFIX(stdlib_strided_dnannsumkbn2_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, CBLAS_INT *n ); From 4433ac7e4050d4a942b43e0312b9ad437ec8d403 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 7 Oct 2024 16:52:45 +0500 Subject: [PATCH 3/9] fix: remove use of abs to calculate sum --- lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c index 5668073f5ccb..ae4de5650e60 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c @@ -82,8 +82,9 @@ double API_SUFFIX(stdlib_strided_dnannsumkbn2_ndarray)( const CBLAS_INT N, const if ( stdlib_base_is_nan( X[ 0 ] ) ) { return sum; } + sum = X[ 0 ] * N; *n += N; - return stdlib_base_abs( X[ 0 ] ) * N; + return sum; } ix = offsetX; ccs = 0.0; // second order correction term for lost lower order bits From 5904699eec526a76f2eb2028c5d8cca2b51126ac Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 7 Oct 2024 17:03:16 +0500 Subject: [PATCH 4/9] fix: remove use of abs to calculate sum --- .../@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js index d8a737109db9..20df8df0915f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js @@ -83,7 +83,7 @@ function dnannsumkbn2( N, x, strideX, offsetX, out, strideOut, offsetOut ) { out[ io+strideOut ] = 0; return out; } - out[ io ] = abs( x[ ix ] ) * N; + out[ io ] = x[ ix ] * N; out[ io+strideOut ] = N; return out; } From 9059001a7d540f0ab870626be0a1cdcef2a1207b Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 7 Oct 2024 12:38:15 +0000 Subject: [PATCH 5/9] docs: fix parameter definition in function comment --- lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c index 5668073f5ccb..ad307e78ddc8 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c @@ -36,7 +36,7 @@ * @param N number of indexed elements * @param X input array * @param strideX stride length -* @param n pointer for storing the number of non-NaN elements +* @param n number of non-NaN elements * @return output value */ double API_SUFFIX(stdlib_strided_dnannsumkbn2)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, CBLAS_INT *n ) { From a939dfd03538d95a2edb2fde7a6fdeff94a0e99d Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Wed, 9 Oct 2024 05:20:13 +0000 Subject: [PATCH 6/9] fix: apply code review suggsetion --- lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md index 2d3fd2a84774..64f51c30b32d 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md @@ -262,7 +262,7 @@ double stdlib_strided_dnannsumkbn2_ndarray( const CBLAS_INT N, const double *X, 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 }; + 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; From cb32fbee39b54efae4464e36eef07f400a26c1ed Mon Sep 17 00:00:00 2001 From: headlessNode Date: Fri, 11 Oct 2024 22:13:44 +0500 Subject: [PATCH 7/9] fix: apply code review suggestion --- .../@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js | 5 ++--- .../@stdlib/blas/ext/base/dnannsumkbn2/src/main.c | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js index 20df8df0915f..c8cc16b59842 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js @@ -68,15 +68,14 @@ function dnannsumkbn2( N, x, strideX, offsetX, out, strideOut, offsetOut ) { var n; var i; - ix = offsetX; - io = offsetOut; - sum = 0.0; + io = offsetOut; if ( N <= 0 ) { out[ io ] = sum; out[ io+strideOut ] = 0; return out; } + ix = offsetX; if ( strideX === 0 ) { if ( isnan( x[ ix ] ) ) { out[ io ] = sum; diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c index 9df501e8c612..42e1d75bfd94 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c @@ -16,8 +16,8 @@ * limitations under the License. */ -#include "stdlib/strided/base/stride2offset.h" #include "stdlib/blas/ext/base/dnannsumkbn2.h" +#include "stdlib/strided/base/stride2offset.h" #include "stdlib/math/base/assert/is_nan.h" #include "stdlib/math/base/special/abs.h" #include "stdlib/blas/base/shared.h" @@ -78,15 +78,15 @@ double API_SUFFIX(stdlib_strided_dnannsumkbn2_ndarray)( const CBLAS_INT N, const if ( N <= 0 ) { return sum; } + ix = offsetX; if ( strideX == 0 ) { - if ( stdlib_base_is_nan( X[ 0 ] ) ) { + if ( stdlib_base_is_nan( X[ ix ] ) ) { return sum; } - sum = X[ 0 ] * N; + sum = X[ ix ] * N; *n += N; return sum; } - ix = offsetX; 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++ ) { From ff42a4e2b8a8c40c595febccdcbba8e3e64b4d17 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 20 Oct 2024 16:57:00 +0000 Subject: [PATCH 8/9] fix: apply code review suggestion --- .../@stdlib/blas/ext/base/dnannsumkbn2/README.md | 8 ++++---- .../@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt | 8 ++++---- .../@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js | 4 ++-- .../blas/ext/base/dnannsumkbn2/lib/ndarray.native.js | 4 ++-- .../@stdlib/blas/ext/base/dnannsumkbn2/src/main.c | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md index 64f51c30b32d..39cf9ee58842 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md @@ -54,9 +54,9 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input [`Float64Array`][@stdlib/array/float64]. -- **strideX**: index increment for `x`. +- **strideX**: stride length for `x`. - **out**: output [`Float64Array`][@stdlib/array/float64] whose first element is the sum and whose second element is the number of non-NaN elements. -- **strideOut**: index increment for `out`. +- **strideOut**: stride length for `out`. The `N` and stride parameters determine which elements are accessed at runtime. For example, to compute the sum of every other element in `x`, @@ -206,7 +206,7 @@ The function accepts the following arguments: - **N**: `[in] CBLAS_INT` number of indexed elements. - **X**: `[in] double*` input array. -- **strideX**: `[in] CBLAS_INT` index increment for `X`. +- **strideX**: `[in] CBLAS_INT` stride length for `X`. - **n**: `[out] CBLAS_INT*` number of non-NaN elements. ```c @@ -229,7 +229,7 @@ The function accepts the following arguments: - **N**: `[in] CBLAS_INT` number of indexed elements. - **X**: `[in] double*` input array. -- **strideX**: `[in] CBLAS_INT` index increment for `X`. +- **strideX**: `[in] CBLAS_INT` stride length for `X`. - **offsetX**: `[in] CBLAS_INT` starting index for `X`. - **n**: `[out] CBLAS_INT*` number of non-NaN elements. diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt index 0674de9936ec..17a33f911082 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt @@ -21,13 +21,13 @@ Input array. strideX: integer - Index increment for `x`. + Stride length for `x`. out: Float64Array Output array. strideOut: integer - Index increment for `out`. + Stride length for `out`. Returns ------- @@ -75,7 +75,7 @@ Input array. strideX: integer - Index increment for `x`. + Stride length for `x`. offsetX: integer Starting index for `x`. @@ -84,7 +84,7 @@ Output array. strideOut: integer - Index increment for `out`. + Stride length for `out`. offsetOut: integer Starting index for `out`. diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js index c8cc16b59842..faf74d10e345 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.js @@ -39,10 +39,10 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} strideX - `x` index increment +* @param {integer} strideX - `x` stride length * @param {NonNegativeInteger} offsetX - `x` starting index * @param {Float64Array} out - output array -* @param {integer} strideOut - `out` index increment +* @param {integer} strideOut - `out` stride length * @param {NonNegativeInteger} offsetOut - `out` starting index * @returns {Float64Array} output array * diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.native.js index 958b68d8a63c..f83cc85d4537 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/lib/ndarray.native.js @@ -30,10 +30,10 @@ var addon = require( './../src/addon.node' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} strideX - `x` index increment +* @param {integer} strideX - `x` stride length * @param {NonNegativeInteger} offsetX - `x` starting index * @param {Float64Array} out - output array -* @param {integer} strideOut - `out` index increment +* @param {integer} strideOut - `out` stride length * @param {NonNegativeInteger} offsetOut - `out` starting index * @returns {Float64Array} output array * diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c index 42e1d75bfd94..55ce38757b7f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c @@ -57,7 +57,7 @@ double API_SUFFIX(stdlib_strided_dnannsumkbn2)( const CBLAS_INT N, const double * * @param N number of indexed elements * @param X input array -* @param strideX index increment +* @param strideX stride length * @param offsetX starting index * @param n number of non-NaN elements * @return output value From fe5ee9b39ed9d373c33569c819a5273622f749c2 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 27 Oct 2024 15:00:24 +0500 Subject: [PATCH 9/9] chore: apply review suggestion --- .../blas/ext/base/dnannsumkbn2/README.md | 2 +- .../base/dnannsumkbn2/benchmark/benchmark.js | 22 ++++++++++++------- .../benchmark/benchmark.native.js | 22 ++++++++++++------- .../benchmark/benchmark.ndarray.js | 22 ++++++++++++------- .../benchmark/benchmark.ndarray.native.js | 22 ++++++++++++------- .../blas/ext/base/dnannsumkbn2/docs/repl.txt | 4 ++-- .../blas/ext/base/dnannsumkbn2/src/main.c | 3 +-- 7 files changed, 60 insertions(+), 37 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md index 39cf9ee58842..90e302f0c23a 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/README.md @@ -106,7 +106,7 @@ The function has the following additional parameters: - **offsetX**: starting index for `x`. - **offsetOut**: starting index for `out`. -While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. 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, offset parameters support indexing semantics based on starting indices. For example, to calculate the sum of every other element starting from the second element: ```javascript var Float64Array = require( '@stdlib/array/float64' ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.js index bfc0715413aa..89548c155c7c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.js @@ -33,6 +33,19 @@ var dnannsumkbn2 = require( './../lib/dnannsumkbn2.js' ); // FUNCTIONS // +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +function rand() { + if ( bernoulli( 0.7 ) > 0 ) { + return discreteUniform( -10.0, 10.0 ); + } + return NaN; +} + /** * Creates a benchmark function. * @@ -44,14 +57,7 @@ function createBenchmark( len ) { var out; var x; - function clbk() { - if ( bernoulli( 0.7 ) > 0 ) { - return discreteUniform( -10, 10 ); - } - return NaN; - } - - x = filledarrayBy( len, 'float64', clbk ); + x = filledarrayBy( len, 'float64', rand ); out = new Float64Array( 2 ); return benchmark; diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.native.js index e2fd7c73834c..8a41c63662c1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.native.js @@ -42,6 +42,19 @@ var opts = { // FUNCTIONS // +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +function rand() { + if ( bernoulli( 0.8 ) > 0 ) { + return discreteUniform( -10.0, 10.0 ); + } + return NaN; +} + /** * Creates a benchmark function. * @@ -53,14 +66,7 @@ function createBenchmark( len ) { var out; var x; - function clbk() { - if ( bernoulli( 0.7 ) > 0 ) { - return discreteUniform( -10, 10 ); - } - return NaN; - } - - x = filledarrayBy( len, 'float64', clbk ); + x = filledarrayBy( len, 'float64', rand ); out = new Float64Array( 2 ); return benchmark; diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.ndarray.js index 7b051c1516e1..5f9768a46876 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.ndarray.js @@ -33,6 +33,19 @@ var dnannsumkbn2 = require( './../lib/ndarray.js' ); // FUNCTIONS // +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +function rand() { + if ( bernoulli( 0.8 ) > 0 ) { + return discreteUniform( -10.0, 10.0 ); + } + return NaN; +} + /** * Creates a benchmark function. * @@ -44,14 +57,7 @@ function createBenchmark( len ) { var out; var x; - function clbk() { - if ( bernoulli( 0.7 ) > 0 ) { - return discreteUniform( -10, 10 ); - } - return NaN; - } - - x = filledarrayBy( len, 'float64', clbk ); + x = filledarrayBy( len, 'float64', rand ); out = new Float64Array( 2 ); return benchmark; diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.ndarray.native.js index 937d22344e6f..495a1a81c825 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/benchmark/benchmark.ndarray.native.js @@ -42,6 +42,19 @@ var opts = { // FUNCTIONS // +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +function rand() { + if ( bernoulli( 0.8 ) > 0 ) { + return discreteUniform( -10.0, 10.0 ); + } + return NaN; +} + /** * Creates a benchmark function. * @@ -53,14 +66,7 @@ function createBenchmark( len ) { var out; var x; - function clbk() { - if ( bernoulli( 0.7 ) > 0 ) { - return discreteUniform( -10, 10 ); - } - return NaN; - } - - x = filledarrayBy( len, 'float64', clbk ); + x = filledarrayBy( len, 'float64', rand ); out = new Float64Array( 2 ); return benchmark; diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt index 17a33f911082..88f3ccac8a93 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/docs/repl.txt @@ -43,7 +43,7 @@ > {{alias}}( x.length, x, 1, out, 1 ) [ 1.0, 3 ] - // Using `N` and `stride` parameters: + // 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 ] ); > out = new {{alias:@stdlib/array/float64}}( 2 ); > {{alias}}( 4, x, 2, out, 1 ) @@ -100,7 +100,7 @@ // Standard Usage: > var x = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, NaN, 2.0 ] ); > var out = new {{alias:@stdlib/array/float64}}( 2 ); - > {{alias}}.ndarray( x.length, x, 1, 0, out, 1, 0 ) + > {{alias}}.ndarray( 4, x, 1, 0, out, 1, 0 ) [ 1.0, 3 ] // Using offset parameter: diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c index 55ce38757b7f..1b33595e5afd 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/src/main.c @@ -83,9 +83,8 @@ double API_SUFFIX(stdlib_strided_dnannsumkbn2_ndarray)( const CBLAS_INT N, const if ( stdlib_base_is_nan( X[ ix ] ) ) { return sum; } - sum = X[ ix ] * N; *n += N; - return sum; + return X[ ix ] * N; } ccs = 0.0; // second order correction term for lost lower order bits cs = 0.0; // first order correction term for lost low order bits