Skip to content

Commit 1746615

Browse files
committed
feat: add blas/ext/base/cwhere
1 parent ba7a157 commit 1746615

33 files changed

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

0 commit comments

Comments
 (0)