Skip to content

Commit 2690b66

Browse files
rreusserkgryte
authored andcommitted
feat: add blas/base/zdscal
PR-URL: stdlib-js#4738 Ref: stdlib-js#2464 Ref: stdlib-js#2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 1854539 commit 2690b66

File tree

15 files changed

+1840
-0
lines changed

15 files changed

+1840
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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+
# zdscal
22+
23+
> Scale a double-precision complex floating-point vector by a double-precision floating-point constant.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var zdscal = require( '@stdlib/blas/base/zdscal' );
31+
```
32+
33+
#### zdscal( N, da, zx, strideZX )
34+
35+
Scales a double-precision complex floating-point vector by a double-precision floating-point constant.
36+
37+
```javascript
38+
var Complex128Array = require( '@stdlib/array/complex128' );
39+
var real = require( '@stdlib/complex/float64/real' );
40+
var imag = require( '@stdlib/complex/float64/imag' );
41+
42+
var zx = new Complex128Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
43+
44+
zdscal( 3, 2.0, zx, 1 );
45+
46+
var z = zx.get( 0 );
47+
// returns <Complex128>
48+
49+
var re = real( z );
50+
// returns 2.0
51+
52+
var im = imag( z );
53+
// returns 2.0
54+
```
55+
56+
The function has the following parameters:
57+
58+
- **N**: number of indexed elements.
59+
- **da**: scalar constant.
60+
- **zx**: input [`Complex128Array`][@stdlib/array/complex128].
61+
- **strideZX**: stride length for `zx`.
62+
63+
The `N` and stride parameters determine which elements in `zx` are scaled by `da`. For example, to scale every other element in `zx` by `da`,
64+
65+
```javascript
66+
var Complex128Array = require( '@stdlib/array/complex128' );
67+
var real = require( '@stdlib/complex/float64/real' );
68+
var imag = require( '@stdlib/complex/float64/imag' );
69+
70+
var zx = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
71+
72+
zdscal( 2, 2.0, zx, 2 );
73+
74+
var z = zx.get( 2 );
75+
// returns <Complex128>
76+
77+
var re = real( z );
78+
// returns 10.0
79+
80+
var im = imag( z );
81+
// returns 12.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+
<!-- eslint-disable stdlib/capitalized-comments -->
87+
88+
```javascript
89+
var Complex128Array = require( '@stdlib/array/complex128' );
90+
var real = require( '@stdlib/complex/float64/real' );
91+
var imag = require( '@stdlib/complex/float64/imag' );
92+
93+
// Initial array:
94+
var zx0 = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
95+
96+
// Create an offset view:
97+
var zx1 = new Complex128Array( zx0.buffer, zx0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
98+
99+
// Scale every element in `zx1`:
100+
zdscal( 3, 2.0, zx1, 1 );
101+
102+
var z = zx0.get( 0 );
103+
// returns <Complex128>
104+
105+
var re = real( z );
106+
// returns 1.0
107+
108+
var im = imag( z );
109+
// returns 2.0
110+
111+
z = zx0.get( 1 );
112+
// returns <Complex128>
113+
114+
re = real( z );
115+
// returns 6.0
116+
117+
im = imag( z );
118+
// returns 8.0
119+
```
120+
121+
#### zdscal.ndarray( N, da, zx, strideZX, offsetZX )
122+
123+
Scales a double-precision complex floating-point vector by a double-precision floating-point constant using alternative indexing semantics.
124+
125+
```javascript
126+
var Complex128Array = require( '@stdlib/array/complex128' );
127+
var real = require( '@stdlib/complex/float64/real' );
128+
var imag = require( '@stdlib/complex/float64/imag' );
129+
130+
var zx = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
131+
132+
zdscal.ndarray( 3, 2.0, zx, 1, 0 );
133+
134+
var z = zx.get( 0 );
135+
// returns <Complex128>
136+
137+
var re = real( z );
138+
// returns 2.0
139+
140+
var im = imag( z );
141+
// returns 4.0
142+
```
143+
144+
The function has the following additional parameters:
145+
146+
- **offsetZX**: starting index for `zx`.
147+
148+
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 element in the input strided array starting from the second element,
149+
150+
```javascript
151+
var Complex128Array = require( '@stdlib/array/complex128' );
152+
var real = require( '@stdlib/complex/float64/real' );
153+
var imag = require( '@stdlib/complex/float64/imag' );
154+
155+
var zx = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
156+
157+
zdscal.ndarray( 2, 2.0, zx, 2, 1 );
158+
159+
var z = zx.get( 3 );
160+
// returns <Complex128>
161+
162+
var re = real( z );
163+
// returns 14.0
164+
165+
var im = imag( z );
166+
// returns 16.0
167+
```
168+
169+
</section>
170+
171+
<!-- /.usage -->
172+
173+
<section class="notes">
174+
175+
## Notes
176+
177+
- If `N <= 0`, both functions return `zx` unchanged.
178+
- `zdscal()` corresponds to the [BLAS][blas] level 1 function [`zdscal`][zdscal].
179+
180+
</section>
181+
182+
<!-- /.notes -->
183+
184+
<section class="examples">
185+
186+
## Examples
187+
188+
<!-- eslint no-undef: "error" -->
189+
190+
```javascript
191+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
192+
var filledarrayBy = require( '@stdlib/array/filled-by' );
193+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
194+
var zdscal = require( '@stdlib/blas/base/zdscal' );
195+
196+
function rand() {
197+
return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
198+
}
199+
200+
var zx = filledarrayBy( 10, 'complex128', rand );
201+
console.log( zx.toString() );
202+
203+
zdscal( zx.length, 2.0, zx, 1 );
204+
console.log( zx.toString() );
205+
```
206+
207+
</section>
208+
209+
<!-- /.examples -->
210+
211+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
212+
213+
<section class="related">
214+
215+
</section>
216+
217+
<!-- /.related -->
218+
219+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
220+
221+
<section class="links">
222+
223+
[blas]: http://www.netlib.org/blas
224+
225+
[zdscal]: https://www.netlib.org/lapack/explore-html/d2/de8/group__scal_ga40d50a435a5fcf16cf41fa80d746819f.html#ga40d50a435a5fcf16cf41fa80d746819f
226+
227+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
228+
229+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
230+
231+
</section>
232+
233+
<!-- /.links -->
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 pow = require( '@stdlib/math/base/special/pow' );
27+
var Complex128Array = require( '@stdlib/array/complex128' );
28+
var pkg = require( './../package.json' ).name;
29+
var zdscal = require( './../lib/zdscal.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 zxbuf;
50+
var zx;
51+
52+
zxbuf = uniform( len*2, -100.0, 100.0, options );
53+
zx = new Complex128Array( zxbuf.buffer );
54+
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
zdscal( zx.length, 2.0, zx, 1 );
69+
if ( isnan( zxbuf[ i%(len*2) ] ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( zxbuf[ i%(len*2) ] ) ) {
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();

0 commit comments

Comments
 (0)