Skip to content

Commit 8c9a3b3

Browse files
feat: add blas/ext/base/gxmy
PR-URL: #13124 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#626
1 parent f30fe01 commit 8c9a3b3

15 files changed

Lines changed: 2116 additions & 0 deletions

File tree

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