Skip to content

Commit cf8cf0a

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/index-of
PR-URL: #7565 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 5e6e13b commit cf8cf0a

File tree

16 files changed

+5110
-0
lines changed

16 files changed

+5110
-0
lines changed
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
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 an [ndarray][@stdlib/ndarray/ctor] dimension.
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 an [ndarray][@stdlib/ndarray/ctor] dimension.
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+
// Perform operation:
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]. Must have at least one dimension.
55+
- **searchElement**: search element. May be either a scalar value or an [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 non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=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]`.
56+
- **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having an integer index 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 non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, a provided [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. If provided a negative integer, the index at which to begin searching along a dimension is determined by counting backward from the last element (where `-1` refers to the last element). Default: `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 an integer index or generic [data type][@stdlib/ndarray/dtypes].
62+
- **dim**: dimension over which to perform operation. If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). Default: `-1`.
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 along an [ndarray][@stdlib/ndarray/ctor], the corresponding element in the returned [ndarray][@stdlib/ndarray/ctor] is `-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+
// Perform operation:
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 begins searching from the first element along the reduction dimension. To begin searching from a different index, provide a `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+
// Perform operation:
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 elements in the last dimension. To perform the operation over a different dimension, provide a `dim` 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+
'dim': 0
109+
});
110+
// returns <ndarray>
111+
112+
var idx = ndarray2array( out );
113+
// returns [ 1, -1 ]
114+
```
115+
116+
By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, 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+
var x = array( [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] );
123+
124+
var opts = {
125+
'dim': 0,
126+
'keepdims': true
127+
};
128+
129+
var out = indexOf( x, -3.0, opts );
130+
// returns <ndarray>
131+
132+
var idx = ndarray2array( out );
133+
// returns [ [ 1, -1 ] ]
134+
```
135+
136+
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.
137+
138+
```javascript
139+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
140+
var dtype = require( '@stdlib/ndarray/dtype' );
141+
var array = require( '@stdlib/ndarray/array' );
142+
143+
var x = array( [ 1.0, 2.0, 3.0, 4.0 ] );
144+
145+
var idx = indexOf( x, 2.0, {
146+
'dtype': 'generic'
147+
});
148+
// returns <ndarray>
149+
150+
var dt = dtype( idx );
151+
// returns 'generic'
152+
```
153+
154+
#### indexOf.assign( x, searchElement\[, fromIndex], out\[, options] )
155+
156+
Returns the first index of a specified search element along an [ndarray][@stdlib/ndarray/ctor] dimension and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
157+
158+
```javascript
159+
var array = require( '@stdlib/ndarray/array' );
160+
var zeros = require( '@stdlib/ndarray/zeros' );
161+
162+
var x = array( [ 1.0, 2.0, 3.0, 4.0 ] );
163+
var y = zeros( [], {
164+
'dtype': 'int32'
165+
});
166+
167+
var out = indexOf.assign( x, 3.0, y );
168+
// returns <ndarray>
169+
170+
var idx = out.get();
171+
// returns 2
172+
173+
var bool = ( out === y );
174+
// returns true
175+
```
176+
177+
The method has the following parameters:
178+
179+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have at least one dimension.
180+
- **searchElement**: search element. May be either a scalar value or an [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 non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=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]`.
181+
- **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having an integer index 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 non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, a provided [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. If provided a negative integer, the index at which to begin searching along a dimension is determined by counting backward from the last element (where `-1` refers to the last element). Default: `0`.
182+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
183+
- **options**: function options (_optional_).
184+
185+
The method accepts the following options:
186+
187+
- **dim**: dimension over which to perform operation. If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). Default: `-1`.
188+
189+
</section>
190+
191+
<!-- /.usage -->
192+
193+
<section class="notes">
194+
195+
## Notes
196+
197+
- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor].
198+
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes].
199+
200+
</section>
201+
202+
<!-- /.notes -->
203+
204+
<section class="examples">
205+
206+
## Examples
207+
208+
<!-- eslint no-undef: "error" -->
209+
210+
```javascript
211+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
212+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
213+
var ndarray = require( '@stdlib/ndarray/ctor' );
214+
var indexOf = require( '@stdlib/blas/ext/index-of' );
215+
216+
// Generate an array of random numbers:
217+
var xbuf = discreteUniform( 10, 0, 20, {
218+
'dtype': 'float64'
219+
});
220+
221+
// Wrap in an ndarray:
222+
var x = new ndarray( 'float64', xbuf, [ 5, 2 ], [ 2, 1 ], 0, 'row-major' );
223+
console.log( ndarray2array( x ) );
224+
225+
// Perform operation:
226+
var idx = indexOf( x, 10.0, {
227+
'dim': 0
228+
});
229+
230+
// Print the results:
231+
console.log( ndarray2array( idx ) );
232+
```
233+
234+
</section>
235+
236+
<!-- /.examples -->
237+
238+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
239+
240+
<section class="related">
241+
242+
</section>
243+
244+
<!-- /.related -->
245+
246+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
247+
248+
<section class="links">
249+
250+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
251+
252+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
253+
254+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
255+
256+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
257+
258+
</section>
259+
260+
<!-- /.links -->
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var zeros = require( '@stdlib/ndarray/zeros' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var indexOf = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var out;
51+
var x;
52+
53+
x = uniform( len, -50.0, 50.0, options );
54+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
55+
56+
out = zeros( [], {
57+
'dtype': 'int32'
58+
});
59+
60+
return benchmark;
61+
62+
/**
63+
* Benchmark function.
64+
*
65+
* @private
66+
* @param {Benchmark} b - benchmark instance
67+
*/
68+
function benchmark( b ) {
69+
var o;
70+
var i;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
o = indexOf.assign( x, 100.0, out );
75+
if ( typeof o !== 'object' ) {
76+
b.fail( 'should return an ndarray' );
77+
}
78+
}
79+
b.toc();
80+
if ( isnan( o.get() ) ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
}
86+
}
87+
88+
89+
// MAIN //
90+
91+
/**
92+
* Main execution sequence.
93+
*
94+
* @private
95+
*/
96+
function main() {
97+
var len;
98+
var min;
99+
var max;
100+
var f;
101+
var i;
102+
103+
min = 1; // 10^min
104+
max = 6; // 10^max
105+
106+
for ( i = min; i <= max; i++ ) {
107+
len = pow( 10, i );
108+
f = createBenchmark( len );
109+
bench( pkg+':assign:dtype='+options.dtype+',len='+len, f );
110+
}
111+
}
112+
113+
main();

0 commit comments

Comments
 (0)