|
| 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 | +# put |
| 22 | + |
| 23 | +> Replace specified elements of an array with provided values. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var put = require( '@stdlib/array/put' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### put( x, indices, values\[, options] ) |
| 34 | + |
| 35 | +Replaces specified elements of an array with provided values. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var x = [ 1, 2, 3, 4 ]; |
| 39 | + |
| 40 | +var out = put( x, [ 1, 3 ], [ 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 | +- **indices**: list of indices. |
| 51 | +- **values**: values to set. When `indices` contains one or more elements, `values` must be broadcast [compatible][@stdlib/ndarray/base/broadcast-shapes] with `indices` (i.e., must have either one element or the same number of elements as `indices`). |
| 52 | +- **options**: function options. |
| 53 | + |
| 54 | +The function supports the following options: |
| 55 | + |
| 56 | +- **mode**: index [mode][@stdlib/ndarray/base/ind]. Default: `'normalize'`. |
| 57 | + |
| 58 | +If `indices` is an empty array, the function returns the input array unchanged. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var x = [ 1, 2, 3, 4 ]; |
| 62 | + |
| 63 | +var out = put( x, [], [ 20, 40 ] ); |
| 64 | +// returns [ 1, 2, 3, 4 ] |
| 65 | +``` |
| 66 | + |
| 67 | +The function supports broadcasting a `values` array containing a single element against an `indices` array containing one or more elements. |
| 68 | + |
| 69 | +```javascript |
| 70 | +var x = [ 1, 2, 3, 4 ]; |
| 71 | + |
| 72 | +var out = put( x, [ 1, 3 ], [ 20 ] ); |
| 73 | +// returns [ 1, 20, 3, 20 ] |
| 74 | +``` |
| 75 | + |
| 76 | +By default, the function normalizes negative integer indices to positive integer index equivalents. |
| 77 | + |
| 78 | +```javascript |
| 79 | +var x = [ 1, 2, 3, 4 ]; |
| 80 | + |
| 81 | +var out = put( x, [ -3, -1 ], [ 20, 40 ] ); |
| 82 | +// returns [ 1, 20, 3, 40 ] |
| 83 | +``` |
| 84 | + |
| 85 | +To specify an alternative index [mode][@stdlib/ndarray/base/ind], provide a `mode` option. |
| 86 | + |
| 87 | +```javascript |
| 88 | +var x = [ 1, 2, 3, 4 ]; |
| 89 | + |
| 90 | +var out = put( x, [ -10, 10 ], [ 20, 40 ], { |
| 91 | + 'mode': 'clamp' |
| 92 | +}); |
| 93 | +// returns [ 20, 2, 3, 40 ] |
| 94 | +``` |
| 95 | + |
| 96 | +</section> |
| 97 | + |
| 98 | +<!-- /.usage --> |
| 99 | + |
| 100 | +<section class="notes"> |
| 101 | + |
| 102 | +## Notes |
| 103 | + |
| 104 | +- The function mutates the input array `x`. |
| 105 | +- 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). |
| 106 | + |
| 107 | +</section> |
| 108 | + |
| 109 | +<!-- /.notes --> |
| 110 | + |
| 111 | +<section class="examples"> |
| 112 | + |
| 113 | +## Examples |
| 114 | + |
| 115 | +<!-- eslint no-undef: "error" --> |
| 116 | + |
| 117 | +```javascript |
| 118 | +var filledBy = require( '@stdlib/array/base/filled-by' ); |
| 119 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 120 | +var linspace = require( '@stdlib/array/base/linspace' ); |
| 121 | +var put = require( '@stdlib/array/put' ); |
| 122 | + |
| 123 | +// Generate a linearly spaced array: |
| 124 | +var x = linspace( 0, 100, 11 ); |
| 125 | +console.log( x ); |
| 126 | + |
| 127 | +// Generate an array of random indices: |
| 128 | +var N = discreteUniform( 5, 15 ); |
| 129 | +var indices = filledBy( N, discreteUniform.factory( 0, x.length-1 ) ); |
| 130 | +console.log( indices ); |
| 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 = put( x, indices, 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/ndarray/base/ind]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ind |
| 158 | + |
| 159 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 160 | + |
| 161 | +[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes |
| 162 | + |
| 163 | +[@stdlib/array/safe-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/safe-casts |
| 164 | + |
| 165 | +[@stdlib/array/same-kind-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/same-kind-casts |
| 166 | + |
| 167 | +</section> |
| 168 | + |
| 169 | +<!-- /.links --> |
0 commit comments