From fe8f4d4e5b3ff5c590b6ff013e3ed433cb99dde6 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 9 Oct 2024 11:43:13 +0500 Subject: [PATCH 1/7] feat: add C ndarray API and refactor --- .../blas/ext/base/dnansumkbn/README.md | 125 +++++++++++++++++- .../dnansumkbn/benchmark/c/benchmark.length.c | 52 +++++++- .../blas/ext/base/dnansumkbn/docs/repl.txt | 14 +- .../ext/base/dnansumkbn/docs/types/index.d.ts | 12 +- .../ext/base/dnansumkbn/examples/c/example.c | 7 +- .../include/stdlib/blas/ext/base/dnansumkbn.h | 9 +- .../ext/base/dnansumkbn/lib/dnansumkbn.js | 46 +------ .../base/dnansumkbn/lib/dnansumkbn.native.js | 6 +- .../blas/ext/base/dnansumkbn/lib/ndarray.js | 23 ++-- .../ext/base/dnansumkbn/lib/ndarray.native.js | 18 +-- .../blas/ext/base/dnansumkbn/manifest.json | 36 ++--- .../blas/ext/base/dnansumkbn/src/addon.c | 36 +++-- .../blas/ext/base/dnansumkbn/src/dnansumkbn.c | 78 ----------- .../blas/ext/base/dnansumkbn/src/main.c | 99 ++++++++++++++ .../base/dnansumkbn/test/test.dnansumkbn.js | 4 +- .../dnansumkbn/test/test.dnansumkbn.native.js | 4 +- .../ext/base/dnansumkbn/test/test.ndarray.js | 4 +- .../dnansumkbn/test/test.ndarray.native.js | 4 +- 18 files changed, 371 insertions(+), 206 deletions(-) delete mode 100644 lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/dnansumkbn.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md index 0a18eb319242..cc6ae3b7c489 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md @@ -36,7 +36,7 @@ limitations under the License. var dnansumkbn = require( '@stdlib/blas/ext/base/dnansumkbn' ); ``` -#### dnansumkbn( N, x, stride ) +#### dnansumkbn( N, x, strideX ) Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved 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**: index increment 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 = dnansumkbn( 4, x1, 2 ); // returns 5.0 ``` -#### dnansumkbn.ndarray( N, x, stride, offset ) +#### dnansumkbn.ndarray( N, x, strideX, offsetX ) Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm and alternative indexing semantics. @@ -95,9 +95,9 @@ var v = dnansumkbn.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' ); @@ -152,8 +152,123 @@ console.log( v ); + + * * * +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/dnansumkbn.h" +``` + +#### stdlib_strided_dnansumkbn( N, \*X, strideX ) + +Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm. + +```c +const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 }; + +double v = stdlib_strided_dnansumkbn( 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` index increment for `X`. + +```c +double stdlib_strided_dnansumkbn( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ); +``` + +#### stdlib_strided_dnansumkbn_ndarray( N, \*X, strideX, offsetX ) + +Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved 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_dnansumkbn_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` index increment for `X`. +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. + +```c +double stdlib_strided_dnansumkbn_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/dnansumkbn.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_dnansumkbn( N, x, strideX ); + + // Print the result: + printf( "sum: %lf\n", v ); +} +``` + +
+ + + +
+ + +
## References diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/c/benchmark.length.c index f20739042d5a..e1c58649eb81 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/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_dnansumkbn_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/dnansumkbn/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt index a396def0a9c4..52c571ceecc3 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved 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 @@ -19,7 +19,7 @@ x: Float64Array Input array. - stride: integer + strideX: integer Index increment. Returns @@ -46,13 +46,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 an improved 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 @@ -63,10 +63,10 @@ x: Float64Array Input array. - stride: integer + strideX: integer Index increment. - offset: integer + offsetX: integer Starting index. Returns diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/types/index.d.ts index 5763eb66a357..af4932a4e128 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/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 = dnansumkbn( 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 an improved 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 = dnansumkbn.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/dnansumkbn/examples/c/example.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/examples/c/example.c index b94fa418f033..7eb2b084577d 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/examples/c/example.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/examples/c/example.c @@ -17,7 +17,6 @@ */ #include "stdlib/blas/ext/base/dnansumkbn.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_dnansumkbn( N, x, stride ); + double v = stdlib_strided_dnansumkbn( N, x, strideX ); // Print the result: printf( "sum: %lf\n", v ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/include/stdlib/blas/ext/base/dnansumkbn.h b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/include/stdlib/blas/ext/base/dnansumkbn.h index 2f243eda59fb..54aa8b84b3fa 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/include/stdlib/blas/ext/base/dnansumkbn.h +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/include/stdlib/blas/ext/base/dnansumkbn.h @@ -19,7 +19,7 @@ #ifndef STDLIB_BLAS_EXT_BASE_DNANSUMKBN_H #define STDLIB_BLAS_EXT_BASE_DNANSUMKBN_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 an improved Kahan–Babuška algorithm. */ -double stdlib_strided_dnansumkbn( const int64_t N, const double *X, const int64_t stride ); +double API_SUFFIX(stdlib_strided_dnansumkbn)( 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 an improved Kahan–Babuška algorithm and alternative indexing semantics. +*/ +double API_SUFFIX(stdlib_strided_dnansumkbn_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/dnansumkbn/lib/dnansumkbn.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.js index 9f1d7660cca5..4b74fd7fa556 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.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,44 +51,8 @@ var abs = require( '@stdlib/math/base/special/abs' ); * var v = dnansumkbn( N, x, 1 ); * // returns 1.0 */ -function dnansumkbn( N, x, stride ) { - var sum; - var ix; - 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; - c = 0.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; - } - ix += stride; - } - return sum + c; +function dnansumkbn( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.native.js index 2ed47f9e56d2..671741e16ab4 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.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 = dnansumkbn( N, x, 1 ); * // returns 1.0 */ -function dnansumkbn( N, x, stride ) { - return addon( N, x, stride ); +function dnansumkbn( N, x, strideX ) { + return addon( N, x, strideX ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js index 4d3efb663492..93e69e151c89 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/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 - index increment +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} sum * * @example @@ -51,7 +51,7 @@ var abs = require( '@stdlib/math/base/special/abs' ); * var v = dnansumkbn( 5, x, 2, 1 ); * // returns 5.0 */ -function dnansumkbn( N, x, stride, offset ) { +function dnansumkbn( N, x, strideX, offsetX ) { var sum; var ix; var v; @@ -59,18 +59,19 @@ function dnansumkbn( N, x, stride, offset ) { var c; var i; + ix = offsetX; + sum = 0.0; + c = 0.0; if ( N <= 0 ) { return 0.0; } - if ( N === 1 || stride === 0 ) { - if ( isnan( x[ offset ] ) ) { - return 0.0; + if ( strideX === 0 ) { + if ( isnan( x[ ix ] ) ) { + return sum; } - return x[ offset ]; + sum = x[ ix ] * N; + return sum; } - ix = offset; - sum = 0.0; - c = 0.0; for ( i = 0; i < N; i++ ) { v = x[ ix ]; if ( isnan( v ) === false ) { @@ -82,7 +83,7 @@ function dnansumkbn( N, x, stride, offset ) { } sum = t; } - ix += stride; + ix += strideX; } return sum + c; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.native.js index 2ac51742d3df..da54ef5ed783 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/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( './dnansumkbn.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // @@ -32,8 +30,8 @@ var addon = require( './dnansumkbn.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 - index increment +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} sum * * @example @@ -44,14 +42,8 @@ var addon = require( './dnansumkbn.native.js' ); * var v = dnansumkbn( 5, x, 2, 1 ); * // returns 5.0 */ -function dnansumkbn( N, x, stride, offset ) { - var view; - - offset = minViewBufferIndex( N, stride, offset ); - - view = offsetView( x, offset ); - - return addon( N, view, stride ); +function dnansumkbn( N, x, strideX, offsetX ) { + return addon.ndarray( N, x, strideX, offsetX ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/manifest.json b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/manifest.json index 095e73f25f3b..6932104e1fa1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/manifest.json +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/manifest.json @@ -28,53 +28,59 @@ { "task": "build", "src": [ - "./src/dnansumkbn.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/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/napi/create-double" ] }, { "task": "benchmark", "src": [ - "./src/dnansumkbn.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/dnansumkbn.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/dnansumkbn/src/addon.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/addon.c index f5ada0bfd11d..0297f1f38ccd 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/addon.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/addon.c @@ -17,12 +17,13 @@ */ #include "stdlib/blas/ext/base/dnansumkbn.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_dnansumkbn( 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_dnansumkbn)( 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_dnansumkbn_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/dnansumkbn/src/dnansumkbn.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/dnansumkbn.c deleted file mode 100644 index 9821d1fb6304..000000000000 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/dnansumkbn.c +++ /dev/null @@ -1,78 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include "stdlib/blas/ext/base/dnansumkbn.h" -#include "stdlib/math/base/assert/is_nan.h" -#include -#include - -/** -* Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm. -* -* ## Method -* -* - This implementation uses an "improved Kahan–Babuška algorithm", as described by Neumaier (1974). -* -* ## References -* -* - Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106](https://doi.org/10.1002/zamm.19740540106). -* -* @param N number of indexed elements -* @param X input array -* @param stride stride length -* @return output value -*/ -double stdlib_strided_dnansumkbn( const int64_t N, const double *X, const int64_t stride ) { - double sum; - int64_t ix; - int64_t i; - double v; - double t; - double c; - - if ( N <= 0 ) { - return 0.0; - } - if ( N == 1 || stride == 0 ) { - if ( stdlib_base_is_nan( X[ 0 ] ) ) { - return 0.0; - } - return X[ 0 ]; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - sum = 0.0; - c = 0.0; - for ( i = 0; i < N; i++ ) { - v = X[ ix ]; - if ( !stdlib_base_is_nan( v ) ) { - t = sum + v; - if ( fabs( sum ) >= fabs( v ) ) { - c += (sum-t) + v; - } else { - c += (v-t) + sum; - } - sum = t; - } - ix += stride; - } - return sum + c; -} diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c new file mode 100644 index 000000000000..08e168ad38db --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/blas/ext/base/dnansumkbn.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" + + +/** +* Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm. +* +* ## Method +* +* - This implementation uses an "improved Kahan–Babuška algorithm", as described by Neumaier (1974). +* +* ## References +* +* - Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106](https://doi.org/10.1002/zamm.19740540106). +* +* @param N number of indexed elements +* @param X input array +* @param strideX stride length +* @return output value +*/ +double API_SUFFIX(stdlib_strided_dnansumkbn)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ) { + CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + return API_SUFFIX(stdlib_strided_dnansumkbn_ndarray)( N, X, strideX, ox ); +} + +/** +* Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm and alternative indexing semantics. +* +* ## Method +* +* - This implementation uses an "improved Kahan–Babuška algorithm", as described by Neumaier (1974). +* +* ## References +* +* - Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106](https://doi.org/10.1002/zamm.19740540106). +* +* @param N number of indexed elements +* @param X input array +* @param strideX index increment +* @param offsetX starting index +* @return output value +*/ +double API_SUFFIX(stdlib_strided_dnansumkbn_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { + double sum; + CBLAS_INT ix; + CBLAS_INT i; + double v; + double t; + double c; + + sum = 0.0; + c = 0.0; + ix = offsetX; + if ( N <= 0 ) { + return sum; + } + if ( strideX == 0 ) { + if ( stdlib_base_is_nan( X[ ix ] ) ) { + return sum; + } + sum = X[ ix ] * N; + return sum; + } + for ( i = 0; i < N; i++ ) { + v = X[ ix ]; + if ( !stdlib_base_is_nan( v ) ) { + t = sum + v; + if ( stdlib_base_abs( sum ) >= stdlib_base_abs( v ) ) { + c += (sum-t) + v; + } else { + c += (v-t) + sum; + } + sum = t; + } + ix += strideX; + } + return sum + c; +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.dnansumkbn.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.dnansumkbn.js index e538d9c967ce..787847da5a38 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.dnansumkbn.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.dnansumkbn.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 = dnansumkbn( 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/dnansumkbn/test/test.dnansumkbn.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.dnansumkbn.native.js index f2f3f5d1db25..a50c347c3510 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.dnansumkbn.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.dnansumkbn.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 = dnansumkbn( 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/dnansumkbn/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.ndarray.js index abda894269df..021dbcf0e93b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/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 = dnansumkbn( 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/dnansumkbn/test/test.ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.ndarray.native.js index ae940922013c..4bcafdbdb009 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/test/test.ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/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 = dnansumkbn( x.length, x, 0, 0 ); - t.strictEqual( v, 1.0, 'returns expected value' ); + t.strictEqual( v, 5.0, 'returns expected value' ); t.end(); }); From ff11b86ead31d35679c3503c0b3d3bb443eb5b0a Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Thu, 10 Oct 2024 09:40:26 +0000 Subject: [PATCH 2/7] fix: remove repeated deps --- lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/manifest.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/manifest.json b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/manifest.json index 6932104e1fa1..57e9af5c51e3 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/manifest.json +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/manifest.json @@ -44,8 +44,6 @@ "@stdlib/strided/base/stride2offset", "@stdlib/blas/base/shared", "@stdlib/math/base/special/abs", - "@stdlib/blas/base/shared", - "@stdlib/strided/base/stride2offset", "@stdlib/napi/create-double" ] }, From a22192cfcce8ddbd6472fafb4a5fd0170b07518c Mon Sep 17 00:00:00 2001 From: headlessNode Date: Fri, 11 Oct 2024 20:48:38 +0500 Subject: [PATCH 3/7] fix: apply code review suggestion --- .../@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js | 2 +- lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js index 93e69e151c89..7a4b88e3db83 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js @@ -59,12 +59,12 @@ function dnansumkbn( N, x, strideX, offsetX ) { var c; var i; - ix = offsetX; sum = 0.0; c = 0.0; if ( N <= 0 ) { return 0.0; } + ix = offsetX; if ( strideX === 0 ) { if ( isnan( x[ ix ] ) ) { return sum; diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c index 08e168ad38db..ee3c90270305 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c @@ -71,10 +71,10 @@ double API_SUFFIX(stdlib_strided_dnansumkbn_ndarray)( const CBLAS_INT N, const d sum = 0.0; c = 0.0; - ix = offsetX; if ( N <= 0 ) { return sum; } + ix = offsetX; if ( strideX == 0 ) { if ( stdlib_base_is_nan( X[ ix ] ) ) { return sum; From 704ecb8913d78b8792c0fd1deea27aa7ffc6f083 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 12 Oct 2024 01:08:55 +0500 Subject: [PATCH 4/7] fix: add code review suggestion --- .../@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js | 2 +- lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js index 7a4b88e3db83..7c91552d0fb9 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js @@ -60,7 +60,6 @@ function dnansumkbn( N, x, strideX, offsetX ) { var i; sum = 0.0; - c = 0.0; if ( N <= 0 ) { return 0.0; } @@ -72,6 +71,7 @@ function dnansumkbn( N, x, strideX, offsetX ) { sum = x[ ix ] * N; return sum; } + c = 0.0; for ( i = 0; i < N; i++ ) { v = x[ ix ]; if ( isnan( v ) === false ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c index ee3c90270305..4737bf169656 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c @@ -70,7 +70,6 @@ double API_SUFFIX(stdlib_strided_dnansumkbn_ndarray)( const CBLAS_INT N, const d double c; sum = 0.0; - c = 0.0; if ( N <= 0 ) { return sum; } @@ -82,6 +81,7 @@ double API_SUFFIX(stdlib_strided_dnansumkbn_ndarray)( const CBLAS_INT N, const d sum = X[ ix ] * N; return sum; } + c = 0.0; for ( i = 0; i < N; i++ ) { v = X[ ix ]; if ( !stdlib_base_is_nan( v ) ) { From 9e35394bda828673da54086c3429dc649f18ae8c Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 20 Oct 2024 22:38:33 +0500 Subject: [PATCH 5/7] fix: apply code review suggesetion --- lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md | 6 +++--- .../@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt | 4 ++-- .../@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js | 2 +- .../@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.native.js | 2 +- .../@stdlib/blas/ext/base/dnansumkbn/src/main.c | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md index cc6ae3b7c489..765f85ff3d21 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md @@ -53,7 +53,7 @@ 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`. 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`, @@ -193,7 +193,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`. ```c double stdlib_strided_dnansumkbn( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ); @@ -214,7 +214,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`. ```c diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt index 52c571ceecc3..e50a21f221b1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt @@ -20,7 +20,7 @@ Input array. strideX: integer - Index increment. + Stride length. Returns ------- @@ -64,7 +64,7 @@ Input array. strideX: integer - Index increment. + Stride length. offsetX: integer Starting index. diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js index 7c91552d0fb9..0b5bcd4fe0a4 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js @@ -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} strideX - index increment +* @param {integer} strideX - stride length * @param {NonNegativeInteger} offsetX - starting index * @returns {number} sum * diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.native.js index da54ef5ed783..13b19ae6edde 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.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} strideX - index increment +* @param {integer} strideX - stride length * @param {NonNegativeInteger} offsetX - starting index * @returns {number} sum * diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c index 4737bf169656..7cf035a5d995 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c @@ -57,7 +57,7 @@ double API_SUFFIX(stdlib_strided_dnansumkbn)( const CBLAS_INT N, const double *X * * @param N number of indexed elements * @param X input array -* @param strideX index increment +* @param strideX stride length * @param offsetX starting index * @return output value */ From f3426fa61f2216e41ded3468546f49641dee99ed Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 27 Oct 2024 14:07:35 +0500 Subject: [PATCH 6/7] chore: apply review changes --- .../blas/ext/base/dnansumkbn/README.md | 6 ++--- .../base/dnansumkbn/benchmark/benchmark.js | 22 ++++++++++++------- .../dnansumkbn/benchmark/benchmark.native.js | 22 ++++++++++++------- .../dnansumkbn/benchmark/benchmark.ndarray.js | 22 ++++++++++++------- .../benchmark/benchmark.ndarray.native.js | 22 ++++++++++++------- .../blas/ext/base/dnansumkbn/docs/repl.txt | 2 +- .../ext/base/dnansumkbn/lib/dnansumkbn.js | 3 +-- .../base/dnansumkbn/lib/dnansumkbn.native.js | 3 +-- .../blas/ext/base/dnansumkbn/lib/index.js | 2 +- .../blas/ext/base/dnansumkbn/lib/ndarray.js | 3 +-- .../blas/ext/base/dnansumkbn/src/main.c | 3 +-- 11 files changed, 65 insertions(+), 45 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md index 765f85ff3d21..72adc940aa20 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/README.md @@ -45,7 +45,7 @@ var Float64Array = require( '@stdlib/array/float64' ); var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); -var v = dnansumkbn( 4, x, 1 ); +var v = dnansumkbn( x.length, x, 1 ); // returns 1.0 ``` @@ -89,7 +89,7 @@ var Float64Array = require( '@stdlib/array/float64' ); var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); -var v = dnansumkbn.ndarray( 4, x, 1, 0 ); +var v = dnansumkbn.ndarray( x.length, x, 1, 0 ); // returns 1.0 ``` @@ -97,7 +97,7 @@ The function has the following additional parameters: - **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 element starting from the second element: ```javascript var Float64Array = require( '@stdlib/array/float64' ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.js index 6cd1f2cb7e34..a4b6e291787a 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.js @@ -32,6 +32,19 @@ var dnansumkbn = require( './../lib/dnansumkbn.js' ); // FUNCTIONS // +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +function rand() { + if ( bernoulli( 0.8 ) > 0 ) { + return uniform( -10.0, 10.0 ); + } + return NaN; +} + /** * Creates a benchmark function. * @@ -40,16 +53,9 @@ var dnansumkbn = require( './../lib/dnansumkbn.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float64', clbk ); + var x = filledarrayBy( len, 'float64', rand ); return benchmark; - function clbk() { - if ( bernoulli( 0.7 ) > 0 ) { - return uniform( -10.0, 10.0 ); - } - return NaN; - } - function benchmark( b ) { var v; var i; diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.native.js index add1384cbcfb..9c5c18dc85a0 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.native.js @@ -41,6 +41,19 @@ var opts = { // FUNCTIONS // +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +function rand() { + if ( bernoulli( 0.8 ) > 0 ) { + return uniform( -10.0, 10.0 ); + } + return NaN; +} + /** * Creates a benchmark function. * @@ -49,16 +62,9 @@ var opts = { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float64', clbk ); + var x = filledarrayBy( len, 'float64', rand ); return benchmark; - function clbk() { - if ( bernoulli( 0.7 ) > 0 ) { - return uniform( -10.0, 10.0 ); - } - return NaN; - } - function benchmark( b ) { var v; var i; diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.ndarray.js index 9ca0964241c3..bd56fa57ca68 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.ndarray.js @@ -32,6 +32,19 @@ var dnansumkbn = require( './../lib/ndarray.js' ); // FUNCTIONS // +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +function rand() { + if ( bernoulli( 0.8 ) > 0 ) { + return uniform( -10.0, 10.0 ); + } + return NaN; +} + /** * Creates a benchmark function. * @@ -40,16 +53,9 @@ var dnansumkbn = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float64', clbk ); + var x = filledarrayBy( len, 'float64', rand ); return benchmark; - function clbk() { - if ( bernoulli( 0.7 ) > 0 ) { - return uniform( -10.0, 10.0 ); - } - return NaN; - } - function benchmark( b ) { var v; var i; diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.ndarray.native.js index a94480e508c1..c759f63b47be 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/benchmark/benchmark.ndarray.native.js @@ -41,6 +41,19 @@ var opts = { // FUNCTIONS // +/** +* Returns a random number. +* +* @private +* @returns {number} random number +*/ +function rand() { + if ( bernoulli( 0.8 ) > 0 ) { + return uniform( -10.0, 10.0 ); + } + return NaN; +} + /** * Creates a benchmark function. * @@ -49,16 +62,9 @@ var opts = { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float64', clbk ); + var x = filledarrayBy( len, 'float64', rand ); return benchmark; - function clbk() { - if ( bernoulli( 0.7 ) > 0 ) { - return uniform( -10.0, 10.0 ); - } - return NaN; - } - function benchmark( b ) { var v; var i; diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt index e50a21f221b1..22d6062c4cef 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/docs/repl.txt @@ -34,7 +34,7 @@ > {{alias}}( x.length, x, 1 ) 1.0 - // 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 ] ); > {{alias}}( 4, x, 2 ) 1.0 diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.js index 4b74fd7fa556..ef7e98a9da8b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.js @@ -46,9 +46,8 @@ var ndarray = require( './ndarray.js' ); * var Float64Array = require( '@stdlib/array/float64' ); * * var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); -* var N = x.length; * -* var v = dnansumkbn( N, x, 1 ); +* var v = dnansumkbn( x.length, x, 1 ); * // returns 1.0 */ function dnansumkbn( N, x, strideX ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.native.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.native.js index 671741e16ab4..fec1b1cadc16 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/dnansumkbn.native.js @@ -37,9 +37,8 @@ var addon = require( './../src/addon.node' ); * var Float64Array = require( '@stdlib/array/float64' ); * * var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); -* var N = x.length; * -* var v = dnansumkbn( N, x, 1 ); +* var v = dnansumkbn( x.length, x, 1 ); * // returns 1.0 */ function dnansumkbn( N, x, strideX ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/index.js index a1039934f507..46a8eb478a5b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/index.js @@ -38,7 +38,7 @@ * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); * -* var v = dnansumkbn.ndarray( 5, x, 2, 1 ); +* var v = dnansumkbn.ndarray( x.length, x, 2, 1 ); * // returns 5.0 */ diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js index 0b5bcd4fe0a4..909dda4ca59d 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js @@ -68,8 +68,7 @@ function dnansumkbn( N, x, strideX, offsetX ) { if ( isnan( x[ ix ] ) ) { return sum; } - sum = x[ ix ] * N; - return sum; + return x[ ix ] * N; } c = 0.0; for ( i = 0; i < N; i++ ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c index 7cf035a5d995..30a7b08a1c41 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c @@ -78,8 +78,7 @@ double API_SUFFIX(stdlib_strided_dnansumkbn_ndarray)( const CBLAS_INT N, const d if ( stdlib_base_is_nan( X[ ix ] ) ) { return sum; } - sum = X[ ix ] * N; - return sum; + return X[ ix ] * N; } c = 0.0; for ( i = 0; i < N; i++ ) { From 1e1049e4953cbac83ca4994b62ae32f513a7e555 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 29 Oct 2024 17:01:09 -0700 Subject: [PATCH 7/7] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/index.js | 2 +- .../@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js | 2 +- lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/index.js index 46a8eb478a5b..a1039934f507 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/index.js @@ -38,7 +38,7 @@ * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); * -* var v = dnansumkbn.ndarray( x.length, x, 2, 1 ); +* var v = dnansumkbn.ndarray( 5, x, 2, 1 ); * // returns 5.0 */ diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js index 909dda4ca59d..bc54ab5d5f84 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/lib/ndarray.js @@ -61,7 +61,7 @@ function dnansumkbn( N, x, strideX, offsetX ) { sum = 0.0; if ( N <= 0 ) { - return 0.0; + return sum; } ix = offsetX; if ( strideX === 0 ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c index 30a7b08a1c41..1e2bc61399b8 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dnansumkbn/src/main.c @@ -22,7 +22,6 @@ #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 an improved Kahan–Babuška algorithm. *