<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# swhere

> Take elements from one of two single-precision floating-point strided arrays depending on a condition.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var swhere = require( '@stdlib/blas/ext/base/swhere' );
```

#### swhere( N, condition, strideC, x, strideX, y, strideY, out, strideOut )

Takes elements from one of two single-precision floating-point strided arrays depending on a condition.

```javascript
var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );

var condition = new BooleanArray( [ true, false, true ] );
var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
var y = new Float32Array( [ 4.0, 5.0, 6.0 ] );
var out = new Float32Array( [ 0.0, 0.0, 0.0 ] );

swhere( 3, condition, 1, x, 1, y, 1, out, 1 );
// out => <Float32Array>[ 1.0, 5.0, 3.0 ]
```

The function has the following parameters:

-   **N**: number of indexed elements.
-   **condition**: condition [`BooleanArray`][@stdlib/array/bool].
-   **strideC**: stride length for `condition`.
-   **x**: first input [`Float32Array`][@stdlib/array/float32].
-   **strideX**: stride length for `x`.
-   **y**: second input [`Float32Array`][@stdlib/array/float32].
-   **strideY**: stride length for `y`.
-   **out**: output [`Float32Array`][@stdlib/array/float32].
-   **strideOut**: stride length for `out`.

The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to select from every other element:

```javascript
var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );

var condition = new BooleanArray( [ true, false, false, true, true, false ] );
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
var out = new Float32Array( [ 0.0, 0.0, 0.0 ] );

swhere( 3, condition, 2, x, 2, y, 2, out, 1 );
// out => <Float32Array>[ 1.0, 9.0, 5.0 ]
```

Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.

<!-- eslint-disable stdlib/capitalized-comments, max-len -->

```javascript
var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );

// Initial arrays...
var condition0 = new BooleanArray( [ false, true, false, true, false, true ] );
var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var y0 = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
var out0 = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );

// Create offset views...
var condition1 = new BooleanArray( condition0.buffer, condition0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var out1 = new Float32Array( out0.buffer, out0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

swhere( 3, condition1, 2, x1, 2, y1, 2, out1, 1 );
// out0 => <Float32Array>[ 0.0, 2.0, 4.0, 6.0, 0.0, 0.0 ]
```

<!-- lint disable maximum-heading-length -->

#### swhere.ndarray( N, condition, strideC, offsetC, x, strideX, offsetX, y, strideY, offsetY, out, strideOut, offsetOut )

<!-- lint enable maximum-heading-length -->

Takes elements from one of two single-precision floating-point strided arrays depending on a condition using alternative indexing semantics.

```javascript
var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );

var condition = new BooleanArray( [ true, false, true ] );
var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
var y = new Float32Array( [ 4.0, 5.0, 6.0 ] );
var out = new Float32Array( [ 0.0, 0.0, 0.0 ] );

swhere.ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0, out, 1, 0 );
// out => <Float32Array>[ 1.0, 5.0, 3.0 ]
```

The function has the following additional parameters:

-   **offsetC**: starting index for `condition`.
-   **offsetX**: starting index for `x`.
-   **offsetY**: starting index for `y`.
-   **offsetOut**: starting index for `out`.

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:

```javascript
var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );

var condition = new BooleanArray( [ false, true, false, false, false, true ] );
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
var out = new Float32Array( [ 0.0, 0.0, 0.0 ] );

swhere.ndarray( 3, condition, 2, 1, x, 2, 1, y, 2, 1, out, 1, 0 );
// out => <Float32Array>[ 2.0, 10.0, 6.0 ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

-   If `N <= 0`, both functions return `out` unchanged.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var bernoulli = require( '@stdlib/random/array/bernoulli' );
var uniform = require( '@stdlib/random/array/uniform' );
var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );
var swhere = require( '@stdlib/blas/ext/base/swhere' );

var cbuf = bernoulli( 20, 0.5, {
    'dtype': 'uint8'
});
var condition = new BooleanArray( cbuf.buffer );
console.log( condition );

var x = uniform( 20, 0.0, 100.0, {
    'dtype': 'float32'
});
console.log( x );

var y = uniform( 20, -100.0, 0.0, {
    'dtype': 'float32'
});
console.log( y );

var out = new Float32Array( condition.length );
console.log( out );

swhere( condition.length, condition, 1, x, 1, y, 1, out, 1 );
console.log( out );
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/blas/ext/base/swhere.h"
```

<!-- lint disable maximum-heading-length -->

#### stdlib_strided_swhere( N, \*Condition, strideC, \*X, strideX, \*Y, strideY, \*Out, strideOut )

<!-- lint enable maximum-heading-length -->

Takes elements from one of two single-precision floating-point strided arrays depending on a condition.

```c
#include <stdbool.h>

const bool condition[] = { true, false, true };
const float x[] = { 1.0f, 2.0f, 3.0f };
const float y[] = { 4.0f, 5.0f, 6.0f };
float out[] = { 0.0f, 0.0f, 0.0f };

stdlib_strided_swhere( 3, condition, 1, x, 1, y, 1, out, 1 );
```

The function accepts the following arguments:

-   **N**: `[in] CBLAS_INT` number of indexed elements.
-   **Condition**: `[in] bool*` condition array.
-   **strideC**: `[in] CBLAS_INT` stride length for `Condition`.
-   **X**: `[in] float*` first input array.
-   **strideX**: `[in] CBLAS_INT` stride length for `X`.
-   **Y**: `[in] float*` second input array.
-   **strideY**: `[in] CBLAS_INT` stride length for `Y`.
-   **Out**: `[out] float*` output array.
-   **strideOut**: `[in] CBLAS_INT` stride length for `Out`.

```c
void stdlib_strided_swhere( const CBLAS_INT N, const bool *Condition, const CBLAS_INT strideC, const float *X, const CBLAS_INT strideX, const float *Y, const CBLAS_INT strideY, float *Out, const CBLAS_INT strideOut );
```

<!-- lint disable maximum-heading-length -->

#### stdlib_strided_swhere_ndarray( N, \*Condition, strideC, offsetC, \*X, strideX, offsetX, \*Y, strideY, offsetY, \*Out, strideOut, offsetOut )

<!-- lint enable maximum-heading-length -->

Takes elements from one of two single-precision floating-point strided arrays depending on a condition using alternative indexing semantics.

```c
#include <stdbool.h>

const bool condition[] = { true, false, true };
const float x[] = { 1.0f, 2.0f, 3.0f };
const float y[] = { 4.0f, 5.0f, 6.0f };
float out[] = { 0.0f, 0.0f, 0.0f };

stdlib_strided_swhere_ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0, out, 1, 0 );
```

The function accepts the following arguments:

-   **N**: `[in] CBLAS_INT` number of indexed elements.
-   **Condition**: `[in] bool*` condition array.
-   **strideC**: `[in] CBLAS_INT` stride length for `Condition`.
-   **offsetC**: `[in] CBLAS_INT` starting index for `Condition`.
-   **X**: `[in] float*` first input array.
-   **strideX**: `[in] CBLAS_INT` stride length for `X`.
-   **offsetX**: `[in] CBLAS_INT` starting index for `X`.
-   **Y**: `[in] float*` second input array.
-   **strideY**: `[in] CBLAS_INT` stride length for `Y`.
-   **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
-   **Out**: `[out] float*` output array.
-   **strideOut**: `[in] CBLAS_INT` stride length for `Out`.
-   **offsetOut**: `[in] CBLAS_INT` starting index for `Out`.

```c
void stdlib_strided_swhere_ndarray( const CBLAS_INT N, const bool *Condition, const CBLAS_INT strideC, const CBLAS_INT offsetC, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY, float *Out, const CBLAS_INT strideOut, const CBLAS_INT offsetOut );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/blas/ext/base/swhere.h"
#include <stdio.h>
#include <stdbool.h>

int main( void ) {
    // Create strided arrays:
    const bool condition[] = { true, false, true, false, true };
    const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
    const float y[] = { 6.0f, 7.0f, 8.0f, 9.0f, 10.0f };
    float out[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };

    // Specify the number of indexed elements:
    const int N = 5;

    // Specify stride lengths:
    const int strideC = 1;
    const int strideX = 1;
    const int strideY = 1;
    const int strideOut = 1;

    // Select from `x` or `y` based on the condition array:
    stdlib_strided_swhere( N, condition, strideC, x, strideX, y, strideY, out, strideOut );

    // Print the result:
    for ( int i = 0; i < N; i++ ) {
        printf( "out[ %i ] = %f\n", i, out[ i ] );
    }
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/array/bool]: https://www.npmjs.com/package/@stdlib/array-bool

[@stdlib/array/float32]: https://www.npmjs.com/package/@stdlib/array-float32

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

</section>

<!-- /.links -->
