Skip to content

Commit c9d34ee

Browse files
committed
feat: add blas/ext/index-of
1 parent 5728404 commit c9d34ee

File tree

14 files changed

+2172
-0
lines changed

14 files changed

+2172
-0
lines changed
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
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 -->
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, 10.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)