Skip to content

Commit 0768604

Browse files
authored
feat: add blas/ext/base/ndarray/glast-index-less-than
PR-URL: #15661 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#1388
1 parent e10fdb6 commit 0768604

10 files changed

Lines changed: 995 additions & 0 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# glastIndexLessThan
22+
23+
> Return the index of the last element in a one-dimensional ndarray which is less than a corresponding element in another 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 glastIndexLessThan = require( '@stdlib/blas/ext/base/ndarray/glast-index-less-than' );
37+
```
38+
39+
#### glastIndexLessThan( arrays )
40+
41+
Returns the index of the last element in a one-dimensional ndarray which is less than a corresponding element in another one-dimensional ndarray.
42+
43+
```javascript
44+
var vector = require( '@stdlib/ndarray/vector/ctor' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
47+
var x = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' );
48+
var y = vector( [ 0.0, 0.0, 1.0, 0.0 ], 'generic' );
49+
50+
var fromIndex = scalar2ndarray( 3, {
51+
'dtype': 'generic'
52+
});
53+
54+
var idx = glastIndexLessThan( [ x, y, fromIndex ] );
55+
// returns 2
56+
```
57+
58+
The function has the following parameters:
59+
60+
- **arrays**: array-like object containing the following ndarrays:
61+
62+
- first one-dimensional input ndarray.
63+
- second one-dimensional input ndarray.
64+
- a zero-dimensional ndarray containing the index from which to begin searching.
65+
66+
If the function is unable to find an element in the first one-dimensional input ndarray which is less than a corresponding element in the second one-dimensional input ndarray, the function returns `-1`.
67+
68+
```javascript
69+
var vector = require( '@stdlib/ndarray/vector/ctor' );
70+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
71+
72+
var x = vector( [ 5.0, 6.0, 7.0, 8.0 ], 'generic' );
73+
var y = vector( [ 1.0, 2.0, 3.0, 4.0 ], 'generic' );
74+
75+
var fromIndex = scalar2ndarray( 3, {
76+
'dtype': 'generic'
77+
});
78+
79+
var idx = glastIndexLessThan( [ x, y, fromIndex ] );
80+
// returns -1
81+
```
82+
83+
</section>
84+
85+
<!-- /.usage -->
86+
87+
<section class="notes">
88+
89+
## Notes
90+
91+
- 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).
92+
- When comparing elements, the function checks whether an element in the first one-dimensional input ndarray is less than a corresponding element in the second one-dimensional input ndarray using the less-than operator `<`. As a consequence, comparisons involving `NaN` always evaluate to `false`.
93+
94+
</section>
95+
96+
<!-- /.notes -->
97+
98+
<section class="examples">
99+
100+
## Examples
101+
102+
<!-- eslint no-undef: "error" -->
103+
104+
```javascript
105+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
106+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
107+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
108+
var ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' );
109+
var glastIndexLessThan = require( '@stdlib/blas/ext/base/ndarray/glast-index-less-than' );
110+
111+
var opts = {
112+
'dtype': 'generic'
113+
};
114+
var x = discreteUniform( [ 10 ], 0, 10, opts );
115+
console.log( ndarray2array( x ) );
116+
117+
var y = discreteUniform( [ 10 ], 0, 10, opts );
118+
console.log( ndarray2array( y ) );
119+
120+
var fromIndex = scalar2ndarray( 9, {
121+
'dtype': 'generic'
122+
});
123+
console.log( 'From Index:', ndarraylike2scalar( fromIndex ) );
124+
125+
var idx = glastIndexLessThan( [ x, y, fromIndex ] );
126+
console.log( idx );
127+
```
128+
129+
</section>
130+
131+
<!-- /.examples -->
132+
133+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
134+
135+
<section class="related">
136+
137+
</section>
138+
139+
<!-- /.related -->
140+
141+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
142+
143+
<section class="links">
144+
145+
</section>
146+
147+
<!-- /.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) 2026 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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
27+
var ones = require( '@stdlib/ndarray/ones' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var glastIndexLessThan = 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 fromIndex;
51+
var x;
52+
var y;
53+
54+
x = ones( [ len ], options );
55+
y = ones( [ len ], options );
56+
fromIndex = scalar2ndarray( len - 1, {
57+
'dtype': 'generic'
58+
});
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
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 = glastIndexLessThan( [ x, y, 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( format( '%s:len=%d', pkg, 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 index of the last element in a one-dimensional ndarray which is
4+
less than a corresponding element in another one-dimensional 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+
When comparing elements, the function checks whether an element in the first
11+
one-dimensional input ndarray is less than a corresponding element in the
12+
second one-dimensional input ndarray using the less-than operator `<`. As a
13+
consequence, comparisons involving `NaN` always evaluate to `false`.
14+
15+
Parameters
16+
----------
17+
arrays: ArrayLikeObject<ndarray>
18+
Array-like object containing the following ndarrays:
19+
20+
- first one-dimensional input ndarray.
21+
- second one-dimensional input ndarray.
22+
- a zero-dimensional ndarray containing the index from which to begin
23+
searching.
24+
25+
Returns
26+
-------
27+
out: integer
28+
Index.
29+
30+
Examples
31+
--------
32+
> var x = {{alias:@stdlib/ndarray/vector/ctor}}( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' );
33+
> var y = {{alias:@stdlib/ndarray/vector/ctor}}( [ 0.0, 0.0, 1.0, 0.0 ], 'generic' );
34+
> var i = {{alias:@stdlib/ndarray/from-scalar}}( 3, { 'dtype': 'generic' } );
35+
> {{alias}}( [ x, y, i ] )
36+
2
37+
38+
See Also
39+
--------
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 index of the last element in a one-dimensional ndarray which is less than a corresponding element in another one-dimensional ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - first one-dimensional input ndarray.
33+
* - second one-dimensional input ndarray.
34+
* - a zero-dimensional ndarray containing the index from which to begin searching.
35+
*
36+
* - When comparing elements, the function checks whether an element in the first one-dimensional input ndarray is less than a corresponding element in the second one-dimensional input ndarray using the less-than operator `<`. As a consequence, comparisons involving `NaN` always evaluate to `false`.
37+
*
38+
* @param arrays - array-like object containing ndarrays
39+
* @returns index
40+
*
41+
* @example
42+
* var vector = require( '@stdlib/ndarray/vector/ctor' );
43+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
44+
*
45+
* var x = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' );
46+
* var y = vector( [ 0.0, 0.0, 1.0, 0.0 ], 'generic' );
47+
*
48+
* var fromIndex = scalar2ndarray( 3, {
49+
* 'dtype': 'generic'
50+
* });
51+
*
52+
* var idx = glastIndexLessThan( [ x, y, fromIndex ] );
53+
* // returns 2
54+
*/
55+
declare function glastIndexLessThan( arrays: [ typedndarray<unknown>, typedndarray<unknown>, typedndarray<number> ] ): number;
56+
57+
58+
// EXPORTS //
59+
60+
export = glastIndexLessThan;

0 commit comments

Comments
 (0)