diff --git a/lib/node_modules/@stdlib/stats/base/nanmskmin/README.md b/lib/node_modules/@stdlib/stats/base/nanmskmin/README.md index e1d63bb6afc5..b0387c607392 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmskmin/README.md +++ b/lib/node_modules/@stdlib/stats/base/nanmskmin/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2020 The Stdlib Authors. +Copyright (c) 2025 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. @@ -52,20 +52,18 @@ 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`. - **mask**: mask [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in computation. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from computation. -- **strideMask**: index increment for `mask`. +- **strideMask**: stride length for `mask`. -The `N` and `stride` parameters determine which elements are accessed at runtime. For example, to compute the minimum value of every other element in `x`, +The `N` and `stride` parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the minimum value of every other element in `x`, ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - var x = [ 1.0, 2.0, -7.0, -2.0, 4.0, 3.0, -5.0, -6.0 ]; var mask = [ 0, 0, 0, 0, 0, 0, 1, 1 ]; -var N = floor( x.length / 2 ); -var v = nanmskmin( N, x, 2, mask, 2 ); + +var v = nanmskmin( 4, x, 2, mask, 2 ); // returns -7.0 ``` @@ -76,7 +74,7 @@ Note that indexing is relative to the first index. To introduce offsets, use [`t ```javascript var Float64Array = require( '@stdlib/array/float64' ); var Uint8Array = require( '@stdlib/array/uint8' ); -var floor = require( '@stdlib/math/base/special/floor' ); + var x0 = new Float64Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] ); var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element @@ -84,9 +82,8 @@ var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd var mask0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] ); var mask1 = new Uint8Array( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); -var v = nanmskmin( N, x1, 2, mask1, 2 ); +var v = nanmskmin( 4, x1, 2, mask1, 2 ); // returns -2.0 ``` @@ -107,16 +104,15 @@ The function has the following additional parameters: - **offsetX**: starting index for `x`. - **offsetMask**: starting index for `mask`. -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 minimum value for 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 +indexing semantics based on +starting indices. For example, to calculate the minimum value for every other value in `x` starting from the second value ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - var x = [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, -5.0, -6.0 ]; var mask = [ 0, 0, 0, 0, 0, 0, 1, 1 ]; -var N = floor( x.length / 2 ); -var v = nanmskmin.ndarray( N, x, 2, 1, mask, 2, 1 ); +var v = nanmskmin.ndarray( 4, x, 2, 1, mask, 2, 1 ); // returns -2.0 ``` @@ -130,6 +126,8 @@ var v = nanmskmin.ndarray( N, x, 2, 1, mask, 2, 1 ); - If `N <= 0`, both functions return `NaN`. - Depending on the environment, the typed versions ([`dnanmskmin`][@stdlib/stats/base/dnanmskmin], [`snanmskmin`][@stdlib/stats/base/snanmskmin], etc.) are likely to be significantly more performant. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). + @@ -142,31 +140,18 @@ var v = nanmskmin.ndarray( N, x, 2, 1, mask, 2, 1 ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); -var Uint8Array = require( '@stdlib/array/uint8' ); +var uniform = require( '@stdlib/random/array/uniform' ); + var bernoulli = require( '@stdlib/random/array/bernoulli' ); var nanmskmin = require( '@stdlib/stats/base/nanmskmin' ); -var mask; -var x; -var i; - -x = new Float64Array( 10 ); -mask = new Uint8Array( x.length ); -for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - mask[ i ] = 1; - } else { - mask[ i ] = 0; - } - if ( randu() < 0.1 ) { - x[ i ] = NaN; - } else { - x[ i ] = round( (randu()*100.0) - 50.0 ); - } -} +var x = uniform( 10, -50.0, 50.0, { + 'dtype': 'float64' + }); console.log( x ); + +var mask = bernoulli( x.length, 0.2, { + 'dtype': 'uint8' + }); console.log( mask ); var v = nanmskmin( x.length, x, 1, mask, 1 ); @@ -201,6 +186,10 @@ console.log( v ); [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array + +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor + + [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray diff --git a/lib/node_modules/@stdlib/stats/base/nanmskmin/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/nanmskmin/benchmark/benchmark.js index b9ddaf5a7d17..4beab2df3e7f 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmskmin/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/nanmskmin/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -21,13 +21,21 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var bernoulli = require( '@stdlib/random/array/bernoulli' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var nanmskmin = require( './../lib/nanmskmin.js' ); +// VARIABLES // + + var options = { + 'dtype': 'generic' + }; + + // FUNCTIONS // /** @@ -38,20 +46,8 @@ var nanmskmin = require( './../lib/nanmskmin.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var mask; - var x; - var i; - - x = []; - mask = []; - for ( i = 0; i < len; i++ ) { - if ( randu() < 0.2 ) { - mask.push( 1 ); - } else { - mask.push( 0 ); - } - x.push( ( randu()*20.0 ) - 10.0 ); - } + var mask = bernoulli( len, 0.2, options ); + var x = uniform( len, -10.0, 10.0, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/nanmskmin/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/nanmskmin/benchmark/benchmark.ndarray.js index b4e6c253ce48..2d3488016bc7 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmskmin/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanmskmin/benchmark/benchmark.ndarray.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -21,13 +21,21 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var bernoulli = require( '@stdlib/random/array/bernoulli' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var nanmskmin = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,20 +46,8 @@ var nanmskmin = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var mask; - var x; - var i; - - x = []; - mask = []; - for ( i = 0; i < len; i++ ) { - if ( randu() < 0.2 ) { - mask.push( 1 ); - } else { - mask.push( 0 ); - } - x.push( ( randu()*20.0 ) - 10.0 ); - } + var mask = bernoulli( len, 0.2, options ); + var x = uniform( len, -10.0, 10.0, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/repl.txt index 6ca57a6c18b0..dbd5f616798f 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/repl.txt @@ -3,8 +3,8 @@ Computes the minimum value of a strided array according to a mask and ignoring `NaN` values. - The `N` and `stride` parameters determine which elements are accessed at - runtime. + The `N` and stride parameters determine which elements in the strided arrays + are accessed at runtime. Indexing is relative to the first index. To introduce offsets, use a typed array views. @@ -26,13 +26,13 @@ Input array. strideX: integer - Index increment for `x`. + Stride length for `x`. mask: Array|TypedArray Mask array. strideMask: integer - Index increment for `mask`. + Stride length for `mask`. Returns ------- @@ -47,11 +47,9 @@ > {{alias}}( x.length, x, 1, mask, 1 ) -2.0 - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0, -4.0 ]; - > mask = [ 0, 0, 0, 0, 0, 0, 1 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}( N, x, 2, mask, 2 ) + > {{alias}}( 3, x, 2, mask, 2 ) -2.0 // Using view offsets: @@ -59,8 +57,7 @@ > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); > var mask0 = new {{alias:@stdlib/array/uint8}}( [ 0, 0, 0, 0, 0, 0, 1 ] ); > var mask1 = new {{alias:@stdlib/array/uint8}}( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > {{alias}}( N, x1, 2, mask1, 2 ) + >{{alias}}( 3, x1, 2, mask1, 2 ) -2.0 {{alias}}.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask ) @@ -80,7 +77,7 @@ Input array. strideX: integer - Index increment for `x`. + Stride length for `x`. offsetX: integer Starting index for `x`. @@ -89,7 +86,7 @@ Mask array. strideMask: integer - Index increment for `mask`. + Stride length for `mask`. offsetMask: integer Starting index for `mask`. @@ -110,8 +107,7 @@ // Using offset parameter: > x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, -4.0 ]; > mask = [ 0, 0, 0, 0, 0, 0, 1 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, x, 2, 1, mask, 2, 1 ) + > {{alias}}.ndarray( 3, x, 2, 1, mask, 2, 1 ) -2.0 See Also diff --git a/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/types/index.d.ts index 1440b3275d73..18e29182678c 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/types/index.d.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -20,7 +20,12 @@ /// -import { NumericArray } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + + /** + * Input array. + */ + type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Interface describing `nanmskmin`. @@ -31,9 +36,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 mask - mask array - * @param strideMask - `mask` stride length + * @param strideMask - stride length for `mask` * @returns minimum value * * @example @@ -43,17 +48,17 @@ interface Routine { * var v = nanmskmin( x.length, x, 1, mask, 1 ); * // returns -2.0 */ - ( N: number, x: NumericArray, strideX: number, mask: NumericArray, strideMask: number ): number; + ( N: number, x: InputArray, strideX: number, mask: InputArray, strideMask: number ): number; /** * Computes the minimum value of a strided array according to a mask, ignoring `NaN` values and using alternative indexing semantics. * * @param N - number of indexed elements * @param x - input array - * @param strideX - `x` stride length + * @param strideX - stride length for `x` * @param offsetX - `x` starting index * @param mask - mask array - * @param strideMask - `mask` stride length + * @param strideMask - stride length for `mask` * @param offsetMask - `mask` starting index * @returns minimum value * @@ -64,7 +69,7 @@ interface Routine { * var v = nanmskmin.ndarray( x.length, x, 1, 0, mask, 1, 0 ); * // returns -2.0 */ - ndarray( N: number, x: NumericArray, strideX: number, offsetX: number, mask: NumericArray, strideMask: number, offsetMask: number ): number; + ndarray( N: number, x: InputArray, strideX: number, offsetX: number, mask: InputArray, strideMask: number, offsetMask: number ): number; } /** @@ -72,9 +77,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 mask - mask array -* @param strideMask - `mask` stride length +* @param strideMask - stride length for `mask` * @returns minimum value * * @example diff --git a/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/types/test.ts index 84a9f398704b..17629d884c53 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/nanmskmin/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -17,6 +17,7 @@ */ import nanmskmin = require( './index' ); +import AccessorArray = require( '@stdlib/array/base/accessor' ); // TESTS // @@ -27,6 +28,7 @@ import nanmskmin = require( './index' ); const mask = new Uint8Array( 10 ); nanmskmin( x.length, x, 1, mask, 1 ); // $ExpectType number + nanmskmin( x.length, new AccessorArray( x ), 1, mask, 1); } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -123,6 +125,7 @@ import nanmskmin = require( './index' ); const mask = new Uint8Array( 10 ); nanmskmin.ndarray( x.length, x, 1, 0, mask, 1, 0 ); // $ExpectType number + nanmskmin.ndarray( x.length, new AccessorArray( x ), 1, 0, new AccessorArray( mask ), 1, 0 ); } // The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... diff --git a/lib/node_modules/@stdlib/stats/base/nanmskmin/examples/index.js b/lib/node_modules/@stdlib/stats/base/nanmskmin/examples/index.js index fc061ffb0006..6a425b7db824 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmskmin/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/nanmskmin/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -18,31 +18,19 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); -var Uint8Array = require( '@stdlib/array/uint8' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var bernoulli = require( '@stdlib/random/array/bernoulli' ); var nanmskmin = require( './../lib' ); -var mask; -var x; -var i; +var x = uniform( 10, -50.0, 50.0, { + 'dtype': 'float64' +}); -x = new Float64Array( 10 ); -mask = new Uint8Array( x.length ); -for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - mask[ i ] = 1; - } else { - mask[ i ] = 0; - } - if ( randu() < 0.1 ) { - x[ i ] = NaN; - } else { - x[ i ] = round( (randu()*100.0) - 50.0 ); - } -} console.log( x ); + +var mask = bernoulli( x.length, 0.2, { + 'dtype': 'uint8' +}); console.log( mask ); var v = nanmskmin( x.length, x, 1, mask, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/nanmskmin/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/nanmskmin/lib/accessors.js new file mode 100644 index 000000000000..9e9bf426301c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/nanmskmin/lib/accessors.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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. +*/ + +'use strict'; + +// MODULES // + +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); + + +// MAIN // + +/** +* Computes the minimum value of a strided array according to a mask, ignoring `NaN` values. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {Object} x - input array object +* @param {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length for `x` +* @param {NonNegativeInteger} offsetX - starting index for `x` +* @param {Object} mask - mask array object +* @param {Collection} mask.data - mask array data +* @param {Array} mask.accessors - mask element accessors +* @param {integer} strideMask - stride length for `mask` +* @param {NonNegativeInteger} offsetMask - starting index for `mask` +* @returns {Object} output minimum value +* +/* +*@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, -5.0, -6.0 ]; +* var mask = [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 ]; +* var N = floor( x.length / 2 ); +* +* var v = mskmax( 5, arraylike2object( toAccessorArray( x ) ), 2, 1, arraylike2object( toAccessorArray( mask ) ), 2, 1 ); +* // returns -2.0 +*/ +function nanmskmin( N, x, StrideX, offsetX, mask, StrideM, offsetMask) { + var xbuf; + var mbuf; + var xget; + var mget; + var min; + var ix; + var im; + var v; + var i; + + // Cache references to array data: + + xbuf = x.data; + mbuf = mask.data; + + // Cache references to element accessors: + + xget = x.accessors[0]; + mget = mask.accessors[0]; + + if(N <= 0){ + return NaN; + } + ix = offsetX; + im = offsetMask; + for(i = 0; i< N; i++) { + if( mget( mbuf, im) === 0){ + break; + } + ix += StrideX; + im += StrideM; + + if( i == N ){ + return NaN + } + min = xget( xbuf, ix); + if( isnan( min )) { + return min; + } + i+1; + for( i; i [range]: https://en.wikipedia.org/wiki/Range_%28statistics%29 - [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array - +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray [@stdlib/stats/base/dnanrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dnanrange - [@stdlib/stats/base/nanmax-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmax-by - [@stdlib/stats/base/nanmin-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmin-by - [@stdlib/stats/base/nanrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanrange - [@stdlib/stats/base/range-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/range-by - [@stdlib/stats/base/snanrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/snanrange diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/nanrange-by/benchmark/benchmark.js index 497323944104..a1850aca45ca 100644 --- a/lib/node_modules/@stdlib/stats/base/nanrange-by/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/benchmark/benchmark.js @@ -21,7 +21,9 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; @@ -37,6 +39,13 @@ var nanrangeBy = require( './../lib/nanrange_by.js' ); * @param {number} value - array element * @returns {number} accessed value */ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); +} + function accessor( value ) { return value * 2.0; } @@ -49,17 +58,7 @@ function accessor( value ) { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - if ( randu() < 0.2 ) { - x.push( NaN ); - } else { - x.push( ( randu()*20.0 ) - 10.0 ); - } - } + var x = filledarrayBy( len, "float64", rand ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/nanrange-by/benchmark/benchmark.ndarray.js index 8b8c84d3bdb2..000aeac3035b 100644 --- a/lib/node_modules/@stdlib/stats/base/nanrange-by/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/benchmark/benchmark.ndarray.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -21,7 +21,9 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; @@ -37,6 +39,13 @@ var nanrangeBy = require( './../lib/ndarray.js' ); * @param {number} value - array element * @returns {number} accessed value */ +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; + } + return uniform( -50.0, 50.0 ); +} + function accessor( value ) { return value * 2.0; } @@ -49,17 +58,7 @@ function accessor( value ) { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - if ( randu() < 0.2 ) { - x.push( NaN ); - } else { - x.push( ( randu()*20.0 ) - 10.0 ); - } - } + var x = filledarrayBy( len, "float64", rand ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/repl.txt index 306a3a977dd2..686c28dd1df3 100644 --- a/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, x, stride, clbk[, thisArg] ) +{{alias}}( N, x, strideX, clbk[, thisArg] ) Calculates the range of a strided array via a callback function, ignoring `NaN` values. - The `N` and `stride` parameters determine which elements in `x` are accessed + The `N` and `strideX` parameters determine which elements in `x` are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use typed @@ -15,7 +15,7 @@ - value: array element. - aidx: array index. - - sidx: strided index (offset + aidx*stride). + - sidx: strided index (offsetX + aidx*strideX). - array: the input array. The callback function should return a numeric value. @@ -34,7 +34,7 @@ Input array/collection. If provided an object, the object must be array- like (excluding strings and functions). - stride: integer + strideX: integer Index increment for `x`. clbk: Function @@ -56,7 +56,7 @@ > {{alias}}( x.length, x, 1, accessor ) 18.0 - // Using `N` and `stride` parameters: + // Using `N` and `strideX` parameters: > x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0, 1.0 ]; > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); > {{alias}}( N, x, 2, accessor ) @@ -69,12 +69,12 @@ > {{alias}}( N, x1, 2, accessor ) 8.0 -{{alias}}.ndarray( N, x, stride, offset, clbk[, thisArg] ) +{{alias}}.ndarray( N, x, strideX, offsetX, clbk[, thisArg] ) Calculates the range of a strided array via a callback function, ignoring `NaN` values and using 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 `offsetX` parameter supports indexing semantics based on a starting index. Parameters @@ -86,10 +86,10 @@ Input array/collection. If provided an object, the object must be array- like (excluding strings and functions). - stride: integer + strideX: integer Index increment for `x`. - offset: integer + offsetX: integer Starting index of `x`. clbk: Function diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/types/index.d.ts index 502636524eb1..01ff85a8ea1b 100644 --- a/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/types/index.d.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -20,7 +20,11 @@ /// -import { Collection } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Returns an accessed value. @@ -61,7 +65,7 @@ type Ternary = ( this: U, value: T, aidx: number, sidx: number ) => number * * @param value - array element * @param aidx - array index -* @param sidx - strided index (offset + aidx*stride) +* @param sidx - strided index (offsetX + aidx*strideX) * @param array - input array * @returns accessed value */ @@ -72,7 +76,7 @@ type Quaternary = ( this: U, value: T, aidx: number, sidx: number, array: * * @param value - array element * @param aidx - array index -* @param sidx - strided index (offset + aidx*stride) +* @param sidx - strided index (offsetX + aidx*strideX) * @param array - input array * @returns accessed value */ @@ -102,7 +106,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @param clbk - callback * @param thisArg - execution context * @returns range @@ -117,7 +121,7 @@ interface Routine { * var v = nanrangeBy( x.length, x, 1, accessor ); * // returns 18.0 */ - ( N: number, x: Collection, stride: number, clbk: Callback, thisArg?: ThisParameterType> ): number; + ( N: number, x: InputArray, stride: number, clbk: Callback, thisArg?: ThisParameterType> ): number; /** * Calculates the range of a strided array via a callback function, ignoring `NaN` values and using alternative indexing semantics. @@ -128,7 +132,7 @@ interface Routine { * * - `value`: array element * - `aidx`: array index - * - `sidx`: strided index (offset + aidx*stride) + * - `sidx`: strided index (offsetX + aidx*strideX) * - `array`: input array * * - The callback function should return a numeric value. @@ -139,8 +143,8 @@ interface Routine { * * @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 * @param clbk - callback * @param thisArg - execution context * @returns range @@ -155,7 +159,7 @@ interface Routine { * var v = nanrangeBy.ndarray( x.length, x, 1, 0, accessor ); * // returns 18.0 */ - ndarray( N: number, x: Collection, stride: number, offset: number, clbk: Callback, thisArg?: ThisParameterType> ): number; + ndarray( N: number, x: InputArray, stride: number, offset: number, clbk: Callback, thisArg?: ThisParameterType> ): number; } /** @@ -167,7 +171,7 @@ interface Routine { * * - `value`: array element * - `aidx`: array index -* - `sidx`: strided index (offset + aidx*stride) +* - `sidx`: strided index (offsetX + aidx*strideX) * - `array`: input array * * - The callback function should return a numeric value. @@ -178,7 +182,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @param clbk - callback * @param thisArg - execution context * @returns range diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/types/test.ts index 076af8773ea2..d411119995c2 100644 --- a/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -17,6 +17,7 @@ */ import nanrangeBy = require( './index' ); +import AccessorArray = require( '@stdlib/array/base/accessor' ); const accessor = (): number => { return 5.0; @@ -29,8 +30,8 @@ const accessor = (): number => { { const x = new Float64Array( 10 ); - nanrangeBy( x.length, x, 1, accessor ); // $ExpectType number - nanrangeBy( x.length, x, 1, accessor, {} ); // $ExpectType number + nanrangeBy( x.length, new AccessorArray( x ), 1, accessor ); // $ExpectType number + nanrangeBy( x.length, new AccessorArray( x ), 1, accessor, {} ); // $ExpectType number } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -101,8 +102,8 @@ const accessor = (): number => { { const x = new Float64Array( 10 ); - nanrangeBy.ndarray( x.length, x, 1, 0, accessor ); // $ExpectType number - nanrangeBy.ndarray( x.length, x, 1, 0, accessor, {} ); // $ExpectType number + nanrangeBy.ndarray( x.length, new AccessorArray( x ), 1, 0, accessor ); // $ExpectType number + nanrangeBy.ndarray( x.length, new AccessorArray( x ), 1, 0, accessor, {} ); // $ExpectType number } // The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/examples/index.js b/lib/node_modules/@stdlib/stats/base/nanrange-by/examples/index.js index 8aaaf469c7a4..83b9f6546aed 100644 --- a/lib/node_modules/@stdlib/stats/base/nanrange-by/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -19,22 +19,23 @@ 'use strict'; var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var nanrangeBy = require( './../lib' ); -function fill() { - if ( randu() < 0.2 ) { +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { return NaN; } - return discreteUniform( -50, 50 ); + return uniform( -50.0, 50.0 ); } function accessor( v ) { return v * 2.0; } -var x = filledarrayBy( 10, 'float64', fill ); +var x = filledarrayBy( 10, 'float64', rand ); console.log( x ); var v = nanrangeBy( x.length, x, 1, accessor ); diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/accessors.js new file mode 100644 index 000000000000..9233b115e530 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/accessors.js @@ -0,0 +1,103 @@ +/** + * @license Apache-2.0 + * + * Copyright (c) 2025 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. + */ + +'use strict'; + +// MODULES // + +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + +// MAIN // + +/** +* Calculates the range of a strided array via a callback function, ignoring `NaN` values. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {Collection} x - input array/collection +* @param {integer} stride - index increment +* @param {NonNegativeInteger} offset - starting index +* @param {Callback} clbk - callback +* @param {*} [thisArg] - execution context +* @returns {number} range +* +* @example +* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0 ]; +* +* function accessor( v ) { +* return v * 2.0; +* } +* +* var v = nanrangeBy( x.length, x, 1, 0, accessor ); +* // returns 18.0 +*/ +function nanrangeBy( N, x, strideX, offsetX, clbk, thisArg ) { + var xbuf; + var get; + var max; + var min; + var ix; + var v; + var i; + + // Cache reference to array data: + xbuf = x.data; + + // Cache a reference to the element accessor: + get = x.accessors[0]; + if ( N <= 0 ) { + return NaN; + } + if ( N === 1 || strideX === 0 ) { + v = clbk.call( thisArg, get( xbuf, 0 ), 0, 0, x ); + if ( v === void 0 || isnan( v ) ) { + return NaN; + } + return 0.0; + } + ix = offsetX; + for ( i = 0; i < N; i++ ) { + min = clbk.call( thisArg, get( xbuf, ix ), i, ix, x ); + if ( min === min && min !== void 0 ) { + break; + } + ix += strideX; + } + if ( i === N ) { + return NaN; + } + max = min; + i += 1; + for ( i; i < N; i++ ) { + ix += strideX; + v = clbk.call( thisArg, get( xbuf, ix ), i, ix, x ); + if ( v === void 0 || isnan( v ) ) { + continue; + } + if ( v < min ) { + min = v; + } else if ( v > max ) { + max = v; + } + } + return max - min; +} + + +// EXPORTS // + +module.exports = nanrangeBy; \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/nanrange_by.js b/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/nanrange_by.js index b7b9dd146f9f..e56bdb424224 100644 --- a/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/nanrange_by.js +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/nanrange_by.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -20,8 +20,8 @@ // MODULES // -var isnan = require( '@stdlib/math/base/assert/is-nan' ); - +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -30,7 +30,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Collection} x - input array/collection -* @param {integer} stride - index increment +* @param {integer} strideX - index increment * @param {Callback} clbk - callback * @param {*} [thisArg] - execution context * @returns {number} range @@ -45,56 +45,11 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); * var v = nanrangeBy( x.length, x, 1, accessor ); * // returns 18.0 */ -function nanrangeBy( N, x, stride, clbk, thisArg ) { - var max; - var min; - var ix; - var v; - var i; - - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - v = clbk.call( thisArg, x[ 0 ], 0, 0, x ); - if ( v === void 0 || isnan( v ) ) { - return NaN; - } - return 0.0; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - for ( i = 0; i < N; i++ ) { - min = clbk.call( thisArg, x[ ix ], i, ix, x ); - if ( min === min && min !== void 0 ) { - break; - } - ix += stride; - } - if ( i === N ) { - return NaN; - } - max = min; - i += 1; - for ( i; i < N; i++ ) { - ix += stride; - v = clbk.call( thisArg, x[ ix ], i, ix, x ); - if ( v === void 0 || isnan( v ) ) { - continue; - } - if ( v < min ) { - min = v; - } else if ( v > max ) { - max = v; - } - } - return max - min; +function nanrangeBy( N, x, strideX, clbk, thisArg ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ), clbk , thisArg); } // EXPORTS // -module.exports = nanrangeBy; +module.exports = nanrangeBy; \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/ndarray.js index 8d7bcbae67df..355d5922abc1 100644 --- a/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/lib/ndarray.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -21,6 +21,8 @@ // MODULES // var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); // MAIN // @@ -30,8 +32,8 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Collection} x - input array/collection -* @param {integer} stride - index increment -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - index increment +* @param {NonNegativeInteger} offsetX - starting index * @param {Callback} clbk - callback * @param {*} [thisArg] - execution context * @returns {number} range @@ -46,9 +48,10 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); * var v = nanrangeBy( x.length, x, 1, 0, accessor ); * // returns 18.0 */ -function nanrangeBy( N, x, stride, offset, clbk, thisArg ) { +function nanrangeBy( N, x, strideX, offsetX, clbk, thisArg ) { var max; var min; + var o; var ix; var v; var i; @@ -56,20 +59,24 @@ function nanrangeBy( N, x, stride, offset, clbk, thisArg ) { if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, o, strideX, offsetX, clbk, thisArg ); + } + if ( N === 1 || strideX === 0 ) { v = clbk.call( thisArg, x[ 0 ], 0, 0, x ); if ( v === void 0 || isnan( v ) ) { return NaN; } return 0.0; } - ix = offset; + ix = offsetX; for ( i = 0; i < N; i++ ) { min = clbk.call( thisArg, x[ ix ], i, ix, x ); if ( min === min && min !== void 0 ) { break; } - ix += stride; + ix += strideX; } if ( i === N ) { return NaN; @@ -77,7 +84,7 @@ function nanrangeBy( N, x, stride, offset, clbk, thisArg ) { max = min; i += 1; for ( i; i < N; i++ ) { - ix += stride; + ix += strideX; v = clbk.call( thisArg, x[ ix ], i, ix, x ); if ( v === void 0 || isnan( v ) ) { continue; @@ -94,4 +101,4 @@ function nanrangeBy( N, x, stride, offset, clbk, thisArg ) { // EXPORTS // -module.exports = nanrangeBy; +module.exports = nanrangeBy; \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/test/test.nanrange_by.js b/lib/node_modules/@stdlib/stats/base/nanrange-by/test/test.nanrange_by.js index ce1e4d8fe3ed..9d72025be10d 100644 --- a/lib/node_modules/@stdlib/stats/base/nanrange-by/test/test.nanrange_by.js +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/test/test.nanrange_by.js @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var floor = require( '@stdlib/math/base/special/floor' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var Float64Array = require( '@stdlib/array/float64' ); @@ -87,6 +88,42 @@ tape( 'the function calculates the range of a strided array via a callback funct t.end(); }); +tape( 'the function calculates the range of a strided array via a callback function, ignoring NaN values (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, NaN, 5.0, 0.0, NaN, 3.0 ]; + v = nanrangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( v, 18.0, 'returns expected value' ); + + x = [ -4.0, NaN, -5.0 ]; + v = nanrangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( v, 2.0, 'returns expected value' ); + + x = [ -0.0, 0.0, NaN, -0.0 ]; + v = nanrangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = nanrangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = nanrangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = new Array( 5 ); // sparse array + v = nanrangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = new Array( 5 ); // sparse array + x[ 2 ] = 1.0; + v = nanrangeBy( x.length, toAccessorArray(x), 1, accessor ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -102,6 +139,21 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanrangeBy( 0, toAccessorArray(x), 1, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = nanrangeBy( -1, toAccessorArray(x), 1, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter equal to `1`, the function returns `0`', function test( t ) { var x; var v; @@ -117,6 +169,21 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns `0`', fun t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, the function returns `0` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanrangeBy( 1, toAccessorArray(x), 1, accessor ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = new Array( 1 ); // sparse array + v = nanrangeBy( 1, toAccessorArray(x), 1, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); tape( 'the function supports a `stride` parameter', function test( t ) { var N; @@ -143,6 +210,31 @@ tape( 'the function supports a `stride` parameter', function test( t ) { t.end(); }); +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + var N; + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0, + NaN, // 4 + NaN + ]; + + N = floor( x.length / 2 ); + v = nanrangeBy( N, toAccessorArray(x), 2, accessor ); + + t.strictEqual( v, 12.0, 'returns expected value' ); + t.end(); +}); + tape( 'the function supports a negative `stride` parameter', function test( t ) { var N; var x; @@ -168,13 +260,38 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var N; + var x; + var v; + + x = [ + NaN, // 4 + NaN, + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + N = floor( x.length / 2 ); + v = nanrangeBy( N, toAccessorArray(x), -2, accessor ); + + t.strictEqual( v, 12.0, 'returns expected value' ); + t.end(); +}); + tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', function test( t ) { var x; var v; x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; - v = nanrangeBy( x.length, x, 0, accessor ); + v = nanrangeBy( x.length, toAccessorArray(x), 0, accessor ); t.strictEqual( v, 0.0, 'returns expected value' ); x = new Array( 1 ); // sparse array @@ -184,6 +301,22 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanrangeBy( x.length, toAccessorArray(x), 0, accessor ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = new Array( 1 ); // sparse array + v = nanrangeBy( 1, toAccessorArray(x), 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports view offsets', function test( t ) { var x0; var x1; @@ -213,6 +346,35 @@ tape( 'the function supports view offsets', function test( t ) { t.end(); }); +tape( 'the function supports view offsets (accessors)', function test( t ) { + var x0; + var x1; + var N; + var v; + + x0 = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + 6.0, + NaN, // 4 + NaN + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + N = floor(x1.length / 2); + + v = nanrangeBy( N, toAccessorArray(x1), 2, accessor ); + t.strictEqual( v, 12.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports providing a callback execution context', function test( t ) { var ctx; var x; @@ -231,3 +393,22 @@ tape( 'the function supports providing a callback execution context', function t return v * 2.0; } }); + +tape( 'the function supports providing a callback execution context (accessors)', function test( t ) { + var ctx; + var x; + + x = [ 1.0, 2.0, 3.0, NaN, 4.0, 5.0 ]; + ctx = { + 'count': 0 + }; + nanrangeBy( x.length, toAccessorArray(x), 1, accessor, ctx ); + + t.strictEqual( ctx.count, x.length, 'returns expected value' ); + t.end(); + + function accessor( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v * 2.0; + } +}); diff --git a/lib/node_modules/@stdlib/stats/base/nanrange-by/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/nanrange-by/test/test.ndarray.js index 33f6c629025d..967e1b1fabfc 100644 --- a/lib/node_modules/@stdlib/stats/base/nanrange-by/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanrange-by/test/test.ndarray.js @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var floor = require( '@stdlib/math/base/special/floor' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var nanrangeBy = require( './../lib/ndarray.js' ); @@ -86,6 +87,42 @@ tape( 'the function calculates the range of a strided array via a callback funct t.end(); }); +tape( 'the function calculates the range of a strided array via a callback function, ignoring NaN values (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, NaN, 5.0, 0.0, NaN, 3.0 ]; + v = nanrangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( v, 18.0, 'returns expected value' ); + + x = [ -4.0, NaN, -5.0 ]; + v = nanrangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( v, 2.0, 'returns expected value' ); + + x = [ -0.0, 0.0, NaN, -0.0 ]; + v = nanrangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = nanrangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = nanrangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = new Array( 5 ); // sparse array + v = nanrangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = new Array( 5 ); // sparse array + x[ 2 ] = 1.0; + v = nanrangeBy( x.length, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -101,6 +138,21 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanrangeBy( 0, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = nanrangeBy( -1, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter equal to `1`, the function returns `0`', function test( t ) { var x; var v; @@ -117,6 +169,22 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns `0`', fun t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, the function returns `0` (accessor)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanrangeBy( 1, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = new Array( 1 ); // sparse array + v = nanrangeBy( 1, toAccessorArray(x), 1, 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports a `stride` parameter', function test( t ) { var N; var x; @@ -142,6 +210,31 @@ tape( 'the function supports a `stride` parameter', function test( t ) { t.end(); }); +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + var N; + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0, + NaN, // 4 + NaN + ]; + + N = floor( x.length / 2 ); + v = nanrangeBy( N, toAccessorArray(x), 2, 0, accessor ); + + t.strictEqual( v, 12.0, 'returns expected value' ); + t.end(); +}); + tape( 'the function supports a negative `stride` parameter', function test( t ) { var N; var x; @@ -167,6 +260,31 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var N; + var x; + var v; + + x = [ + NaN, // 4 + NaN, + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + N = floor( x.length / 2 ); + v = nanrangeBy( N, toAccessorArray(x), -2, 8, accessor ); + + t.strictEqual( v, 12.0, 'returns expected value' ); + t.end(); +}); + tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', function test( t ) { var x; var v; @@ -183,6 +301,22 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = nanrangeBy( x.length, toAccessorArray(x), 0, 0, accessor ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = new Array( 1 ); // sparse array + v = nanrangeBy( 1, toAccessorArray(x), 0, 0, accessor ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports an offset parameter', function test( t ) { var x; var v; @@ -204,6 +338,27 @@ tape( 'the function supports an offset parameter', function test( t ) { t.end(); }); +tape( 'the function supports an offset parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, + -2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + -6.0, // 2 + NaN, + NaN // 3 + ]; + + v = nanrangeBy( 4, toAccessorArray(x), 2, 1, accessor ); + t.strictEqual( v, 20.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports providing a callback execution context', function test( t ) { var ctx; var x; @@ -222,3 +377,22 @@ tape( 'the function supports providing a callback execution context', function t return v * 2.0; } }); + +tape( 'the function supports providing a callback execution context (accessors)', function test( t ) { + var ctx; + var x; + + x = [ 1.0, 2.0, 3.0, NaN, 4.0, 5.0 ]; + ctx = { + 'count': 0 + }; + nanrangeBy( x.length, toAccessorArray(x), 1, 0, accessor, ctx ); + + t.strictEqual( ctx.count, x.length, 'returns expected value' ); + t.end(); + + function accessor( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v * 2.0; + } +}); \ No newline at end of file