Skip to content

Commit 0935821

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/ndarray/sindex-of
PR-URL: #7529 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent f9aa653 commit 0935821

File tree

10 files changed

+855
-0
lines changed

10 files changed

+855
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
# sindexOf
22+
23+
> Return the first index of a search element in a one-dimensional single-precision floating-point ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var sindexOf = require( '@stdlib/blas/ext/base/ndarray/sindex-of' );
37+
```
38+
39+
#### sindexOf( arrays )
40+
41+
Returns the first index of a specified search element in a one-dimensional single-precision floating-point ndarray.
42+
43+
```javascript
44+
var Float32Array = require( '@stdlib/array/float32' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
47+
48+
var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] );
49+
var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
50+
51+
var searchElement = scalar2ndarray( 2.0, {
52+
'dtype': 'float32'
53+
});
54+
55+
var fromIndex = scalar2ndarray( 0, {
56+
'dtype': 'generic'
57+
});
58+
59+
var idx = sindexOf( [ x, searchElement, fromIndex ] );
60+
// returns 3
61+
```
62+
63+
The function has the following parameters:
64+
65+
- **arrays**: array-like object containing the following ndarrays:
66+
67+
- a one-dimensional input ndarray.
68+
- a zero-dimensional ndarray containing the search element.
69+
- a zero-dimensional ndarray containing the index from which to begin searching.
70+
71+
If the function is unable to find a search element, the function returns `-1`.
72+
73+
```javascript
74+
var Float32Array = require( '@stdlib/array/float32' );
75+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
76+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
77+
78+
var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] );
79+
var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
80+
81+
var searchElement = scalar2ndarray( 10.0, {
82+
'dtype': 'float32'
83+
});
84+
85+
var fromIndex = scalar2ndarray( 0, {
86+
'dtype': 'generic'
87+
});
88+
89+
var idx = sindexOf( [ x, searchElement, fromIndex ] );
90+
// returns -1
91+
```
92+
93+
</section>
94+
95+
<!-- /.usage -->
96+
97+
<section class="notes">
98+
99+
## Notes
100+
101+
- If a specified starting search index is negative, the function resolves the starting search index by counting backward from the last element (where `-1` refers to the last element).
102+
103+
</section>
104+
105+
<!-- /.notes -->
106+
107+
<section class="examples">
108+
109+
## Examples
110+
111+
<!-- eslint no-undef: "error" -->
112+
113+
```javascript
114+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
115+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
116+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
117+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
118+
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
119+
var sindexOf = require( '@stdlib/blas/ext/base/ndarray/sindex-of' );
120+
121+
var xbuf = discreteUniform( 10, -100, 100, {
122+
'dtype': 'float32'
123+
});
124+
var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
125+
console.log( ndarray2array( x ) );
126+
127+
var searchElement = scalar2ndarray( 80.0, {
128+
'dtype': 'float32'
129+
});
130+
console.log( 'Search Element:', ndarraylike2scalar( searchElement ) );
131+
132+
var fromIndex = scalar2ndarray( 0, {
133+
'dtype': 'generic'
134+
});
135+
console.log( 'From Index:', ndarraylike2scalar( fromIndex ) );
136+
137+
var idx = sindexOf( [ x, searchElement, fromIndex ] );
138+
console.log( idx );
139+
```
140+
141+
</section>
142+
143+
<!-- /.examples -->
144+
145+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
146+
147+
<section class="related">
148+
149+
</section>
150+
151+
<!-- /.related -->
152+
153+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="links">
156+
157+
</section>
158+
159+
<!-- /.links -->
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
29+
var pkg = require( './../package.json' ).name;
30+
var sindexOf = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float32'
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 searchElement;
51+
var fromIndex;
52+
var xbuf;
53+
var x;
54+
55+
xbuf = uniform( len, 0.0, 100.0, options );
56+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
57+
58+
searchElement = scalar2ndarray( -10.0, {
59+
'dtype': 'float32'
60+
});
61+
fromIndex = scalar2ndarray( 0, {
62+
'dtype': 'generic'
63+
});
64+
65+
return benchmark;
66+
67+
function benchmark( b ) {
68+
var out;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
out = sindexOf( [ x, searchElement, fromIndex ] );
74+
if ( out !== out ) {
75+
b.fail( 'should return an integer' );
76+
}
77+
}
78+
b.toc();
79+
if ( !isInteger( out ) ) {
80+
b.fail( 'should return an integer' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( pkg+':len='+len, f );
109+
}
110+
}
111+
112+
main();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( arrays )
3+
Returns the first index of a search element in a one-dimensional single-
4+
precision floating-point ndarray.
5+
6+
If a specified starting search index is negative, the function resolves the
7+
starting search index by counting backward from the last element (where `-1`
8+
refers to the last element).
9+
10+
Parameters
11+
----------
12+
arrays: ArrayLikeObject<ndarray>
13+
Array-like object containing the following ndarrays:
14+
15+
- a one-dimensional input ndarray.
16+
- a zero-dimensional ndarray containing the search element.
17+
- a zero-dimensional ndarray containing the index from which to begin
18+
searching.
19+
20+
Returns
21+
-------
22+
out: integer
23+
Index.
24+
25+
Examples
26+
--------
27+
> var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0 ] );
28+
> var dt = 'float32';
29+
> var sh = [ xbuf.length ];
30+
> var sx = [ 1 ];
31+
> var ox = 0;
32+
> var ord = 'row-major';
33+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
34+
> var v = {{alias:@stdlib/ndarray/from-scalar}}( 2.0, { 'dtype': dt } );
35+
> var i = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } );
36+
> {{alias}}( [ x, v, i ] )
37+
1
38+
39+
See Also
40+
--------
41+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { float32ndarray, typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns the first index of a search element in a one-dimensional single-precision floating-point ndarray.
27+
*
28+
* @param arrays - array-like object containing a one-dimensional input ndarray, a zero-dimensional ndarray containing the search element, and a zero-dimensional ndarray containing the index from which to begin searching
29+
* @returns index
30+
*
31+
* @example
32+
* var Float32Array = require( '@stdlib/array/float32' );
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
35+
* var sindexOf = require( '@stdlib/blas/ext/base/ndarray/sindex-of' );
36+
*
37+
* var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] );
38+
* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
39+
*
40+
* var searchElement = scalar2ndarray( 2.0, {
41+
* 'dtype': 'float32'
42+
* });
43+
*
44+
* var fromIndex = scalar2ndarray( 0, {
45+
* 'dtype': 'generic'
46+
* });
47+
*
48+
* var v = sindexOf( [ x, searchElement, fromIndex ] );
49+
* // returns 3
50+
*/
51+
declare function sindexOf( arrays: [ float32ndarray, typedndarray<number>, typedndarray<number> ] ): number;
52+
53+
54+
// EXPORTS //
55+
56+
export = sindexOf;

0 commit comments

Comments
 (0)