Skip to content

Commit b5ae032

Browse files
feat: add blas/ext/base/gaxpby
PR-URL: #12380 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#613
1 parent 6ff2ede commit b5ae032

15 files changed

Lines changed: 2286 additions & 0 deletions

File tree

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
# gaxpby
22+
23+
> Multiply a strided array `x` by a constant and add the result to a strided array `y` multiplied by a constant.
24+
25+
<section class="intro">
26+
27+
This BLAS extension implements the operation
28+
29+
<!-- <equation class="equation" label="eq:axpby" align="center" raw="\mathbf{y} = \alpha \mathbf{x} + \beta \mathbf{y}" alt="Equation for axpby operation."> -->
30+
31+
```math
32+
\mathbf{y} = \alpha \mathbf{x} + \beta \mathbf{y}
33+
```
34+
35+
<!-- </equation> -->
36+
37+
This API is complementary to the package [`@stdlib/blas/base/gaxpy`][@stdlib/blas/base/gaxpy] and is a common BLAS extension in libraries such as Intel MKL, OpenBLAS, and others.
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var gaxpby = require( '@stdlib/blas/ext/base/gaxpby' );
49+
```
50+
51+
#### gaxpby( N, alpha, x, strideX, beta, y, strideY )
52+
53+
Multiplies a strided array `x` by a constant and adds the result to a strided array `y` multiplied by a constant.
54+
55+
```javascript
56+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
57+
var y = [ 2.0, 3.0, 4.0, 5.0, 6.0 ];
58+
59+
gaxpby( x.length, 5.0, x, 1, 2.0, y, 1 );
60+
// y => [ 9.0, 16.0, 23.0, 30.0, 37.0 ]
61+
```
62+
63+
The function has the following parameters:
64+
65+
- **N**: number of indexed elements.
66+
- **alpha**: first scalar constant.
67+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
68+
- **strideX**: stride length for `x`.
69+
- **beta**: second scalar constant.
70+
- **y**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
71+
- **strideY**: stride length for `y`.
72+
73+
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` and add to every other element in `y` multiplied by `beta`:
74+
75+
```javascript
76+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
77+
var y = [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ];
78+
79+
gaxpby( 3, 5.0, x, 2, 2.0, y, 2 );
80+
// y => [ 19.0, 8.0, 33.0, 10.0, 47.0, 12.0 ]
81+
```
82+
83+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
84+
85+
```javascript
86+
var Float64Array = require( '@stdlib/array/float64' );
87+
88+
// Initial arrays...
89+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
90+
var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
91+
92+
// Create offset views...
93+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
94+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
95+
96+
gaxpby( 3, 5.0, x1, 1, 2.0, y1, 1 );
97+
// y0 => <Float64Array>[ 7.0, 8.0, 28.0, 35.0, 42.0, 12.0 ]
98+
```
99+
100+
#### gaxpby.ndarray( N, alpha, x, strideX, offsetX, beta, y, strideY, offsetY )
101+
102+
Multiplies a strided array `x` by a constant and adds the result to a strided array `y` multiplied by a constant using alternative indexing semantics.
103+
104+
```javascript
105+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
106+
var y = [ 2.0, 3.0, 4.0, 5.0, 6.0 ];
107+
108+
gaxpby.ndarray( x.length, 5.0, x, 1, 0, 2.0, y, 1, 0 );
109+
// y => [ 9.0, 16.0, 23.0, 30.0, 37.0 ]
110+
```
111+
112+
The function has the following additional parameters:
113+
114+
- **offsetX**: starting index for `x`.
115+
- **offsetY**: starting index for `y`.
116+
117+
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` and add to the last three elements of `y` multiplied by `beta`:
118+
119+
```javascript
120+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
121+
var y = [ 6.0, 7.0, 8.0, 9.0, 10.0 ];
122+
123+
gaxpby.ndarray( 3, 5.0, x, 1, x.length-3, 2.0, y, 1, y.length-3 );
124+
// y => [ 6.0, 7.0, 31.0, 38.0, 45.0 ]
125+
```
126+
127+
</section>
128+
129+
<!-- /.usage -->
130+
131+
<section class="notes">
132+
133+
## Notes
134+
135+
- If `N <= 0`, both functions return `y` unchanged.
136+
- 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]).
137+
138+
</section>
139+
140+
<!-- /.notes -->
141+
142+
<section class="examples">
143+
144+
## Examples
145+
146+
<!-- eslint no-undef: "error" -->
147+
148+
```javascript
149+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
150+
var gaxpby = require( '@stdlib/blas/ext/base/gaxpby' );
151+
152+
var x = discreteUniform( 10, -100, 100, {
153+
'dtype': 'float64'
154+
});
155+
console.log( x );
156+
157+
var y = discreteUniform( 10, -100, 100, {
158+
'dtype': 'float64'
159+
});
160+
console.log( y );
161+
162+
gaxpby( x.length, 5.0, x, 1, 2.0, y, 1 );
163+
console.log( y );
164+
```
165+
166+
</section>
167+
168+
<!-- /.examples -->
169+
170+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
171+
172+
<section class="related">
173+
174+
</section>
175+
176+
<!-- /.related -->
177+
178+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
179+
180+
<section class="links">
181+
182+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
183+
184+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
185+
186+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
187+
188+
[@stdlib/blas/base/gaxpy]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/gaxpy
189+
190+
<!-- <related-links> -->
191+
192+
<!-- </related-links> -->
193+
194+
</section>
195+
196+
<!-- /.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 gaxpby = 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 y = 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 = gaxpby( x.length, 5.0, x, 1, 3.0, y, 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)