Skip to content

Commit ab94c83

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add blas/base/wasm/zscal
PR-URL: #6810 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent afee9b8 commit ab94c83

34 files changed

+5730
-1
lines changed
Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
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+
# zscal
22+
23+
> Scale a double-precision complex floating-point vector by a double-precision complex floating-point constant.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var zscal = require( '@stdlib/blas/base/wasm/zscal' );
31+
```
32+
33+
#### zscal.main( N, alpha, x, strideX )
34+
35+
Scales values from `x` by `alpha`.
36+
37+
```javascript
38+
var Complex128Array = require( '@stdlib/array/complex128' );
39+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
40+
41+
// Define a strided array:
42+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
43+
44+
// Define a scalar constant:
45+
var alpha = new Complex128( 2.0, 2.0 );
46+
47+
// Perform operation:
48+
zscal.main( x.length, alpha, x, 1 );
49+
// x => <Complex128Array>[ -2.0, 6.0, -2.0, 14.0, -2.0, 22.0 ]
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **N**: number of indexed elements.
55+
- **alpha**: scalar [`Complex128`][@stdlib/complex/float64/ctor] constant.
56+
- **x**: input [`Complex128Array`][@stdlib/array/complex128].
57+
- **strideX**: stride length for `x`.
58+
59+
The `N` and stride parameters determine which elements in the input strided array are accessed at runtime. For example, to scale every other value in `x` by `alpha`,
60+
61+
```javascript
62+
var Complex128Array = require( '@stdlib/array/complex128' );
63+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
64+
65+
// Define a strided array:
66+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
67+
68+
// Define a scalar constant:
69+
var alpha = new Complex128( 2.0, 2.0 );
70+
71+
// Perform operation:
72+
zscal.main( 2, alpha, x, 2 );
73+
// x => <Complex128Array>[ -2.0, 6.0, 3.0, 4.0, -2.0, 22.0 ]
74+
```
75+
76+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
77+
78+
<!-- eslint-disable stdlib/capitalized-comments -->
79+
80+
```javascript
81+
var Complex128Array = require( '@stdlib/array/complex128' );
82+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
83+
84+
// Initial array:
85+
var x0 = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
86+
87+
// Define a scalar constant:
88+
var alpha = new Complex128( 2.0, 2.0 );
89+
90+
// Create an offset view:
91+
var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
92+
93+
// Scales every other value from `x1` by `alpha`...
94+
zscal.main( 3, alpha, x1, 1 );
95+
// x0 => <Complex128Array>[ 1.0, 2.0, -2.0, 14.0, -2.0, 22.0, -2.0, 30.0 ]
96+
```
97+
98+
#### zscal.ndarray( N, alpha, x, strideX, offsetX )
99+
100+
Scales values from `x` by `alpha` using alternative indexing semantics.
101+
102+
```javascript
103+
var Complex128Array = require( '@stdlib/array/complex128' );
104+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
105+
106+
// Define a strided array:
107+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
108+
109+
// Define a scalar constant:
110+
var alpha = new Complex128( 2.0, 2.0 );
111+
112+
// Perform operation:
113+
zscal.ndarray( x.length, alpha, x, 1, 0 );
114+
// x => <Complex128Array>[ -2.0, 6.0, -2.0, 14.0, -2.0, 22.0 ]
115+
```
116+
117+
The function has the following additional parameters:
118+
119+
- **offsetX**: starting index for `x`.
120+
121+
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 scale every other value in the input strided array starting from the second element,
122+
123+
```javascript
124+
var Complex128Array = require( '@stdlib/array/complex128' );
125+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
126+
127+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
128+
var alpha = new Complex128( 2.0, 2.0 );
129+
130+
zscal.ndarray( 2, alpha, x, 2, 1 );
131+
// x => <Complex128Array>[ 1.0, 2.0, -2.0, 14.0, 5.0, 6.0, -2.0, 30.0 ]
132+
```
133+
134+
* * *
135+
136+
### Module
137+
138+
#### zscal.Module( memory )
139+
140+
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.
141+
142+
<!-- eslint-disable node/no-sync -->
143+
144+
```javascript
145+
var Memory = require( '@stdlib/wasm/memory' );
146+
147+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
148+
var mem = new Memory({
149+
'initial': 10,
150+
'maximum': 100
151+
});
152+
153+
// Create a BLAS routine:
154+
var mod = new zscal.Module( mem );
155+
// returns <Module>
156+
157+
// Initialize the routine:
158+
mod.initializeSync();
159+
```
160+
161+
#### zscal.Module.prototype.main( N, ap, xp, sx )
162+
163+
Scales values from `x` by `alpha` .
164+
165+
<!-- eslint-disable node/no-sync -->
166+
167+
```javascript
168+
var Memory = require( '@stdlib/wasm/memory' );
169+
var oneTo = require( '@stdlib/array/one-to' );
170+
var ones = require( '@stdlib/array/ones' );
171+
var zeros = require( '@stdlib/array/zeros' );
172+
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
173+
var Float64Array = require( '@stdlib/array/float64' );
174+
var Complex128Array = require( '@stdlib/array/complex128' );
175+
var reinterpretComplex128 = require( '@stdlib/strided/base/reinterpret-complex128' );
176+
var zscal = require( '@stdlib/blas/base/wasm/zscal' );
177+
178+
// Create a new memory instance with an initial size of 10 pages (320KiB) and a maximum size of 100 pages (6.4MiB):
179+
var mem = new Memory({
180+
'initial': 10,
181+
'maximum': 100
182+
});
183+
184+
// Create a BLAS routine:
185+
var mod = new zscal.Module( mem );
186+
// returns <Module>
187+
188+
// Initialize the routine:
189+
mod.initializeSync();
190+
191+
// Define a vector data type:
192+
var dtype = 'complex128';
193+
194+
// Specify a vector length:
195+
var N = 5;
196+
197+
// Define a pointer (i.e., byte offset) for storing the input vector:
198+
var xptr = 0;
199+
200+
// Define a pointer for storing a complex number:
201+
var zptr = N * bytesPerElement( dtype );
202+
203+
// Write vector values to module memory:
204+
var xbuf = oneTo( N*2, 'float64' );
205+
var x = new Complex128Array( xbuf.buffer );
206+
mod.write( xptr, x );
207+
208+
// Write a complex number to module memory:
209+
mod.write( zptr, new Float64Array( [ 2.0, 2.0 ] ) );
210+
211+
// Perform computation:
212+
mod.main( N, zptr, xptr, 1 );
213+
214+
// Read out the results:
215+
var view = zeros( N, dtype );
216+
mod.read( xptr, view );
217+
218+
console.log( reinterpretComplex128( view, 0 ) );
219+
// => <Float64Array>[ -2.0, 6.0, -2.0, 14.0, -2.0, 22.0, -2.0, 30.0, -2.0, 38.0 ]
220+
```
221+
222+
The function has the following parameters:
223+
224+
- **N**: number of indexed elements.
225+
- **ap**: pointer (i.e., byte offset) to a scalar [`Complex128`][@stdlib/complex/float64/ctor] constant.
226+
- **xp**: input [`Complex128Array`][@stdlib/array/complex128] pointer (i.e., byte offset).
227+
- **sx**: stride length for `x`.
228+
229+
#### zscal.Module.prototype.ndarray( N, ap, xp, sx, ox )
230+
231+
Scales values from `x` by `alpha` using alternative indexing semantics.
232+
233+
<!-- eslint-disable node/no-sync -->
234+
235+
```javascript
236+
var Memory = require( '@stdlib/wasm/memory' );
237+
var oneTo = require( '@stdlib/array/one-to' );
238+
var ones = require( '@stdlib/array/ones' );
239+
var zeros = require( '@stdlib/array/zeros' );
240+
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
241+
var Float64Array = require( '@stdlib/array/float64' );
242+
var Complex128Array = require( '@stdlib/array/complex128' );
243+
var reinterpretComplex128 = require( '@stdlib/strided/base/reinterpret-complex128' );
244+
var zscal = require( '@stdlib/blas/base/wasm/zscal' );
245+
246+
// Create a new memory instance with an initial size of 10 pages (320KiB) and a maximum size of 100 pages (6.4MiB):
247+
var mem = new Memory({
248+
'initial': 10,
249+
'maximum': 100
250+
});
251+
252+
// Create a BLAS routine:
253+
var mod = new zscal.Module( mem );
254+
// returns <Module>
255+
256+
// Initialize the routine:
257+
mod.initializeSync();
258+
259+
// Define a vector data type:
260+
var dtype = 'complex128';
261+
262+
// Specify a vector length:
263+
var N = 5;
264+
265+
// Define a pointer (i.e., byte offset) for storing the input vector:
266+
var xptr = 0;
267+
268+
// Define a pointer for storing a complex number:
269+
var zptr = N * bytesPerElement( dtype );
270+
271+
// Write vector values to module memory:
272+
var xbuf = oneTo( N*2, 'float64' );
273+
var x = new Complex128Array( xbuf.buffer );
274+
mod.write( xptr, x );
275+
276+
// Write a complex number to module memory:
277+
mod.write( zptr, new Float64Array( [ 2.0, 2.0 ] ) );
278+
279+
// Perform computation:
280+
mod.ndarray( N, zptr, xptr, 1, 0 );
281+
282+
// Read out the results:
283+
var view = zeros( N, dtype );
284+
mod.read( xptr, view );
285+
286+
console.log( reinterpretComplex128( view, 0 ) );
287+
// => <Float64Array>[ -2.0, 6.0, -2.0, 14.0, -2.0, 22.0, -2.0, 30.0, -2.0, 38.0 ]
288+
```
289+
290+
The function has the following additional parameters:
291+
292+
- **ox**: starting index for `x`.
293+
294+
</section>
295+
296+
<!-- /.usage -->
297+
298+
<section class="notes">
299+
300+
* * *
301+
302+
## Notes
303+
304+
- If `N <= 0`, `x` is left unchanged.
305+
- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `zscal` 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/zscal`][@stdlib/blas/base/zscal]. 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/zscal`][@stdlib/blas/base/zscal]. 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.
306+
- `zscal()` corresponds to the [BLAS][blas] level 1 function [`zscal`][zscal].
307+
308+
</section>
309+
310+
<!-- /.notes -->
311+
312+
<section class="examples">
313+
314+
* * *
315+
316+
## Examples
317+
318+
<!-- eslint no-undef: "error" -->
319+
320+
```javascript
321+
var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
322+
var oneTo = require( '@stdlib/array/one-to' );
323+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
324+
var Complex128Array = require( '@stdlib/array/complex128' );
325+
var reinterpretComplex128 = require( '@stdlib/strided/base/reinterpret-complex128' );
326+
var zscal = require( '@stdlib/blas/base/wasm/zscal' );
327+
328+
// Specify a vector length:
329+
var N = 5;
330+
331+
// Create an input array:
332+
var xbuf = oneTo( N*2, 'float64' );
333+
var x = new Complex128Array( xbuf.buffer );
334+
335+
// Create a complex number:
336+
var alpha = new Complex128( 2.0, 2.0 );
337+
338+
// Perform computation:
339+
zscal.ndarray( N, alpha, x, 1, 0 );
340+
341+
// Print the results:
342+
console.log( reinterpretComplex128( x, 0 ) );
343+
// => <Float64Array>[ -2.0, 6.0, -2.0, 14.0, -2.0, 22.0, -2.0, 30.0, -2.0, 38.0 ]
344+
```
345+
346+
</section>
347+
348+
<!-- /.examples -->
349+
350+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
351+
352+
<section class="related">
353+
354+
</section>
355+
356+
<!-- /.related -->
357+
358+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
359+
360+
<section class="links">
361+
362+
[blas]: http://www.netlib.org/blas
363+
364+
[zscal]: http://www.netlib.org/lapack/explore-html/da/df6/group__complex__blas__level1.html
365+
366+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
367+
368+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
369+
370+
[@stdlib/complex/float64/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/ctor
371+
372+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
373+
374+
[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper
375+
376+
[@stdlib/blas/base/zscal]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/zscal
377+
378+
</section>
379+
380+
<!-- /.links -->

0 commit comments

Comments
 (0)