|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# fillSlice |
| 22 | + |
| 23 | +> Fill an input [`ndarray`][@stdlib/ndarray/ctor] view with a specified value. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var fillSlice = require( '@stdlib/ndarray/fill-slice' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### fillSlice( x, value, ...s\[, options] ) |
| 40 | + |
| 41 | +Fills an input [`ndarray`][@stdlib/ndarray/ctor] view with a specified value. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 45 | +var MultiSlice = require( '@stdlib/slice/multi' ); |
| 46 | +var Slice = require( '@stdlib/slice/ctor' ); |
| 47 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 48 | + |
| 49 | +var x = zeros( [ 3, 4 ], { |
| 50 | + 'dtype': 'float64' |
| 51 | +}); |
| 52 | + |
| 53 | +// Define the fill region: |
| 54 | +var s0 = new Slice( 1, 3 ); |
| 55 | +var s1 = new Slice( 2, 4 ); |
| 56 | +var s = new MultiSlice( s0, s1 ); |
| 57 | + |
| 58 | +// Fill the region with a scalar value: |
| 59 | +var y = fillSlice( x, 5.0, s ); |
| 60 | +// returns <ndarray> |
| 61 | + |
| 62 | +var bool = ( y === x ); |
| 63 | +// returns true |
| 64 | + |
| 65 | +var arr = ndarray2array( x ); |
| 66 | +// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 5.0, 5.0 ], [ 0.0, 0.0, 5.0, 5.0 ] ] |
| 67 | +``` |
| 68 | + |
| 69 | +The function accepts the following arguments: |
| 70 | + |
| 71 | +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. |
| 72 | +- **value**: fill value. |
| 73 | +- **s**: a [`MultiSlice`][@stdlib/slice/multi] instance, an array of slice arguments, or slice arguments as separate arguments. |
| 74 | +- **options**: function options. |
| 75 | + |
| 76 | +The function supports three (mutually exclusive) means for providing slice arguments: |
| 77 | + |
| 78 | +1. providing a single [`MultiSlice`][@stdlib/slice/multi] instance. |
| 79 | +2. providing a single array of slice arguments. |
| 80 | +3. providing slice arguments as separate arguments. |
| 81 | + |
| 82 | +The following example demonstrates each invocation style achieving equivalent results. |
| 83 | + |
| 84 | +```javascript |
| 85 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 86 | +var MultiSlice = require( '@stdlib/slice/multi' ); |
| 87 | +var Slice = require( '@stdlib/slice/ctor' ); |
| 88 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 89 | + |
| 90 | +var opts = { |
| 91 | + 'dtype': 'float64' |
| 92 | +}; |
| 93 | + |
| 94 | +// 1. Using a MultiSlice: |
| 95 | +var x = zeros( [ 3, 4 ], opts ); |
| 96 | + |
| 97 | +var s0 = new Slice( 1, 3 ); |
| 98 | +var s1 = new Slice( 2, 4 ); |
| 99 | +var s = new MultiSlice( s0, s1 ); |
| 100 | + |
| 101 | +var out = fillSlice( x, 5.0, s ); |
| 102 | +// returns <ndarray> |
| 103 | + |
| 104 | +var arr = ndarray2array( out ); |
| 105 | +// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 5.0, 5.0 ], [ 0.0, 0.0, 5.0, 5.0 ] ] |
| 106 | + |
| 107 | +// 2. Using an array of slice arguments: |
| 108 | +x = zeros( [ 3, 4 ], opts ); |
| 109 | + |
| 110 | +out = fillSlice( x, 6.0, [ s0, s1 ] ); |
| 111 | +// returns <ndarray> |
| 112 | + |
| 113 | +arr = ndarray2array( out ); |
| 114 | +// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 6.0, 6.0 ], [ 0.0, 0.0, 6.0, 6.0 ] ] |
| 115 | + |
| 116 | +// 3. Providing separate arguments: |
| 117 | +x = zeros( [ 3, 4 ], opts ); |
| 118 | + |
| 119 | +out = fillSlice( x, 7.0, s0, s1 ); |
| 120 | +// returns <ndarray> |
| 121 | + |
| 122 | +arr = ndarray2array( out ); |
| 123 | +// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 7.0, 7.0 ], [ 0.0, 0.0, 7.0, 7.0 ] ] |
| 124 | +``` |
| 125 | + |
| 126 | +The function supports the following options: |
| 127 | + |
| 128 | +- **strict**: boolean indicating whether to enforce strict bounds checking. |
| 129 | + |
| 130 | +By default, the function throws an error when provided a slice which exceeds array bounds. To ignore slice indices exceeding array bounds, set the `strict` option to `false`. |
| 131 | + |
| 132 | +```javascript |
| 133 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 134 | +var MultiSlice = require( '@stdlib/slice/multi' ); |
| 135 | +var Slice = require( '@stdlib/slice/ctor' ); |
| 136 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 137 | + |
| 138 | +var x = zeros( [ 3, 4 ], { |
| 139 | + 'dtype': 'float64' |
| 140 | +}); |
| 141 | + |
| 142 | +// Define the fill region: |
| 143 | +var s0 = new Slice( 1, null, 1 ); |
| 144 | +var s1 = new Slice( 10, 20, 1 ); |
| 145 | +var s = new MultiSlice( s0, s1 ); |
| 146 | + |
| 147 | +// Fill the region with a scalar value: |
| 148 | +var y = fillSlice( x, 5.0, s, { |
| 149 | + 'strict': false |
| 150 | +}); |
| 151 | +// returns <ndarray> |
| 152 | + |
| 153 | +var arr = ndarray2array( x ); |
| 154 | +// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0 ] ] |
| 155 | +``` |
| 156 | + |
| 157 | +</section> |
| 158 | + |
| 159 | +<!-- /.usage --> |
| 160 | + |
| 161 | +<section class="notes"> |
| 162 | + |
| 163 | +## Notes |
| 164 | + |
| 165 | +- An input [`ndarray`][@stdlib/ndarray/ctor] **must** be writable. If provided a **read-only** [`ndarray`][@stdlib/ndarray/ctor], the function throws an error. |
| 166 | +- A **slice argument** must be either a [`Slice`][@stdlib/slice/ctor], an integer, `null`, or `undefined`. |
| 167 | +- If a fill value is a number and `x` has a complex [data type][@stdlib/ndarray/dtypes], the function fills an input [`ndarray`][@stdlib/ndarray/ctor] with a complex number whose real component equals the provided fill `value` and whose imaginary component is zero. |
| 168 | +- A fill value must be able to safely cast to the input [`ndarray`][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Fill values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a `'float32'` input [`ndarray`][@stdlib/ndarray/ctor]). |
| 169 | +- The function **mutates** the input [`ndarray`][@stdlib/ndarray/ctor]. |
| 170 | + |
| 171 | +</section> |
| 172 | + |
| 173 | +<!-- /.notes --> |
| 174 | + |
| 175 | +<section class="examples"> |
| 176 | + |
| 177 | +## Examples |
| 178 | + |
| 179 | +<!-- eslint no-undef: "error" --> |
| 180 | + |
| 181 | +```javascript |
| 182 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 183 | +var MultiSlice = require( '@stdlib/slice/multi' ); |
| 184 | +var Slice = require( '@stdlib/slice/ctor' ); |
| 185 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 186 | +var fillSlice = require( '@stdlib/ndarray/fill-slice' ); |
| 187 | + |
| 188 | +// Create a zero-filled ndarray: |
| 189 | +var x = zeros( [ 2, 3, 4 ], { |
| 190 | + 'dtype': 'generic' |
| 191 | +}); |
| 192 | +console.log( ndarray2array( x ) ); |
| 193 | + |
| 194 | +// Specify the fill region: |
| 195 | +var s0 = new Slice( 1, 2 ); |
| 196 | +var s1 = new Slice( null, null ); |
| 197 | +var s2 = new Slice( 2, 4 ); |
| 198 | +var s = new MultiSlice( s0, s1, s2 ); |
| 199 | + |
| 200 | +// Fill a slice with a scalar value: |
| 201 | +fillSlice( x, 10.0, s ); |
| 202 | +console.log( ndarray2array( x ) ); |
| 203 | +``` |
| 204 | + |
| 205 | +</section> |
| 206 | + |
| 207 | +<!-- /.examples --> |
| 208 | + |
| 209 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 210 | + |
| 211 | +<section class="related"> |
| 212 | + |
| 213 | +</section> |
| 214 | + |
| 215 | +<!-- /.related --> |
| 216 | + |
| 217 | +<section class="links"> |
| 218 | + |
| 219 | +[@stdlib/slice/multi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/multi |
| 220 | + |
| 221 | +[@stdlib/slice/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/ctor |
| 222 | + |
| 223 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 224 | + |
| 225 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 226 | + |
| 227 | +</section> |
| 228 | + |
| 229 | +<!-- /.links --> |
0 commit comments