Skip to content

Commit 262e4c6

Browse files
feat: add blas/ext/base/ndarray/zwxsa
PR-URL: #13682 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Karan Anand <anandkarancompsci@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#724
1 parent 41598f0 commit 262e4c6

10 files changed

Lines changed: 1106 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)