|
| 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 | +# unary4dBy |
| 22 | + |
| 23 | +> Apply a unary function to each element retrieved from a four-dimensional nested input array according to a callback function and assign results to elements in a four-dimensional nested output array. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var unary4dBy = require( '@stdlib/array/base/unary4d-by' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### unary4dBy( arrays, shape, fcn, clbk\[, thisArg] ) |
| 40 | + |
| 41 | +Applies a unary function to each element retrieved from a four-dimensional nested input array according to a callback function and assigns results to elements in a four-dimensional nested output array. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 45 | + |
| 46 | +function accessor( v ) { |
| 47 | + return v * 2.0; |
| 48 | +} |
| 49 | + |
| 50 | +var x = [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ]; |
| 51 | +var shape = [ 1, 1, 2, 2 ]; |
| 52 | + |
| 53 | +unary4dBy( [ x, x ], shape, abs, accessor ); |
| 54 | +// x => [ [ [ [ 2.0, 4.0 ], [ 6.0, 8.0 ] ] ] ] |
| 55 | +``` |
| 56 | + |
| 57 | +The function accepts the following arguments: |
| 58 | + |
| 59 | +- **arrays**: array-like object containing one input nested array and one output nested array. |
| 60 | +- **shape**: array shape. |
| 61 | +- **fcn**: unary function to apply to callback return values. |
| 62 | +- **clbk**: callback function. |
| 63 | +- **thisArg**: callback function execution context (optional). |
| 64 | + |
| 65 | +The invoked callback function is provided the following arguments: |
| 66 | + |
| 67 | +- **value**: array element. |
| 68 | +- **indices**: current array element indices. |
| 69 | +- **arrays**: input and output arrays. |
| 70 | + |
| 71 | +To set the callback execution context, provide a `thisArg`. |
| 72 | + |
| 73 | +<!-- eslint-disable no-invalid-this --> |
| 74 | + |
| 75 | +```javascript |
| 76 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 77 | + |
| 78 | +function accessor( v ) { |
| 79 | + this.count += 1; |
| 80 | + return v * 2.0; |
| 81 | +} |
| 82 | + |
| 83 | +var context = { |
| 84 | + 'count': 0 |
| 85 | +}; |
| 86 | + |
| 87 | +var x = [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ]; |
| 88 | +var y = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ]; |
| 89 | + |
| 90 | +var shape = [ 1, 1, 2, 2 ]; |
| 91 | + |
| 92 | +unary4dBy( [ x, y ], shape, abs, accessor, context ); |
| 93 | +// y => [ [ [ [ 2.0, 4.0 ], [ 6.0, 8.0 ] ] ] ] |
| 94 | + |
| 95 | +var cnt = context.count; |
| 96 | +// returns 4 |
| 97 | +``` |
| 98 | + |
| 99 | +</section> |
| 100 | + |
| 101 | +<!-- /.usage --> |
| 102 | + |
| 103 | +<section class="notes"> |
| 104 | + |
| 105 | +## Notes |
| 106 | + |
| 107 | +- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**. |
| 108 | + |
| 109 | + ```javascript |
| 110 | + var abs = require( '@stdlib/math/base/special/abs' ); |
| 111 | + |
| 112 | + function accessor() { |
| 113 | + // No-op... |
| 114 | + } |
| 115 | + |
| 116 | + var x = [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ]; |
| 117 | + var y = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ]; |
| 118 | + |
| 119 | + var shape = [ 1, 1, 2, 2 ]; |
| 120 | + |
| 121 | + unary4dBy( [ x, y ], shape, abs, accessor ); |
| 122 | + // y => [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ] |
| 123 | + ``` |
| 124 | + |
| 125 | +- The function assumes that the input and output arrays have the same shape. |
| 126 | + |
| 127 | +</section> |
| 128 | + |
| 129 | +<!-- /.notes --> |
| 130 | + |
| 131 | +<section class="examples"> |
| 132 | + |
| 133 | +## Examples |
| 134 | + |
| 135 | +<!-- eslint no-undef: "error" --> |
| 136 | + |
| 137 | +```javascript |
| 138 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; |
| 139 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 140 | +var filled4dBy = require( '@stdlib/array/base/filled4d-by' ); |
| 141 | +var zeros4d = require( '@stdlib/array/base/zeros4d' ); |
| 142 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 143 | +var unary4dBy = require( '@stdlib/array/base/unary4d-by' ); |
| 144 | + |
| 145 | +function accessor( v ) { |
| 146 | + // Randomly determine whether a value should be considered "missing": |
| 147 | + return ( bernoulli( 0.5 ) > 0 ) ? v : void 0; |
| 148 | +} |
| 149 | + |
| 150 | +var shape = [ 2, 2, 3, 3 ]; |
| 151 | + |
| 152 | +var x = filled4dBy( shape, discreteUniform( -100, 100 ) ); |
| 153 | +console.log( x ); |
| 154 | + |
| 155 | +var y = zeros4d( shape ); |
| 156 | +console.log( y ); |
| 157 | + |
| 158 | +unary4dBy( [ x, y ], shape, abs, accessor ); |
| 159 | +console.log( y ); |
| 160 | +``` |
| 161 | + |
| 162 | +</section> |
| 163 | + |
| 164 | +<!-- /.examples --> |
| 165 | + |
| 166 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 167 | + |
| 168 | +<section class="related"> |
| 169 | + |
| 170 | +</section> |
| 171 | + |
| 172 | +<!-- /.related --> |
| 173 | + |
| 174 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 175 | + |
| 176 | +<section class="links"> |
| 177 | + |
| 178 | +</section> |
| 179 | + |
| 180 | +<!-- /.links --> |
0 commit comments