diff --git a/CHANGELOG.md b/CHANGELOG.md index d3a21056d..4849cc127 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1357,6 +1357,7 @@ A total of 8 people contributed to this release. Thank you to the following cont
+- [`bb638df`](https://github.com/stdlib-js/stdlib/commit/bb638df38852d5ba3e341e9d607a0900a5d55723) - **refactor:** update `blas/ext/base/gnannsumkbn` to follow current project conventions [(#4631)](https://github.com/stdlib-js/stdlib/pull/4631) _(by Muhammad Haris)_ - [`0639c11`](https://github.com/stdlib-js/stdlib/commit/0639c11f0aac69a249c9710c9031e4f9cec14f62) - **refactor:** update `blas/ext/base/gsumkbn2` to follow current project conventions [(#4678)](https://github.com/stdlib-js/stdlib/pull/4678) _(by Muhammad Haris)_ - [`c36f0e7`](https://github.com/stdlib-js/stdlib/commit/c36f0e73214c70c686defbaf5adc2d3e86a6595b) - **refactor:** update `blas/ext/base/gsumors` to follow current project conventions [(#4679)](https://github.com/stdlib-js/stdlib/pull/4679) _(by Muhammad Haris)_ - [`dbade8e`](https://github.com/stdlib-js/stdlib/commit/dbade8ed06df1ad8cef3ecf6c1f1f729ad62a53c) - **refactor:** update `blas/ext/base/gsumpw` to follow current project conventions [(#4680)](https://github.com/stdlib-js/stdlib/pull/4680) _(by Muhammad Haris)_ diff --git a/ext/base/gnannsumkbn/README.md b/ext/base/gnannsumkbn/README.md index e8c146079..b1e612b3d 100644 --- a/ext/base/gnannsumkbn/README.md +++ b/ext/base/gnannsumkbn/README.md @@ -52,20 +52,17 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. -- **strideX**: index increment for `x`. +- **strideX**: stride length for `x`. - **out**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array] 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`, +The `N` and stride parameters determine which elements are accessed at runtime. For example, to compute the sum of every other element: ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - var x = [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ]; var out = [ 0.0, 0 ]; -var N = floor( x.length / 2 ); -var v = gnannsumkbn( N, x, 2, out, 1 ); +var v = gnannsumkbn( 4, x, 2, out, 1 ); // returns [ 5.0, 2 ] ``` @@ -75,7 +72,6 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); var x0 = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element @@ -83,9 +79,7 @@ var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd var out0 = new Float64Array( 4 ); var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); // start at 3rd element -var N = floor( x0.length / 2 ); - -var v = gnannsumkbn( N, x1, 2, out1, 1 ); +var v = gnannsumkbn( 4, x1, 2, out1, 1 ); // returns [ 5.0, 4 ] ``` @@ -106,16 +100,13 @@ 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, the 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 floor = require( '@stdlib/math/base/special/floor' ); - var x = [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ]; var out = [ 0.0, 0.0, 0.0, 0 ]; -var N = floor( x.length / 2 ); -var v = gnannsumkbn.ndarray( N, x, 2, 1, out, 2, 1 ); +var v = gnannsumkbn.ndarray( 4, x, 2, 1, out, 2, 1 ); // returns [ 0.0, 5.0, 0.0, 4 ] ``` @@ -140,22 +131,20 @@ var v = gnannsumkbn.ndarray( N, x, 2, 1, out, 2, 1 ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var Float64Array = require( '@stdlib/array/float64' ); var gnannsumkbn = require( '@stdlib/blas/ext/base/gnannsumkbn' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - x[ i ] = NaN; - } else { - x[ i ] = round( randu()*100.0 ); +function rand() { + if ( bernoulli( 0.8 ) > 0 ) { + return discreteUniform( 0, 100 ); } + return NaN; } + +var x = filledarrayBy( 10, 'float64', rand ); console.log( x ); var out = new Float64Array( 2 ); diff --git a/ext/base/gnannsumkbn/benchmark/benchmark.js b/ext/base/gnannsumkbn/benchmark/benchmark.js index 16729be1a..503273366 100644 --- a/ext/base/gnannsumkbn/benchmark/benchmark.js +++ b/ext/base/gnannsumkbn/benchmark/benchmark.js @@ -21,15 +21,31 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); +var zeros = require( '@stdlib/array/base/zeros' ); var pkg = require( './../package.json' ).name; var gnannsumkbn = require( './../lib/main.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. * @@ -38,19 +54,8 @@ var gnannsumkbn = require( './../lib/main.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var out; - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - if ( randu() < 0.2 ) { - x.push( NaN ); - } else { - x.push( ( randu()*10.0 ) - 20.0 ); - } - } - out = [ 0.0, 0 ]; + var out = zeros( 2 ); + var x = filledarrayBy( len, 'generic', rand ); return benchmark; function benchmark( b ) { diff --git a/ext/base/gnannsumkbn/benchmark/benchmark.ndarray.js b/ext/base/gnannsumkbn/benchmark/benchmark.ndarray.js index fdffbb52f..e745ede58 100644 --- a/ext/base/gnannsumkbn/benchmark/benchmark.ndarray.js +++ b/ext/base/gnannsumkbn/benchmark/benchmark.ndarray.js @@ -21,15 +21,31 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); +var zeros = require( '@stdlib/array/base/zeros' ); var pkg = require( './../package.json' ).name; var gnannsumkbn = require( './../lib/ndarray.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. * @@ -38,19 +54,8 @@ var gnannsumkbn = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var out; - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - if ( randu() < 0.2 ) { - x.push( NaN ); - } else { - x.push( ( randu()*10.0 ) - 20.0 ); - } - } - out = [ 0.0, 0 ]; + var out = zeros( 2 ); + var x = filledarrayBy( len, 'generic', rand ); return benchmark; function benchmark( b ) { diff --git a/ext/base/gnannsumkbn/docs/repl.txt b/ext/base/gnannsumkbn/docs/repl.txt index 37e78bd71..ff1eb7a20 100644 --- a/ext/base/gnannsumkbn/docs/repl.txt +++ b/ext/base/gnannsumkbn/docs/repl.txt @@ -3,7 +3,7 @@ Computes the sum of strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm. - The `N` and `stride` parameters determine which elements are accessed at + The `N` and stride parameters determine which elements are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use a typed @@ -20,13 +20,13 @@ Input array. strideX: integer - Index increment for `x`. + Stride length for `x`. out: Array|TypedArray Output array. strideOut: integer - Index increment for `out`. + Stride length for `out`. Returns ------- @@ -42,28 +42,27 @@ > {{alias}}( x.length, x, 1, out, 1 ) [ 1.0, 3 ] - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0, NaN, NaN ]; > out = [ 0.0, 0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}( N, x, 2, out, 1 ) + > {{alias}}( 4, x, 2, out, 1 ) [ 1.0, 3 ] // Using view offsets: > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, 1.0, NaN, NaN ] ); > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); > out = [ 0.0, 0 ]; - > {{alias}}( N, x1, 2, out, 1 ) + > {{alias}}( 4, x1, 2, out, 1 ) [ 1.0, 3 ] + {{alias}}.ndarray( N, x, strideX, offsetX, out, strideOut, offsetOut ) Computes the sum of 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 - starting index. + buffer, the offset parameters support indexing semantics based on a starting + indices. Parameters ---------- @@ -74,7 +73,7 @@ Input array. strideX: integer - Index increment for `x`. + Stride length for `x`. offsetX: integer Starting index for `x`. @@ -83,7 +82,7 @@ Output array. strideOut: integer - Index increment for `out`. + Stride length for `out`. offsetOut: integer Starting index for `out`. @@ -104,9 +103,8 @@ // Using offset parameter: > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, 1.0, NaN, NaN ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); > out = [ 0.0, 0 ]; - > {{alias}}.ndarray( N, x, 2, 1, out, 1, 0 ) + > {{alias}}.ndarray( 4, x, 2, 1, out, 1, 0 ) [ 1.0, 3 ] See Also diff --git a/ext/base/gnannsumkbn/docs/types/index.d.ts b/ext/base/gnannsumkbn/docs/types/index.d.ts index e38292bfc..ccec623bb 100644 --- a/ext/base/gnannsumkbn/docs/types/index.d.ts +++ b/ext/base/gnannsumkbn/docs/types/index.d.ts @@ -31,9 +31,9 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array - * @param strideX - `x` stride length + * @param strideX - stride length for `x` * @param out - output array whose first element is the sum and whose second element is the number of non-NaN elements - * @param strideOut - `out` stride length + * @param strideOut - stride length for `out` * @returns output array * * @example @@ -50,11 +50,11 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array - * @param strideX - `x` stride length - * @param offsetX - `x` starting index + * @param strideX - stride length for `x` + * @param offsetX - starting index for `x` * @param out - output array whose first element is the sum and whose second element is the number of non-NaN elements - * @param strideOut - `out` stride length - * @param offsetOut - `out` starting index + * @param strideOut - stride length for `out` + * @param offsetOut - starting index for `out` * @returns output array * * @example @@ -72,9 +72,9 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array -* @param strideX - `x` stride length +* @param strideX - stride length for `x` * @param out - output array whose first element is the sum and whose second element is the number of non-NaN elements -* @param strideOut - `out` stride length +* @param strideOut - stride length for `out` * @returns output array * * @example diff --git a/ext/base/gnannsumkbn/examples/index.js b/ext/base/gnannsumkbn/examples/index.js index 3695ba3f2..8b293afeb 100644 --- a/ext/base/gnannsumkbn/examples/index.js +++ b/ext/base/gnannsumkbn/examples/index.js @@ -18,25 +18,22 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); var Float64Array = require( '@stdlib/array/float64' ); var gnannsumkbn = require( './../lib' ); -var out; -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - x[ i ] = NaN; - } else { - x[ i ] = round( randu()*100.0 ); +function rand() { + if ( bernoulli( 0.8 ) > 0 ) { + return discreteUniform( 0, 100 ); } + return NaN; } + +var x = filledarrayBy( 10, 'float64', rand ); console.log( x ); -out = new Float64Array( 2 ); +var out = new Float64Array( 2 ); gnannsumkbn( x.length, x, 1, out, 1 ); console.log( out ); diff --git a/ext/base/gnannsumkbn/lib/index.js b/ext/base/gnannsumkbn/lib/index.js index 8a2ceed50..5d63097f7 100644 --- a/ext/base/gnannsumkbn/lib/index.js +++ b/ext/base/gnannsumkbn/lib/index.js @@ -33,15 +33,12 @@ * // returns [ 1.0, 3 ] * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var gnannsumkbn = require( '@stdlib/blas/ext/base/gnannsumkbn' ); * * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ]; * var out = [ 0.0, 0 ]; * -* var N = floor( x.length / 2 ); -* -* var v = gnannsumkbn.ndarray( N, x, 2, 1, out, 1, 0 ); +* var v = gnannsumkbn.ndarray( 5, x, 2, 1, out, 1, 0 ); * // returns [ 5.0, 4 ] */ diff --git a/ext/base/gnannsumkbn/lib/main.js b/ext/base/gnannsumkbn/lib/main.js index da1eca5a1..c65bfa317 100644 --- a/ext/base/gnannsumkbn/lib/main.js +++ b/ext/base/gnannsumkbn/lib/main.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,9 +39,9 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} strideX - `x` stride length +* @param {integer} strideX - stride length for `x` * @param {NumericArray} out - output array -* @param {integer} strideOut - `out` stride length +* @param {integer} strideOut - stride length for `out` * @returns {NumericArray} output array * * @example @@ -52,60 +52,9 @@ var abs = require( '@stdlib/math/base/special/abs' ); * // returns [ 1.0, 3 ] */ function gnannsumkbn( N, x, strideX, out, strideOut ) { - var sum; - var ix; - var io; - var v; - var t; - var c; - var n; - var i; - - if ( strideX < 0 ) { - ix = (1-N) * strideX; - } else { - ix = 0; - } - 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; - } - c = 0.0; - 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; - n += 1; - } - ix += strideX; - } - out[ io ] = sum + c; - out[ io+strideOut ] = n; - return out; + var ix = stride2offset( N, strideX ); + var io = stride2offset( 2, strideOut ); + return ndarray( N, x, strideX, ix, out, strideOut, io ); } diff --git a/ext/base/gnannsumkbn/lib/ndarray.js b/ext/base/gnannsumkbn/lib/ndarray.js index 976fa79aa..1c2cc2d86 100644 --- a/ext/base/gnannsumkbn/lib/ndarray.js +++ b/ext/base/gnannsumkbn/lib/ndarray.js @@ -39,51 +39,44 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} strideX - `x` stride length -* @param {NonNegativeInteger} offsetX - `x` starting index +* @param {integer} strideX - stride length for `x` +* @param {NonNegativeInteger} offsetX - starting index for `x` * @param {NumericArray} out - output array -* @param {integer} strideOut - `out` stride length -* @param {NonNegativeInteger} offsetOut - `out` starting index +* @param {integer} strideOut - stride length for `out` +* @param {NonNegativeInteger} offsetOut - starting index for `out` * @returns {NumericArray} output array * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); -* * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ]; * var out = [ 0.0, 0 ]; * -* var N = floor( x.length / 2 ); -* -* var v = gnannsumkbn( N, x, 2, 1, out, 1, 0 ); +* var v = gnannsumkbn( 5, x, 2, 1, out, 1, 0 ); * // returns [ 5.0, 4 ] */ function gnannsumkbn( N, x, strideX, offsetX, out, strideOut, offsetOut ) { var sum; var ix; - var io; var v; var t; var c; var n; var i; - ix = offsetX; - io = offsetOut; - sum = 0.0; if ( N <= 0 ) { - out[ io ] = sum; - out[ io+strideOut ] = 0; + out[ offsetOut ] = sum; + out[ offsetOut+strideOut ] = 0; return out; } - if ( N === 1 || strideX === 0 ) { + ix = offsetX; + if ( strideX === 0 ) { if ( isnan( x[ ix ] ) ) { - out[ io ] = sum; - out[ io+strideOut ] = 0; + out[ offsetOut ] = sum; + out[ offsetOut+strideOut ] = 0; return out; } - out[ io ] = x[ ix ]; - out[ io+strideOut ] = 1; + out[ offsetOut ] = x[ ix ] * N; + out[ offsetOut+strideOut ] = N; return out; } c = 0.0; @@ -102,8 +95,8 @@ function gnannsumkbn( N, x, strideX, offsetX, out, strideOut, offsetOut ) { } ix += strideX; } - out[ io ] = sum + c; - out[ io+strideOut ] = n; + out[ offsetOut ] = sum + c; + out[ offsetOut+strideOut ] = n; return out; } diff --git a/ext/base/gnannsumkbn/test/test.main.js b/ext/base/gnannsumkbn/test/test.main.js index e72184b62..42f172749 100644 --- a/ext/base/gnannsumkbn/test/test.main.js +++ b/ext/base/gnannsumkbn/test/test.main.js @@ -21,7 +21,6 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var Float64Array = require( '@stdlib/array/float64' ); var gnannsumkbn = require( './../lib' ); @@ -163,7 +162,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns a sum equ tape( 'the function supports `stride` parameters', function test( t ) { var expected; var out; - var N; var x; var v; @@ -181,8 +179,7 @@ tape( 'the function supports `stride` parameters', function test( t ) { ]; out = [ 0.0, 0.0, 0.0, 0.0 ]; - N = floor( x.length / 2 ); - v = gnannsumkbn( N, x, 2, out, 2 ); + v = gnannsumkbn( 5, x, 2, out, 2 ); expected = [ 5.0, 0.0, 4.0, 0.0 ]; t.deepEqual( v, expected, 'returns expected value' ); @@ -193,7 +190,6 @@ tape( 'the function supports `stride` parameters', function test( t ) { tape( 'the function supports negative `stride` parameters', function test( t ) { var expected; var out; - var N; var x; var v; @@ -211,8 +207,7 @@ tape( 'the function supports negative `stride` parameters', function test( t ) { ]; out = [ 0.0, 0.0, 0.0, 0.0 ]; - N = floor( x.length / 2 ); - v = gnannsumkbn( N, x, -2, out, -2 ); + v = gnannsumkbn( 5, x, -2, out, -2 ); expected = [ 4.0, 0.0, 5.0, 0.0 ]; t.deepEqual( v, expected, 'returns expected value' ); @@ -220,7 +215,7 @@ 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 of the first element repeated N times', function test( t ) { var expected; var out; var x; @@ -231,7 +226,24 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns a sum out = [ 0.0, 0.0 ]; v = gnannsumkbn( x.length, x, 0, out, 1 ); - expected = [ 1.0, 1.0 ]; + expected = [ 5.0, 5.0 ]; + t.deepEqual( v, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `stride` parameter equal to `0` and the first element is `NaN`, the function returns `0.0`', function test( t ) { + var expected; + var out; + var x; + var v; + + x = [ NaN, -2.0, -4.0, 5.0, 3.0 ]; + + out = [ 0.0, 0.0 ]; + v = gnannsumkbn( x.length, x, 0, out, 1 ); + + expected = [ 0.0, 0.0 ]; t.deepEqual( v, expected, 'returns expected value' ); t.end(); @@ -244,7 +256,6 @@ tape( 'the function supports view offsets', function test( t ) { var out1; var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -264,9 +275,8 @@ tape( 'the function supports view offsets', function test( t ) { x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); // start at the 3rd element - N = floor(x1.length / 2); - v = gnannsumkbn( N, x1, 2, out1, 1 ); + v = gnannsumkbn( 5, x1, 2, out1, 1 ); expected0 = new Float64Array( [ 0.0, 0.0, 5.0, 4.0 ] ); expected1 = new Float64Array( [ 5.0, 4.0 ] ); diff --git a/ext/base/gnannsumkbn/test/test.ndarray.js b/ext/base/gnannsumkbn/test/test.ndarray.js index d1b61d27c..b3fb5364b 100644 --- a/ext/base/gnannsumkbn/test/test.ndarray.js +++ b/ext/base/gnannsumkbn/test/test.ndarray.js @@ -21,7 +21,6 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var gnannsumkbn = require( './../lib/ndarray.js' ); @@ -162,7 +161,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first tape( 'the function supports a `stride` parameter', function test( t ) { var expected; var out; - var N; var x; var v; @@ -180,8 +178,7 @@ tape( 'the function supports a `stride` parameter', function test( t ) { ]; out = [ 0.0, 0.0, 0.0, 0.0 ]; - N = floor( x.length / 2 ); - v = gnannsumkbn( N, x, 2, 0, out, 2, 0 ); + v = gnannsumkbn( 5, x, 2, 0, out, 2, 0 ); expected = [ 5.0, 0.0, 4.0, 0.0 ]; t.deepEqual( v, expected, 'returns expected value' ); @@ -192,7 +189,6 @@ tape( 'the function supports a `stride` parameter', function test( t ) { tape( 'the function supports a negative `stride` parameter', function test( t ) { var expected; var out; - var N; var x; var v; @@ -210,8 +206,7 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) ]; out = [ 0.0, 0.0, 0.0, 0.0 ]; - N = floor( x.length / 2 ); - v = gnannsumkbn( N, x, -2, 8, out, -2, 2 ); + v = gnannsumkbn( 5, x, -2, 8, out, -2, 2 ); expected = [ 4.0, 0.0, 5.0, 0.0 ]; t.deepEqual( v, expected, 'returns expected value' ); @@ -219,7 +214,7 @@ 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 of the first element repeated N times', function test( t ) { var expected; var out; var x; @@ -230,7 +225,24 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f out = [ 0.0, 0.0 ]; v = gnannsumkbn( x.length, x, 0, 0, out, 1, 0 ); - expected = [ 1.0, 1.0 ]; + expected = [ 5.0, 5.0 ]; + t.deepEqual( v, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `stride` parameter equal to `0` and the first element is `NaN`, the function returns `0.0`', function test( t ) { + var expected; + var out; + var x; + var v; + + x = [ NaN, -2.0, -4.0, 5.0, 3.0 ]; + + out = [ 0.0, 0.0 ]; + v = gnannsumkbn( x.length, x, 0, 0, out, 1, 0 ); + + expected = [ 0.0, 0.0 ]; t.deepEqual( v, expected, 'returns expected value' ); t.end(); @@ -239,7 +251,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f tape( 'the function supports `offset` parameters', function test( t ) { var expected; var out; - var N; var x; var v; @@ -256,9 +267,8 @@ tape( 'the function supports `offset` parameters', function test( t ) { NaN // 4 ]; out = [ 0.0, 0.0, 0.0, 0.0 ]; - N = floor( x.length / 2 ); - v = gnannsumkbn( N, x, 2, 1, out, 2, 1 ); + v = gnannsumkbn( 5, x, 2, 1, out, 2, 1 ); expected = [ 0.0, 5.0, 0.0, 4.0 ]; t.deepEqual( v, expected, 'returns expected value' );