Skip to content

Commit 7990082

Browse files
authored
feat: add blas/ext/base/ndarray/sindex-of-truthy
PR-URL: #13882 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#853
1 parent a718677 commit 7990082

10 files changed

Lines changed: 663 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
# sindexOfTruthy
22+
23+
> Return the index of the first truthy 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 sindexOfTruthy = require( '@stdlib/blas/ext/base/ndarray/sindex-of-truthy' );
37+
```
38+
39+
#### sindexOfTruthy( arrays )
40+
41+
Returns the index of the first truthy element in a one-dimensional single-precision floating-point ndarray.
42+
43+
```javascript
44+
var Float32Vector = require( '@stdlib/ndarray/vector/float32' );
45+
46+
var x = new Float32Vector( [ 0.0, 0.0, 3.0, 2.0 ] );
47+
48+
var idx = sindexOfTruthy( [ x ] );
49+
// returns 2
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **arrays**: array-like object containing a one-dimensional input ndarray.
55+
56+
If the function is unable to find a truthy element, the function returns `-1`.
57+
58+
```javascript
59+
var Float32Vector = require( '@stdlib/ndarray/vector/float32' );
60+
61+
var x = new Float32Vector( [ 0.0, 0.0, 0.0, 0.0 ] );
62+
63+
var idx = sindexOfTruthy( [ x ] );
64+
// returns -1
65+
```
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<section class="notes">
72+
73+
## Notes
74+
75+
- The function explicitly treats `NaN` values as falsy.
76+
77+
</section>
78+
79+
<!-- /.notes -->
80+
81+
<section class="examples">
82+
83+
## Examples
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
```javascript
88+
var bernoulli = require( '@stdlib/random/bernoulli' );
89+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
90+
var sindexOfTruthy = require( '@stdlib/blas/ext/base/ndarray/sindex-of-truthy' );
91+
92+
var opts = {
93+
'dtype': 'float32'
94+
};
95+
96+
var x = bernoulli( [ 10 ], 0.7, opts );
97+
console.log( ndarray2array( x ) );
98+
99+
var idx = sindexOfTruthy( [ x ] );
100+
console.log( idx );
101+
```
102+
103+
</section>
104+
105+
<!-- /.examples -->
106+
107+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
108+
109+
<section class="related">
110+
111+
</section>
112+
113+
<!-- /.related -->
114+
115+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
116+
117+
<section class="links">
118+
119+
</section>
120+
121+
<!-- /.links -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var sindexOfTruthy = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var x = zeros( [ len ], options );
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var out;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
out = sindexOfTruthy( [ x ] );
65+
if ( out !== out ) {
66+
b.fail( 'should return an integer' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isInteger( out ) ) {
71+
b.fail( 'should return an integer' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( format( '%s:len=%d', pkg, len ), f );
100+
}
101+
}
102+
103+
main();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( arrays )
3+
Returns the index of the first truthy element in a one-dimensional single-
4+
precision floating-point ndarray.
5+
6+
If unable to find a truthy element, the function returns `-1`.
7+
8+
Parameters
9+
----------
10+
arrays: ArrayLikeObject<ndarray>
11+
Array-like object containing the following ndarrays:
12+
13+
- a one-dimensional input ndarray.
14+
15+
Returns
16+
-------
17+
out: integer
18+
Index.
19+
20+
Examples
21+
--------
22+
> var x = new {{alias:@stdlib/ndarray/vector/float32}}( [ 0.0, 0.0, 3.0 ] );
23+
> {{alias}}( [ x ] )
24+
2
25+
26+
See Also
27+
--------
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 { float32ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns the index of the first truthy element in a one-dimensional single-precision floating-point ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
*
34+
* @param arrays - array-like object containing ndarrays
35+
* @returns index
36+
*
37+
* @example
38+
* var Float32Vector = require( '@stdlib/ndarray/vector/float32' );
39+
*
40+
* var x = new Float32Vector( [ 0.0, 0.0, 3.0, 2.0 ] );
41+
*
42+
* var v = sindexOfTruthy( [ x ] );
43+
* // returns 2
44+
*/
45+
declare function sindexOfTruthy( arrays: [ float32ndarray ] ): number;
46+
47+
48+
// EXPORTS //
49+
50+
export = sindexOfTruthy;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import sindexOfTruthy = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns a number...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'float32'
31+
});
32+
33+
sindexOfTruthy( [ x ] ); // $ExpectType number
34+
}
35+
36+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
37+
{
38+
sindexOfTruthy( '10' ); // $ExpectError
39+
sindexOfTruthy( 10 ); // $ExpectError
40+
sindexOfTruthy( true ); // $ExpectError
41+
sindexOfTruthy( false ); // $ExpectError
42+
sindexOfTruthy( null ); // $ExpectError
43+
sindexOfTruthy( undefined ); // $ExpectError
44+
sindexOfTruthy( [] ); // $ExpectError
45+
sindexOfTruthy( {} ); // $ExpectError
46+
sindexOfTruthy( ( x: number ): number => x ); // $ExpectError
47+
}
48+
49+
// The compiler throws an error if the function is provided an unsupported number of arguments...
50+
{
51+
const x = zeros( [ 10 ], {
52+
'dtype': 'float32'
53+
});
54+
55+
sindexOfTruthy(); // $ExpectError
56+
sindexOfTruthy( [ x ], {} ); // $ExpectError
57+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
var bernoulli = require( '@stdlib/random/bernoulli' );
22+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
23+
var sindexOfTruthy = require( './../lib' );
24+
25+
var opts = {
26+
'dtype': 'float32'
27+
};
28+
29+
var x = bernoulli( [ 10 ], 0.7, opts );
30+
console.log( ndarray2array( x ) );
31+
32+
var idx = sindexOfTruthy( [ x ] );
33+
console.log( idx );

0 commit comments

Comments
 (0)