|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 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 | +# place |
| 22 | + |
| 23 | +> Replace elements of an array with provided values according to a provided mask array. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var place = require( '@stdlib/array/place' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### place( x, mask, values\[, options] ) |
| 34 | + |
| 35 | +Replaces elements of an array with provided values according to a provided mask array. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var x = [ 1, 2, 3, 4 ]; |
| 39 | + |
| 40 | +var out = place( x, [ 0, 1, 0, 1 ], [ 20, 40 ] ); |
| 41 | +// returns [ 1, 20, 3, 40 ] |
| 42 | + |
| 43 | +var bool = ( out === x ); |
| 44 | +// returns true |
| 45 | +``` |
| 46 | + |
| 47 | +The function supports the following parameters: |
| 48 | + |
| 49 | +- **x**: input array. |
| 50 | +- **mask**: mask array. |
| 51 | +- **values**: values to set. |
| 52 | +- **options**: function options. |
| 53 | + |
| 54 | +The function supports the following options: |
| 55 | + |
| 56 | +- **mode**: string specifying behavior when the number of `values` does not equal the number of truthy `mask` values. Default: `'repeat'`. |
| 57 | + |
| 58 | +The function supports the following modes: |
| 59 | + |
| 60 | +- `'strict'`: specifies that the function must raise an exception when the number of `values` does not **exactly** equal the number of truthy `mask` values. |
| 61 | +- `'non_strict'`: specifies that the function must raise an exception when the function is provided insufficient `values` to satisfy the `mask` array. |
| 62 | +- `'strict_broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the number of `values` does not **exactly** equal the number of truthy `mask` values. |
| 63 | +- `'broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the function is provided insufficient `values` to satisfy the `mask` array. |
| 64 | +- `'repeat'`: specifies that the function must reuse provided `values` when replacing elements in `x` in order to satisfy the `mask` array. |
| 65 | + |
| 66 | +In broadcasting modes, the function supports broadcasting a `values` array containing a single element against the number of truthy values in the `mask` array. |
| 67 | + |
| 68 | +```javascript |
| 69 | +var x = [ 1, 2, 3, 4 ]; |
| 70 | + |
| 71 | +var out = place( x, [ 0, 1, 0, 1 ], [ 20 ], { |
| 72 | + 'mode': 'strict_broadcast' |
| 73 | +}); |
| 74 | +// returns [ 1, 20, 3, 20 ] |
| 75 | + |
| 76 | +var bool = ( out === x ); |
| 77 | +// returns true |
| 78 | +``` |
| 79 | + |
| 80 | +In repeat mode, the function supports recycling elements in a `values` array to satisfy the number of truthy values in the `mask` array. |
| 81 | + |
| 82 | +```javascript |
| 83 | +var x = [ 1, 2, 3, 4 ]; |
| 84 | + |
| 85 | +var out = place( x, [ 1, 1, 0, 1 ], [ 20, 40 ], { |
| 86 | + 'mode': 'repeat' |
| 87 | +}); |
| 88 | +// returns [ 20, 40, 3, 20 ] |
| 89 | + |
| 90 | +var bool = ( out === x ); |
| 91 | +// returns true |
| 92 | +``` |
| 93 | + |
| 94 | +</section> |
| 95 | + |
| 96 | +<!-- /.usage --> |
| 97 | + |
| 98 | +<section class="notes"> |
| 99 | + |
| 100 | +## Notes |
| 101 | + |
| 102 | +- The function mutates the input array `x`. |
| 103 | +- If a `mask` array element is truthy, the corresponding element in `x` is **replaced**; otherwise, the corresponding element in `x` is "masked" and thus left unchanged. |
| 104 | +- The `values` array must have a [data type][@stdlib/array/dtypes] which can be [safely cast][@stdlib/array/safe-casts] to the input array data type. Floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the [same kind][@stdlib/array/same-kind-casts] (e.g., element values from a `'float64'` values array can be assigned to corresponding elements in a `'float32'` input array). |
| 105 | + |
| 106 | +</section> |
| 107 | + |
| 108 | +<!-- /.notes --> |
| 109 | + |
| 110 | +<section class="examples"> |
| 111 | + |
| 112 | +## Examples |
| 113 | + |
| 114 | +<!-- eslint no-undef: "error" --> |
| 115 | + |
| 116 | +```javascript |
| 117 | +var filledBy = require( '@stdlib/array/base/filled-by' ); |
| 118 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 119 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 120 | +var linspace = require( '@stdlib/array/base/linspace' ); |
| 121 | +var place = require( '@stdlib/array/place' ); |
| 122 | + |
| 123 | +// Generate a linearly spaced array: |
| 124 | +var x = linspace( 0, 100, 11 ); |
| 125 | +console.log( x ); |
| 126 | + |
| 127 | +// Generate a random mask array: |
| 128 | +var N = discreteUniform( 5, 15 ); |
| 129 | +var mask = filledBy( N, bernoulli.factory( 0.3 ) ); |
| 130 | +console.log( mask ); |
| 131 | + |
| 132 | +// Generate an array of random values: |
| 133 | +var values = filledBy( N, discreteUniform.factory( 1000, 2000 ) ); |
| 134 | +console.log( values ); |
| 135 | + |
| 136 | +// Update a random sample of elements in `x`: |
| 137 | +var out = place( x, mask, values ); |
| 138 | +console.log( out ); |
| 139 | +``` |
| 140 | + |
| 141 | +</section> |
| 142 | + |
| 143 | +<!-- /.examples --> |
| 144 | + |
| 145 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 146 | + |
| 147 | +<section class="related"> |
| 148 | + |
| 149 | +</section> |
| 150 | + |
| 151 | +<!-- /.related --> |
| 152 | + |
| 153 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 154 | + |
| 155 | +<section class="links"> |
| 156 | + |
| 157 | +[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes |
| 158 | + |
| 159 | +[@stdlib/array/safe-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/safe-casts |
| 160 | + |
| 161 | +[@stdlib/array/same-kind-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/same-kind-casts |
| 162 | + |
| 163 | +</section> |
| 164 | + |
| 165 | +<!-- /.links --> |
0 commit comments