-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add blas/ext/base/swhere
#11582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
daaea03
feat: add blas/ext/base/swhere
headlessNode c0cae82
Merge branch 'develop' into blas/dwhere
headlessNode d8e84d4
refactor: use booleanarray
headlessNode c206414
Merge branch 'develop' into blas/dwhere
headlessNode 6f51c47
Apply suggestions from code review
kgryte 7822b01
Apply suggestions from code review
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
382 changes: 382 additions & 0 deletions
382
lib/node_modules/@stdlib/blas/ext/base/swhere/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,382 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
| - The `condition` argument must be a [`BooleanArray`][@stdlib/array/bool]. | ||
|
|
||
| </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://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/bool | ||
|
|
||
| [@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32 | ||
|
|
||
| [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.