|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# indexOf |
| 22 | + |
| 23 | +> Return the first index of a specified search element along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var indexOf = require( '@stdlib/blas/ext/index-of' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### indexOf( x, searchElement\[, fromIndex]\[, options] ) |
| 34 | + |
| 35 | +Returns the first index of a specified search element along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var array = require( '@stdlib/ndarray/array' ); |
| 39 | + |
| 40 | +// Create an input ndarray: |
| 41 | +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 42 | +// returns <ndarray> |
| 43 | + |
| 44 | +// Find index: |
| 45 | +var out = indexOf( x, 2.0 ); |
| 46 | +// returns <ndarray> |
| 47 | + |
| 48 | +var idx = out.get(); |
| 49 | +// returns 1 |
| 50 | +``` |
| 51 | + |
| 52 | +The function has the following parameters: |
| 53 | + |
| 54 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 55 | +- **searchElement**: element in an input [ndarray][@stdlib/ndarray/ctor] for which to find an index. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] same as the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided a scalar value, the value is cast to the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, the search element [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], the search element [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 56 | +- **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a numeric or `generic` [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the the index from which to begin searching is `0`. |
| 57 | +- **options**: function options (_optional_). |
| 58 | + |
| 59 | +The function accepts the following options: |
| 60 | + |
| 61 | +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a "integer_index" [data type][@stdlib/ndarray/dtypes]. |
| 62 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 63 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. |
| 64 | + |
| 65 | +If the function is unable to find a search element, the function returns `-1`. |
| 66 | + |
| 67 | +```javascript |
| 68 | +var array = require( '@stdlib/ndarray/array' ); |
| 69 | + |
| 70 | +// Create an input ndarray: |
| 71 | +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 72 | +// returns <ndarray> |
| 73 | + |
| 74 | +// Find index: |
| 75 | +var out = indexOf( x, 10.0 ); |
| 76 | +// returns <ndarray> |
| 77 | + |
| 78 | +var idx = out.get(); |
| 79 | +// returns -1 |
| 80 | +``` |
| 81 | + |
| 82 | +By default, the function uses `0` as the index from which to begin searching. To begin searching from a different index, provide the `fromIndex` argument. |
| 83 | + |
| 84 | +```javascript |
| 85 | +var array = require( '@stdlib/ndarray/array' ); |
| 86 | + |
| 87 | +// Create an input ndarray: |
| 88 | +var x = array( [ 1.0, 2.0, 3.0, 4.0, 2.0, 6.0 ] ); |
| 89 | +// returns <ndarray> |
| 90 | + |
| 91 | +// Find index: |
| 92 | +var out = indexOf( x, 2.0, 2 ); |
| 93 | +// returns <ndarray> |
| 94 | + |
| 95 | +var idx = out.get(); |
| 96 | +// returns 4 |
| 97 | +``` |
| 98 | + |
| 99 | +By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option. |
| 100 | + |
| 101 | +```javascript |
| 102 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 103 | +var array = require( '@stdlib/ndarray/array' ); |
| 104 | + |
| 105 | +var x = array( [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] ); |
| 106 | + |
| 107 | +var out = indexOf( x, -3.0, { |
| 108 | + 'dims': [ 0 ] |
| 109 | +}); |
| 110 | +// returns <ndarray> |
| 111 | + |
| 112 | +var idx = ndarray2array( out ); |
| 113 | +// returns [ 1, -1 ] |
| 114 | +``` |
| 115 | + |
| 116 | +By default, the function returns an [`ndarray`][@stdlib/ndarray/ctor] having a shape matching only the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor] (i.e., the reduced dimensions are dropped). To include the reduced dimensions as singleton dimensions in the output [`ndarray`][@stdlib/ndarray/ctor], set the `keepdims` option to `true`. |
| 117 | + |
| 118 | +```javascript |
| 119 | +var array = require( '@stdlib/ndarray/array' ); |
| 120 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 121 | + |
| 122 | +// Create an input ndarray: |
| 123 | +var x = array( [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] ); |
| 124 | +// returns <ndarray> |
| 125 | + |
| 126 | +var opts = { |
| 127 | + 'dims': [ 0 ], |
| 128 | + 'keepdims': true |
| 129 | +}; |
| 130 | + |
| 131 | +// Find index: |
| 132 | +var out = indexOf( x, -3.0, opts ); |
| 133 | +// returns <ndarray> |
| 134 | + |
| 135 | +var idx = ndarray2array( out ); |
| 136 | +// returns [ [ 1, -1 ] ] |
| 137 | +``` |
| 138 | + |
| 139 | +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. |
| 140 | + |
| 141 | +```javascript |
| 142 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 143 | +var dtype = require( '@stdlib/ndarray/dtype' ); |
| 144 | +var array = require( '@stdlib/ndarray/array' ); |
| 145 | + |
| 146 | +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); |
| 147 | + |
| 148 | +var idx = indexOf( x, 2.0, { |
| 149 | + 'dtype': 'generic' |
| 150 | +}); |
| 151 | +// returns <ndarray> |
| 152 | + |
| 153 | +var dt = dtype( idx ); |
| 154 | +// returns 'generic' |
| 155 | +``` |
| 156 | + |
| 157 | +#### indexOf.assign( x, searchElement\[, fromIndex], out\[, options] ) |
| 158 | + |
| 159 | +Returns the first index of a specified search element along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. |
| 160 | + |
| 161 | +```javascript |
| 162 | +var array = require( '@stdlib/ndarray/array' ); |
| 163 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 164 | + |
| 165 | +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); |
| 166 | +var y = zeros( [], { |
| 167 | + 'dtype': 'int32' |
| 168 | +}); |
| 169 | + |
| 170 | +var out = indexOf.assign( x, 3.0, y ); |
| 171 | +// returns <ndarray> |
| 172 | + |
| 173 | +var idx = out.get(); |
| 174 | +// returns 2 |
| 175 | + |
| 176 | +var bool = ( out === y ); |
| 177 | +// returns true |
| 178 | +``` |
| 179 | + |
| 180 | +The method has the following parameters: |
| 181 | + |
| 182 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 183 | +- **searchElement**: element in an input [ndarray][@stdlib/ndarray/ctor] for which to find an index. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] same as the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided a scalar value, the value is cast to the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, the search element [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] initial value must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 184 | +- **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a numeric or `generic` [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the the index from which to begin searching is `0`. |
| 185 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or generic [data type][@stdlib/ndarray/dtypes]. |
| 186 | +- **options**: function options (_optional_). |
| 187 | + |
| 188 | +The method accepts the following options: |
| 189 | + |
| 190 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 191 | + |
| 192 | +</section> |
| 193 | + |
| 194 | +<!-- /.usage --> |
| 195 | + |
| 196 | +<section class="notes"> |
| 197 | + |
| 198 | +## Notes |
| 199 | + |
| 200 | +- Both functions iterate over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of the input [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an input [ndarray][@stdlib/ndarray/ctor] to contiguous memory before computing the cumulative sum. |
| 201 | + |
| 202 | +</section> |
| 203 | + |
| 204 | +<!-- /.notes --> |
| 205 | + |
| 206 | +<section class="examples"> |
| 207 | + |
| 208 | +## Examples |
| 209 | + |
| 210 | +<!-- eslint no-undef: "error" --> |
| 211 | + |
| 212 | +```javascript |
| 213 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 214 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 215 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 216 | +var indexOf = require( '@stdlib/blas/ext/index-of' ); |
| 217 | + |
| 218 | +// Generate an array of random numbers: |
| 219 | +var xbuf = discreteUniform( 10, 0, 20, { |
| 220 | + 'dtype': 'float64' |
| 221 | +}); |
| 222 | + |
| 223 | +// Wrap in an ndarray: |
| 224 | +var x = new ndarray( 'float64', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); |
| 225 | +console.log( ndarray2array( x ) ); |
| 226 | + |
| 227 | +// Find index: |
| 228 | +var idx = indexOf( x, 10.0, { |
| 229 | + 'dims': [ 0 ] |
| 230 | +}); |
| 231 | + |
| 232 | +// Print the results: |
| 233 | +console.log( ndarray2array( idx ) ); |
| 234 | +``` |
| 235 | + |
| 236 | +</section> |
| 237 | + |
| 238 | +<!-- /.examples --> |
| 239 | + |
| 240 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 241 | + |
| 242 | +<section class="related"> |
| 243 | + |
| 244 | +</section> |
| 245 | + |
| 246 | +<!-- /.related --> |
| 247 | + |
| 248 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 249 | + |
| 250 | +<section class="links"> |
| 251 | + |
| 252 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 253 | + |
| 254 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 255 | + |
| 256 | +[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies |
| 257 | + |
| 258 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 259 | + |
| 260 | +</section> |
| 261 | + |
| 262 | +<!-- /.links --> |
0 commit comments