Skip to content

Commit 0f8d445

Browse files
feat: add blas/ext/base/ndarray/zaxpb
PR-URL: #12381 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#638
1 parent fedd3a9 commit 0f8d445

10 files changed

Lines changed: 863 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)