Skip to content

Commit b930fc4

Browse files
feat: add blas/ext/base/ndarray/cxsa
PR-URL: #12383 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#676
1 parent 31bf95f commit b930fc4

10 files changed

Lines changed: 795 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)