diff --git a/lib/node_modules/@stdlib/stats/incr/nansumprod/README.md b/lib/node_modules/@stdlib/stats/incr/nansumprod/README.md new file mode 100644 index 000000000000..1e67e5769c7d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/README.md @@ -0,0 +1,178 @@ + + +# incrnansumprod + +> Compute a sum of products incrementally ignoring `NaN` values. + +
+ +The sum of products is defined as + + + +```math +s = \sum_{i=0}^{n-1} x_i y_i +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnansumprod = require( '@stdlib/stats/incr/nansumprod' ); +``` + +#### incrnansumprod() + +Returns an accumulator `function` which incrementally computes a sum of products, ignoring `NaN` values. + +```javascript +var accumulator = incrnansumprod(); +``` + +#### accumulator( \[x, y] ) + +If provided input values `x` and `y`, the accumulator function returns an updated sum. If not provided input value `x` and `y`, the accumulator function returns the current sum. + +```javascript +var accumulator = incrnansumprod(); + +var sum = accumulator( 2.0, 3.0 ); +// returns 6.0 + +sum = accumulator( 1.0, -1.0 ); +// returns 5.0 + +sum = accumulator( NaN, NaN ); +// returns 5.0 + +sum = accumulator( 3, NaN ); +// returns 5.0 + +sum = accumulator( 3.0, 4.0 ); +// returns 17.0 + +sum = accumulator(); +// returns 17.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. +- For long running accumulations or accumulations of large numbers, care should be taken to prevent overflow. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnansumprod = require( '@stdlib/stats/incr/nansumprod' ); + +var accumulator; +var v1; +var v2; +var i; + +// Initialize an accumulator: +accumulator = incrnansumprod(); + +// For each simulated datum, update the sum-product... +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v1 = NaN; + v2 = NaN; + } else if ( randu() < 0.3 ) { + v1 = NaN; + v2 = randu() * 100.0; + } else { + v1 = randu() * 100.0; + v2 = randu() * 100.0; + } + accumulator( v1, v2 ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nansumprod/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nansumprod/benchmark/benchmark.js new file mode 100644 index 000000000000..ea387dbf2b4b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/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 incrnansumprod = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnansumprod(); + 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 = incrnansumprod(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu()*10.0, 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/nansumprod/docs/img/equation_sum_product.svg b/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/img/equation_sum_product.svg new file mode 100644 index 000000000000..3e2856635040 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/img/equation_sum_product.svg @@ -0,0 +1,40 @@ + +s equals sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts x Subscript i Baseline y Subscript i + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/repl.txt new file mode 100644 index 000000000000..0aedf70e24cc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}() + Returns an accumulator function which incrementally computes a sum of + products, ignoring `NaN` values. + + If provided input values, the accumulator function returns an updated sum. + If not provided input values, the accumulator function returns the current + sum. + + For long running accumulations or accumulations of large numbers, care + should be taken to prevent overflow. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var s = accumulator() + null + > s = accumulator( 2.0, 3.0 ) + 6.0 + > s = accumulator( -5.0, 2.0 ) + -4.0 + > s = accumulator( NaN, 6.0 ) + -4.0 + > s = accumulator() + -4.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/types/index.d.ts new file mode 100644 index 000000000000..8f873b7f8467 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/types/index.d.ts @@ -0,0 +1,60 @@ +/** +* @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 arguments, returns an updated sum of products; otherwise, returns the current sum of products. +* +* @param x - value +* @param y - value +* @returns sum of products +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a sum of products, ignoring `NaN` values. +* +* @returns accumulator function +* +* @example +* var accumulator = incrsumprod(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0, 3.0 ); +* // returns 6.0 +* +* v = accumulator( -5.0, 2.0 ); +* // returns -4.0 +* +* v = accumulator( NaN, 6.0 ); +* // returns -4.0 +* +* v = accumulator(); +* // returns -4.0 +*/ +declare function incrnansumprod(): accumulator; + + +// EXPORTS // + +export = incrnansumprod; diff --git a/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/types/test.ts new file mode 100644 index 000000000000..957fc5b1c8ab --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/types/test.ts @@ -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. +*/ + +import incrnansumprod = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnansumprod(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided arguments... +{ + incrnansumprod( '5' ); // $ExpectError + incrnansumprod( 5 ); // $ExpectError + incrnansumprod( true ); // $ExpectError + incrnansumprod( false ); // $ExpectError + incrnansumprod( null ); // $ExpectError + incrnansumprod( undefined ); // $ExpectError + incrnansumprod( [] ); // $ExpectError + incrnansumprod( {} ); // $ExpectError + incrnansumprod( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnansumprod(); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnansumprod(); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nansumprod/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nansumprod/examples/index.js new file mode 100644 index 000000000000..d580d8f20c97 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/examples/index.js @@ -0,0 +1,49 @@ +/** +* @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 incrnansumprod = require( './../lib' ); + +var accumulator; +var sum; +var v1; +var v2; +var i; + +// Initialize an accumulator: +accumulator = incrnansumprod(); + +// For each simulated datum, update the sum-product... +console.log( '\nValue\tValue\tSum-Product\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v1 = NaN; + v2 = NaN; + } else if ( randu() < 0.3 ) { + v1 = NaN; + v2 = randu() * 100.0; + } else { + v1 = randu() * 100.0; + v2 = randu() * 100.0; + } + sum = accumulator( v1, v2 ); + console.log( '%d\t%d\t%d', v1.toFixed( 4 ), v2.toFixed( 4 ), sum.toFixed( 4 ) ); +} +console.log( '\nFinal sum-product: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nansumprod/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nansumprod/lib/index.js new file mode 100644 index 000000000000..a1008a89d265 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/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 sum of products incrementally ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nansumprod +* +* @example +* var incrnansumprod = require( '@stdlib/stats/incr/nansumprod' ); +* +* var accumulator = incrnansumprod(); +* +* var sum = accumulator( 2.0, 3.0 ); +* // returns 6.0 +* +* sum = accumulator( 1.0, -1.0 ); +* // returns 5.0 +* +* sum = accumulator( NaN, NaN ); +* // returns 5.0 +* +* sum = accumulator( 3, NaN ); +* // returns 5.0 +* +* sum = accumulator( 3.0, 4.0 ); +* // returns 17.0 +* +* sum = accumulator(); +* // returns 17.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nansumprod/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nansumprod/lib/main.js new file mode 100644 index 000000000000..ec2aa05606e0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/lib/main.js @@ -0,0 +1,78 @@ +/** +* @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 incrsumprod = require( '@stdlib/stats/incr/sumprod' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a sum of products, ignoring NaN values. +* +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnansumprod(); +* +* var sum = accumulator( 2.0, 3.0 ); +* // returns 6.0 +* +* sum = accumulator( 1.0, -1.0 ); +* // returns 5.0 +* +* sum = accumulator( NaN, NaN ); +* // returns 5.0 +* +* sum = accumulator( 3, NaN ); +* // returns 5.0 +* +* sum = accumulator( 3.0, 4.0 ); +* // returns 17.0 +* +* sum = accumulator(); +* // returns 17.0 +*/ +function incrnansumprod() { + var sumprod = incrsumprod(); + return accumulator; + + /** + * If provided input values, the accumulator function returns an updated sum. If not provided input values, the accumulator function returns the current sum. + * + * @private + * @param {number} [x] - first value + * @param {number} [y] - second value + * @returns {(number|null)} sum-product or null + */ + function accumulator( x, y ) { + if ( arguments.length === 0 || isnan( x ) || isnan( y ) ) { + return sumprod(); + } + return sumprod( x, y ); + } +} + + +// EXPORTS // + +module.exports = incrnansumprod; diff --git a/lib/node_modules/@stdlib/stats/incr/nansumprod/package.json b/lib/node_modules/@stdlib/stats/incr/nansumprod/package.json new file mode 100644 index 000000000000..f332659cb42e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/stats/incr/nansumprod", + "version": "0.0.0", + "description": "Compute a sum of products incrementally. 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", + "summation", + "sum", + "total", + "product", + "prod", + "dot", + "dot product", + "sum-product", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nansumprod/test/test.js b/lib/node_modules/@stdlib/stats/incr/nansumprod/test/test.js new file mode 100644 index 000000000000..77216a31ce5e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nansumprod/test/test.js @@ -0,0 +1,111 @@ +/** +* @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 incrnansumprod = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnansumprod, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnansumprod(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = incrnansumprod(); + t.equal( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a sum of products (aka dot product), ignoring NaN values', function test( t ) { + var expected; + var actual; + var data; + var acc; + var sum; + var N; + var x; + var y; + var i; + var a; + + data = [ + [ 2.0, 3.0 ], + [ 3.0, NaN ], + [ 2.0, 5.0 ], + [ NaN, -4.0 ], + [ 3.0, 0.0 ], + [ NaN, NaN ] + ]; + N = data.length; + + expected = []; + actual = []; + for ( a = 0; a < N; a++ ) { + expected.push(undefined); + actual.push(undefined); + } + + acc = incrnansumprod(); + + sum = 0; + for ( i = 0; i < N; i++ ) { + x = data[ i ][ 0 ]; + y = data[ i ][ 1 ]; + if ( isnan( x ) === false && isnan( y ) === false ) { + sum += x * y; + } + expected[ i ] = sum; + actual[ i ] = acc( x, y ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current sum of products', function test( t ) { + var data; + var acc; + var i; + + data = [ + [ 2.0, 3.0 ], + [ 3.0, NaN ], + [ 2.0, 5.0 ], + [ NaN, -4.0 ], + [ 3.0, 0.0 ], + [ NaN, NaN ] + ]; + acc = incrnansumprod(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ][ 0 ], data[ i ][ 1 ] ); + } + t.equal( acc(), 16.0, 'returns expected value' ); + t.end(); +});