feat: add `blas/ext/base/ndarray/dindex-of-less-than-equal` · stdlib-js/stdlib@4867aca · GitHub
Skip to content

Commit 4867aca

Browse files
authored
feat: add blas/ext/base/ndarray/dindex-of-less-than-equal
PR-URL: #15888 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#1279
1 parent ba23e07 commit 4867aca

10 files changed

Lines changed: 1074 additions & 0 deletions

File tree

Lines changed: 154 additions & 0 deletions
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 zeros = require( '@stdlib/ndarray/zeros' );
25+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var dindexOfLessThanEqual = 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 searchElement;
51+
var fromIndex;
52+
var x;
53+
54+
x = zeros( [ len ], options );
55+
searchElement = scalar2ndarray( -1.0, options );
56+
fromIndex = scalar2ndarray( 0, {
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 = dindexOfLessThanEqual( [ 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( format( '%s:len=%d', pkg, len ), f );
109+
}
110+
}
111+
112+
main();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{{alias}}( arrays )
3+
Returns the first index of an element in a one-dimensional double-precision
4+
floating-point ndarray which is less than or equal to a specified search
5+
element.
6+
7+
If a specified starting search index is negative, the function resolves the
8+
starting search index by counting backward from the last element (where `-1`
9+
refers to the last element).
10+
11+
When comparing elements, the function uses the less-than-or-equal operator
12+
`<=`. As a consequence, comparisons involving `NaN` always evaluate to
13+
`false`, and `-0` and `+0` are considered the same.
14+
15+
If unable to find an element which is less than or equal to a specified
16+
search element, the function returns `-1`.
17+
18+
Parameters
19+
----------
20+
arrays: ArrayLikeObject<ndarray>
21+
Array-like object containing the following ndarrays:
22+
23+
- a one-dimensional input ndarray.
24+
- a zero-dimensional ndarray containing the search element.
25+
- a zero-dimensional ndarray containing the index from which to begin
26+
searching.
27+
28+
Returns
29+
-------
30+
out: integer
31+
Index.
32+
33+
Examples
34+
--------
35+
> var x = new {{alias:@stdlib/ndarray/vector/float64}}( [ 3.0, 4.0, 1.0, 2.0 ] );
36+
> var v = {{alias:@stdlib/ndarray/from-scalar}}( 2.0, { 'dtype': 'float64' } );
37+
> var i = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } );
38+
> {{alias}}( [ x, v, i ] )
39+
2
40+
41+
See Also
42+
--------
Lines changed: 63 additions & 0 deletions

0 commit comments

Comments
 (0)