Skip to content

Commit 1321911

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add blas/base/wasm/scasum
PR-URL: #6285 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 2a15634 commit 1321911

33 files changed

+4325
-0
lines changed
Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
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+
# scasum
22+
23+
> Compute the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var scasum = require( '@stdlib/blas/base/wasm/scasum' );
31+
```
32+
33+
#### scasum.main( N, x, strideX )
34+
35+
Computes the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector.
36+
37+
```javascript
38+
var Complex64Array = require( '@stdlib/array/complex64' );
39+
40+
var x = new Complex64Array( [ 2.0, 1.0, 3.0, 5.0, 4.0, 0.0, 1.0, 3.0 ] );
41+
42+
var sum = scasum.main( x.length, x, 1 );
43+
// returns 19.0
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **x**: input [`Complex64Array`][@stdlib/array/complex64].
50+
- **strideX**: index increment for `x`.
51+
52+
The `N` and stride parameters determine which elements in the input strided array are accessed at runtime. For example, to compute the sum of every other value,
53+
54+
```javascript
55+
var Complex64Array = require( '@stdlib/array/complex64' );
56+
57+
var x = new Complex64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
58+
59+
var sum = scasum.main( 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+
<!-- eslint-disable stdlib/capitalized-comments -->
66+
67+
```javascript
68+
var Complex64Array = require( '@stdlib/array/complex64' );
69+
70+
// Initial array:
71+
var x0 = new Complex64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, 8.0 ] );
72+
73+
// Create a typed array view:
74+
var x1 = new Complex64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
75+
76+
var sum = scasum.main( 2, x1, 2 );
77+
// returns 22.0
78+
```
79+
80+
#### scasum.ndarray( N, x, strideX, offsetX )
81+
82+
Computes the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector using alternative indexing semantics.
83+
84+
```javascript
85+
var Complex64Array = require( '@stdlib/array/complex64' );
86+
87+
var x = new Complex64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
88+
89+
var sum = scasum.ndarray( x.length, x, 1, 0 );
90+
// returns 19.0
91+
```
92+
93+
The function has the following additional parameters:
94+
95+
- **offsetX**: starting index for `x`.
96+
97+
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 compute the sum of the last three elements,
98+
99+
```javascript
100+
var Complex64Array = require( '@stdlib/array/complex64' );
101+
102+
var x = new Complex64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
103+
104+
var sum = scasum.ndarray( 3, x, 1, x.length-3 );
105+
// returns 16.0
106+
```
107+
108+
* * *
109+
110+
### Module
111+
112+
#### scasum.Module( memory )
113+
114+
Returns a new WebAssembly [module wrapper][@stdlib/wasm/module-wrapper] instance which uses the provided WebAssembly [memory][@stdlib/wasm/memory] instance as its underlying memory.
115+
116+
<!-- eslint-disable node/no-sync -->
117+
118+
```javascript
119+
var Memory = require( '@stdlib/wasm/memory' );
120+
121+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
122+
var mem = new Memory({
123+
'initial': 10,
124+
'maximum': 100
125+
});
126+
127+
// Create a BLAS routine:
128+
var mod = new scasum.Module( mem );
129+
// returns <Module>
130+
131+
// Initialize the routine:
132+
mod.initializeSync();
133+
```
134+
135+
#### scasum.Module.prototype.main( N, xp, sx )
136+
137+
Computes the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector.
138+
139+
<!-- eslint-disable node/no-sync -->
140+
141+
```javascript
142+
var Memory = require( '@stdlib/wasm/memory' );
143+
var oneTo = require( '@stdlib/array/one-to' );
144+
145+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
146+
var mem = new Memory({
147+
'initial': 10,
148+
'maximum': 100
149+
});
150+
151+
// Create a BLAS routine:
152+
var mod = new scasum.Module( mem );
153+
// returns <Module>
154+
155+
// Initialize the routine:
156+
mod.initializeSync();
157+
158+
// Define a vector data type:
159+
var dtype = 'complex64';
160+
161+
// Specify a vector length:
162+
var N = 4;
163+
164+
// Define a pointer (i.e., byte offset) for storing the input vector:
165+
var xptr = 0;
166+
167+
// Write vector values to module memory:
168+
mod.write( xptr, oneTo( N, dtype ) );
169+
170+
// Perform computation:
171+
var sum = mod.main( N, xptr, 1 );
172+
// returns 10.0
173+
```
174+
175+
The function has the following parameters:
176+
177+
- **N**: number of indexed elements.
178+
- **xp**: input [`Complex64Array`][@stdlib/array/complex64] pointer (i.e., byte offset).
179+
- **sx**: index increment for `x`.
180+
181+
#### scasum.Module.prototype.ndarray( N, xp, sx, ox )
182+
183+
Computes the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector using alternative indexing semantics.
184+
185+
<!-- eslint-disable node/no-sync -->
186+
187+
```javascript
188+
var Memory = require( '@stdlib/wasm/memory' );
189+
var oneTo = require( '@stdlib/array/one-to' );
190+
191+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
192+
var mem = new Memory({
193+
'initial': 10,
194+
'maximum': 100
195+
});
196+
197+
// Create a BLAS routine:
198+
var mod = new scasum.Module( mem );
199+
// returns <Module>
200+
201+
// Initialize the routine:
202+
mod.initializeSync();
203+
204+
// Define a vector data type:
205+
var dtype = 'complex64';
206+
207+
// Specify a vector length:
208+
var N = 4;
209+
210+
// Define a pointer (i.e., byte offset) for storing the input vector:
211+
var xptr = 0;
212+
213+
// Write vector values to module memory:
214+
mod.write( xptr, oneTo( N, dtype ) );
215+
216+
// Perform computation:
217+
var sum = mod.ndarray( N, xptr, 1, 0 );
218+
// returns 10.0
219+
```
220+
221+
The function has the following additional parameters:
222+
223+
- **ox**: starting index for `x`.
224+
225+
</section>
226+
227+
<!-- /.usage -->
228+
229+
<section class="notes">
230+
231+
* * *
232+
233+
## Notes
234+
235+
- If `N <= 0`, both `main` and `ndarray` methods return `0.0`.
236+
- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `scasum` module memory instance, data must be explicitly copied to module memory prior to computation. Data movement may entail a performance cost, and, thus, if you are using arrays external to module memory, you should prefer using [`@stdlib/blas/base/scasum`][@stdlib/blas/base/scasum]. However, if working with arrays which are allocated and explicitly managed on module memory, you can achieve better performance when compared to the pure JavaScript implementations found in [`@stdlib/blas/base/scasum`][@stdlib/blas/base/scasum]. Beware that such performance gains may come at the cost of additional complexity when having to perform manual memory management. Choosing between implementations depends heavily on the particular needs and constraints of your application, with no one choice universally better than the other.
237+
- `scasum()` corresponds to the [BLAS][blas] level 1 function [`scasum`][scasum].
238+
239+
</section>
240+
241+
<!-- /.notes -->
242+
243+
<section class="examples">
244+
245+
* * *
246+
247+
## Examples
248+
249+
<!-- eslint no-undef: "error" -->
250+
251+
```javascript
252+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
253+
var filledarrayBy = require( '@stdlib/array/filled-by' );
254+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
255+
var scasum = require( '@stdlib/blas/base/wasm/scasum' );
256+
257+
function rand() {
258+
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
259+
}
260+
261+
var x = filledarrayBy( 10, 'complex64', rand );
262+
console.log( x.toString() );
263+
264+
var out = scasum.ndarray( x.length, x, 1, 0 );
265+
console.log( out );
266+
```
267+
268+
</section>
269+
270+
<!-- /.examples -->
271+
272+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
273+
274+
<section class="related">
275+
276+
</section>
277+
278+
<!-- /.related -->
279+
280+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
281+
282+
<section class="links">
283+
284+
[blas]: http://www.netlib.org/blas
285+
286+
[scasum]: https://www.netlib.org/lapack/explore-html-3.6.1/df/d28/group__single__blas__level1_gadd7d12f994e4868ca9998b054707d934.html
287+
288+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
289+
290+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
291+
292+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
293+
294+
[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper
295+
296+
[@stdlib/blas/base/scasum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/scasum
297+
298+
</section>
299+
300+
<!-- /.links -->

0 commit comments

Comments
 (0)