Skip to content

Commit c6a3eec

Browse files
committed
feat(lib): added nansumprod package
This commits add a package nansumprod, which works like sumprod in which it calculates sum of product of two number iteratively ignoring NaN values --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 77c2bfd commit c6a3eec

File tree

11 files changed

+814
-0
lines changed

11 files changed

+814
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# incrnansumprod
22+
23+
> Compute a sum of products incrementally ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The sum of products is defined as
28+
29+
<!-- <equation class="equation" label="eq:sum_product" align="center" raw="s = \sum_{i=0}^{n-1} x_i y_i" alt="Equation for the sum of products."> -->
30+
31+
```math
32+
s = \sum_{i=0}^{n-1} x_i y_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="s = \sum_{i=0}^{n-1} x_i y_i" data-equation="eq:sum_product">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/nansumprod/docs/img/equation_sum_product.svg" alt="Equation for the sum of products.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var incrnansumprod = require( '@stdlib/stats/incr/nansumprod' );
52+
```
53+
54+
#### incrnansumprod()
55+
56+
Returns an accumulator `function` which incrementally computes a sum of products, ignoring `NaN` values.
57+
58+
```javascript
59+
var accumulator = incrnansumprod();
60+
```
61+
62+
#### accumulator( \[x, y] )
63+
64+
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.
65+
66+
```javascript
67+
var accumulator = incrnansumprod();
68+
69+
var sum = accumulator( 2.0, 3.0 );
70+
// returns 6.0
71+
72+
sum = accumulator( 1.0, -1.0 );
73+
// returns 5.0
74+
75+
sum = accumulator( NaN, NaN );
76+
// returns 5.0
77+
78+
sum = accumulator( 3, NaN );
79+
// returns 5.0
80+
81+
sum = accumulator( 3.0, 4.0 );
82+
// returns 17.0
83+
84+
sum = accumulator();
85+
// returns 17.0
86+
```
87+
88+
</section>
89+
90+
<!-- /.usage -->
91+
92+
<section class="notes">
93+
94+
## Notes
95+
96+
- 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.
97+
- For long running accumulations or accumulations of large numbers, care should be taken to prevent overflow.
98+
99+
</section>
100+
101+
<!-- /.notes -->
102+
103+
<section class="examples">
104+
105+
## Examples
106+
107+
<!-- eslint no-undef: "error" -->
108+
109+
```javascript
110+
var randu = require( '@stdlib/random/base/randu' );
111+
var incrnansumprod = require( '@stdlib/stats/incr/nansumprod' );
112+
113+
var accumulator;
114+
var v1;
115+
var v2;
116+
var i;
117+
118+
// Initialize an accumulator:
119+
accumulator = incrnansumprod();
120+
121+
// For each simulated datum, update the sum-product...
122+
for ( i = 0; i < 100; i++ ) {
123+
if ( randu() < 0.2 ) {
124+
v1 = NaN;
125+
v2 = NaN;
126+
} else if ( randu() < 0.3 ) {
127+
v1 = NaN;
128+
v2 = randu() * 100.0;
129+
} else {
130+
v1 = randu() * 100.0;
131+
v2 = randu() * 100.0;
132+
}
133+
accumulator( v1, v2 );
134+
}
135+
console.log( accumulator() );
136+
```
137+
138+
</section>
139+
140+
<!-- /.examples -->
141+
142+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
143+
144+
<section class="related">
145+
146+
* * *
147+
148+
## See Also
149+
150+
- <span class="package-name">[`@stdlib/stats/incr/sumprod`][@stdlib/stats/incr/sumprod]</span><span class="delimiter">: </span><span class="description">Compute a sum of products incrementally.</span>
151+
- <span class="package-name">[`@stdlib/stats/incr/nansum`][@stdlib/stats/incr/nansum]</span><span class="delimiter">: </span><span class="description">Compute a sum incrementally, ignoring `NaN` values.</span>
152+
- <span class="package-name">[`@stdlib/stats/incr/prod`][@stdlib/stats/incr/prod]</span><span class="delimiter">: </span><span class="description">compute a product incrementally.</span>
153+
- <span class="package-name">[`@stdlib/stats/incr/sum`][@stdlib/stats/incr/sum]</span><span class="delimiter">: </span><span class="description">compute a sum incrementally.</span>
154+
155+
156+
</section>
157+
158+
<!-- /.related -->
159+
160+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="links">
163+
164+
<!-- <related-links> -->
165+
166+
[@stdlib/stats/incr/sumprod]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/sumprod
167+
168+
[@stdlib/stats/incr/prod]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/prod
169+
170+
[@stdlib/stats/incr/sum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/sum
171+
172+
[@stdlib/stats/incr/nansum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nansum
173+
174+
<!-- </related-links> -->
175+
176+
</section>
177+
178+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrnansumprod = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnansumprod();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnansumprod();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu()*10.0, randu()*10.0 );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 40 additions & 0 deletions
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes a sum of
4+
products, ignoring `NaN` values.
5+
6+
If provided input values, the accumulator function returns an updated sum.
7+
If not provided input values, the accumulator function returns the current
8+
sum.
9+
10+
For long running accumulations or accumulations of large numbers, care
11+
should be taken to prevent overflow.
12+
13+
Returns
14+
-------
15+
acc: Function
16+
Accumulator function.
17+
18+
Examples
19+
--------
20+
> var accumulator = {{alias}}();
21+
> var s = accumulator()
22+
null
23+
> s = accumulator( 2.0, 3.0 )
24+
6.0
25+
> s = accumulator( -5.0, 2.0 )
26+
-4.0
27+
> s = accumulator( NaN, 6.0 )
28+
-4.0
29+
> s = accumulator()
30+
-4.0
31+
32+
See Also
33+
--------
34+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* If provided arguments, returns an updated sum of products; otherwise, returns the current sum of products.
25+
*
26+
* @param x - value
27+
* @param y - value
28+
* @returns sum of products
29+
*/
30+
type accumulator = ( x?: number, y?: number ) => number | null;
31+
32+
/**
33+
* Returns an accumulator function which incrementally computes a sum of products, ignoring `NaN` values.
34+
*
35+
* @returns accumulator function
36+
*
37+
* @example
38+
* var accumulator = incrsumprod();
39+
*
40+
* var v = accumulator();
41+
* // returns null
42+
*
43+
* v = accumulator( 2.0, 3.0 );
44+
* // returns 6.0
45+
*
46+
* v = accumulator( -5.0, 2.0 );
47+
* // returns -4.0
48+
*
49+
* v = accumulator( NaN, 6.0 );
50+
* // returns -4.0
51+
*
52+
* v = accumulator();
53+
* // returns -4.0
54+
*/
55+
declare function incrnansumprod(): accumulator;
56+
57+
58+
// EXPORTS //
59+
60+
export = incrnansumprod;

0 commit comments

Comments
 (0)