|
| 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 | +# gwhere |
| 22 | + |
| 23 | +> Take elements from one of two 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 gwhere = require( '@stdlib/blas/ext/base/gwhere' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### gwhere( N, condition, strideC, x, strideX, y, strideY, out, strideOut ) |
| 40 | + |
| 41 | +Takes elements from one of two strided arrays depending on a condition. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var condition = [ 1, 0, 1 ]; |
| 45 | +var x = [ 1.0, 2.0, 3.0 ]; |
| 46 | +var y = [ 4.0, 5.0, 6.0 ]; |
| 47 | +var out = [ 0.0, 0.0, 0.0 ]; |
| 48 | + |
| 49 | +gwhere( 3, condition, 1, x, 1, y, 1, out, 1 ); |
| 50 | +// out => [ 1.0, 5.0, 3.0 ] |
| 51 | +``` |
| 52 | + |
| 53 | +The function has the following parameters: |
| 54 | + |
| 55 | +- **N**: number of indexed elements. |
| 56 | +- **condition**: condition [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. |
| 57 | +- **strideC**: stride length for `condition`. |
| 58 | +- **x**: first input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. |
| 59 | +- **strideX**: stride length for `x`. |
| 60 | +- **y**: second input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. |
| 61 | +- **strideY**: stride length for `y`. |
| 62 | +- **out**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. |
| 63 | +- **strideOut**: stride length for `out`. |
| 64 | + |
| 65 | +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to select from every other element: |
| 66 | + |
| 67 | +```javascript |
| 68 | +var condition = [ 1, 0, 0, 1, 1, 0 ]; |
| 69 | +var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; |
| 70 | +var y = [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; |
| 71 | +var out = [ 0.0, 0.0, 0.0 ]; |
| 72 | + |
| 73 | +gwhere( 3, condition, 2, x, 2, y, 2, out, 1 ); |
| 74 | +// out => [ 1.0, 9.0, 5.0 ] |
| 75 | +``` |
| 76 | + |
| 77 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 78 | + |
| 79 | +<!-- eslint-disable stdlib/capitalized-comments, max-len --> |
| 80 | + |
| 81 | +```javascript |
| 82 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 83 | + |
| 84 | +// Initial arrays... |
| 85 | +var condition0 = new Float64Array( [ 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ] ); |
| 86 | +var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 87 | +var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 88 | +var out0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 89 | + |
| 90 | +// Create offset views... |
| 91 | +var condition1 = new Float64Array( condition0.buffer, condition0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 92 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 93 | +var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 94 | +var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 95 | + |
| 96 | +gwhere( 3, condition1, 2, x1, 2, y1, 2, out1, 1 ); |
| 97 | +// out0 => <Float64Array>[ 0.0, 2.0, 4.0, 6.0, 0.0, 0.0 ] |
| 98 | +``` |
| 99 | + |
| 100 | +<!-- lint disable maximum-heading-length --> |
| 101 | + |
| 102 | +#### gwhere.ndarray( N, condition, strideC, offsetC, x, strideX, offsetX, y, strideY, offsetY, out, strideOut, offsetOut ) |
| 103 | + |
| 104 | +<!-- lint enable maximum-heading-length --> |
| 105 | + |
| 106 | +Takes elements from one of two strided arrays depending on a condition using alternative indexing semantics. |
| 107 | + |
| 108 | +```javascript |
| 109 | +var condition = [ 1, 0, 1 ]; |
| 110 | +var x = [ 1.0, 2.0, 3.0 ]; |
| 111 | +var y = [ 4.0, 5.0, 6.0 ]; |
| 112 | +var out = [ 0.0, 0.0, 0.0 ]; |
| 113 | + |
| 114 | +gwhere.ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0, out, 1, 0 ); |
| 115 | +// out => [ 1.0, 5.0, 3.0 ] |
| 116 | +``` |
| 117 | + |
| 118 | +The function has the following additional parameters: |
| 119 | + |
| 120 | +- **offsetC**: starting index for `condition`. |
| 121 | +- **offsetX**: starting index for `x`. |
| 122 | +- **offsetY**: starting index for `y`. |
| 123 | +- **offsetOut**: starting index for `out`. |
| 124 | + |
| 125 | +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: |
| 126 | + |
| 127 | +```javascript |
| 128 | +var condition = [ 0, 1, 0, 0, 0, 1 ]; |
| 129 | +var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; |
| 130 | +var y = [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; |
| 131 | +var out = [ 0.0, 0.0, 0.0 ]; |
| 132 | + |
| 133 | +gwhere.ndarray( 3, condition, 2, 1, x, 2, 1, y, 2, 1, out, 1, 0 ); |
| 134 | +// out => [ 2.0, 10.0, 6.0 ] |
| 135 | +``` |
| 136 | + |
| 137 | +</section> |
| 138 | + |
| 139 | +<!-- /.usage --> |
| 140 | + |
| 141 | +<section class="notes"> |
| 142 | + |
| 143 | +## Notes |
| 144 | + |
| 145 | +- If `N <= 0`, both functions return `out` unchanged. |
| 146 | +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). |
| 147 | +- Depending on the environment, the typed versions ([`dwhere`][@stdlib/blas/ext/base/dwhere], [`swhere`][@stdlib/blas/ext/base/swhere], etc.) are likely to be significantly more performant. |
| 148 | + |
| 149 | +</section> |
| 150 | + |
| 151 | +<!-- /.notes --> |
| 152 | + |
| 153 | +<section class="examples"> |
| 154 | + |
| 155 | +## Examples |
| 156 | + |
| 157 | +<!-- eslint no-undef: "error" --> |
| 158 | + |
| 159 | +```javascript |
| 160 | +var bernoulli = require( '@stdlib/random/array/bernoulli' ); |
| 161 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 162 | +var zeros = require( '@stdlib/array/base/zeros' ); |
| 163 | +var gwhere = require( '@stdlib/blas/ext/base/gwhere' ); |
| 164 | + |
| 165 | +var condition = bernoulli( 20, 0.5, { |
| 166 | + 'dtype': 'generic' |
| 167 | +}); |
| 168 | +console.log( condition ); |
| 169 | +var x = discreteUniform( 20, 0, 100, { |
| 170 | + 'dtype': 'generic' |
| 171 | +}); |
| 172 | +console.log( x ); |
| 173 | +var y = discreteUniform( 20, -100, 0, { |
| 174 | + 'dtype': 'generic' |
| 175 | +}); |
| 176 | +console.log( y ); |
| 177 | + |
| 178 | +var out = zeros( condition.length ); |
| 179 | +console.log( out ); |
| 180 | + |
| 181 | +gwhere( condition.length, condition, 1, x, 1, y, 1, out, 1 ); |
| 182 | +console.log( out ); |
| 183 | +``` |
| 184 | + |
| 185 | +</section> |
| 186 | + |
| 187 | +<!-- /.examples --> |
| 188 | + |
| 189 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 190 | + |
| 191 | +<section class="related"> |
| 192 | + |
| 193 | +</section> |
| 194 | + |
| 195 | +<!-- /.related --> |
| 196 | + |
| 197 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 198 | + |
| 199 | +<section class="links"> |
| 200 | + |
| 201 | +[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array |
| 202 | + |
| 203 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 204 | + |
| 205 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/array-base-accessor |
| 206 | + |
| 207 | +[@stdlib/blas/ext/base/dwhere]: https://github.com/stdlib-js/blas/tree/main/ext/base/dwhere |
| 208 | + |
| 209 | +[@stdlib/blas/ext/base/swhere]: https://github.com/stdlib-js/blas/tree/main/ext/base/swhere |
| 210 | + |
| 211 | +</section> |
| 212 | + |
| 213 | +<!-- /.links --> |
0 commit comments