Skip to content

Commit 9951f2e

Browse files
committed
feat: add blas/ext/base/dwhere
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent bb1eae9 commit 9951f2e

33 files changed

Lines changed: 5118 additions & 0 deletions
Lines changed: 382 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,382 @@
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+
- The `condition` argument must be a [`BooleanArray`][@stdlib/array/bool].
160+
161+
</section>
162+
163+
<!-- /.notes -->
164+
165+
<section class="examples">
166+
167+
## Examples
168+
169+
<!-- eslint no-undef: "error" -->
170+
171+
```javascript
172+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
173+
var uniform = require( '@stdlib/random/array/uniform' );
174+
var BooleanArray = require( '@stdlib/array/bool' );
175+
var Float64Array = require( '@stdlib/array/float64' );
176+
var dwhere = require( '@stdlib/blas/ext/base/dwhere' );
177+
178+
var cbuf = bernoulli( 20, 0.5, {
179+
'dtype': 'uint8'
180+
});
181+
var condition = new BooleanArray( cbuf.buffer );
182+
console.log( condition );
183+
184+
var x = uniform( 20, 0.0, 100.0, {
185+
'dtype': 'float64'
186+
});
187+
console.log( x );
188+
189+
var y = uniform( 20, -100.0, 0.0, {
190+
'dtype': 'float64'
191+
});
192+
console.log( y );
193+
194+
var out = new Float64Array( condition.length );
195+
console.log( out );
196+
197+
dwhere( condition.length, condition, 1, x, 1, y, 1, out, 1 );
198+
console.log( out );
199+
```
200+
201+
</section>
202+
203+
<!-- /.examples -->
204+
205+
<!-- C interface documentation. -->
206+
207+
* * *
208+
209+
<section class="c">
210+
211+
## C APIs
212+
213+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
214+
215+
<section class="intro">
216+
217+
</section>
218+
219+
<!-- /.intro -->
220+
221+
<!-- C usage documentation. -->
222+
223+
<section class="usage">
224+
225+
### Usage
226+
227+
```c
228+
#include "stdlib/blas/ext/base/dwhere.h"
229+
```
230+
231+
<!-- lint disable maximum-heading-length -->
232+
233+
#### stdlib_strided_dwhere( N, \*Condition, strideC, \*X, strideX, \*Y, strideY, \*Out, strideOut )
234+
235+
<!-- lint enable maximum-heading-length -->
236+
237+
Takes elements from one of two double-precision floating-point strided arrays depending on a condition.
238+
239+
```c
240+
#include <stdbool.h>
241+
242+
const bool condition[] = { true, false, true };
243+
const double x[] = { 1.0, 2.0, 3.0 };
244+
const double y[] = { 4.0, 5.0, 6.0 };
245+
double out[] = { 0.0, 0.0, 0.0 };
246+
247+
stdlib_strided_dwhere( 3, condition, 1, x, 1, y, 1, out, 1 );
248+
```
249+
250+
The function accepts the following arguments:
251+
252+
- **N**: `[in] CBLAS_INT` number of indexed elements.
253+
- **Condition**: `[in] bool*` condition array.
254+
- **strideC**: `[in] CBLAS_INT` stride length for `Condition`.
255+
- **X**: `[in] double*` first input array.
256+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
257+
- **Y**: `[in] double*` second input array.
258+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
259+
- **Out**: `[out] double*` output array.
260+
- **strideOut**: `[in] CBLAS_INT` stride length for `Out`.
261+
262+
```c
263+
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 );
264+
```
265+
266+
<!-- lint disable maximum-heading-length -->
267+
268+
#### stdlib_strided_dwhere_ndarray( N, \*Condition, strideC, offsetC, \*X, strideX, offsetX, \*Y, strideY, offsetY, \*Out, strideOut, offsetOut )
269+
270+
<!-- lint enable maximum-heading-length -->
271+
272+
Takes elements from one of two double-precision floating-point strided arrays depending on a condition using alternative indexing semantics.
273+
274+
```c
275+
#include <stdbool.h>
276+
277+
const bool condition[] = { true, false, true };
278+
const double x[] = { 1.0, 2.0, 3.0 };
279+
const double y[] = { 4.0, 5.0, 6.0 };
280+
double out[] = { 0.0, 0.0, 0.0 };
281+
282+
stdlib_strided_dwhere_ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0, out, 1, 0 );
283+
```
284+
285+
The function accepts the following arguments:
286+
287+
- **N**: `[in] CBLAS_INT` number of indexed elements.
288+
- **Condition**: `[in] bool*` condition array.
289+
- **strideC**: `[in] CBLAS_INT` stride length for `Condition`.
290+
- **offsetC**: `[in] CBLAS_INT` starting index for `Condition`.
291+
- **X**: `[in] double*` first input array.
292+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
293+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
294+
- **Y**: `[in] double*` second input array.
295+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
296+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
297+
- **Out**: `[out] double*` output array.
298+
- **strideOut**: `[in] CBLAS_INT` stride length for `Out`.
299+
- **offsetOut**: `[in] CBLAS_INT` starting index for `Out`.
300+
301+
```c
302+
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 );
303+
```
304+
305+
</section>
306+
307+
<!-- /.usage -->
308+
309+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
310+
311+
<section class="notes">
312+
313+
</section>
314+
315+
<!-- /.notes -->
316+
317+
<!-- C API usage examples. -->
318+
319+
<section class="examples">
320+
321+
### Examples
322+
323+
```c
324+
#include "stdlib/blas/ext/base/dwhere.h"
325+
#include <stdio.h>
326+
#include <stdbool.h>
327+
328+
int main( void ) {
329+
// Create strided arrays:
330+
const bool condition[] = { true, false, true, false, true };
331+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
332+
const double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };
333+
double out[] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
334+
335+
// Specify the number of indexed elements:
336+
const int N = 5;
337+
338+
// Specify stride lengths:
339+
const int strideC = 1;
340+
const int strideX = 1;
341+
const int strideY = 1;
342+
const int strideOut = 1;
343+
344+
// Select from `x` or `y` based on the condition array:
345+
stdlib_strided_dwhere( N, condition, strideC, x, strideX, y, strideY, out, strideOut );
346+
347+
// Print the result:
348+
for ( int i = 0; i < N; i++ ) {
349+
printf( "out[ %i ] = %lf\n", i, out[ i ] );
350+
}
351+
}
352+
```
353+
354+
</section>
355+
356+
<!-- /.examples -->
357+
358+
</section>
359+
360+
<!-- /.c -->
361+
362+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
363+
364+
<section class="related">
365+
366+
</section>
367+
368+
<!-- /.related -->
369+
370+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
371+
372+
<section class="links">
373+
374+
[@stdlib/array/bool]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/bool
375+
376+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
377+
378+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
379+
380+
</section>
381+
382+
<!-- /.links -->

0 commit comments

Comments
 (0)