Skip to content

Commit 1294ee8

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/gfirst-index-not-equal
PR-URL: #13264 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#342
1 parent 32c8d66 commit 1294ee8

15 files changed

Lines changed: 2017 additions & 0 deletions

File tree

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
# gfirstIndexNotEqual
22+
23+
> Return the index of the first element in a strided array which is not equal to the corresponding element in another strided array.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gfirstIndexNotEqual = require( '@stdlib/blas/ext/base/gfirst-index-not-equal' );
37+
```
38+
39+
#### gfirstIndexNotEqual( N, x, strideX, y, strideY )
40+
41+
Returns the index of the first element in a strided array which is not equal to the corresponding element in another strided array.
42+
43+
```javascript
44+
var x = [ 0, 0, 1, 0 ];
45+
var y = [ 0, 0, 0, 0 ];
46+
47+
var idx = gfirstIndexNotEqual( x.length, x, 1, y, 1 );
48+
// returns 2
49+
```
50+
51+
The function has the following parameters:
52+
53+
- **N**: number of indexed elements.
54+
- **x**: first input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
55+
- **strideX**: stride length for `x`.
56+
- **y**: second input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
57+
- **strideY**: stride length for `y`.
58+
59+
If unable to find an element in `x` which is not equal to the corresponding element in `y`, the function returns `-1`.
60+
61+
```javascript
62+
var x = [ 0, 0, 0, 0 ];
63+
var y = [ 0, 0, 0, 0 ];
64+
65+
var idx = gfirstIndexNotEqual( x.length, x, 1, y, 1 );
66+
// returns -1
67+
```
68+
69+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compare every other element in `x` to every other element in `y`:
70+
71+
```javascript
72+
var x = [ 0, 0, 1, 0, 0, 0 ];
73+
var y = [ 0, 0, 1, 0, 9, 0 ];
74+
75+
var idx = gfirstIndexNotEqual( 3, x, 2, y, 2 );
76+
// returns 2
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( [ 0, 0, 0, 1, 0, 0 ] );
86+
var y0 = new Float64Array( [ 0, 0, 0, 0, 0, 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*1 ); // start at 2nd element
91+
92+
var idx = gfirstIndexNotEqual( x1.length, x1, 1, y1, 1 );
93+
// returns 2
94+
```
95+
96+
#### gfirstIndexNotEqual.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
97+
98+
Returns the index of the first element in a strided array which is not equal to the corresponding element in another strided array using alternative indexing semantics.
99+
100+
```javascript
101+
var x = [ 0, 0, 1, 0 ];
102+
var y = [ 0, 0, 0, 0 ];
103+
104+
var idx = gfirstIndexNotEqual.ndarray( x.length, x, 1, 0, y, 1, 0 );
105+
// returns 2
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 compare the last three elements of `x` to the last three elements of `y`:
114+
115+
```javascript
116+
var x = [ 0, 0, 0, 1 ];
117+
var y = [ 1, 0, 0, 0 ];
118+
119+
var idx = gfirstIndexNotEqual.ndarray( 3, x, 1, x.length-3, y, 1, y.length-3 );
120+
// returns 2
121+
```
122+
123+
</section>
124+
125+
<!-- /.usage -->
126+
127+
<section class="notes">
128+
129+
## Notes
130+
131+
- If `N <= 0`, both functions return `-1`.
132+
- Both functions determine whether corresponding elements are equal using strict equality (i.e., the `===` operator). As a consequence, corresponding `NaN` elements are considered unequal (as `NaN !== NaN` always evaluates to `true`), while `-0` and `+0` are considered equal.
133+
- 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]).
134+
135+
</section>
136+
137+
<!-- /.notes -->
138+
139+
<section class="examples">
140+
141+
## Examples
142+
143+
<!-- eslint no-undef: "error" -->
144+
145+
```javascript
146+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
147+
var gfirstIndexNotEqual = require( '@stdlib/blas/ext/base/gfirst-index-not-equal' );
148+
149+
var x = discreteUniform( 10, -5, 5, {
150+
'dtype': 'generic'
151+
});
152+
console.log( x );
153+
154+
var y = discreteUniform( 10, -5, 5, {
155+
'dtype': 'generic'
156+
});
157+
console.log( y );
158+
159+
var idx = gfirstIndexNotEqual( x.length, x, 1, y, 1 );
160+
console.log( idx );
161+
```
162+
163+
</section>
164+
165+
<!-- /.examples -->
166+
167+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
168+
169+
<section class="related">
170+
171+
</section>
172+
173+
<!-- /.related -->
174+
175+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
176+
177+
<section class="links">
178+
179+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
180+
181+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
182+
183+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
184+
185+
</section>
186+
187+
<!-- /.links -->
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
26+
var oneTo = require( '@stdlib/array/one-to' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var gfirstIndexNotEqual = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x = oneTo( len, 'float64' );
43+
var y = oneTo( len, 'float64' );
44+
return benchmark;
45+
46+
/**
47+
* Benchmark function.
48+
*
49+
* @private
50+
* @param {Benchmark} b - benchmark instance
51+
*/
52+
function benchmark( b ) {
53+
var out;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
out = gfirstIndexNotEqual( x.length, x, 1, y, 1 );
59+
if ( out !== out ) {
60+
b.fail( 'should return an integer' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isInteger( out ) ) {
65+
b.fail( 'should return an integer' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
93+
f = createBenchmark( len );
94+
bench( format( '%s:len=%d', pkg, len ), f );
95+
}
96+
}
97+
98+
main();
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
26+
var oneTo = require( '@stdlib/array/one-to' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var gfirstIndexNotEqual = require( './../lib/ndarray.js' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x = oneTo( len, 'float64' );
43+
var y = oneTo( len, 'float64' );
44+
return benchmark;
45+
46+
/**
47+
* Benchmark function.
48+
*
49+
* @private
50+
* @param {Benchmark} b - benchmark instance
51+
*/
52+
function benchmark( b ) {
53+
var out;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
out = gfirstIndexNotEqual( x.length, x, 1, 0, y, 1, 0 );
59+
if ( out !== out ) {
60+
b.fail( 'should return an integer' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isInteger( out ) ) {
65+
b.fail( 'should return an integer' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
93+
f = createBenchmark( len );
94+
bench( format( '%s:ndarray:len=%d', pkg, len ), f );
95+
}
96+
}
97+
98+
main();

0 commit comments

Comments
 (0)