Skip to content

Commit 5e7c277

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add blas/base/csscal
PR-URL: #6513 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 5501d03 commit 5e7c277

File tree

15 files changed

+1682
-0
lines changed

15 files changed

+1682
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
# csscal
22+
23+
> Scale a single-precision complex floating-point vector by a single-precision floating-point constant.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var csscal = require( '@stdlib/blas/base/csscal' );
31+
```
32+
33+
#### csscal( N, alpha, x, strideX )
34+
35+
Scales a single-precision complex floating-point vector by a single-precision floating-point constant.
36+
37+
```javascript
38+
var Complex64Array = require( '@stdlib/array/complex64' );
39+
40+
var x = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
41+
42+
csscal( 3, 2.0, x, 1 );
43+
// x => <Complex64Array>[ 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 ]
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **alpha**: scalar constant.
50+
- **x**: input [`Complex64Array`][@stdlib/array/complex64].
51+
- **strideX**: stride length for `x`.
52+
53+
The `N` and stride parameters determine which elements in `x` are scaled by `alpha`. For example, to scale every other element in `x` by `alpha`,
54+
55+
```javascript
56+
var Complex64Array = require( '@stdlib/array/complex64' );
57+
58+
var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
59+
60+
csscal( 2, 2.0, x, 2 );
61+
// x => <Complex64Array>[ 2.0, 4.0, 3.0, 4.0, 10.0, 12.0, 7.0, 8.0 ]
62+
```
63+
64+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
65+
66+
<!-- eslint-disable stdlib/capitalized-comments -->
67+
68+
```javascript
69+
var Complex64Array = require( '@stdlib/array/complex64' );
70+
71+
// Initial array:
72+
var x0 = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
73+
74+
// Create an offset view:
75+
var x1 = new Complex64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
76+
77+
// Scale every element in `x1`:
78+
csscal( 3, 2.0, x1, 1 );
79+
// x0 => <Complex64Array>[ 1.0, 2.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0 ]
80+
```
81+
82+
#### csscal.ndarray( N, alpha, x, strideX, offsetX )
83+
84+
Scales a single-precision complex floating-point vector by a single-precision floating-point constant using alternative indexing semantics.
85+
86+
```javascript
87+
var Complex64Array = require( '@stdlib/array/complex64' );
88+
89+
var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
90+
91+
csscal.ndarray( 3, 2.0, x, 1, 0 );
92+
// x => <Complex64Array>[ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ]
93+
```
94+
95+
The function has the following additional parameters:
96+
97+
- **offsetX**: starting index for `x`.
98+
99+
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,
100+
101+
```javascript
102+
var Complex64Array = require( '@stdlib/array/complex64' );
103+
104+
var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
105+
106+
csscal.ndarray( 2, 2.0, x, 2, 1 );
107+
// x => <Complex64Array>[ 1.0, 2.0, 6.0, 8.0, 5.0, 6.0, 14.0, 16.0 ]
108+
```
109+
110+
</section>
111+
112+
<!-- /.usage -->
113+
114+
<section class="notes">
115+
116+
## Notes
117+
118+
- If `N <= 0`, both functions return `x` unchanged.
119+
- `csscal()` corresponds to the [BLAS][blas] level 1 function [`csscal`][csscal].
120+
121+
</section>
122+
123+
<!-- /.notes -->
124+
125+
<section class="examples">
126+
127+
## Examples
128+
129+
<!-- eslint no-undef: "error" -->
130+
131+
```javascript
132+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
133+
var filledarrayBy = require( '@stdlib/array/filled-by' );
134+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
135+
var csscal = require( '@stdlib/blas/base/csscal' );
136+
137+
function rand() {
138+
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
139+
}
140+
141+
var x = filledarrayBy( 10, 'complex64', rand );
142+
console.log( x.toString() );
143+
144+
csscal( x.length, 2.0, x, 1 );
145+
console.log( x.toString() );
146+
```
147+
148+
</section>
149+
150+
<!-- /.examples -->
151+
152+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
153+
154+
<section class="related">
155+
156+
</section>
157+
158+
<!-- /.related -->
159+
160+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="links">
163+
164+
[blas]: http://www.netlib.org/blas
165+
166+
[csscal]: https://www.netlib.org/lapack/explore-html/d2/de8/group__scal_ga40d50a435a5fcf16cf41fa80d746819f.html#ga40d50a435a5fcf16cf41fa80d746819f
167+
168+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
169+
170+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
171+
172+
</section>
173+
174+
<!-- /.links -->
Lines changed: 107 additions & 0 deletions
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Complex64Array = require( '@stdlib/array/complex64' );
28+
var pkg = require( './../package.json' ).name;
29+
var csscal = require( './../lib/csscal.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
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 xbuf;
50+
var x;
51+
52+
xbuf = uniform( len*2, -100.0, 100.0, options );
53+
x = new Complex64Array( xbuf.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+
csscal( x.length, 2.0, x, 1 );
69+
if ( isnanf( xbuf[ i%(len*2) ] ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnanf( xbuf[ 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();
Lines changed: 107 additions & 0 deletions
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Complex64Array = require( '@stdlib/array/complex64' );
28+
var pkg = require( './../package.json' ).name;
29+
var csscal = require( './../lib/ndarray.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
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 xbuf;
50+
var x;
51+
52+
xbuf = uniform( len*2, -100.0, 100.0, options );
53+
x = new Complex64Array( xbuf.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+
csscal( x.length, 2.0, x, 1, 0 );
69+
if ( isnanf( xbuf[ i%(len*2) ] ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnanf( xbuf[ 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+':ndarray:len='+len, f );
104+
}
105+
}
106+
107+
main();

0 commit comments

Comments
 (0)