Skip to content

Commit 77694b3

Browse files
Merge branch 'develop' into fix/typo
2 parents b74665e + f0362db commit 77694b3

16 files changed

Lines changed: 2267 additions & 0 deletions

File tree

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
# gwaxpb
22+
23+
> Multiply each element in a strided array `x` by a scalar constant and add a scalar constant before assigning the results to a strided array `w`.
24+
25+
<section class="intro">
26+
27+
This BLAS extension implements the linear transformation
28+
29+
<!-- <equation class="equation" label="eq:waxpb" align="center" raw="\mathbf{w} = \alpha \mathbf{x} + \beta" alt="Equation for waxpb operation."> -->
30+
31+
```math
32+
\mathbf{w} = \alpha \mathbf{x} + \beta
33+
```
34+
35+
<!-- </equation> -->
36+
37+
</section>
38+
39+
<!-- /.intro -->
40+
41+
<section class="usage">
42+
43+
## Usage
44+
45+
```javascript
46+
var gwaxpb = require( '@stdlib/blas/ext/base/gwaxpb' );
47+
```
48+
49+
#### gwaxpb( N, alpha, beta, x, strideX, w, strideW )
50+
51+
Multiplies each element in a strided array `x` by a scalar constant and adds a scalar constant before assigning the results to a strided array `w`.
52+
53+
```javascript
54+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
55+
var w = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
56+
57+
gwaxpb( x.length, 5.0, 3.0, x, 1, w, 1 );
58+
// w => [ 8.0, 13.0, 18.0, 23.0, 28.0 ]
59+
```
60+
61+
The function has the following parameters:
62+
63+
- **N**: number of indexed elements.
64+
- **alpha**: first scalar constant.
65+
- **beta**: second scalar constant.
66+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
67+
- **strideX**: stride length for `x`.
68+
- **w**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
69+
- **strideW**: stride length for `w`.
70+
71+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to multiply every other element in `x` by `alpha`, add `beta`, and assign the results to every other element in `w`:
72+
73+
```javascript
74+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
75+
var w = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
76+
77+
gwaxpb( 3, 5.0, 3.0, x, 2, w, 2 );
78+
// w => [ 8.0, 0.0, 18.0, 0.0, 28.0, 0.0 ]
79+
```
80+
81+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
82+
83+
```javascript
84+
var Float64Array = require( '@stdlib/array/float64' );
85+
86+
// Initial arrays...
87+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
88+
var w0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
89+
90+
// Create offset views...
91+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
92+
var w1 = new Float64Array( w0.buffer, w0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
93+
94+
gwaxpb( 3, 5.0, 3.0, x1, 1, w1, 1 );
95+
// w0 => <Float64Array>[ 0.0, 0.0, 13.0, 18.0, 23.0, 0.0 ]
96+
```
97+
98+
#### gwaxpb.ndarray( N, alpha, beta, x, strideX, offsetX, w, strideW, offsetW )
99+
100+
Multiplies each element in a strided array `x` by a scalar constant and adds a scalar constant before assigning the results to a strided array `w` using alternative indexing semantics.
101+
102+
```javascript
103+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
104+
var w = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
105+
106+
gwaxpb.ndarray( x.length, 5.0, 3.0, x, 1, 0, w, 1, 0 );
107+
// w => [ 8.0, 13.0, 18.0, 23.0, 28.0 ]
108+
```
109+
110+
The function has the following additional parameters:
111+
112+
- **offsetX**: starting index for `x`.
113+
- **offsetW**: starting index for `w`.
114+
115+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to multiply the last three elements of `x` by `alpha`, add `beta`, and assign the results to the last three elements of `w`:
116+
117+
```javascript
118+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
119+
var w = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
120+
121+
gwaxpb.ndarray( 3, 5.0, 3.0, x, 1, x.length-3, w, 1, w.length-3 );
122+
// w => [ 0.0, 0.0, 18.0, 23.0, 28.0 ]
123+
```
124+
125+
</section>
126+
127+
<!-- /.usage -->
128+
129+
<section class="notes">
130+
131+
## Notes
132+
133+
- If `N <= 0`, both functions return `w` unchanged.
134+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
135+
136+
</section>
137+
138+
<!-- /.notes -->
139+
140+
<section class="examples">
141+
142+
## Examples
143+
144+
<!-- eslint no-undef: "error" -->
145+
146+
```javascript
147+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
148+
var gwaxpb = require( '@stdlib/blas/ext/base/gwaxpb' );
149+
150+
var x = discreteUniform( 10, -100, 100, {
151+
'dtype': 'float64'
152+
});
153+
console.log( x );
154+
155+
var w = discreteUniform( 10, -100, 100, {
156+
'dtype': 'float64'
157+
});
158+
console.log( w );
159+
160+
gwaxpb( x.length, 5.0, 3.0, x, 1, w, 1 );
161+
console.log( w );
162+
```
163+
164+
</section>
165+
166+
<!-- /.examples -->
167+
168+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
169+
170+
<section class="related">
171+
172+
</section>
173+
174+
<!-- /.related -->
175+
176+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
177+
178+
<section class="links">
179+
180+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
181+
182+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
183+
184+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
185+
186+
<!-- <related-links> -->
187+
188+
<!-- </related-links> -->
189+
190+
</section>
191+
192+
<!-- /.links -->
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var gwaxpb = require( './../lib/main.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Create a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var x = uniform( len, -100, 100, options );
50+
var w = uniform( len, -100, 100, options );
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var z;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
z = gwaxpb( x.length, 5.0, 3.0, x, 1, w, 1 );
66+
if ( isnan( z[ i%x.length ] ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( z[ i%x.length ] ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( format( '%s:len=%d', pkg, len ), f );
101+
}
102+
}
103+
104+
main();

0 commit comments

Comments
 (0)