diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/README.md b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/README.md index bef6ea106656..dbb67f2c95f9 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/README.md @@ -36,7 +36,7 @@ limitations under the License. var dsumkbn = require( '@stdlib/blas/ext/base/dsumkbn' ); ``` -#### dsumkbn( N, x, stride ) +#### dsumkbn( N, x, strideX ) Computes the sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm. @@ -44,9 +44,8 @@ Computes the sum of double-precision floating-point strided array elements using var Float64Array = require( '@stdlib/array/float64' ); var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); -var N = x.length; -var v = dsumkbn( N, x, 1 ); +var v = dsumkbn( x.length, x, 1 ); // returns 1.0 ``` @@ -54,9 +53,9 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input [`Float64Array`][@stdlib/array/float64]. -- **stride**: index increment for `x`. +- **strideX**: stride length for `x`. -The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element in the strided array, +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element: ```javascript var Float64Array = require( '@stdlib/array/float64' ); @@ -81,7 +80,7 @@ var v = dsumkbn( 4, x1, 2 ); // returns 5.0 ``` -#### dsumkbn.ndarray( N, x, stride, offset ) +#### dsumkbn.ndarray( N, x, strideX, offsetX ) Computes the sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm and alternative indexing semantics. @@ -96,9 +95,9 @@ var v = dsumkbn.ndarray( 3, 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 the strided array 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' ); @@ -130,11 +129,12 @@ var v = dsumkbn.ndarray( 4, x, 2, 1 ); ```javascript -var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var dsumkbn = require( '@stdlib/blas/ext/base/dsumkbn' ); -var x = filledarrayBy( 10, 'float64', discreteUniform( 0, 100 ) ); +var x = discreteUniform( 10.0, -100, 100, { + 'dtype': 'float64' +}); console.log( x ); var v = dsumkbn( x.length, x, 1 ); @@ -145,8 +145,123 @@ console.log( v ); + + * * * +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/dsumkbn.h" +``` + +#### stdlib_strided_dsumkbn( N, \*X, strideX ) + +Computes the sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm. + +```c +const double x[] = { 1.0, 2.0, 3.0, 4.0 }; + +double v = stdlib_strided_dsumkbn( 4, x, 1 ); +// returns 10.0 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **X**: `[in] double*` input array. +- **strideX**: `[in] CBLAS_INT` stride length for `X`. + +```c +double stdlib_strided_dsumkbn( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ); +``` + +#### stdlib_strided_dsumkbn_ndarray( N, \*X, strideX, offsetX ) + +Computes the sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm and alternative indexing semantics. + +```c +const double x[] = { 1.0, 2.0, 3.0, 4.0 }; + +double v = stdlib_strided_dsumkbn_ndarray( 4, x, 1, 0 ); +// returns 10.0 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **X**: `[in] double*` input array. +- **strideX**: `[in] CBLAS_INT` stride length for `X`. +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. + +```c +double stdlib_strided_dsumkbn_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/dsumkbn.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 }; + + // Specify the number of elements: + const int N = 5; + + // Specify the stride length: + const int strideX = 2; + + // Compute the sum: + double v = stdlib_strided_dsumkbn( N, x, strideX ); + + // Print the result: + printf( "sum: %lf\n", v ); +} +``` + +
+ + + +
+ + +
## References diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.js index bb60ff5bea1c..e7403f20dbf9 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.js @@ -21,17 +21,18 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var uniform = require( '@stdlib/random/base/uniform' ).factory; +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); -var filledarrayBy = require( '@stdlib/array/filled-by' ); var pkg = require( './../package.json' ).name; var dsumkbn = require( './../lib/dsumkbn.js' ); // VARIABLES // -var rand = uniform( -10.0, 10.0 ); +var options = { + 'dtype': 'float64' +}; // FUNCTIONS // @@ -44,7 +45,7 @@ var rand = uniform( -10.0, 10.0 ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float64', rand ); + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.native.js index 6638a0088a06..84410f038584 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.native.js @@ -22,10 +22,9 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var uniform = require( '@stdlib/random/base/uniform' ).factory; +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); -var filledarrayBy = require( '@stdlib/array/filled-by' ); var tryRequire = require( '@stdlib/utils/try-require' ); var pkg = require( './../package.json' ).name; @@ -36,7 +35,9 @@ var dsumkbn = tryRequire( resolve( __dirname, './../lib/dsumkbn.native.js' ) ); var opts = { 'skip': ( dsumkbn instanceof Error ) }; -var rand = uniform( -10.0, 10.0 ); +var options = { + 'dtype': 'float64' +}; // FUNCTIONS // @@ -49,7 +50,7 @@ var rand = uniform( -10.0, 10.0 ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float64', rand ); + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.ndarray.js index 1bcd072999e5..2c255884475c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.ndarray.js @@ -21,17 +21,18 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var uniform = require( '@stdlib/random/base/uniform' ).factory; +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); -var filledarrayBy = require( '@stdlib/array/filled-by' ); var pkg = require( './../package.json' ).name; var dsumkbn = require( './../lib/ndarray.js' ); // VARIABLES // -var rand = uniform( -10.0, 10.0 ); +var options = { + 'dtype': 'float64' +}; // FUNCTIONS // @@ -44,7 +45,7 @@ var rand = uniform( -10.0, 10.0 ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float64', rand ); + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.ndarray.native.js index 7be25c303c6f..857c5e281a61 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/benchmark.ndarray.native.js @@ -22,10 +22,9 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var uniform = require( '@stdlib/random/base/uniform' ).factory; +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); -var filledarrayBy = require( '@stdlib/array/filled-by' ); var tryRequire = require( '@stdlib/utils/try-require' ); var pkg = require( './../package.json' ).name; @@ -36,7 +35,9 @@ var dsumkbn = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ); var opts = { 'skip': ( dsumkbn instanceof Error ) }; -var rand = uniform( -10.0, 10.0 ); +var options = { + 'dtype': 'float64' +}; // FUNCTIONS // @@ -49,7 +50,7 @@ var rand = uniform( -10.0, 10.0 ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float64', rand ); + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/c/benchmark.length.c index 5998610e0822..fe711d1a42ca 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/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; @@ -107,6 +107,7 @@ static double benchmark( int iterations, int len ) { v = 0.0; t = tic(); for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar v = stdlib_strided_dsumkbn( len, x, 1 ); if ( v != v ) { printf( "should not return NaN\n" ); @@ -120,6 +121,40 @@ 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++ ) { + x[ i ] = ( rand_double() * 20000.0 ) - 10000.0; + } + v = 0.0; + t = tic(); + for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar + v = stdlib_strided_dsumkbn_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. */ @@ -142,7 +177,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/dsumkbn/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/docs/repl.txt index b5c8ed97899e..c3e2ccab4e7d 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/docs/repl.txt @@ -1,5 +1,5 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Computes the sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm. @@ -19,8 +19,8 @@ x: Float64Array Input array. - stride: integer - Index increment. + strideX: integer + Stride length. 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 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 - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/docs/types/index.d.ts index b85e383077e6..20f7ada1a15c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/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 = dsumkbn( 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 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 = dsumkbn.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/dsumkbn/examples/c/example.c b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/examples/c/example.c index 07389de4767b..c2608f48435f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/examples/c/example.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/examples/c/example.c @@ -17,7 +17,6 @@ */ #include "stdlib/blas/ext/base/dsumkbn.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 }; // Specify the number of elements: - const int64_t N = 4; + const int N = 5; // Specify the stride length: - const int64_t stride = 2; + const int strideX = 2; // Compute the sum: - double v = stdlib_strided_dsumkbn( N, x, stride ); + double v = stdlib_strided_dsumkbn( N, x, strideX ); // Print the result: printf( "sum: %lf\n", v ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/examples/index.js index a2856bbd3e81..303d03a1fe61 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/examples/index.js @@ -18,11 +18,12 @@ 'use strict'; -var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var dsumkbn = require( './../lib' ); -var x = filledarrayBy( 10, 'float64', discreteUniform( 0, 100 ) ); +var x = discreteUniform( 10.0, -100, 100, { + 'dtype': 'float64' +}); console.log( x ); var v = dsumkbn( x.length, x, 1 ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/include/stdlib/blas/ext/base/dsumkbn.h b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/include/stdlib/blas/ext/base/dsumkbn.h index 248bf0067274..845d21507cbe 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/include/stdlib/blas/ext/base/dsumkbn.h +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/include/stdlib/blas/ext/base/dsumkbn.h @@ -19,7 +19,7 @@ #ifndef STDLIB_BLAS_EXT_BASE_DSUMKBN_H #define STDLIB_BLAS_EXT_BASE_DSUMKBN_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 using an improved Kahan–Babuška algorithm. */ -double stdlib_strided_dsumkbn( const int64_t N, const double *X, const int64_t stride ); +double API_SUFFIX(stdlib_strided_dsumkbn)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ); + +/** +* Computes the sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm and alternative indexing semantics. +*/ +double API_SUFFIX(stdlib_strided_dsumkbn_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/dsumkbn/lib/dsumkbn.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/dsumkbn.js index ed7dcf956ccc..d44aaa889bc3 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/dsumkbn.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/dsumkbn.js @@ -20,7 +20,8 @@ // MODULES // -var abs = require( '@stdlib/math/base/special/abs' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -38,51 +39,19 @@ 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 * var Float64Array = require( '@stdlib/array/float64' ); * * var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = dsumkbn( N, x, 1 ); +* var v = dsumkbn( x.length, x, 1 ); * // returns 1.0 */ -function dsumkbn( 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 ) { - 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 ]; - 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 dsumkbn( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/dsumkbn.native.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/dsumkbn.native.js index e934be9e5bf0..321b0c71f5aa 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/dsumkbn.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/dsumkbn.native.js @@ -30,20 +30,19 @@ 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 * var Float64Array = require( '@stdlib/array/float64' ); * * var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = dsumkbn( N, x, 1 ); +* var v = dsumkbn( x.length, x, 1 ); * // returns 1.0 */ -function dsumkbn( N, x, stride ) { - return addon( N, x, stride ); +function dsumkbn( N, x, strideX ) { + return addon( N, x, strideX ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/index.js index 01c3e958a702..fecaed4dab23 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/index.js @@ -28,20 +28,17 @@ * var dsumkbn = require( '@stdlib/blas/ext/base/dsumkbn' ); * * var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = dsumkbn( N, x, 1 ); +* var v = dsumkbn( x.length, x, 1 ); * // returns 1.0 * * @example * var Float64Array = require( '@stdlib/array/float64' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * var dsumkbn = require( '@stdlib/blas/ext/base/dsumkbn' ); * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -* var N = floor( x.length / 2 ); * -* var v = dsumkbn.ndarray( N, x, 2, 1 ); +* var v = dsumkbn.ndarray( 4, x, 2, 1 ); * // returns 5.0 */ diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/ndarray.js index 6cc55a374b04..087896426bcc 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/ndarray.js @@ -38,21 +38,19 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} sum * * @example * var Float64Array = require( '@stdlib/array/float64' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -* var N = floor( x.length / 2 ); * -* var v = dsumkbn( N, x, 2, 1 ); +* var v = dsumkbn( 4, x, 2, 1 ); * // returns 5.0 */ -function dsumkbn( N, x, stride, offset ) { +function dsumkbn( N, x, strideX, offsetX ) { var sum; var ix; var v; @@ -63,10 +61,10 @@ function dsumkbn( N, x, stride, offset ) { if ( N <= 0 ) { return 0.0; } - if ( N === 1 || stride === 0 ) { - return x[ offset ]; + ix = offsetX; + if ( strideX === 0 ) { + return N * x[ ix ]; } - ix = offset; sum = 0.0; c = 0.0; for ( i = 0; i < N; i++ ) { @@ -78,7 +76,7 @@ function dsumkbn( N, x, stride, offset ) { c += (v-t) + sum; } sum = t; - ix += stride; + ix += strideX; } return sum + c; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/ndarray.native.js index 8886a19e6c07..314447152ee7 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/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( './dsumkbn.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // @@ -32,25 +30,20 @@ var addon = require( './dsumkbn.native.js' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} sum * * @example * var Float64Array = require( '@stdlib/array/float64' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -* var N = floor( x.length / 2 ); * -* var v = dsumkbn( N, x, 2, 1 ); +* var v = dsumkbn( 4, x, 2, 1 ); * // returns 5.0 */ -function dsumkbn( N, x, stride, offset ) { - var view; - offset = minViewBufferIndex( N, stride, offset ); - view = offsetView( x, offset ); - return addon( N, view, stride ); +function dsumkbn( N, x, strideX, offsetX ) { + return addon.ndarray( N, x, strideX, offsetX ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/manifest.json b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/manifest.json index 8cf6a46b89e7..95e0331dd32d 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/manifest.json +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/manifest.json @@ -25,52 +25,58 @@ } ], "confs": [ - { + { "task": "build", "src": [ - "./src/dsumkbn.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/napi/argv-strided-float64array", + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/base/shared", + "@stdlib/math/base/special/abs", + "@stdlib/napi/create-double" ] }, - { + { "task": "benchmark", "src": [ - "./src/dsumkbn.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], - "dependencies": [] + "dependencies": [ + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/base/shared", + "@stdlib/math/base/special/abs" + ] }, - { + { "task": "examples", "src": [ - "./src/dsumkbn.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], - "dependencies": [] + "dependencies": [ + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/base/shared", + "@stdlib/math/base/special/abs" + ] } ] } diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/src/addon.c b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/src/addon.c index 9e57a2e69963..2554c329ac15 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/src/addon.c +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/src/addon.c @@ -17,10 +17,12 @@ */ #include "stdlib/blas/ext/base/dsumkbn.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 /** @@ -33,14 +35,27 @@ 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_dsumkbn( N, X, stride ), &v ); - assert( status == napi_ok ); + 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_dsumkbn)( N, X, strideX ), v ); + return v; +} +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon_method( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 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_dsumkbn_ndarray)( N, X, strideX, offsetX ), v ); return v; } -STDLIB_NAPI_MODULE_EXPORT_FCN( addon ); \ No newline at end of file +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ) diff --git a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/src/dsumkbn.c b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/src/dsumkbn.c deleted file mode 100644 index 6a947ae10d11..000000000000 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/src/dsumkbn.c +++ /dev/null @@ -1,72 +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/dsumkbn.h" -#include -#include - -/** -* Computes the sum of double-precision floating-point strided array elements 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_dsumkbn( 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 ) { - 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 ]; - 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/dsumkbn/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/src/main.c new file mode 100644 index 000000000000..badb8b4db3a1 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/src/main.c @@ -0,0 +1,91 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* 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/dsumkbn.h" +#include "stdlib/strided/base/stride2offset.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 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_dsumkbn)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ) { + CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + return API_SUFFIX(stdlib_strided_dsumkbn_ndarray)( N, X, strideX, ox ); +} + +/** +* Computes the sum of double-precision floating-point strided array elements 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 stride length +* @param offsetX starting index +* @return output value +*/ +double API_SUFFIX(stdlib_strided_dsumkbn_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { + CBLAS_INT ix; + CBLAS_INT i; + double sum; + double v; + double t; + double c; + + if ( N <= 0 ) { + return 0.0; + } + ix = offsetX; + if ( strideX == 0 ) { + return N * X[ ix ]; + } + sum = 0.0; + c = 0.0; + for ( i = 0; i < N; i++ ) { + v = X[ ix ]; + 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/dsumkbn/test/test.dsumkbn.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.dsumkbn.js index 0d52849e2ac8..ab410abf6dc4 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.dsumkbn.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.dsumkbn.js @@ -135,14 +135,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 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 = dsumkbn( 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/dsumkbn/test/test.dsumkbn.native.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.dsumkbn.native.js index 9c0e55556897..4b4b888ad50b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.dsumkbn.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.dsumkbn.native.js @@ -226,14 +226,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 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 = dsumkbn( 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/dsumkbn/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.ndarray.js index 6f323d3db6b1..da0c43eb0fa1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.ndarray.js @@ -135,14 +135,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 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 = dsumkbn( 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/dsumkbn/test/test.ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.ndarray.native.js index 288b341f8c98..c17b2cdcd49e 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/dsumkbn/test/test.ndarray.native.js @@ -144,14 +144,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 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 = dsumkbn( x.length, x, 0, 0 ); - t.strictEqual( v, 1.0, 'returns expected value' ); + t.strictEqual( v, 5.0, 'returns expected value' ); t.end(); });