Skip to content

Commit 7f321d8

Browse files
authored
feat: add blas/ext/base/gcuany
PR-URL: #12535 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#542
1 parent 8e32423 commit 7f321d8

15 files changed

Lines changed: 2042 additions & 0 deletions

File tree

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
# gcuany
22+
23+
> Cumulatively test whether at least one element in a strided array is truthy.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gcuany = require( '@stdlib/blas/ext/base/gcuany' );
37+
```
38+
39+
#### gcuany( N, x, strideX, out, strideOut )
40+
41+
Cumulatively tests whether at least one element in a strided array is truthy.
42+
43+
```javascript
44+
var x = [ 0, 0, 1, 0 ];
45+
var out = [ false, false, false, false ];
46+
47+
gcuany( x.length, x, 1, out, 1 );
48+
// out => [ false, false, true, true ]
49+
```
50+
51+
The function has the following parameters:
52+
53+
- **N**: number of indexed elements.
54+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
55+
- **strideX**: stride length for `x`.
56+
- **out**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
57+
- **strideOut**: stride length for `out`.
58+
59+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to cumulatively test every other element:
60+
61+
```javascript
62+
var x = [ 0, 0, 0, 0, 1, 0, 1, 0 ];
63+
var out = [ false, false, false, false ];
64+
65+
gcuany( 4, x, 2, out, 1 );
66+
// out => [ false, false, true, true ]
67+
```
68+
69+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
70+
71+
<!-- eslint-disable stdlib/capitalized-comments -->
72+
73+
```javascript
74+
var Uint8Array = require( '@stdlib/array/uint8' );
75+
76+
// Initial arrays...
77+
var x0 = new Uint8Array( [ 0, 0, 0, 1, 1, 1 ] );
78+
var o0 = new Uint8Array( x0.length );
79+
80+
// Create offset views...
81+
var x1 = new Uint8Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
82+
var o1 = new Uint8Array( o0.buffer, o0.BYTES_PER_ELEMENT*3 ); // start at 4th element
83+
84+
gcuany( 3, x1, 2, o1, 1 );
85+
// o0 => <Uint8Array>[ 0, 0, 0, 0, 1, 1 ]
86+
```
87+
88+
#### gcuany.ndarray( N, x, strideX, offsetX, out, strideOut, offsetOut )
89+
90+
Cumulatively tests whether at least one element in a strided array is truthy using alternative indexing semantics.
91+
92+
```javascript
93+
var x = [ 0, 0, 1, 0 ];
94+
var out = [ false, false, false, false ];
95+
96+
gcuany.ndarray( x.length, x, 1, 0, out, 1, 0 );
97+
// out => [ false, false, true, true ]
98+
```
99+
100+
The function has the following additional parameters:
101+
102+
- **offsetX**: starting index for `x`.
103+
- **offsetOut**: starting index for `out`.
104+
105+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example, to cumulatively test every other element starting from the third element:
106+
107+
```javascript
108+
var x = [ 0, 0, 0, 0, 1, 0, 1, 0 ];
109+
var out = [ false, false, false ];
110+
111+
gcuany.ndarray( 3, x, 2, 2, out, 1, 0 );
112+
// out => [ false, true, true ]
113+
```
114+
115+
</section>
116+
117+
<!-- /.usage -->
118+
119+
<section class="notes">
120+
121+
## Notes
122+
123+
- If `N <= 0`, both functions return `out` unchanged.
124+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
125+
126+
</section>
127+
128+
<!-- /.notes -->
129+
130+
<section class="examples">
131+
132+
## Examples
133+
134+
<!-- eslint no-undef: "error" -->
135+
136+
```javascript
137+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
138+
var filled = require( '@stdlib/array/base/filled' );
139+
var gcuany = require( '@stdlib/blas/ext/base/gcuany' );
140+
141+
var x = bernoulli( 10, 0.5, {
142+
'dtype': 'generic'
143+
});
144+
console.log( x );
145+
146+
var out = filled( false, x.length );
147+
gcuany( x.length, x, 1, out, 1 );
148+
console.log( out );
149+
```
150+
151+
</section>
152+
153+
<!-- /.examples -->
154+
155+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
156+
157+
<section class="related">
158+
159+
</section>
160+
161+
<!-- /.related -->
162+
163+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
164+
165+
<section class="links">
166+
167+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
168+
169+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
170+
171+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
172+
173+
<!-- <related-links> -->
174+
175+
<!-- </related-links> -->
176+
177+
</section>
178+
179+
<!-- /.links -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 bernoulli = require( '@stdlib/random/array/bernoulli' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var filled = require( '@stdlib/array/base/filled' );
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 gcuany = require( './../lib/main.js' );
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 out = filled( false, len );
51+
var x = bernoulli( len, 0.5, options );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var v;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
v = gcuany( len, x, 1, out, 1 );
67+
if ( typeof v[ i%len ] !== 'boolean' ) {
68+
b.fail( 'should return boolean' );
69+
}
70+
}
71+
b.toc();
72+
if ( !isBoolean( v[ i%len ] ) ) {
73+
b.fail( 'should return boolean' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 6; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = pow( 10, i );
100+
f = createBenchmark( len );
101+
bench( format( '%s:len=%d', pkg, len ), f );
102+
}
103+
}
104+
105+
main();
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 bernoulli = require( '@stdlib/random/array/bernoulli' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var filled = require( '@stdlib/array/base/filled' );
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 gcuany = require( './../lib/ndarray.js' );
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 out = filled( false, len );
51+
var x = bernoulli( len, 0.5, options );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var v;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
v = gcuany( len, x, 1, 0, out, 1, 0 );
67+
if ( typeof v[ i%len ] !== 'boolean' ) {
68+
b.fail( 'should return boolean' );
69+
}
70+
}
71+
b.toc();
72+
if ( !isBoolean( v[ i%len ] ) ) {
73+
b.fail( 'should return boolean' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 6; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = pow( 10, i );
100+
f = createBenchmark( len );
101+
bench( format( '%s:ndarray:len=%d', pkg, len ), f );
102+
}
103+
}
104+
105+
main();

0 commit comments

Comments
 (0)