Skip to content

Commit cf7b40a

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/gmskrev
PR-URL: #10925 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#195
1 parent eb60bb4 commit cf7b40a

15 files changed

Lines changed: 2106 additions & 0 deletions

File tree

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
# gmskrev
22+
23+
> Reverse a strided array in-place according to a mask.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var gmskrev = require( '@stdlib/blas/ext/base/gmskrev' );
31+
```
32+
33+
#### gmskrev( N, x, strideX, mask, strideMask )
34+
35+
Reverses a strided array in-place according to a mask.
36+
37+
```javascript
38+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
39+
var mask = [ 0, 0, 0, 1, 0, 0, 0, 0 ];
40+
41+
gmskrev( x.length, x, 1, mask, 1 );
42+
// x => [ -3.0, -1.0, 0.0, -5.0, 4.0, 3.0, 1.0, -2.0 ]
43+
```
44+
45+
The function has the following parameters:
46+
47+
- **N**: number of indexed elements.
48+
- **x**: input array.
49+
- **strideX**: stride length for `x`.
50+
- **mask**: mask array. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in the reversal. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from the reversal.
51+
- **strideMask**: stride length for `mask`.
52+
53+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to reverse every other element:
54+
55+
```javascript
56+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
57+
var mask = [ 0, 0, 0, 0 ];
58+
59+
gmskrev( 4, x, 2, mask, 1 );
60+
// x => [ -1.0, 1.0, 4.0, -5.0, 3.0, 0.0, -2.0, -3.0 ]
61+
```
62+
63+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
64+
65+
```javascript
66+
var Float64Array = require( '@stdlib/array/float64' );
67+
var Uint8Array = require( '@stdlib/array/uint8' );
68+
69+
// Initial arrays...
70+
var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
71+
var m0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0 ] );
72+
73+
// Create offset views...
74+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
75+
var m1 = new Uint8Array( m0.buffer, m0.BYTES_PER_ELEMENT*1 );
76+
77+
// Reverse every other element...
78+
gmskrev( 3, x1, 2, m1, 1 );
79+
// x0 => <Float64Array>[ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ]
80+
```
81+
82+
#### gmskrev.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask )
83+
84+
Reverses a strided array in-place according to a mask and using alternative indexing semantics.
85+
86+
```javascript
87+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
88+
var mask = [ 0, 0, 0, 1, 0, 0, 0, 0 ];
89+
90+
gmskrev.ndarray( x.length, x, 1, 0, mask, 1, 0 );
91+
// x => [ -3.0, -1.0, 0.0, -5.0, 4.0, 3.0, 1.0, -2.0 ]
92+
```
93+
94+
The function has the following additional parameters:
95+
96+
- **offsetX**: starting index for `x`.
97+
- **offsetMask**: starting index for `mask`.
98+
99+
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 access only the last three elements:
100+
101+
```javascript
102+
var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
103+
var mask = [ 0, 0, 0, 0, 0, 0 ];
104+
105+
gmskrev.ndarray( 3, x, 1, x.length-3, mask, 1, 3 );
106+
// x => [ 1.0, -2.0, 3.0, -6.0, 5.0, -4.0 ]
107+
```
108+
109+
</section>
110+
111+
<!-- /.usage -->
112+
113+
<section class="notes">
114+
115+
## Notes
116+
117+
- If `N <= 0`, both functions return `x` unchanged.
118+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/complex64`][@stdlib/array/complex64]).
119+
- Where possible, one should "reverse" a strided array by negating its stride, which is an `O(1)` operation, in contrast to performing an in-place reversal, which is `O(N)`. However, in certain circumstances, this is not tenable, particularly when interfacing with libraries which assume and/or expect a specific memory layout (e.g., strided array elements arranged in memory in ascending order). In general, when working with strided arrays, only perform an in-place reversal when strictly necessary.
120+
- Depending on the environment, the typed versions ([`dmskrev`][@stdlib/blas/ext/base/dmskrev], [`smskrev`][@stdlib/blas/ext/base/smskrev], etc.) are likely to be significantly more performant.
121+
122+
</section>
123+
124+
<!-- /.notes -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
134+
var Uint8Array = require( '@stdlib/array/uint8' );
135+
var gmskrev = require( '@stdlib/blas/ext/base/gmskrev' );
136+
137+
var x = discreteUniform( 10, -100, 100, {
138+
'dtype': 'generic'
139+
});
140+
console.log( x );
141+
142+
var mask = new Uint8Array( [ 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 ] );
143+
console.log( mask );
144+
145+
gmskrev( x.length, x, 1, mask, 1 );
146+
console.log( x );
147+
```
148+
149+
</section>
150+
151+
<!-- /.examples -->
152+
153+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
154+
155+
<section class="related">
156+
157+
</section>
158+
159+
<!-- /.related -->
160+
161+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
162+
163+
<section class="links">
164+
165+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
166+
167+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
168+
169+
[@stdlib/blas/ext/base/dmskrev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dmskrev
170+
171+
[@stdlib/blas/ext/base/smskrev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/smskrev
172+
173+
<!-- <related-links> -->
174+
175+
<!-- </related-links> -->
176+
177+
</section>
178+
179+
<!-- /.links -->
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 zeros = require( '@stdlib/array/zeros' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gmskrev = require( './../lib/main.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Create a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var mask = zeros( len, 'uint8' );
44+
var x = uniform( len, -100.0, 100.0, {
45+
'dtype': 'generic'
46+
});
47+
return benchmark;
48+
49+
/**
50+
* Benchmark function.
51+
*
52+
* @private
53+
* @param {Benchmark} b - benchmark instance
54+
*/
55+
function benchmark( b ) {
56+
var y;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
y = gmskrev( x.length, x, 1, mask, 1 );
62+
if ( isnan( y[ i%x.length ] ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
}
66+
b.toc();
67+
if ( isnan( y[ i%x.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
}
73+
}
74+
75+
76+
// MAIN //
77+
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( format( '%s:len=%d', pkg, len ), f );
92+
}
93+
}
94+
95+
main();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 zeros = require( '@stdlib/array/zeros' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gmskrev = require( './../lib/ndarray.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Create a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var mask = zeros( len, 'uint8' );
44+
var x = uniform( len, -100.0, 100.0, {
45+
'dtype': 'generic'
46+
});
47+
return benchmark;
48+
49+
/**
50+
* Benchmark function.
51+
*
52+
* @private
53+
* @param {Benchmark} b - benchmark instance
54+
*/
55+
function benchmark( b ) {
56+
var y;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
y = gmskrev( x.length, x, 1, 0, mask, 1, 0 );
62+
if ( isnan( y[ i%x.length ] ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
}
66+
b.toc();
67+
if ( isnan( y[ i%x.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
}
73+
}
74+
75+
76+
// MAIN //
77+
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( format( '%s:ndarray:len=%d', pkg, len ), f );
92+
}
93+
}
94+
95+
main();

0 commit comments

Comments
 (0)