Skip to content

Commit bfaddc8

Browse files
authored
feat: add blas/ext/base/ndarray/scunone
PR-URL: #12887 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#690
1 parent b8c28ee commit bfaddc8

10 files changed

Lines changed: 847 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)