|
| 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 | +# fillBy |
| 22 | + |
| 23 | +> Fill an input [`ndarray`][@stdlib/ndarray/ctor] according to a callback function. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var fillBy = require( '@stdlib/ndarray/fill-by' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### fillBy( x, fcn\[, thisArg] ) |
| 40 | + |
| 41 | +Fills an input [`ndarray`][@stdlib/ndarray/ctor] according to a callback function. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 45 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 46 | + |
| 47 | +function fcn( value ) { |
| 48 | + return value + 10.0; |
| 49 | +} |
| 50 | + |
| 51 | +var x = zeros( [ 3, 1, 2 ], { |
| 52 | + 'dtype': 'float64' |
| 53 | +}); |
| 54 | + |
| 55 | +var y = fillBy( x, fcn ); |
| 56 | +// returns <ndarray> |
| 57 | + |
| 58 | +var bool = ( y === x ); |
| 59 | +// returns true |
| 60 | + |
| 61 | +var arr = ndarray2array( y ); |
| 62 | +// returns [ [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ] ] |
| 63 | +``` |
| 64 | + |
| 65 | +The function accepts the following arguments: |
| 66 | + |
| 67 | +- **x**: array-like object containing an input [`ndarray`][@stdlib/ndarray/ctor]. |
| 68 | +- **fcn**: callback function. |
| 69 | +- **thisArg**: callback function execution context (_optional_). |
| 70 | + |
| 71 | +To set the callback function execution context, provide a `thisArg`. |
| 72 | + |
| 73 | +<!-- eslint-disable no-invalid-this --> |
| 74 | + |
| 75 | +```javascript |
| 76 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 77 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 78 | + |
| 79 | +function fcn( value ) { |
| 80 | + return value + this.factor; |
| 81 | +} |
| 82 | + |
| 83 | +var x = zeros( [ 3, 1, 2 ], { |
| 84 | + 'dtype': 'float64' |
| 85 | +}); |
| 86 | + |
| 87 | +var ctx = { |
| 88 | + 'factor': 10.0 |
| 89 | +}; |
| 90 | +var y = fillBy( x, fcn, ctx ); |
| 91 | +// returns <ndarray> |
| 92 | + |
| 93 | +var arr = ndarray2array( y ); |
| 94 | +// returns [ [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ] ] |
| 95 | +``` |
| 96 | + |
| 97 | +</section> |
| 98 | + |
| 99 | +<!-- /.usage --> |
| 100 | + |
| 101 | +<section class="notes"> |
| 102 | + |
| 103 | +## Notes |
| 104 | + |
| 105 | +- An input [`ndarray`][@stdlib/ndarray/ctor] **must** be writable. If provided a **read-only** [`ndarray`][@stdlib/ndarray/ctor], the function throws an error. |
| 106 | +- The function **mutates** the input [`ndarray`][@stdlib/ndarray/ctor]. |
| 107 | + |
| 108 | +</section> |
| 109 | + |
| 110 | +<!-- /.notes --> |
| 111 | + |
| 112 | +<section class="examples"> |
| 113 | + |
| 114 | +## Examples |
| 115 | + |
| 116 | +<!-- eslint no-undef: "error" --> |
| 117 | + |
| 118 | +```javascript |
| 119 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; |
| 120 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 121 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 122 | +var fillBy = require( '@stdlib/ndarray/fill-by' ); |
| 123 | + |
| 124 | +// Create a zero-filled ndarray: |
| 125 | +var x = zeros( [ 5, 2 ], { |
| 126 | + 'dtype': 'generic' |
| 127 | +}); |
| 128 | +console.log( ndarray2array( x ) ); |
| 129 | + |
| 130 | +// Fill the ndarray with random values: |
| 131 | +fillBy( x, discreteUniform( -100, 100 ) ); |
| 132 | +console.log( ndarray2array( x ) ); |
| 133 | +``` |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.examples --> |
| 138 | + |
| 139 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 140 | + |
| 141 | +<section class="related"> |
| 142 | + |
| 143 | +</section> |
| 144 | + |
| 145 | +<!-- /.related --> |
| 146 | + |
| 147 | +<section class="links"> |
| 148 | + |
| 149 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 150 | + |
| 151 | +<!-- <related-links> --> |
| 152 | + |
| 153 | +<!-- </related-links> --> |
| 154 | + |
| 155 | +</section> |
| 156 | + |
| 157 | +<!-- /.links --> |
0 commit comments