Skip to content

Commit 0b0da4d

Browse files
committed
feat: add blas/base/ndarray/cscal
1 parent afe8812 commit 0b0da4d

10 files changed

Lines changed: 919 additions & 0 deletions

File tree

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
# cscal
22+
23+
> Multiply a one-dimensional single-precision complex floating-point ndarray by a scalar constant.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var cscal = require( '@stdlib/blas/base/ndarray/cscal' );
37+
```
38+
39+
#### cscal( arrays )
40+
41+
Multiplies a one-dimensional single-precision complex floating-point ndarray by a scalar constant.
42+
43+
```javascript
44+
var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
45+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
46+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
47+
48+
var x = new Complex64Vector( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
49+
50+
var alpha = scalar2ndarray( new Complex64( 2.0, 0.0 ), {
51+
'dtype': 'complex64'
52+
});
53+
54+
var y = cscal( [ x, alpha ] );
55+
// returns <ndarray>[ <Complex64>[ 2.0, 4.0 ], <Complex64>[ 6.0, 8.0 ], <Complex64>[ 10.0, 12.0 ] ]
56+
57+
var bool = ( y === x );
58+
// returns true
59+
```
60+
61+
The function has the following parameters:
62+
63+
- **arrays**: array-like object containing the following ndarrays:
64+
65+
- a one-dimensional input ndarray.
66+
- a zero-dimensional ndarray containing a scalar constant.
67+
68+
</section>
69+
70+
<!-- /.usage -->
71+
72+
<section class="notes">
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
86+
var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
87+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
88+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
89+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
90+
var cscal = require( '@stdlib/blas/base/ndarray/cscal' );
91+
92+
var opts = {
93+
'dtype': 'float32'
94+
};
95+
96+
var x = new Complex64Vector( discreteUniform( 10, 0, 100, opts ) );
97+
console.log( ndarray2array( x ) );
98+
99+
var alpha = scalar2ndarray( new Complex64( 2.0, 0.0 ), {
100+
'dtype': 'complex64'
101+
});
102+
103+
var out = cscal( [ x, alpha ] );
104+
console.log( ndarray2array( out ) );
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
112+
113+
<section class="related">
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
</section>
124+
125+
<!-- /.links -->
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var realf = require( '@stdlib/complex/float32/real' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
29+
var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
30+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
31+
var format = require( '@stdlib/string/format' );
32+
var pkg = require( './../package.json' ).name;
33+
var cscal = require( './../lib' );
34+
35+
36+
// VARIABLES //
37+
38+
var options = {
39+
'dtype': 'complex64'
40+
};
41+
42+
43+
// FUNCTIONS //
44+
45+
/**
46+
* Creates a benchmark function.
47+
*
48+
* @private
49+
* @param {PositiveInteger} len - array length
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark( len ) {
53+
var alpha;
54+
var xbuf;
55+
var x;
56+
57+
xbuf = uniform( len*2, -100.0, 100.0, {
58+
'dtype': 'float32'
59+
});
60+
x = new Complex64Vector( xbuf.buffer );
61+
62+
alpha = scalar2ndarray( new Complex64( 2.0, 0.0 ), options );
63+
64+
return benchmark;
65+
66+
/**
67+
* Benchmark function.
68+
*
69+
* @private
70+
* @param {Benchmark} b - benchmark instance
71+
*/
72+
function benchmark( b ) {
73+
var z;
74+
var i;
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
z = cscal( [ x, alpha ] );
79+
if ( typeof z !== 'object' ) {
80+
b.fail( 'should return an ndarray' );
81+
}
82+
}
83+
b.toc();
84+
if ( isnanf( realf( z.get( i%len ) ) ) ) {
85+
b.fail( 'should not return NaN' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
}
90+
}
91+
92+
93+
// MAIN //
94+
95+
/**
96+
* Main execution sequence.
97+
*
98+
* @private
99+
*/
100+
function main() {
101+
var len;
102+
var min;
103+
var max;
104+
var f;
105+
var i;
106+
107+
min = 1; // 10^min
108+
max = 6; // 10^max
109+
110+
for ( i = min; i <= max; i++ ) {
111+
len = pow( 10, i );
112+
f = createBenchmark( len );
113+
bench( format( '%s:len=%d', pkg, len ), f );
114+
}
115+
}
116+
117+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( arrays )
3+
Multiplies a one-dimensional single-precision complex floating-point
4+
ndarray by a scalar constant.
5+
6+
If provided an empty input ndarray, the function returns the input ndarray
7+
unchanged.
8+
9+
Parameters
10+
----------
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing the following ndarrays:
13+
14+
- a one-dimensional input ndarray.
15+
- a zero-dimensional ndarray containing a scalar constant `alpha`.
16+
17+
Returns
18+
-------
19+
out: ndarray
20+
Input ndarray.
21+
22+
Examples
23+
--------
24+
> var x = new {{alias:@stdlib/ndarray/vector/complex64}}( [ 4.0, 2.0, -3.0, 5.0 ] );
25+
> var ca = new {{alias:@stdlib/complex/float32/ctor}}( 2.0, 0.0 );
26+
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( ca, { 'dtype': 'complex64' } );
27+
28+
> {{alias}}( [ x, alpha ] );
29+
> x
30+
<ndarray>[ <Complex64>[ 8.0, 4.0 ], <Complex64>[ -6.0, 10.0 ] ]
31+
32+
See Also
33+
--------
34+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { complex64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Multiplies a one-dimensional single-precision complex floating-point ndarray by a scalar constant.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
* - a zero-dimensional ndarray containing a scalar constant.
34+
*
35+
* @param arrays - array-like object containing ndarrays
36+
* @returns input ndarray
37+
*
38+
* @example
39+
* var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
40+
* var Complex64 = require( '@stdlib/complex/float32/ctor' );
41+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
42+
*
43+
* var x = new Complex64Vector( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
44+
*
45+
* var alpha = scalar2ndarray( new Complex64( 2.0, 0.0 ), {
46+
* 'dtype': 'complex64'
47+
* });
48+
*
49+
* var y = cscal( [ x, alpha ] );
50+
* // returns <ndarray>[ <Complex64>[ 2.0, 4.0 ], <Complex64>[ 6.0, 8.0 ], <Complex64>[ 10.0, 12.0 ] ]
51+
*
52+
* var bool = ( y === x );
53+
* // returns true
54+
*/
55+
declare function cscal( arrays: [ complex64ndarray, complex64ndarray ] ): complex64ndarray;
56+
57+
58+
// EXPORTS //
59+
60+
export = cscal;

0 commit comments

Comments
 (0)