|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2023 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 | +# map5d |
| 22 | + |
| 23 | +> Apply a function to elements in a five-dimensional nested input array and assign results to elements in a new five-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 map5d = require( '@stdlib/array/base/map5d' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### map5d( x, shape, fcn\[, thisArg] ) |
| 40 | + |
| 41 | +Applies a function to elements in a five-dimensional nested input array and assigns results to elements in a new five-dimensional nested output array. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 45 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 46 | + |
| 47 | +var x = [ [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ] ]; |
| 48 | +var shape = [ 1, 1, 1, 2, 2 ]; |
| 49 | + |
| 50 | +var y = map5d( x, shape, naryFunction( abs, 1 ) ); |
| 51 | +// returns [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ] |
| 52 | +``` |
| 53 | + |
| 54 | +The function accepts the following arguments: |
| 55 | + |
| 56 | +- **x**: input nested array. |
| 57 | +- **shape**: array shape. |
| 58 | +- **fcn**: function to apply. |
| 59 | +- **thisArg**: applied function execution context (_optional_). |
| 60 | + |
| 61 | +To set the applied function's execution context, provide a `thisArg`. |
| 62 | + |
| 63 | +<!-- eslint-disable no-invalid-this --> |
| 64 | + |
| 65 | +```javascript |
| 66 | +function fcn( x ) { |
| 67 | + this.count += 1; |
| 68 | + return x; |
| 69 | +} |
| 70 | + |
| 71 | +var x = [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ]; |
| 72 | +var shape = [ 1, 1, 1, 2, 2 ]; |
| 73 | + |
| 74 | +var ctx = { |
| 75 | + 'count': 0 |
| 76 | +}; |
| 77 | + |
| 78 | +var y = map5d( x, shape, fcn, ctx ); |
| 79 | +// returns [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ] |
| 80 | + |
| 81 | +var v = ctx.count; |
| 82 | +// returns 4 |
| 83 | +``` |
| 84 | + |
| 85 | +#### map5d.assign( x, y, shape, fcn\[, thisArg] ) |
| 86 | + |
| 87 | +Applies a function to elements in a five-dimensional nested input array and assigns results to elements in a five-dimensional nested output array. |
| 88 | + |
| 89 | +```javascript |
| 90 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 91 | +var zeros5d = require( '@stdlib/array/base/zeros5d' ); |
| 92 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 93 | + |
| 94 | +var x = [ [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ] ]; |
| 95 | +var shape = [ 1, 1, 1, 2, 2 ]; |
| 96 | + |
| 97 | +var y = zeros5d( shape ); |
| 98 | + |
| 99 | +var out = map5d.assign( x, y, shape, naryFunction( abs, 1 ) ); |
| 100 | +// returns [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ] |
| 101 | + |
| 102 | +var bool = ( out === y ); |
| 103 | +// returns true |
| 104 | +``` |
| 105 | + |
| 106 | +The function accepts the following arguments: |
| 107 | + |
| 108 | +- **x**: input nested array. |
| 109 | +- **y**: output nested array. |
| 110 | +- **shape**: array shape. |
| 111 | +- **fcn**: function to apply. |
| 112 | +- **thisArg**: applied function execution context (_optional_). |
| 113 | + |
| 114 | +The function assumes that the input and output arrays have the same shape. |
| 115 | + |
| 116 | +</section> |
| 117 | + |
| 118 | +<!-- /.usage --> |
| 119 | + |
| 120 | +<section class="notes"> |
| 121 | + |
| 122 | +</section> |
| 123 | + |
| 124 | +<!-- /.notes --> |
| 125 | + |
| 126 | +<section class="examples"> |
| 127 | + |
| 128 | +## Examples |
| 129 | + |
| 130 | +<!-- eslint no-undef: "error" --> |
| 131 | + |
| 132 | +```javascript |
| 133 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; |
| 134 | +var filled5dBy = require( '@stdlib/array/base/filled5d-by' ); |
| 135 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 136 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 137 | +var map5d = require( '@stdlib/array/base/map5d' ); |
| 138 | + |
| 139 | +var shape = [ 1, 1, 3, 3, 3 ]; |
| 140 | + |
| 141 | +var x = filled5dBy( shape, discreteUniform( -100, 100 ) ); |
| 142 | +console.log( x ); |
| 143 | + |
| 144 | +var y = map5d( x, shape, naryFunction( abs, 1 ) ); |
| 145 | +console.log( y ); |
| 146 | +``` |
| 147 | + |
| 148 | +</section> |
| 149 | + |
| 150 | +<!-- /.examples --> |
| 151 | + |
| 152 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 153 | + |
| 154 | +<section class="related"> |
| 155 | + |
| 156 | +</section> |
| 157 | + |
| 158 | +<!-- /.related --> |
| 159 | + |
| 160 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 161 | + |
| 162 | +<section class="links"> |
| 163 | + |
| 164 | +</section> |
| 165 | + |
| 166 | +<!-- /.links --> |
0 commit comments