Skip to content

Commit 2efac32

Browse files
feat: add blas/ext/base/ndarray/glast-index-of
PR-URL: #7607 Ref: #2656 Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent c811786 commit 2efac32

File tree

10 files changed

+847
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)