Skip to content

Commit cbead2a

Browse files
committed
feat: add blas/base/dzasum
--- 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: na - 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 53c0427 commit cbead2a

File tree

14 files changed

+1347
-0
lines changed

14 files changed

+1347
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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+
# dzasum
22+
23+
> Compute the sum of the [absolute values][@stdlib/blas/base/dcab1] of each element in a double-precision [complex][@stdlib/complex/float64/ctor] floating-point vector.
24+
25+
<section class="intro">
26+
27+
dzasum is defined as
28+
29+
<!-- <equation class="equation" label="eq:l1norm" align="center" raw="\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \left( |a_i| + |b_i| \right)" alt="L1 norm definition."> -->
30+
31+
```math
32+
\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \left( |a_i| + |b_i| \right)
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \left( |a_i| + |b_i| \right)" data-equation="eq:l1norm">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@c403cb0cbb15d9b7b453e3cea34ca2379500ddd4/lib/node_modules/@stdlib/blas/base/dasum/docs/img/equation_l1norm.svg" alt="L1 norm definition.">
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 dzasum = require( '@stdlib/blas/base/dzasum' );
52+
```
53+
54+
#### dzasum( N, x, stride )
55+
56+
Compute the sum of the [absolute values][@stdlib/blas/base/dcab1] of each element in a double-precision [complex][@stdlib/complex/float64/ctor] floating-point vector.
57+
58+
```javascript
59+
var Complex128Array = require( '@stdlib/array/complex128' );
60+
61+
var x = new Complex128Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
62+
63+
var sum = dzasum( x.length, x, 1 );
64+
// returns 19.0
65+
```
66+
67+
The function has the following parameters:
68+
69+
- **N**: number of indexed elements.
70+
- **x**: input [`Complex128Array`][complex128array].
71+
- **stride**: index increment.
72+
73+
The `N` and `stride` parameters determine which elements in `x` are used to compute the sum. For example, to sum every other value,
74+
75+
```javascript
76+
var Complex128Array = require( '@stdlib/array/complex128' );
77+
78+
var x = new Complex128Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
79+
80+
var sum = dzasum( 2, x, 2 );
81+
// returns 7.0
82+
```
83+
84+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
85+
86+
```javascript
87+
var Complex128Array = require( '@stdlib/array/complex128' );
88+
89+
// Initial array...
90+
var x0 = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 ] );
91+
92+
// Create an offset view...
93+
var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
94+
95+
// Sum every other value...
96+
var sum = dzasum( 2, x1, 2 );
97+
// returns 22.0
98+
```
99+
100+
If `N` is less than or equal to `0`, the function returns `0`.
101+
102+
#### dzasum.ndarray( N, x, stride, offset )
103+
104+
Compute the sum of the [absolute values][@stdlib/blas/base/dcab1] of each element in a double-precision [complex][@stdlib/complex/float64/ctor] floating-point vector using alternative indexing semantics.
105+
106+
```javascript
107+
var Complex128Array = require( '@stdlib/array/complex128' );
108+
109+
var x = new Complex128Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
110+
111+
var sum = dzasum.ndarray( x.length, x, 1, 0 );
112+
// returns 19.0
113+
```
114+
115+
The function has the following additional parameters:
116+
117+
- **offset**: starting index.
118+
119+
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 sum the last three elements,
120+
121+
```javascript
122+
var Complex128Array = require( '@stdlib/array/complex128' );
123+
124+
var x = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 ] );
125+
126+
var sum = dzasum.ndarray( 3, x, 1, x.length-3 );
127+
// returns 33.0
128+
129+
// Using a negative stride to sum from the last element:
130+
sum = dzasum.ndarray( 3, x, -1, x.length-1 );
131+
// returns 33.0
132+
```
133+
134+
</section>
135+
136+
<!-- /.usage -->
137+
138+
<section class="notes">
139+
140+
## Notes
141+
142+
- If `N <= 0`, the sum is `0`.
143+
- `dzasum()` corresponds to the [BLAS][blas] level 1 function dzasum.
144+
145+
</section>
146+
147+
<!-- /.notes -->
148+
149+
<section class="examples">
150+
151+
## Examples
152+
153+
<!-- eslint no-undef: "error" -->
154+
155+
```javascript
156+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
157+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
158+
var filledarrayBy = require( '@stdlib/array/filled-by' );
159+
var dzasum = require( '@stdlib/blas/base/dzasum' );
160+
161+
function rand() {
162+
return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
163+
}
164+
165+
// Generate random input arrays:
166+
var x = filledarrayBy( 10, 'complex128', rand );
167+
console.log( x.toString() );
168+
169+
var out = dzasum( x.length, x, 1 );
170+
console.log( out );
171+
```
172+
173+
</section>
174+
175+
<!-- /.examples -->
176+
177+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
178+
179+
<section class="related">
180+
181+
</section>
182+
183+
<!-- /.related -->
184+
185+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
186+
187+
<section class="links">
188+
189+
[blas]: http://www.netlib.org/blas
190+
191+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
192+
193+
[@stdlib/blas/base/dcab1]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dcab1
194+
195+
[complex128array]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
196+
197+
[@stdlib/complex/float64/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/ctor
198+
199+
<!-- <related-links> -->
200+
201+
<!-- </related-links> -->
202+
203+
</section>
204+
205+
<!-- /.links -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 pow = require( '@stdlib/math/base/special/pow' );
27+
var Complex128Array = require( '@stdlib/array/complex128' );
28+
var pkg = require( './../package.json' ).name;
29+
var dzasum = require( './../lib/dzasum.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float64'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var x;
50+
51+
x = new Complex128Array( uniform( len*2, -100.0, 100.0, options ) );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var y;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
y = dzasum( x.length, x, 1 );
67+
if ( isnan( y ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( y ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 6; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = pow( 10, i );
100+
f = createBenchmark( len );
101+
bench( pkg+':len='+len, f );
102+
}
103+
}
104+
105+
main();

0 commit comments

Comments
 (0)