Skip to content

Commit 9ce4dc4

Browse files
feat: add blas/ext/base/ndarray/gwapx
PR-URL: #15694 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#741 Ref: #14404
1 parent 7665af9 commit 9ce4dc4

10 files changed

Lines changed: 1028 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
# gwapx
22+
23+
> Add a scalar constant to each element in an input one-dimensional ndarray and assign the results to elements in a one-dimensional 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 gwapx = require( '@stdlib/blas/ext/base/ndarray/gwapx' );
37+
```
38+
39+
#### gwapx( arrays )
40+
41+
Adds a scalar constant to each element in an input one-dimensional ndarray and assigns the results to elements in a one-dimensional output ndarray.
42+
43+
```javascript
44+
var vector = require( '@stdlib/ndarray/vector/ctor' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
47+
var x = vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ], 'generic' );
48+
var w = vector( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], 'generic' );
49+
50+
var alpha = scalar2ndarray( 5.0, {
51+
'dtype': 'generic'
52+
});
53+
54+
gwapx( [ x, w, alpha ] );
55+
// w => <ndarray>[ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.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 one-dimensional output ndarray.
64+
- a zero-dimensional ndarray containing the scalar constant to add.
65+
66+
</section>
67+
68+
<!-- /.usage -->
69+
70+
<section class="notes">
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
```javascript
83+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
84+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
85+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
86+
var ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' );
87+
var gwapx = require( '@stdlib/blas/ext/base/ndarray/gwapx' );
88+
89+
var opts = {
90+
'dtype': 'generic'
91+
};
92+
93+
var x = discreteUniform( [ 10 ], -100, 100, opts );
94+
console.log( ndarray2array( x ) );
95+
96+
var w = discreteUniform( [ 10 ], -100, 100, opts );
97+
console.log( ndarray2array( w ) );
98+
99+
var alpha = scalar2ndarray( 5.0, opts );
100+
console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) );
101+
102+
gwapx( [ x, w, alpha ] );
103+
console.log( ndarray2array( w ) );
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111+
112+
<section class="related">
113+
114+
</section>
115+
116+
<!-- /.related -->
117+
118+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
119+
120+
<section class="links">
121+
122+
</section>
123+
124+
<!-- /.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/uniform' );
25+
var zeros = require( '@stdlib/ndarray/zeros' );
26+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gwapx = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - ndarray length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var alpha;
51+
var x;
52+
var w;
53+
54+
x = uniform( [ len ], -100.0, 100.0, options );
55+
w = zeros( [ len ], options );
56+
alpha = scalar2ndarray( 5.0, options );
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 = gwapx( [ 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( arrays )
3+
Adds a scalar constant to each element in an input one-dimensional ndarray
4+
and assigns the results to elements in a one-dimensional output ndarray.
5+
6+
Parameters
7+
----------
8+
arrays: ArrayLikeObject<ndarray>
9+
Array-like object containing the following ndarrays:
10+
11+
- a one-dimensional input ndarray.
12+
- a one-dimensional output ndarray.
13+
- a zero-dimensional ndarray containing the scalar constant to add.
14+
15+
Returns
16+
-------
17+
out: ndarray
18+
Output ndarray.
19+
20+
Examples
21+
--------
22+
> var xbuf = [ -2.0, 1.0, 3.0, -5.0 ];
23+
> var x = {{alias:@stdlib/ndarray/vector/ctor}}( xbuf, 'generic' );
24+
> var wbuf = [ 0.0, 0.0, 0.0, 0.0 ];
25+
> var w = {{alias:@stdlib/ndarray/vector/ctor}}( wbuf, 'generic' );
26+
> var opts = { 'dtype': 'generic' };
27+
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, opts );
28+
> {{alias}}( [ x, w, alpha ] )
29+
<ndarray>[ 3.0, 6.0, 8.0, 0.0 ]
30+
31+
See Also
32+
--------
33+
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 { typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Adds a scalar constant to each element in an input one-dimensional ndarray and assigns the results to elements in a one-dimensional output ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
* - a one-dimensional output ndarray.
34+
* - a zero-dimensional ndarray containing the scalar constant to add.
35+
*
36+
* @param arrays - array-like object containing ndarrays
37+
* @returns output ndarray
38+
*
39+
* @example
40+
* var vector = require( '@stdlib/ndarray/vector/ctor' );
41+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
42+
*
43+
* var x = vector( [ -2.0, 1.0, 3.0, -5.0 ], 'generic' );
44+
* var w = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' );
45+
*
46+
* var alpha = scalar2ndarray( 5.0, {
47+
* 'dtype': 'generic'
48+
* });
49+
*
50+
* var out = gwapx( [ x, w, alpha ] );
51+
* // returns <ndarray>[ 3.0, 6.0, 8.0, 0.0 ]
52+
*/
53+
declare function gwapx<T extends typedndarray<number> = typedndarray<number>>( arrays: [ typedndarray<number>, T, typedndarray<number> ] ): T;
54+
55+
56+
// EXPORTS //
57+
58+
export = gwapx;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 gwapx = require( './index' );
24+
25+
26+
// TESTS //
27+
28+
// The function returns an ndarray...
29+
{
30+
const x = zeros( [ 10 ], {
31+
'dtype': 'generic'
32+
});
33+
const w = zeros( [ 10 ], {
34+
'dtype': 'generic'
35+
});
36+
const alpha = scalar2ndarray( 5.0, {
37+
'dtype': 'generic'
38+
});
39+
40+
gwapx( [ x, w, alpha ] ); // $ExpectType genericndarray<number>
41+
}
42+
43+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
44+
{
45+
gwapx( '10' ); // $ExpectError
46+
gwapx( 5 ); // $ExpectError
47+
gwapx( true ); // $ExpectError
48+
gwapx( false ); // $ExpectError
49+
gwapx( null ); // $ExpectError
50+
gwapx( undefined ); // $ExpectError
51+
gwapx( [] ); // $ExpectError
52+
gwapx( {} ); // $ExpectError
53+
gwapx( ( x: number ): number => x ); // $ExpectError
54+
}
55+
56+
// The compiler throws an error if the function is provided an unsupported number of arguments...
57+
{
58+
const x = zeros( [ 10 ], {
59+
'dtype': 'generic'
60+
});
61+
const w = zeros( [ 10 ], {
62+
'dtype': 'generic'
63+
});
64+
const alpha = scalar2ndarray( 5.0, {
65+
'dtype': 'generic'
66+
});
67+
68+
gwapx(); // $ExpectError
69+
gwapx( [ x, w, alpha ], {} ); // $ExpectError
70+
}

0 commit comments

Comments
 (0)