Skip to content

Commit 6cb1706

Browse files
committed
feat: add blas/ext/base/gwhere
1 parent a04c190 commit 6cb1706

File tree

15 files changed

+2705
-0
lines changed

15 files changed

+2705
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
# gwhere
22+
23+
> Take elements from one of two strided arrays depending on a condition.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gwhere = require( '@stdlib/blas/ext/base/gwhere' );
37+
```
38+
39+
#### gwhere( N, condition, strideC, x, strideX, y, strideY )
40+
41+
Takes elements from one of two strided arrays depending on a condition.
42+
43+
```javascript
44+
var condition = [ 1, 0, 1 ];
45+
var x = [ 1.0, 2.0, 3.0 ];
46+
var y = [ 4.0, 5.0, 6.0 ];
47+
48+
var out = gwhere( 3, condition, 1, x, 1, y, 1 );
49+
// returns [ 1.0, 5.0, 3.0 ]
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **N**: number of indexed elements.
55+
- **condition**: condition array.
56+
- **strideC**: stride length for `condition`.
57+
- **x**: first input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
58+
- **strideX**: stride length for `x`.
59+
- **y**: second input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
60+
- **strideY**: stride length for `y`.
61+
62+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to select from every other element:
63+
64+
```javascript
65+
var condition = [ 1, 0, 0, 1, 1, 0 ];
66+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
67+
var y = [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ];
68+
69+
var out = gwhere( 3, condition, 2, x, 2, y, 2 );
70+
// returns [ 1.0, 9.0, 5.0 ]
71+
```
72+
73+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
74+
75+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
76+
77+
```javascript
78+
var Float64Array = require( '@stdlib/array/float64' );
79+
80+
// Initial arrays...
81+
var condition0 = new Float64Array( [ 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ] );
82+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
83+
var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
84+
85+
// Create offset views...
86+
var condition1 = new Float64Array( condition0.buffer, condition0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
87+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
88+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
89+
90+
var out = gwhere( 3, condition1, 2, x1, 2, y1, 2 );
91+
// returns [ 2.0, 4.0, 6.0 ]
92+
```
93+
94+
<!-- lint disable maximum-heading-length -->
95+
96+
#### gwhere.ndarray( N, condition, strideC, offsetC, x, strideX, offsetX, y, strideY, offsetY )
97+
98+
<!-- lint enable maximum-heading-length -->
99+
100+
Takes elements from one of two strided arrays depending on a condition using alternative indexing semantics.
101+
102+
```javascript
103+
var condition = [ 1, 0, 1 ];
104+
var x = [ 1.0, 2.0, 3.0 ];
105+
var y = [ 4.0, 5.0, 6.0 ];
106+
107+
var out = gwhere.ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0 );
108+
// returns [ 1.0, 5.0, 3.0 ]
109+
```
110+
111+
The function has the following additional parameters:
112+
113+
- **offsetC**: starting index for `condition`.
114+
- **offsetX**: starting index for `x`.
115+
- **offsetY**: starting index for `y`.
116+
117+
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 select from every other element starting from the second element:
118+
119+
```javascript
120+
var condition = [ 0, 1, 0, 0, 0, 1 ];
121+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
122+
var y = [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ];
123+
124+
var out = gwhere.ndarray( 3, condition, 2, 1, x, 2, 1, y, 2, 1 );
125+
// returns [ 2.0, 10.0, 6.0 ]
126+
```
127+
128+
</section>
129+
130+
<!-- /.usage -->
131+
132+
<section class="notes">
133+
134+
## Notes
135+
136+
- If `N <= 0`, both functions return an empty array.
137+
- 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]).
138+
- Both functions return a new generic `Array`. To assign results to a specific output array, use [`@stdlib/array/base/where`][@stdlib/array/base/where].
139+
140+
</section>
141+
142+
<!-- /.notes -->
143+
144+
<section class="examples">
145+
146+
## Examples
147+
148+
<!-- eslint no-undef: "error" -->
149+
150+
```javascript
151+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
152+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
153+
var gwhere = require( '@stdlib/blas/ext/base/gwhere' );
154+
155+
var condition = bernoulli( 20, 0.5, {
156+
'dtype': 'generic'
157+
});
158+
var x = discreteUniform( 20, 0, 100, {
159+
'dtype': 'generic'
160+
});
161+
var y = discreteUniform( 20, -100, 0, {
162+
'dtype': 'generic'
163+
});
164+
console.log( condition );
165+
console.log( x );
166+
console.log( y );
167+
168+
var out = gwhere( condition.length, condition, 1, x, 1, y, 1 );
169+
console.log( out );
170+
```
171+
172+
</section>
173+
174+
<!-- /.examples -->
175+
176+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
177+
178+
<section class="related">
179+
180+
</section>
181+
182+
<!-- /.related -->
183+
184+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
185+
186+
<section class="links">
187+
188+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
189+
190+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
191+
192+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
193+
194+
[@stdlib/array/base/where]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/where
195+
196+
</section>
197+
198+
<!-- /.links -->
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 uniform = require( '@stdlib/random/array/uniform' );
26+
var isArray = require( '@stdlib/assert/is-array' );
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 gwhere = 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 condition = bernoulli( len, 0.5, options );
51+
var x = uniform( len, -100, 100, options );
52+
var y = uniform( len, -100, 100, options );
53+
return benchmark;
54+
55+
/**
56+
* Benchmark function.
57+
*
58+
* @private
59+
* @param {Benchmark} b - benchmark instance
60+
*/
61+
function benchmark( b ) {
62+
var out;
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
condition[ i%len ] = ( condition[ i%len ] + 1 ) % 2;
68+
out = gwhere( len, condition, 1, x, 1, y, 1 );
69+
if ( !isArray( out ) ) {
70+
b.fail( 'should return an array' );
71+
}
72+
}
73+
b.toc();
74+
if ( !isArray( out ) ) {
75+
b.fail( 'should return an array' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( format( '%s:len=%d', pkg, len ), f );
104+
}
105+
}
106+
107+
main();

0 commit comments

Comments
 (0)