|
| 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 --> |
0 commit comments