Skip to content

Commit 81a57cc

Browse files
committed
feat: add blas/ext/base/ndarray/gfindLastIndex
1 parent 477ffde commit 81a57cc

File tree

10 files changed

+987
-0
lines changed

10 files changed

+987
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
# gfindLastIndex
22+
23+
> Return the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gfindLastIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index' );
37+
```
38+
39+
#### gfindLastIndex( arrays, clbk\[, thisArg] )
40+
41+
Returns the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function.
42+
43+
```javascript
44+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
function isEven( v ) {
48+
return v % 2.0 === 0.0;
49+
}
50+
51+
var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
52+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
53+
54+
var idx = gfindLastIndex( [ x ], isEven );
55+
// returns 3
56+
```
57+
58+
If the function is unable to find a search element, the function returns `-1`.
59+
60+
```javascript
61+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
62+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
63+
64+
function isEven( v ) {
65+
return v % 2.0 === 0.0;
66+
}
67+
68+
var xbuf = [ 1.0, 3.0, 5.0, 7.0 ];
69+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
70+
71+
var idx = gfindLastIndex( [ x ], isEven );
72+
// returns -1
73+
```
74+
75+
The function has the following parameters:
76+
77+
- **arrays**: array-like object containing a one-dimensional input ndarray.
78+
- **clbk**: callback function.
79+
- **thisArg**: callback execution context (_optional_).
80+
81+
The callback function is provided the following arguments:
82+
83+
- **value**: current array element.
84+
- **idx**: current array element index.
85+
- **array**: the input ndarray.
86+
87+
To set the callback execution context, provide a `thisArg`.
88+
89+
```javascript
90+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
91+
92+
function isEven( v ) {
93+
this.count += 1;
94+
return v % 2.0 === 0.0;
95+
}
96+
97+
var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
98+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
99+
var ctx = {
100+
'count': 0
101+
};
102+
103+
var v = gfindLastIndex( [ x ], clbk, ctx );
104+
// returns 3
105+
106+
var count = ctx.count;
107+
// returns 4
108+
```
109+
110+
</section>
111+
112+
<!-- /.usage -->
113+
114+
<section class="notes">
115+
116+
## Notes
117+
118+
- If provided an empty one-dimensional ndarray, the function returns `-1`.
119+
- A provided callback function should return a boolean value.
120+
121+
</section>
122+
123+
<!-- /.notes -->
124+
125+
<section class="examples">
126+
127+
## Examples
128+
129+
<!-- eslint no-undef: "error" -->
130+
131+
```javascript
132+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
133+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
134+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
135+
var gfindLastIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index' );
136+
137+
function isEven( v ) {
138+
return v % 2.0 === 0.0;
139+
}
140+
141+
var xbuf = discreteUniform( 10, -100, 100, {
142+
'dtype': 'generic'
143+
});
144+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
145+
console.log( ndarray2array( x ) );
146+
147+
var idx = gfindLastIndex( [ x ], isEven );
148+
console.log( idx );
149+
```
150+
151+
</section>
152+
153+
<!-- /.examples -->
154+
155+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
156+
157+
<section class="related">
158+
159+
</section>
160+
161+
<!-- /.related -->
162+
163+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
164+
165+
<section class="links">
166+
167+
</section>
168+
169+
<!-- /.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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var pkg = require( './../package.json' ).name;
29+
var gfindLastIndex = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Callback function.
43+
*
44+
* @private
45+
* @param {number} v - array element
46+
* @returns {number} callback result
47+
*/
48+
function clbk( v ) {
49+
return v < 0.0;
50+
}
51+
52+
/**
53+
* Creates a benchmark function.
54+
*
55+
* @private
56+
* @param {PositiveInteger} len - array length
57+
* @returns {Function} benchmark function
58+
*/
59+
function createBenchmark( len ) {
60+
var xbuf;
61+
var x;
62+
63+
xbuf = uniform( len, 0.0, 100.0, options );
64+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
65+
66+
return benchmark;
67+
68+
function benchmark( b ) {
69+
var v;
70+
var i;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
v = gfindLastIndex( [ x ], clbk );
75+
if ( isnan( v ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
}
79+
b.toc();
80+
if ( isnan( v ) ) {
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+':len='+len, f );
110+
}
111+
}
112+
113+
main();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
{{alias}}( arrays, clbk[, thisArg] )
3+
Returns the index of the last element in a one-dimensional ndarray which
4+
passes a test implemented by a predicate function.
5+
6+
If provided an empty ndarray, the function returns `-1`.
7+
8+
The callback function is provided three arguments:
9+
10+
- value: current array element.
11+
- index: current array index.
12+
- array: the input ndarray.
13+
14+
The callback function should return a boolean value.
15+
16+
Parameters
17+
----------
18+
arrays: ArrayLikeObject<ndarray>
19+
Array-like object containing a one-dimensional input ndarray.
20+
21+
clbk: Function
22+
Callback function.
23+
24+
thisArg: any (optional)
25+
Callback execution context.
26+
27+
Returns
28+
-------
29+
out: integer
30+
Index.
31+
32+
Examples
33+
--------
34+
> var xbuf = [ 1.0, -2.0, 2.0 ];
35+
> var dt = 'generic';
36+
> var sh = [ xbuf.length ];
37+
> var sx = [ 1 ];
38+
> var ox = 0;
39+
> var ord = 'row-major';
40+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
41+
> function f ( v ) { return v % 2.0 === 0.0; };
42+
> {{alias}}( [ x ], f )
43+
2
44+
45+
See Also
46+
--------

0 commit comments

Comments
 (0)