Skip to content

Commit a071b8b

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/ndarray/gindex-of-falsy
PR-URL: #12992 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#843
1 parent 7f1c224 commit a071b8b

10 files changed

Lines changed: 721 additions & 0 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
# gindexOfFalsy
22+
23+
> Return the index of the first falsy 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 gindexOfFalsy = require( '@stdlib/blas/ext/base/ndarray/gindex-of-falsy' );
37+
```
38+
39+
#### gindexOfFalsy( arrays )
40+
41+
Returns the index of the first falsy element in a one-dimensional ndarray.
42+
43+
```javascript
44+
var vector = require( '@stdlib/ndarray/vector/ctor' );
45+
46+
var x = vector( [ 1.0, 3.0, 0.0, 2.0 ], 'generic' );
47+
48+
var idx = gindexOfFalsy( [ x ] );
49+
// returns 2
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **arrays**: array-like object containing the following ndarrays:
55+
56+
- a one-dimensional input ndarray.
57+
58+
If the function is unable to find a falsy element, the function returns `-1`.
59+
60+
```javascript
61+
var vector = require( '@stdlib/ndarray/vector/ctor' );
62+
63+
var x = vector( [ 1.0, 2.0, 3.0, 4.0 ], 'generic' );
64+
65+
var idx = gindexOfFalsy( [ x ] );
66+
// returns -1
67+
```
68+
69+
</section>
70+
71+
<!-- /.usage -->
72+
73+
<section class="notes">
74+
75+
## Notes
76+
77+
- The function treats `NaN` values a falsy.
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
91+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
92+
var gindexOfFalsy = require( '@stdlib/blas/ext/base/ndarray/gindex-of-falsy' );
93+
94+
var opts = {
95+
'dtype': 'generic'
96+
};
97+
98+
var x = discreteUniform( [ 10 ], 0, 5, opts );
99+
console.log( ndarray2array( x ) );
100+
101+
var idx = gindexOfFalsy( [ x ] );
102+
console.log( idx );
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
110+
111+
<section class="related">
112+
113+
</section>
114+
115+
<!-- /.related -->
116+
117+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
118+
119+
<section class="links">
120+
121+
</section>
122+
123+
<!-- /.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 ones = require( '@stdlib/ndarray/ones' );
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 gindexOfFalsy = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
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 = ones( [ 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 = gindexOfFalsy( [ 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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( arrays )
3+
Returns the index of the first falsy element in a one-dimensional ndarray.
4+
5+
If unable to find a falsy element, the function returns `-1`.
6+
7+
Parameters
8+
----------
9+
arrays: ArrayLikeObject<ndarray>
10+
Array-like object containing the following ndarrays:
11+
12+
- a one-dimensional input ndarray.
13+
14+
Returns
15+
-------
16+
out: integer
17+
Index.
18+
19+
Examples
20+
--------
21+
> var x = {{alias:@stdlib/ndarray/vector/ctor}}( [ 1.0, 0.0, 3.0 ], 'generic' );
22+
> {{alias}}( [ x ] )
23+
1
24+
25+
See Also
26+
--------
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 { typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns the index of the first falsy element in a one-dimensional 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 vector = require( '@stdlib/ndarray/vector/ctor' );
39+
*
40+
* var x = vector( [ 1.0, 3.0, 0.0, 2.0 ], 'generic' );
41+
*
42+
* var v = gindexOfFalsy( [ x ] );
43+
* // returns 2
44+
*/
45+
declare function gindexOfFalsy( arrays: [ typedndarray<number> ] ): number;
46+
47+
48+
// EXPORTS //
49+
50+
export = gindexOfFalsy;
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 gindexOfFalsy = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns a number...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'generic'
31+
});
32+
33+
gindexOfFalsy( [ 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+
gindexOfFalsy( '10' ); // $ExpectError
39+
gindexOfFalsy( 10 ); // $ExpectError
40+
gindexOfFalsy( true ); // $ExpectError
41+
gindexOfFalsy( false ); // $ExpectError
42+
gindexOfFalsy( null ); // $ExpectError
43+
gindexOfFalsy( undefined ); // $ExpectError
44+
gindexOfFalsy( [] ); // $ExpectError
45+
gindexOfFalsy( {} ); // $ExpectError
46+
gindexOfFalsy( ( 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': 'generic'
53+
});
54+
55+
gindexOfFalsy(); // $ExpectError
56+
gindexOfFalsy( [ 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 discreteUniform = require( '@stdlib/random/discrete-uniform' );
22+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
23+
var gindexOfFalsy = require( './../lib' );
24+
25+
var opts = {
26+
'dtype': 'generic'
27+
};
28+
29+
var x = discreteUniform( [ 10 ], 0, 5, opts );
30+
console.log( ndarray2array( x ) );
31+
32+
var idx = gindexOfFalsy( [ x ] );
33+
console.log( idx );

0 commit comments

Comments
 (0)