Skip to content

Commit c630332

Browse files
committed
Auto-generated commit
1 parent 047291d commit c630332

File tree

16 files changed

+3059
-0
lines changed

16 files changed

+3059
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`85b48f1`](https://github.com/stdlib-js/stdlib/commit/85b48f1932349ff73045a1e53e7477296233381c) - add `blas/ext/base/gwhere` [(#11345)](https://github.com/stdlib-js/stdlib/pull/11345)
1314
- [`2ab6254`](https://github.com/stdlib-js/stdlib/commit/2ab6254a552c8bb1024eb5fc5baa35cd4812f64b) - update `blas/ext/base/ndarray` TypeScript declarations [(#11496)](https://github.com/stdlib-js/stdlib/pull/11496)
1415
- [`aacc13a`](https://github.com/stdlib-js/stdlib/commit/aacc13a91e7131c5829cdb3622990a92805aabc9) - add `dsort` to namespace
1516
- [`56329f6`](https://github.com/stdlib-js/stdlib/commit/56329f6843adb81d057638457604f1442db76484) - add `ssort` to namespace
@@ -864,6 +865,7 @@ A total of 57 issues were closed in this release:
864865

865866
<details>
866867

868+
- [`85b48f1`](https://github.com/stdlib-js/stdlib/commit/85b48f1932349ff73045a1e53e7477296233381c) - **feat:** add `blas/ext/base/gwhere` [(#11345)](https://github.com/stdlib-js/stdlib/pull/11345) _(by Muhammad Haris, Athan Reines)_
867869
- [`10210e4`](https://github.com/stdlib-js/stdlib/commit/10210e49f0b5e71b93243d12b650048dc17abed3) - **docs:** update namespace table of contents [(#11497)](https://github.com/stdlib-js/stdlib/pull/11497) _(by stdlib-bot)_
868870
- [`2ab6254`](https://github.com/stdlib-js/stdlib/commit/2ab6254a552c8bb1024eb5fc5baa35cd4812f64b) - **feat:** update `blas/ext/base/ndarray` TypeScript declarations [(#11496)](https://github.com/stdlib-js/stdlib/pull/11496) _(by stdlib-bot)_
869871
- [`aacc13a`](https://github.com/stdlib-js/stdlib/commit/aacc13a91e7131c5829cdb3622990a92805aabc9) - **feat:** add `dsort` to namespace _(by Athan Reines)_

ext/base/gwhere/README.md

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

0 commit comments

Comments
 (0)