Skip to content

Commit d9231e2

Browse files
MeKaustubh07kgryte
andauthored
feat: add blas/ext/base/ndarray/csumkbn
PR-URL: #8772 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Gururaj Gurram <gururajgurram1512@gmail.com>
1 parent 09c6032 commit d9231e2

File tree

10 files changed

+775
-0
lines changed

10 files changed

+775
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# csumkbn
22+
23+
> Compute the sum of all elements in a one-dimensional single-precision complex floating-point ndarray using an 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 csumkbn = require( '@stdlib/blas/ext/base/ndarray/csumkbn' );
37+
```
38+
39+
#### csumkbn( arrays )
40+
41+
Computes the sum of all elements in a one-dimensional single-precision complex floating-point ndarray using an improved Kahan–Babuška algorithm.
42+
43+
```javascript
44+
var Complex64Array = require( '@stdlib/array/complex64' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = new Complex64Array( [ 1.0, -2.0, 2.0, 3.0 ] );
48+
var x = new ndarray( 'complex64', xbuf, [ 2 ], [ 1 ], 0, 'row-major' );
49+
50+
var v = csumkbn( [ x ] );
51+
// returns <Complex64>[ 3.0, 1.0 ]
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **arrays**: array-like object containing a one-dimensional input ndarray.
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<section class="notes">
63+
64+
## Notes
65+
66+
- If provided an empty one-dimensional ndarray, the function returns `0.0 + 0.0i`.
67+
68+
</section>
69+
70+
<!-- /.notes -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
80+
var Complex64Array = require( '@stdlib/array/complex64' );
81+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
82+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
83+
var csumkbn = require( '@stdlib/blas/ext/base/ndarray/csumkbn' );
84+
85+
var xbuf = discreteUniform( 10, -50, 50, {
86+
'dtype': 'float32'
87+
});
88+
xbuf = new Complex64Array( xbuf );
89+
90+
var x = new ndarray( 'complex64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
91+
console.log( ndarray2array( x ) );
92+
93+
var v = csumkbn( [ x ] );
94+
console.log( v );
95+
```
96+
97+
</section>
98+
99+
<!-- /.examples -->
100+
101+
<section class="references">
102+
103+
## References
104+
105+
- 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].
106+
107+
</section>
108+
109+
<!-- /.references -->
110+
111+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
112+
113+
<section class="related">
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
[@neumaier:1974a]: https://doi.org/10.1002/zamm.19740540106
124+
125+
</section>
126+
127+
<!-- /.links -->
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var realf = require( '@stdlib/complex/float32/real' );
27+
var imagf = require( '@stdlib/complex/float32/imag' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var Complex64Array = require( '@stdlib/array/complex64' );
31+
var pkg = require( './../package.json' ).name;
32+
var csumkbn = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var options = {
38+
'dtype': 'complex64'
39+
};
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Creates a benchmark function.
46+
*
47+
* @private
48+
* @param {PositiveInteger} len - array length
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( len ) {
52+
var xbuf;
53+
var x;
54+
55+
xbuf = uniform( len*2, -10.0, 10.0, {
56+
'dtype': 'float32'
57+
});
58+
x = new ndarray( options.dtype, new Complex64Array( xbuf ), [ len ], [ 1 ], 0, 'row-major' );
59+
60+
return benchmark;
61+
62+
function benchmark( b ) {
63+
var v;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
v = csumkbn( [ x ] );
69+
if ( isnanf( realf( v ) ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnanf( imagf( v ) ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( pkg+':len='+len, f );
104+
}
105+
}
106+
107+
main();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( arrays )
3+
Computes the sum of all elements in a one-dimensional single-precision
4+
complex floating-point ndarray using an improved Kahan–Babuška algorithm.
5+
6+
If provided an empty ndarray, the function returns `0.0`.
7+
8+
Parameters
9+
----------
10+
arrays: ArrayLikeObject<ndarray>
11+
Array-like object containing a one-dimensional input ndarray.
12+
13+
Returns
14+
-------
15+
out: Complex64
16+
Sum.
17+
18+
Examples
19+
--------
20+
> var xbuf = new {{alias:@stdlib/array/complex64}}( [ 1.0, -2.0, 2.0, 3.0 ] );
21+
> var dt = 'complex64';
22+
> var sh = [ 2 ];
23+
> var sx = [ 1 ];
24+
> var ox = 0;
25+
> var ord = 'row-major';
26+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
27+
> {{alias}}( [ x ] )
28+
<Complex64>[ 3.0, 1.0 ]
29+
30+
See Also
31+
--------
32+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
import { complex64ndarray } from '@stdlib/types/ndarray';
24+
import { Complex64 } from '@stdlib/types/complex';
25+
26+
/**
27+
* Computes the sum of all elements in a one-dimensional single-precision complex floating-point ndarray using an improved Kahan–Babuška algorithm.
28+
*
29+
* @param arrays - array-like object containing an input ndarray
30+
* @returns sum
31+
*
32+
* @example
33+
* var Complex64Array = require( '@stdlib/array/complex64' );
34+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
35+
*
36+
* var xbuf = new Complex64Array( [ 1.0, -2.0, 2.0, 3.0 ] );
37+
* var x = new ndarray( 'complex64', xbuf, [ 2 ], [ 1 ], 0, 'row-major' );
38+
*
39+
* var v = csumkbn( [ x ] );
40+
* // returns <Complex64>[ 3.0, 1.0 ]
41+
*/
42+
declare function csumkbn( arrays: [ complex64ndarray ] ): Complex64;
43+
44+
45+
// EXPORTS //
46+
47+
export = csumkbn;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import csumkbn = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns a complex number...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'complex64'
31+
});
32+
33+
csumkbn( [ x ] ); // $ExpectType Complex64
34+
}
35+
36+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
37+
{
38+
csumkbn( '10' ); // $ExpectError
39+
csumkbn( 10 ); // $ExpectError
40+
csumkbn( true ); // $ExpectError
41+
csumkbn( false ); // $ExpectError
42+
csumkbn( null ); // $ExpectError
43+
csumkbn( undefined ); // $ExpectError
44+
csumkbn( [] ); // $ExpectError
45+
csumkbn( {} ); // $ExpectError
46+
csumkbn( ( x: number ): number => x ); // $ExpectError
47+
}
48+
49+
// The compiler throws an error if the function is provided an unsupported number of arguments...
50+
{
51+
const x = zeros( [ 10 ], {
52+
'dtype': 'complex64'
53+
});
54+
55+
csumkbn(); // $ExpectError
56+
csumkbn( [ x ], {} ); // $ExpectError
57+
}

0 commit comments

Comments
 (0)