|
| 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] with a specified value in the region defined by a [`MultiSlice`][@stdlib/slice/multi]. |
| 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, s, value ) |
| 40 | + |
| 41 | +Fills an input [`ndarray`][@stdlib/ndarray/ctor] with a specified value in the region defined by a [`MultiSlice`][@stdlib/slice/multi]. |
| 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 | +// Create a MultiSlice to specify 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 ndarray with a scalar value: |
| 59 | +var y = fillSlice( x, s, 5.0 ); |
| 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, 5, 5 ], [ 0, 0, 5, 5 ] ] |
| 67 | +``` |
| 68 | + |
| 69 | +The function accepts the following arguments: |
| 70 | + |
| 71 | +- **x**: array-like object containing an input [`ndarray`][@stdlib/ndarray/ctor]. |
| 72 | +- **s**: [`MultiSlice`][@stdlib/slice/multi] specifying the fill region of the input [`ndarray`][@stdlib/ndarray/ctor]. |
| 73 | +- **value**: scalar value. |
| 74 | + |
| 75 | +</section> |
| 76 | + |
| 77 | +<!-- /.usage --> |
| 78 | + |
| 79 | +<section class="notes"> |
| 80 | + |
| 81 | +## Notes |
| 82 | + |
| 83 | +- An input [`ndarray`][@stdlib/ndarray/ctor] **must** be writable. If provided a **read-only** [`ndarray`][@stdlib/ndarray/ctor], the function throws an error. |
| 84 | +- If `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 scalar `value` and whose imaginary component is zero. |
| 85 | +- A `value` must be able to safely cast to the input [`ndarray`][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Scalar 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]). |
| 86 | +- The function **mutates** the input [`ndarray`][@stdlib/ndarray/ctor]. |
| 87 | +- Multi-slice instances have no explicit functionality; however, they are used by [`ndarray`][@stdlib/ndarray/ctor] and other packages for creating views into multi-dimensional data structures. |
| 88 | + |
| 89 | +</section> |
| 90 | + |
| 91 | +<!-- /.notes --> |
| 92 | + |
| 93 | +<section class="examples"> |
| 94 | + |
| 95 | +## Examples |
| 96 | + |
| 97 | +<!-- eslint no-undef: "error" --> |
| 98 | + |
| 99 | +```javascript |
| 100 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 101 | +var MultiSlice = require( '@stdlib/slice/multi' ); |
| 102 | +var Slice = require( '@stdlib/slice/ctor' ); |
| 103 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 104 | +var fillSlice = require( '@stdlib/ndarray/fill-slice' ); |
| 105 | + |
| 106 | +// Create a zero-filled ndarray: |
| 107 | +var x = zeros( [ 2, 3, 4 ], { |
| 108 | + 'dtype': 'generic' |
| 109 | +}); |
| 110 | +console.log( ndarray2array( x ) ); |
| 111 | + |
| 112 | +// Create a MultiSlice to specify the fill region: |
| 113 | +var s0 = new Slice( 1, 2 ); |
| 114 | +var s1 = new Slice( null, null ); |
| 115 | +var s2 = new Slice( 2, 4 ); |
| 116 | +var s = new MultiSlice( s0, s1, s2 ); |
| 117 | + |
| 118 | +// Fill the ndarray with a scalar value: |
| 119 | +fillSlice( x, 10.0 ); |
| 120 | +console.log( ndarray2array( x ) ); |
| 121 | +``` |
| 122 | + |
| 123 | +</section> |
| 124 | + |
| 125 | +<!-- /.examples --> |
| 126 | + |
| 127 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 128 | + |
| 129 | +<section class="related"> |
| 130 | + |
| 131 | +</section> |
| 132 | + |
| 133 | +<!-- /.related --> |
| 134 | + |
| 135 | +<section class="links"> |
| 136 | + |
| 137 | +[@stdlib/slice/multi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/multi |
| 138 | + |
| 139 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 140 | + |
| 141 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 142 | + |
| 143 | +</section> |
| 144 | + |
| 145 | +<!-- /.links --> |
0 commit comments