diff --git a/lib/node_modules/@stdlib/stats/incr/nanrange/README.md b/lib/node_modules/@stdlib/stats/incr/nanrange/README.md new file mode 100644 index 000000000000..11d0922417a7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrange/README.md @@ -0,0 +1,147 @@ + + +# incrnanrange + +> Compute a [range][range] incrementally, ignoring `NaN` values. + +
+ +The [**range**][range] is defined as the difference between the maximum and minimum values. This implementation ignores `NaN` values. + +
+ + + +
+ +## Usage + +```javascript +var incrnanrange = require( '@stdlib/stats/incr/nanrange' ); +``` + +#### incrnanrange() + +Returns an accumulator `function` which incrementally computes a [range][range], ignoring `NaN` values. + +```javascript +var accumulator = incrnanrange(); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated [range][range], ignoring `NaN` values. If not provided an input value `x`, the accumulator function returns the current [range][range]. + +```javascript +var accumulator = incrnanrange(); + +var range = accumulator( -2.0 ); +// returns 0.0 + +range = accumulator( NaN ); +// returns 0.0 (ignores NaN) + +range = accumulator( 1.0 ); +// returns 3.0 + +range = accumulator( 3.0 ); +// returns 5.0 + +range = accumulator(); +// returns 5.0 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. +- Unlike `incrrange`, `incrnanrange` ignores `NaN` values, ensuring they do not affect the computed range. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanrange = require( '@stdlib/stats/incr/nanrange' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanrange(); + +// For each simulated datum, update the range... +for ( i = 0; i < 100; i++ ) { + v = ( randu() > 0.1 ) ? ( randu() * 100.0 ) : NaN; + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanrange/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanrange/benchmark/benchmark.js new file mode 100644 index 000000000000..55ec66038b3e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrange/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var incrnanrange = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanrange(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanrange(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu()*10.0 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/nanrange/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanrange/docs/repl.txt new file mode 100644 index 000000000000..c399e95ae1cf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrange/docs/repl.txt @@ -0,0 +1,30 @@ +{{alias}}() + Returns an accumulator function which incrementally computes a range, + ignoring `NaN` values. + + If provided a value, the accumulator function returns an updated range. If + not provided a value, the accumulator function returns the current range. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var v = accumulator() + null + > v = accumulator( -2.0 ) + 0.0 + > v = accumulator( NaN ) + 0.0 + > v = accumulator( 3.0 ) + 5.0 + > v = accumulator( 1.0 ) + 5.0 + > v = accumulator() + 5.0 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/incr/nanrange/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanrange/docs/types/index.d.ts new file mode 100644 index 000000000000..ed1b80172ca1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrange/docs/types/index.d.ts @@ -0,0 +1,66 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided a value, returns an updated range; otherwise, returns the current range. +* +* ## Notes +* +* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations. +* +* @param x - value +* @returns range +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a range. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanrange(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 0.0 +* +* v = accumulator( -1.0 ); +* // returns 3.0 +* +* v = accumulator( NaN ); +* // returns 3.0 +* +* v = accumulator( -3.0 ); +* // returns 5.0 +* +* v = accumulator(); +* // returns 5.0 +*/ +declare function incrnanrange(): accumulator; + + +// EXPORTS // + +export = incrnanrange; diff --git a/lib/node_modules/@stdlib/stats/incr/nanrange/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanrange/docs/types/test.ts new file mode 100644 index 000000000000..4d77a63c52db --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrange/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @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. +*/ + +import incrnanrange = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanrange(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided arguments... +{ + incrnanrange( '5' ); // $ExpectError + incrnanrange( 5 ); // $ExpectError + incrnanrange( true ); // $ExpectError + incrnanrange( false ); // $ExpectError + incrnanrange( null ); // $ExpectError + incrnanrange( undefined ); // $ExpectError + incrnanrange( [] ); // $ExpectError + incrnanrange( {} ); // $ExpectError + incrnanrange( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanrange(); + + acc(); // $ExpectType number | null + acc( 3.14 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanrange(); + + acc( '5' ); // $ExpectError + acc( true ); // $ExpectError + acc( false ); // $ExpectError + acc( null ); // $ExpectError + acc( [] ); // $ExpectError + acc( {} ); // $ExpectError + acc( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanrange/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanrange/examples/index.js new file mode 100644 index 000000000000..04a353ec0fd1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrange/examples/index.js @@ -0,0 +1,43 @@ +/** +* @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'; + +var randu = require( '@stdlib/random/base/randu' ); +var incrnanrange = require( './../lib' ); + +var accumulator; +var range; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanrange(); + +// For each simulated datum, update the range... +console.log( '\nValue\tRange\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + range = accumulator( v ); + console.log( '%d\t%s', v.toFixed( 4 ), ( range !== null && typeof range !== 'undefined' ) ? range.toFixed( 4 ) : 'NaN' ); +} +console.log( '\nFinal range: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanrange/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanrange/lib/index.js new file mode 100644 index 000000000000..4aa2df877513 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrange/lib/index.js @@ -0,0 +1,57 @@ +/** +* @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'; + +/** +* Compute a range incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanrange +* +* @example +* var incrnanrange = require( '@stdlib/stats/incr/nanrange' ); +* +* var accumulator = incrnanrange(); +* +* var range = accumulator(); +* // returns null +* +* range = accumulator( 3.14 ); +* // returns 0.0 +* +* range = accumulator( NaN ); +* // returns 8.14 +* +* range = accumulator( -5.0 ); +* // returns 8.14 +* +* range = accumulator( 10.1 ); +* // returns 15.1 +* +* range = accumulator(); +* // returns 15.1 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanrange/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanrange/lib/main.js new file mode 100644 index 000000000000..0246ec0b2209 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrange/lib/main.js @@ -0,0 +1,91 @@ +/** +* @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 PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a range while ignoring NaN values. +* +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanrange(); +* +* var range = accumulator(); +* // returns null +* +* range = accumulator( 3.14 ); +* // returns 0.0 +* +* range = accumulator( NaN ); +* // returns 0.0 +* +* range = accumulator( -5.0 ); +* // returns 8.14 +* +* range = accumulator( 10.1 ); +* // returns 15.1 +* +* range = accumulator(); +* // returns 15.1 +*/ +function incrnanrange() { + var range; + var max = NINF; + var min = PINF; + + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated range while ignoring NaN values. If not provided a value, the accumulator function returns the current range. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} range or null + */ + function accumulator( x ) { + if ( arguments.length === 0 ) { + return ( range === void 0 ) ? null : range; + } + if ( isnan( x ) ) { + return range; + } + if ( x > max ) { + max = x; + } + if ( x < min ) { + min = x; + } + range = max - min; + return range; + } +} + + +// EXPORTS // + +module.exports = incrnanrange; diff --git a/lib/node_modules/@stdlib/stats/incr/nanrange/package.json b/lib/node_modules/@stdlib/stats/incr/nanrange/package.json new file mode 100644 index 000000000000..f2408612cf2f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrange/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/stats/incr/nanrange", + "version": "0.0.0", + "description": "Compute a range incrementally, while ignoring `NaN` values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "maximum", + "max", + "minimum", + "min", + "range", + "dispersion", + "variance", + "domain", + "extent", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanrange/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanrange/test/test.js new file mode 100644 index 000000000000..0a64cb2e615e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanrange/test/test.js @@ -0,0 +1,125 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrnanrange = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanrange, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanrange(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'if not provided any values, the initial returned range is `null`', function test( t ) { + var acc = incrnanrange(); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a range while ignoring NaN values', function test( t ) { + var expected; + var actual; + var data; + var acc; + var max; + var min; + var N; + var d; + var i; + + data = [ 2.0, NaN, 3.0, NaN, 4.0, 43.0, NaN, 4.0 ]; + N = data.length; + + expected = []; + actual = []; + + acc = incrnanrange(); + + max = null; + min = null; + for ( i = 0; i < N; i++ ) { + d = data[ i ]; + if ( !isnan( d ) ) { + if ( max === null || d > max ) { + max = d; + } + if ( min === null || d < min ) { + min = d; + } + } + expected.push( ( max !== null && min !== null ) ? ( max - min ) : null ); + actual.push( acc( d ) ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current range while ignoring the NaN value', function test( t ) { + var data; + var acc; + var i; + + data = [ NaN, -2.0, NaN, 3.0, 1.0 ]; + acc = incrnanrange(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 5.0, 'returns the current accumulated range' ); + t.end(); +}); + +tape( 'if provided a `NaN`, the accumulator function returns `NaN` for all future invocations', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, NaN, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]; + acc = incrnanrange(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( isnan( acc() ), false, 'returns expected value' ); + t.end(); +}); + +tape( 'if all provided values are `NaN`, the accumulator function returns `null`', function test( t ) { + var data; + var acc; + var i; + + data = [ NaN, NaN, NaN ]; + acc = incrnanrange(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), null, 'returns null' ); + t.end(); +});