|
| 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 | +# swhere |
| 22 | + |
| 23 | +> Take elements from one of two single-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 swhere = require( '@stdlib/blas/ext/base/swhere' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### swhere( N, condition, strideC, x, strideX, y, strideY, out, strideOut ) |
| 40 | + |
| 41 | +Takes elements from one of two single-precision floating-point strided arrays depending on a condition. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var BooleanArray = require( '@stdlib/array/bool' ); |
| 45 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 46 | + |
| 47 | +var condition = new BooleanArray( [ true, false, true ] ); |
| 48 | +var x = new Float32Array( [ 1.0, 2.0, 3.0 ] ); |
| 49 | +var y = new Float32Array( [ 4.0, 5.0, 6.0 ] ); |
| 50 | +var out = new Float32Array( [ 0.0, 0.0, 0.0 ] ); |
| 51 | + |
| 52 | +swhere( 3, condition, 1, x, 1, y, 1, out, 1 ); |
| 53 | +// out => <Float32Array>[ 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 [`Float32Array`][@stdlib/array/float32]. |
| 62 | +- **strideX**: stride length for `x`. |
| 63 | +- **y**: second input [`Float32Array`][@stdlib/array/float32]. |
| 64 | +- **strideY**: stride length for `y`. |
| 65 | +- **out**: output [`Float32Array`][@stdlib/array/float32]. |
| 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 Float32Array = require( '@stdlib/array/float32' ); |
| 73 | + |
| 74 | +var condition = new BooleanArray( [ true, false, false, true, true, false ] ); |
| 75 | +var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 76 | +var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 77 | +var out = new Float32Array( [ 0.0, 0.0, 0.0 ] ); |
| 78 | + |
| 79 | +swhere( 3, condition, 2, x, 2, y, 2, out, 1 ); |
| 80 | +// out => <Float32Array>[ 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 Float32Array = require( '@stdlib/array/float32' ); |
| 90 | + |
| 91 | +// Initial arrays... |
| 92 | +var condition0 = new BooleanArray( [ false, true, false, true, false, true ] ); |
| 93 | +var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 94 | +var y0 = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 95 | +var out0 = new Float32Array( [ 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 Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 100 | +var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 101 | +var out1 = new Float32Array( out0.buffer, out0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 102 | + |
| 103 | +swhere( 3, condition1, 2, x1, 2, y1, 2, out1, 1 ); |
| 104 | +// out0 => <Float32Array>[ 0.0, 2.0, 4.0, 6.0, 0.0, 0.0 ] |
| 105 | +``` |
| 106 | + |
| 107 | +<!-- lint disable maximum-heading-length --> |
| 108 | + |
| 109 | +#### swhere.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 single-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 Float32Array = require( '@stdlib/array/float32' ); |
| 118 | + |
| 119 | +var condition = new BooleanArray( [ true, false, true ] ); |
| 120 | +var x = new Float32Array( [ 1.0, 2.0, 3.0 ] ); |
| 121 | +var y = new Float32Array( [ 4.0, 5.0, 6.0 ] ); |
| 122 | +var out = new Float32Array( [ 0.0, 0.0, 0.0 ] ); |
| 123 | + |
| 124 | +swhere.ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0, out, 1, 0 ); |
| 125 | +// out => <Float32Array>[ 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 Float32Array = require( '@stdlib/array/float32' ); |
| 140 | + |
| 141 | +var condition = new BooleanArray( [ false, true, false, false, false, true ] ); |
| 142 | +var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 143 | +var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 144 | +var out = new Float32Array( [ 0.0, 0.0, 0.0 ] ); |
| 145 | + |
| 146 | +swhere.ndarray( 3, condition, 2, 1, x, 2, 1, y, 2, 1, out, 1, 0 ); |
| 147 | +// out => <Float32Array>[ 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 Float32Array = require( '@stdlib/array/float32' ); |
| 176 | +var swhere = require( '@stdlib/blas/ext/base/swhere' ); |
| 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': 'float32' |
| 186 | +}); |
| 187 | +console.log( x ); |
| 188 | + |
| 189 | +var y = uniform( 20, -100.0, 0.0, { |
| 190 | + 'dtype': 'float32' |
| 191 | +}); |
| 192 | +console.log( y ); |
| 193 | + |
| 194 | +var out = new Float32Array( condition.length ); |
| 195 | +console.log( out ); |
| 196 | + |
| 197 | +swhere( 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/swhere.h" |
| 229 | +``` |
| 230 | + |
| 231 | +<!-- lint disable maximum-heading-length --> |
| 232 | + |
| 233 | +#### stdlib_strided_swhere( N, \*Condition, strideC, \*X, strideX, \*Y, strideY, \*Out, strideOut ) |
| 234 | + |
| 235 | +<!-- lint enable maximum-heading-length --> |
| 236 | + |
| 237 | +Takes elements from one of two single-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 float x[] = { 1.0f, 2.0f, 3.0f }; |
| 244 | +const float y[] = { 4.0f, 5.0f, 6.0f }; |
| 245 | +float out[] = { 0.0f, 0.0f, 0.0f }; |
| 246 | + |
| 247 | +stdlib_strided_swhere( 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] float*` first input array. |
| 256 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 257 | +- **Y**: `[in] float*` second input array. |
| 258 | +- **strideY**: `[in] CBLAS_INT` stride length for `Y`. |
| 259 | +- **Out**: `[out] float*` output array. |
| 260 | +- **strideOut**: `[in] CBLAS_INT` stride length for `Out`. |
| 261 | +
|
| 262 | +```c |
| 263 | +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 ); |
| 264 | +``` |
| 265 | + |
| 266 | +<!-- lint disable maximum-heading-length --> |
| 267 | + |
| 268 | +#### stdlib_strided_swhere_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 single-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 float x[] = { 1.0f, 2.0f, 3.0f }; |
| 279 | +const float y[] = { 4.0f, 5.0f, 6.0f }; |
| 280 | +float out[] = { 0.0f, 0.0f, 0.0f }; |
| 281 | + |
| 282 | +stdlib_strided_swhere_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] float*` first input array. |
| 292 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 293 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 294 | +- **Y**: `[in] float*` second input array. |
| 295 | +- **strideY**: `[in] CBLAS_INT` stride length for `Y`. |
| 296 | +- **offsetY**: `[in] CBLAS_INT` starting index for `Y`. |
| 297 | +- **Out**: `[out] float*` 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_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 ); |
| 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/swhere.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 float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }; |
| 332 | + const float y[] = { 6.0f, 7.0f, 8.0f, 9.0f, 10.0f }; |
| 333 | + float out[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; |
| 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_swhere( 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 ] = %f\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/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32 |
| 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