Skip to content

Commit 5c9fae7

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/dwhere
PR-URL: #11831 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#422
1 parent 4985313 commit 5c9fae7

33 files changed

Lines changed: 5116 additions & 0 deletions
Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
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+
# dwhere
22+
23+
> Take elements from one of two double-precision floating-point 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 dwhere = require( '@stdlib/blas/ext/base/dwhere' );
37+
```
38+
39+
#### dwhere( N, condition, strideC, x, strideX, y, strideY, out, strideOut )
40+
41+
Takes elements from one of two double-precision floating-point strided arrays depending on a condition.
42+
43+
```javascript
44+
var BooleanArray = require( '@stdlib/array/bool' );
45+
var Float64Array = require( '@stdlib/array/float64' );
46+
47+
var condition = new BooleanArray( [ true, false, true ] );
48+
var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
49+
var y = new Float64Array( [ 4.0, 5.0, 6.0 ] );
50+
var out = new Float64Array( [ 0.0, 0.0, 0.0 ] );
51+
52+
dwhere( 3, condition, 1, x, 1, y, 1, out, 1 );
53+
// out => <Float64Array>[ 1.0, 5.0, 3.0 ]
54+
```
55+
56+
The function has the following parameters:
57+
58+
- **N**: number of indexed elements.
59+
- **condition**: condition [`BooleanArray`][@stdlib/array/bool].
60+
- **strideC**: stride length for `condition`.
61+
- **x**: first input [`Float64Array`][@stdlib/array/float64].
62+
- **strideX**: stride length for `x`.
63+
- **y**: second input [`Float64Array`][@stdlib/array/float64].
64+
- **strideY**: stride length for `y`.
65+
- **out**: output [`Float64Array`][@stdlib/array/float64].
66+
- **strideOut**: stride length for `out`.
67+
68+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to select from every other element:
69+
70+
```javascript
71+
var BooleanArray = require( '@stdlib/array/bool' );
72+
var Float64Array = require( '@stdlib/array/float64' );
73+
74+
var condition = new BooleanArray( [ true, false, false, true, true, false ] );
75+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
76+
var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
77+
var out = new Float64Array( [ 0.0, 0.0, 0.0 ] );
78+
79+
dwhere( 3, condition, 2, x, 2, y, 2, out, 1 );
80+
// out => <Float64Array>[ 1.0, 9.0, 5.0 ]
81+
```
82+
83+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
84+
85+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
86+
87+
```javascript
88+
var BooleanArray = require( '@stdlib/array/bool' );
89+
var Float64Array = require( '@stdlib/array/float64' );
90+
91+
// Initial arrays...
92+
var condition0 = new BooleanArray( [ false, true, false, true, false, true ] );
93+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
94+
var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
95+
var out0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
96+
97+
// Create offset views...
98+
var condition1 = new BooleanArray( condition0.buffer, condition0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
99+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
100+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
101+
var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
102+
103+
dwhere( 3, condition1, 2, x1, 2, y1, 2, out1, 1 );
104+
// out0 => <Float64Array>[ 0.0, 2.0, 4.0, 6.0, 0.0, 0.0 ]
105+
```
106+
107+
<!-- lint disable maximum-heading-length -->
108+
109+
#### dwhere.ndarray( N, condition, strideC, offsetC, x, strideX, offsetX, y, strideY, offsetY, out, strideOut, offsetOut )
110+
111+
<!-- lint enable maximum-heading-length -->
112+
113+
Takes elements from one of two double-precision floating-point strided arrays depending on a condition using alternative indexing semantics.
114+
115+
```javascript
116+
var BooleanArray = require( '@stdlib/array/bool' );
117+
var Float64Array = require( '@stdlib/array/float64' );
118+
119+
var condition = new BooleanArray( [ true, false, true ] );
120+
var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
121+
var y = new Float64Array( [ 4.0, 5.0, 6.0 ] );
122+
var out = new Float64Array( [ 0.0, 0.0, 0.0 ] );
123+
124+
dwhere.ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0, out, 1, 0 );
125+
// out => <Float64Array>[ 1.0, 5.0, 3.0 ]
126+
```
127+
128+
The function has the following additional parameters:
129+
130+
- **offsetC**: starting index for `condition`.
131+
- **offsetX**: starting index for `x`.
132+
- **offsetY**: starting index for `y`.
133+
- **offsetOut**: starting index for `out`.
134+
135+
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:
136+
137+
```javascript
138+
var BooleanArray = require( '@stdlib/array/bool' );
139+
var Float64Array = require( '@stdlib/array/float64' );
140+
141+
var condition = new BooleanArray( [ false, true, false, false, false, true ] );
142+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
143+
var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
144+
var out = new Float64Array( [ 0.0, 0.0, 0.0 ] );
145+
146+
dwhere.ndarray( 3, condition, 2, 1, x, 2, 1, y, 2, 1, out, 1, 0 );
147+
// out => <Float64Array>[ 2.0, 10.0, 6.0 ]
148+
```
149+
150+
</section>
151+
152+
<!-- /.usage -->
153+
154+
<section class="notes">
155+
156+
## Notes
157+
158+
- If `N <= 0`, both functions return `out` unchanged.
159+
160+
</section>
161+
162+
<!-- /.notes -->
163+
164+
<section class="examples">
165+
166+
## Examples
167+
168+
<!-- eslint no-undef: "error" -->
169+
170+
```javascript
171+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
172+
var uniform = require( '@stdlib/random/array/uniform' );
173+
var BooleanArray = require( '@stdlib/array/bool' );
174+
var Float64Array = require( '@stdlib/array/float64' );
175+
var dwhere = require( '@stdlib/blas/ext/base/dwhere' );
176+
177+
var cbuf = bernoulli( 20, 0.5, {
178+
'dtype': 'uint8'
179+
});
180+
var condition = new BooleanArray( cbuf.buffer );
181+
console.log( condition );
182+
183+
var x = uniform( 20, 0.0, 100.0, {
184+
'dtype': 'float64'
185+
});
186+
console.log( x );
187+
188+
var y = uniform( 20, -100.0, 0.0, {
189+
'dtype': 'float64'
190+
});
191+
console.log( y );
192+
193+
var out = new Float64Array( condition.length );
194+
console.log( out );
195+
196+
dwhere( condition.length, condition, 1, x, 1, y, 1, out, 1 );
197+
console.log( out );
198+
```
199+
200+
</section>
201+
202+
<!-- /.examples -->
203+
204+
<!-- C interface documentation. -->
205+
206+
* * *
207+
208+
<section class="c">
209+
210+
## C APIs
211+
212+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
213+
214+
<section class="intro">
215+
216+
</section>
217+
218+
<!-- /.intro -->
219+
220+
<!-- C usage documentation. -->
221+
222+
<section class="usage">
223+
224+
### Usage
225+
226+
```c
227+
#include "stdlib/blas/ext/base/dwhere.h"
228+
```
229+
230+
<!-- lint disable maximum-heading-length -->
231+
232+
#### stdlib_strided_dwhere( N, \*Condition, strideC, \*X, strideX, \*Y, strideY, \*Out, strideOut )
233+
234+
<!-- lint enable maximum-heading-length -->
235+
236+
Takes elements from one of two double-precision floating-point strided arrays depending on a condition.
237+
238+
```c
239+
#include <stdbool.h>
240+
241+
const bool condition[] = { true, false, true };
242+
const double x[] = { 1.0, 2.0, 3.0 };
243+
const double y[] = { 4.0, 5.0, 6.0 };
244+
double out[] = { 0.0, 0.0, 0.0 };
245+
246+
stdlib_strided_dwhere( 3, condition, 1, x, 1, y, 1, out, 1 );
247+
```
248+
249+
The function accepts the following arguments:
250+
251+
- **N**: `[in] CBLAS_INT` number of indexed elements.
252+
- **Condition**: `[in] bool*` condition array.
253+
- **strideC**: `[in] CBLAS_INT` stride length for `Condition`.
254+
- **X**: `[in] double*` first input array.
255+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
256+
- **Y**: `[in] double*` second input array.
257+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
258+
- **Out**: `[out] double*` output array.
259+
- **strideOut**: `[in] CBLAS_INT` stride length for `Out`.
260+
261+
```c
262+
void stdlib_strided_dwhere( const CBLAS_INT N, const bool *Condition, const CBLAS_INT strideC, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY, double *Out, const CBLAS_INT strideOut );
263+
```
264+
265+
<!-- lint disable maximum-heading-length -->
266+
267+
#### stdlib_strided_dwhere_ndarray( N, \*Condition, strideC, offsetC, \*X, strideX, offsetX, \*Y, strideY, offsetY, \*Out, strideOut, offsetOut )
268+
269+
<!-- lint enable maximum-heading-length -->
270+
271+
Takes elements from one of two double-precision floating-point strided arrays depending on a condition using alternative indexing semantics.
272+
273+
```c
274+
#include <stdbool.h>
275+
276+
const bool condition[] = { true, false, true };
277+
const double x[] = { 1.0, 2.0, 3.0 };
278+
const double y[] = { 4.0, 5.0, 6.0 };
279+
double out[] = { 0.0, 0.0, 0.0 };
280+
281+
stdlib_strided_dwhere_ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0, out, 1, 0 );
282+
```
283+
284+
The function accepts the following arguments:
285+
286+
- **N**: `[in] CBLAS_INT` number of indexed elements.
287+
- **Condition**: `[in] bool*` condition array.
288+
- **strideC**: `[in] CBLAS_INT` stride length for `Condition`.
289+
- **offsetC**: `[in] CBLAS_INT` starting index for `Condition`.
290+
- **X**: `[in] double*` first input array.
291+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
292+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
293+
- **Y**: `[in] double*` second input array.
294+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
295+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
296+
- **Out**: `[out] double*` output array.
297+
- **strideOut**: `[in] CBLAS_INT` stride length for `Out`.
298+
- **offsetOut**: `[in] CBLAS_INT` starting index for `Out`.
299+
300+
```c
301+
void stdlib_strided_dwhere_ndarray( const CBLAS_INT N, const bool *Condition, const CBLAS_INT strideC, const CBLAS_INT offsetC, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY, double *Out, const CBLAS_INT strideOut, const CBLAS_INT offsetOut );
302+
```
303+
304+
</section>
305+
306+
<!-- /.usage -->
307+
308+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
309+
310+
<section class="notes">
311+
312+
</section>
313+
314+
<!-- /.notes -->
315+
316+
<!-- C API usage examples. -->
317+
318+
<section class="examples">
319+
320+
### Examples
321+
322+
```c
323+
#include "stdlib/blas/ext/base/dwhere.h"
324+
#include <stdio.h>
325+
#include <stdbool.h>
326+
327+
int main( void ) {
328+
// Create strided arrays:
329+
const bool condition[] = { true, false, true, false, true };
330+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
331+
const double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };
332+
double out[] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
333+
334+
// Specify the number of indexed elements:
335+
const int N = 5;
336+
337+
// Specify stride lengths:
338+
const int strideC = 1;
339+
const int strideX = 1;
340+
const int strideY = 1;
341+
const int strideOut = 1;
342+
343+
// Select from `x` or `y` based on the condition array:
344+
stdlib_strided_dwhere( N, condition, strideC, x, strideX, y, strideY, out, strideOut );
345+
346+
// Print the result:
347+
for ( int i = 0; i < N; i++ ) {
348+
printf( "out[ %i ] = %lf\n", i, out[ i ] );
349+
}
350+
}
351+
```
352+
353+
</section>
354+
355+
<!-- /.examples -->
356+
357+
</section>
358+
359+
<!-- /.c -->
360+
361+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
362+
363+
<section class="related">
364+
365+
</section>
366+
367+
<!-- /.related -->
368+
369+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
370+
371+
<section class="links">
372+
373+
[@stdlib/array/bool]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/bool
374+
375+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
376+
377+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
378+
379+
</section>
380+
381+
<!-- /.links -->

0 commit comments

Comments
 (0)