Skip to content

Commit 18a0cf8

Browse files
feat: add blas/ext/base/snancusumkbn
--- 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 befc79b commit 18a0cf8

File tree

14 files changed

+2029
-0
lines changed

14 files changed

+2029
-0
lines changed
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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+
# snancusumkbn
22+
23+
> Calculate the cumulative sum of single-precision floating-point strided array elements using an improved Kahan–Babuška algorithm, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var snancusumkbn = require( '@stdlib/blas/ext/base/snancusumkbn' );
37+
```
38+
39+
#### snancusumkbn( N, sum, x, strideX, y, strideY )
40+
41+
Computes the cumulative sum of single-precision floating-point strided array elements using an improved Kahan–Babuška algorithm, ignoring `NaN` values.
42+
43+
```javascript
44+
var Float32Array = require( '@stdlib/array/float32' );
45+
46+
var x = new Float32Array( [ 1.0, NaN, 2.0 ] );
47+
var y = new Float32Array( x.length );
48+
49+
snancusumkbn( x.length, 0.0, x, 1, y, 1 );
50+
// y => <Float32Array>[ 1.0, 1.0, 3.0 ]
51+
52+
x = new Float32Array( [ 1.0, -2.0, NaN ] );
53+
y = new Float32Array( x.length );
54+
55+
snancusumkbn( x.length, 10.0, x, 1, y, 1 );
56+
// y => <Float32Array>[ 11.0, 9.0, 9.0 ]
57+
```
58+
59+
The function has the following parameters:
60+
61+
- **N**: number of indexed elements.
62+
- **sum**: initial sum.
63+
- **x**: input [`Float32Array`][@stdlib/array/float32].
64+
- **strideX**: stride length for `x`.
65+
- **y**: output [`Float32Array`][@stdlib/array/float32].
66+
- **strideY**: stride length for `y`.
67+
68+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the cumulative sum of every other element:
69+
70+
```javascript
71+
var Float32Array = require( '@stdlib/array/float32' );
72+
var x = new Float32Array( [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ] );
73+
var y = new Float32Array( x.length );
74+
75+
var v = snancusumkbn( 4, 0.0, x, 2, y, 1 );
76+
// y => <Float32Array>[ 1.0, 1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ]
77+
```
78+
79+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
80+
81+
<!-- eslint-disable stdlib/capitalized-comments -->
82+
83+
```javascript
84+
var Float32Array = require( '@stdlib/array/float32' );
85+
86+
// Initial arrays...
87+
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, NaN, -2.0, NaN, 3.0, 4.0 ] );
88+
var y0 = new Float32Array( x0.length );
89+
90+
// Create offset views...
91+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
92+
var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element
93+
94+
snancusumkbn( 4, 0.0, x1, -2, y1, 1 );
95+
// y0 => <Float32Array>[ 0.0, 0.0, 0.0, 4.0, 4.0, 4.0, 5.0, 0.0 ]
96+
```
97+
98+
#### snancusumkbn.ndarray( N, sum, x, strideX, offsetX, y, strideY, offsetY )
99+
100+
Computes the cumulative sum of single-precision floating-point strided array elements using an improved Kahan–Babuška algorithm, ignoring `NaN` values and alternative indexing semantics.
101+
102+
```javascript
103+
var Float32Array = require( '@stdlib/array/float32' );
104+
105+
var x = new Float32Array( [ 1.0, NaN, 2.0 ] );
106+
var y = new Float32Array( 3 );
107+
108+
snancusumkbn.ndarray( 3, 0.0, x, 1, 0, y, 1, 0 );
109+
// y => <Float32Array>[ 1.0, 1.0, 3.0 ]
110+
```
111+
112+
The function has the following additional parameters:
113+
114+
- **offsetX**: starting index for `x`.
115+
- **offsetY**: starting index for `y`.
116+
117+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to calculate the cumulative sum of every other element starting from the second element and to store in the last `N` elements of `y` starting from the last element:
118+
119+
```javascript
120+
var Float32Array = require( '@stdlib/array/float32' );
121+
122+
var x = new Float32Array( [ 2.0, 1.0, NaN, -2.0, NaN, 2.0, 3.0, 4.0 ] );
123+
var y = new Float32Array( x.length );
124+
125+
snancusumkbn.ndarray( 4, 0.0, x, 2, 1, y, -1, y.length-1 );
126+
// y => <Float32Array>[ 0.0, 0.0, 0.0, 0.0, 5.0, 1.0, -1.0, 1.0 ]
127+
```
128+
129+
</section>
130+
131+
<!-- /.usage -->
132+
133+
<section class="notes">
134+
135+
## Notes
136+
137+
- If `N <= 0`, both functions return `y` unchanged.
138+
139+
</section>
140+
141+
<!-- /.notes -->
142+
143+
<section class="examples">
144+
145+
## Examples
146+
147+
<!-- eslint no-undef: "error" -->
148+
149+
```javascript
150+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
151+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
152+
var filledarrayBy = require( '@stdlib/array/filled-by' );
153+
var snancusumkbn = require( '@stdlib/blas/ext/base/snancusumkbn' );
154+
155+
function rand() {
156+
if ( bernoulli( 0.7 ) > 0 ) {
157+
return discreteUniform( 0, 100 );
158+
}
159+
return NaN;
160+
}
161+
162+
function constant() {
163+
return 0.0;
164+
}
165+
166+
var x = filledarrayBy( 10, 'float32', rand );
167+
console.log( x );
168+
169+
var y = filledarrayBy( 10, 'float32', constant );
170+
console.log( y );
171+
172+
snancusumkbn( x.length, 0.0, x, 1, y, -1 );
173+
console.log( y );
174+
```
175+
176+
</section>
177+
178+
<!-- /.examples -->
179+
180+
<section class="references">
181+
182+
</section>
183+
184+
<!-- /.references -->
185+
186+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
187+
188+
<section class="related">
189+
190+
* * *
191+
192+
## See Also
193+
194+
- <span class="package-name">[`@stdlib/blas/ext/base/dcusumkbn`][@stdlib/blas/ext/base/dcusumkbn]</span><span class="delimiter">: </span><span class="description">calculate the cumulative sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm.</span>
195+
- <span class="package-name">[`@stdlib/blas/ext/base/gcusumkbn`][@stdlib/blas/ext/base/gcusumkbn]</span><span class="delimiter">: </span><span class="description">calculate the cumulative sum of strided array elements using an improved Kahan–Babuška algorithm.</span>
196+
- <span class="package-name">[`@stdlib/blas/ext/base/scusum`][@stdlib/blas/ext/base/scusum]</span><span class="delimiter">: </span><span class="description">calculate the cumulative sum of single-precision floating-point strided array elements.</span>
197+
198+
</section>
199+
200+
<!-- /.related -->
201+
202+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
203+
204+
<section class="links">
205+
206+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
207+
208+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
209+
210+
<!-- <related-links> -->
211+
212+
[@stdlib/blas/ext/base/dcusumkbn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dcusumkbn
213+
214+
[@stdlib/blas/ext/base/gcusumkbn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/gcusumkbn
215+
216+
[@stdlib/blas/ext/base/scusum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/scusum
217+
218+
<!-- </related-links> -->
219+
220+
</section>
221+
222+
<!-- /.links -->
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
27+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var pkg = require( './../package.json' ).name;
30+
var snancusumkbn = require( './../lib/main.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float32'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Returns a random number.
44+
*
45+
* @private
46+
* @returns {number} random number
47+
*/
48+
function rand() {
49+
if ( bernoulli( 0.8 ) > 0 ) {
50+
return uniform( -10.0, 10.0 );
51+
}
52+
return NaN;
53+
}
54+
55+
function constant() {
56+
return 0.0;
57+
}
58+
59+
/**
60+
* Creates a benchmark function.
61+
*
62+
* @private
63+
* @param {PositiveInteger} len - array length
64+
* @returns {Function} benchmark function
65+
*/
66+
function createBenchmark( len ) {
67+
var x = filledarrayBy( len, options.dtype, rand );
68+
var y = filledarrayBy( len, options.dtype, constant );
69+
return benchmark;
70+
71+
function benchmark( b ) {
72+
var v;
73+
var i;
74+
75+
for ( i = 0; i < len; i++ ) {
76+
y[ i ] = 0.0;
77+
}
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
x[ 0 ] += 1.0;
81+
v = snancusumkbn( x.length, 0.0, x, 1, y, 1 );
82+
if ( isnanf( v[ i % len ] ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
}
86+
b.toc();
87+
if ( isnanf( v[ i % len ] ) ) {
88+
b.fail( 'should not return NaN' );
89+
}
90+
b.pass( 'benchmark finished' );
91+
b.end();
92+
}
93+
}
94+
95+
96+
// MAIN //
97+
98+
/**
99+
* Main execution sequence.
100+
*
101+
* @private
102+
*/
103+
function main() {
104+
var len;
105+
var min;
106+
var max;
107+
var f;
108+
var i;
109+
110+
min = 1; // 10^min
111+
max = 6; // 10^max
112+
113+
for ( i = min; i <= max; i++ ) {
114+
len = pow( 10, i );
115+
f = createBenchmark( len );
116+
bench( pkg+':len='+len, f );
117+
}
118+
}
119+
120+
main();

0 commit comments

Comments
 (0)