Skip to content

Commit d1c43bf

Browse files
committed
feat: add blas/ext/base/ndarray/zsum
--- 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 22f1408 commit d1c43bf

File tree

10 files changed

+759
-0
lines changed

10 files changed

+759
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
# zsum
22+
23+
> Compute the sum of all elements in a one-dimensional double-precision complex floating-point ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var zsum = require( '@stdlib/blas/ext/base/ndarray/zsum' );
37+
```
38+
39+
#### zsum( arrays )
40+
41+
Computes the sum of all elements in a one-dimensional double-precision complex floating-point ndarray.
42+
43+
```javascript
44+
var Complex128Array = require( '@stdlib/array/complex128' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = new Complex128Array( [ 1.0, 3.0, 4.0, 2.0 ] );
48+
var x = new ndarray( 'complex128', xbuf, [ 2 ], [ 1 ], 0, 'row-major' );
49+
50+
var v = zsum( [ x ] );
51+
// returns <Complex128>[ 5.0, 5.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 Complex128Array = require( '@stdlib/array/complex128' );
81+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
82+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
83+
var zsum = require( '@stdlib/blas/ext/base/ndarray/zsum' );
84+
85+
var xbuf = discreteUniform( 10, -50, 50, {
86+
'dtype': 'float64'
87+
});
88+
xbuf = new Complex128Array( xbuf );
89+
90+
var x = new ndarray( 'complex128', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
91+
console.log( ndarray2array( x ) );
92+
93+
var v = zsum( [ x ] );
94+
console.log( v );
95+
```
96+
97+
</section>
98+
99+
<!-- /.examples -->
100+
101+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
102+
103+
<section class="related">
104+
105+
</section>
106+
107+
<!-- /.related -->
108+
109+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
110+
111+
<section class="links">
112+
113+
</section>
114+
115+
<!-- /.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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var real = require( '@stdlib/complex/float64/real' );
27+
var imag = require( '@stdlib/complex/float64/imag' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var Complex128Array = require( '@stdlib/array/complex128' );
31+
var pkg = require( './../package.json' ).name;
32+
var zsum = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var options = {
38+
'dtype': 'complex128'
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': 'float64'
57+
});
58+
x = new ndarray( options.dtype, new Complex128Array( 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 = zsum( [ x ] );
69+
if ( isnan( real( v ) ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( imag( 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 double-precision
4+
complex floating-point ndarray.
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: number
16+
Sum.
17+
18+
Examples
19+
--------
20+
> var xbuf = new {{alias:@stdlib/array/complex128}}( [ 1.0, 2.0, 3.0, 4.0 ] );
21+
> var dt = 'complex128';
22+
> var sh = [ xbuf.length ];
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+
<Complex128>[ 4.0, 6.0 ]
29+
30+
See Also
31+
--------
32+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 { complex128ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the sum of all elements in a one-dimensional double-precision complex floating-point ndarray.
27+
*
28+
* @param arrays - array-like object containing an input ndarray
29+
* @returns sum
30+
*
31+
* @example
32+
* var Complex128Array = require( '@stdlib/array/complex128' );
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
*
35+
* var xbuf = new Complex128Array( [ 1.0, 3.0, 4.0, 2.0 ] );
36+
* var x = new ndarray( 'complex128', xbuf, [ 2 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* var v = zsum( [ x ] );
39+
* // returns <Complex128>[ 5.0, 5.0 ]
40+
*/
41+
declare function zsum( arrays: [ complex128ndarray ] ): number;
42+
43+
44+
// EXPORTS //
45+
46+
export = zsum;
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 zsum = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns a number...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'complex128'
31+
});
32+
33+
zsum( [ x ] ); // $ExpectType number
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+
zsum( '10' ); // $ExpectError
39+
zsum( 10 ); // $ExpectError
40+
zsum( true ); // $ExpectError
41+
zsum( false ); // $ExpectError
42+
zsum( null ); // $ExpectError
43+
zsum( undefined ); // $ExpectError
44+
zsum( [] ); // $ExpectError
45+
zsum( {} ); // $ExpectError
46+
zsum( ( 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': 'complex128'
53+
});
54+
55+
zsum(); // $ExpectError
56+
zsum( [ x ], {} ); // $ExpectError
57+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var Complex128Array = require( '@stdlib/array/complex128' );
23+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
24+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
25+
var zsum = require( './../lib' );
26+
27+
var xbuf = discreteUniform( 10, -50, 50, {
28+
'dtype': 'float64'
29+
});
30+
xbuf = new Complex128Array( xbuf );
31+
32+
var x = new ndarray( 'complex128', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
33+
console.log( ndarray2array( x ) );
34+
35+
var v = zsum( [ x ] );
36+
console.log( v );

0 commit comments

Comments
 (0)