Skip to content

Commit 1ff31b2

Browse files
gururaj1512kgryteShabiShett07MeKaustubh07
authored
feat: add blas/base/dzasum
PR-URL: #4697 Co-authored-by: Athan Reines <kgryte@gmail.com> Co-authored-by: Shabareesh Shetty <shabishetty07@gmail.com> Co-authored-by: Kaustubh Patange <kaustubh.mp007@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Shabareesh Shetty <shabishetty07@gmail.com> Reviewed-by: Kaustubh Patange <kaustubh.mp007@gmail.com> Signed-off-by: Shabareesh Shetty <shabishetty07@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Ref: #2039
1 parent 95a8c41 commit 1ff31b2

15 files changed

Lines changed: 1680 additions & 0 deletions

File tree

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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 of the real and imaginary components of a double-precision complex floating-point strided array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dzasum = require( '@stdlib/blas/base/dzasum' );
31+
```
32+
33+
#### dzasum( N, x, strideX )
34+
35+
Computes the sum of the absolute values of the real and imaginary components of a double-precision complex floating-point strided array.
36+
37+
```javascript
38+
var Complex128Array = require( '@stdlib/array/complex128' );
39+
40+
var x = new Complex128Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2 ] );
41+
42+
var out = dzasum( 4, x, 1 );
43+
// returns ~1.6
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **x**: input [`Complex128Array`][@stdlib/array/complex128].
50+
- **strideX**: stride length.
51+
52+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to traverse every other value,
53+
54+
```javascript
55+
var Complex128Array = require( '@stdlib/array/complex128' );
56+
57+
var x = new Complex128Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
58+
59+
var out = dzasum( 2, x, 2 );
60+
// returns 7.0
61+
```
62+
63+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
64+
65+
```javascript
66+
var Complex128Array = require( '@stdlib/array/complex128' );
67+
68+
// Initial array:
69+
var x0 = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
70+
71+
// Create an offset view:
72+
var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
73+
74+
// Compute the sum of absolute values:
75+
var out = dzasum( 2, x1, 1 );
76+
// returns 18.0
77+
```
78+
79+
#### dzasum.ndarray( N, x, strideX, offsetX )
80+
81+
Computes the sum of the absolute values of the real and imaginary components of a double-precision complex floating-point strided array using alternative indexing semantics.
82+
83+
```javascript
84+
var Complex128Array = require( '@stdlib/array/complex128' );
85+
86+
var x = new Complex128Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2 ] );
87+
88+
var out = dzasum.ndarray( 4, x, 1, 0 );
89+
// returns ~1.6
90+
```
91+
92+
The function has the following additional parameters:
93+
94+
- **offsetX**: starting index.
95+
96+
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 start from the second element
97+
98+
```javascript
99+
var Complex128Array = require( '@stdlib/array/complex128' );
100+
101+
var x = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
102+
103+
var out = dzasum.ndarray( 2, x, 1, 1 );
104+
// returns 18.0
105+
```
106+
107+
</section>
108+
109+
<!-- /.usage -->
110+
111+
<section class="notes">
112+
113+
## Notes
114+
115+
- If `N <= 0`, both functions return `0.0`.
116+
- `dzasum()` corresponds to the [BLAS][blas] level 1 function [`dzasum`][dzasum].
117+
118+
</section>
119+
120+
<!-- /.notes -->
121+
122+
<section class="examples">
123+
124+
## Examples
125+
126+
<!-- eslint no-undef: "error" -->
127+
128+
```javascript
129+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
130+
var filledarrayBy = require( '@stdlib/array/filled-by' );
131+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
132+
var dzasum = require( '@stdlib/blas/base/dzasum' );
133+
134+
function rand() {
135+
return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
136+
}
137+
138+
var x = filledarrayBy( 10, 'complex128', rand );
139+
console.log( x.toString() );
140+
141+
// Compute the sum of the absolute values of real and imaginary components:
142+
var out = dzasum( x.length, x, 1 );
143+
console.log( out );
144+
```
145+
146+
</section>
147+
148+
<!-- /.examples -->
149+
150+
<!-- C interface documentation. -->
151+
152+
* * *
153+
154+
<section class="c">
155+
156+
## C APIs
157+
158+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
159+
160+
<section class="intro">
161+
162+
</section>
163+
164+
<!-- /.intro -->
165+
166+
<!-- C usage documentation. -->
167+
168+
<section class="usage">
169+
170+
### Usage
171+
172+
```c
173+
TODO
174+
```
175+
176+
#### TODO
177+
178+
TODO.
179+
180+
```c
181+
TODO
182+
```
183+
184+
TODO
185+
186+
```c
187+
TODO
188+
```
189+
190+
</section>
191+
192+
<!-- /.usage -->
193+
194+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
195+
196+
<section class="notes">
197+
198+
</section>
199+
200+
<!-- /.notes -->
201+
202+
<!-- C API usage examples. -->
203+
204+
<section class="examples">
205+
206+
### Examples
207+
208+
```c
209+
TODO
210+
```
211+
212+
</section>
213+
214+
<!-- /.examples -->
215+
216+
</section>
217+
218+
<!-- /.c -->
219+
220+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
221+
222+
<section class="related">
223+
224+
</section>
225+
226+
<!-- /.related -->
227+
228+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
229+
230+
<section class="links">
231+
232+
[blas]: http://www.netlib.org/blas
233+
234+
[dzasum]: https://www.netlib.org/lapack/explore-html/d5/d72/group__asum_ga89c76eef329f84ba9ed106b34fedab16.html#ga89c76eef329f84ba9ed106b34fedab16
235+
236+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
237+
238+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
239+
240+
</section>
241+
242+
<!-- /.links -->
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var dzasum = require( './../lib/dzasum.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Create a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var x = new Complex128Array( uniform( len*2, -10.0, 10.0, options ) );
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var out;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
out = dzasum( x.length, x, 1 );
66+
if ( isnan( out ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( out ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( format( '%s:len=%d', pkg, len ), f );
101+
}
102+
}
103+
104+
main();

0 commit comments

Comments
 (0)