Skip to content

Commit ef5019c

Browse files
committed
Add strided interface to add a constant and compute the sum using an improved Kahan-Babuska algorithm
1 parent 44439bb commit ef5019c

File tree

15 files changed

+1528
-0
lines changed

15 files changed

+1528
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
# gapxsumkbn
22+
23+
> Add a constant to each strided array element and compute the sum using improved Kahan–Babuška algorithm.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gapxsumkbn = require( '@stdlib/blas/ext/base/gapxsumkbn' );
37+
```
38+
39+
#### gapxsumkbn( N, alpha, x, stride )
40+
41+
Adds a constant to each strided array element and computes the sum using improved Kahan–Babuška algorithm.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, 2.0 ];
45+
var N = x.length;
46+
47+
var v = gapxsumkbn( N, 5.0, x, 1 );
48+
// returns 16.0
49+
```
50+
51+
The function has the following parameters:
52+
53+
- **N**: number of indexed elements.
54+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
55+
- **stride**: index increment for `x`.
56+
57+
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element in `x`,
58+
59+
```javascript
60+
var floor = require( '@stdlib/math/base/special/floor' );
61+
62+
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
63+
var N = floor( x.length / 2 );
64+
65+
var v = gapxsumkbn( N, 5.0, x, 2 );
66+
// returns 25.0
67+
```
68+
69+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
70+
71+
<!-- eslint-disable stdlib/capitalized-comments -->
72+
73+
```javascript
74+
var Float64Array = require( '@stdlib/array/float64' );
75+
var floor = require( '@stdlib/math/base/special/floor' );
76+
77+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
78+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
79+
80+
var N = floor( x0.length / 2 );
81+
82+
var v = gapxsumkbn( N, 5.0, x1, 2 );
83+
// returns 25.0
84+
```
85+
86+
#### gapxsumkbn.ndarray( N, alpha, x, stride, offset )
87+
88+
Adds a constant to each strided array element and computes the sum using improved Kahan–Babuška algorithm and alternative indexing semantics.
89+
90+
```javascript
91+
var x = [ 1.0, -2.0, 2.0 ];
92+
var N = x.length;
93+
94+
var v = gapxsumkbn.ndarray( N, 5.0, x, 1, 0 );
95+
// returns 16.0
96+
```
97+
98+
The function has the following additional parameters:
99+
100+
- **offset**: starting index for `x`.
101+
102+
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 access every other value in `x` starting from the second value
103+
104+
```javascript
105+
var floor = require( '@stdlib/math/base/special/floor' );
106+
107+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
108+
var N = floor( x.length / 2 );
109+
110+
var v = gapxsumkbn.ndarray( N, 5.0, x, 2, 1 );
111+
// returns 25.0
112+
```
113+
114+
</section>
115+
116+
<!-- /.usage -->
117+
118+
<section class="notes">
119+
120+
## Notes
121+
122+
- If `N <= 0`, both functions return `0.0`.
123+
- Depending on the environment, the typed versions ([`dapxsum`][@stdlib/blas/ext/base/dapxsum], [`sapxsum`][@stdlib/blas/ext/base/sapxsum], etc.) are likely to be significantly more performant.
124+
125+
</section>
126+
127+
<!-- /.notes -->
128+
129+
<section class="examples">
130+
131+
## Examples
132+
133+
<!-- eslint no-undef: "error" -->
134+
135+
```javascript
136+
var randu = require( '@stdlib/random/base/randu' );
137+
var round = require( '@stdlib/math/base/special/round' );
138+
var Float64Array = require( '@stdlib/array/float64' );
139+
var gapxsumkbn = require( '@stdlib/blas/ext/base/gapxsumkbn' );
140+
141+
var x;
142+
var i;
143+
144+
x = new Float64Array( 10 );
145+
for ( i = 0; i < x.length; i++ ) {
146+
x[ i ] = round( randu()*100.0 );
147+
}
148+
console.log( x );
149+
150+
var v = gapxsumkbn( x.length, 5.0, x, 1 );
151+
console.log( v );
152+
```
153+
154+
</section>
155+
156+
<!-- /.examples -->
157+
158+
* * *
159+
160+
<section class="references">
161+
162+
## References
163+
164+
- Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106][@neumaier:1974a].
165+
166+
</section>
167+
168+
<!-- /.references -->
169+
170+
<section class="links">
171+
172+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
173+
174+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
175+
176+
[@stdlib/blas/ext/base/dapxsum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dapxsum
177+
178+
[@stdlib/blas/ext/base/sapxsum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/sapxsum
179+
180+
[@neumaier:1974a]: https://doi.org/10.1002/zamm.19740540106
181+
182+
</section>
183+
184+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var gapxsumkbn = require( './../lib/gapxsumkbn.js' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x;
42+
var i;
43+
44+
x = [];
45+
for ( i = 0; i < len; i++ ) {
46+
x.push( ( randu()*20.0 ) - 10.0 );
47+
}
48+
return benchmark;
49+
50+
function benchmark( b ) {
51+
var v;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
v = gapxsumkbn( x.length, 5.0, x, 1 );
57+
if ( isnan( v ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( v ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( pkg+':len='+len, f );
92+
}
93+
}
94+
95+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var gapxsumkbn = require( './../lib/ndarray.js' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x;
42+
var i;
43+
44+
x = [];
45+
for ( i = 0; i < len; i++ ) {
46+
x.push( ( randu()*20.0 ) - 10.0 );
47+
}
48+
return benchmark;
49+
50+
function benchmark( b ) {
51+
var v;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
v = gapxsumkbn( x.length, 5.0, x, 1, 0 );
57+
if ( isnan( v ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( v ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( pkg+':ndarray:len='+len, f );
92+
}
93+
}
94+
95+
main();

0 commit comments

Comments
 (0)