|
| 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 | +# Unary |
| 22 | + |
| 23 | +> Constructor for applying a unary function to each element in an input array. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var Unary = require( '@stdlib/math/array/tools/unary' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### Unary( fcn, idtypes, odtypes, policy ) |
| 34 | + |
| 35 | +Constructor for applying a unary function to each element in an input array. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 39 | + |
| 40 | +var dtypes = [ 'float64', 'float32', 'generic' ]; |
| 41 | +var policy = 'same'; |
| 42 | + |
| 43 | +var unary = new Unary( abs, dtypes, dtypes, policy ); |
| 44 | +``` |
| 45 | + |
| 46 | +The constructor has the following parameters: |
| 47 | + |
| 48 | +- **fcn**: unary function to apply. |
| 49 | +- **idtypes**: list of supported input data types. |
| 50 | +- **odtypes**: list of supported input data types. |
| 51 | +- **policy**: output data type policy. |
| 52 | + |
| 53 | +#### Unary.prototype.apply( x\[, options] ) |
| 54 | + |
| 55 | +Applies a unary function to each element in a provided input array. |
| 56 | + |
| 57 | +```javascript |
| 58 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 59 | + |
| 60 | +var dtypes = [ 'float64', 'float32', 'generic' ]; |
| 61 | +var policy = 'same'; |
| 62 | + |
| 63 | +var unary = new Unary( abs, dtypes, dtypes, policy ); |
| 64 | + |
| 65 | +var v = unary.apply( [ -1.0, 2.0, -3.0, 4.0 ] ); |
| 66 | +// returns [ 1.0, 2.0, 3.0, 4.0 ] |
| 67 | +``` |
| 68 | + |
| 69 | +The method has the following parameters: |
| 70 | + |
| 71 | +- **x**: input array. |
| 72 | +- **options**: function options. |
| 73 | + |
| 74 | +The method accepts the following options: |
| 75 | + |
| 76 | +- **dtype**: output array data type. Setting this option, overrides the output data type policy. |
| 77 | + |
| 78 | +By default, the method returns an array having a data type determined by the output data type policy. To override the default behavior, set the `dtype` option. |
| 79 | + |
| 80 | +```javascript |
| 81 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 82 | + |
| 83 | +var dtypes = [ 'float64', 'float32', 'generic' ]; |
| 84 | +var policy = 'same'; |
| 85 | + |
| 86 | +var unary = new Unary( abs, dtypes, dtypes, policy ); |
| 87 | + |
| 88 | +var v = unary.apply( [ -1.0, 2.0, -3.0, 4.0 ], { |
| 89 | + 'dtype': 'float64' |
| 90 | +}); |
| 91 | +// returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ] |
| 92 | +``` |
| 93 | + |
| 94 | +#### Unary.prototype.assign( x, out ) |
| 95 | + |
| 96 | +Applies a unary function to each element in a provided input array and assigns results to a provided output array. |
| 97 | + |
| 98 | +```javascript |
| 99 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 100 | +var zeros = require( '@stdlib/array/zeros' ); |
| 101 | + |
| 102 | +var dtypes = [ 'float64', 'float32', 'generic' ]; |
| 103 | +var policy = 'same'; |
| 104 | + |
| 105 | +var unary = new Unary( abs, dtypes, dtypes, policy ); |
| 106 | + |
| 107 | +var out = zeros( 4, 'float64' ); |
| 108 | +// returns <Float64Array>[ 0.0, 0.0, 0.0, 0.0 ] |
| 109 | + |
| 110 | +var v = unary.assign( [ -1.0, 2.0, -3.0, 4.0 ], out ); |
| 111 | +// returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ] |
| 112 | + |
| 113 | +var bool = ( v === out ); |
| 114 | +// returns true |
| 115 | +``` |
| 116 | + |
| 117 | +The method has the following parameters: |
| 118 | + |
| 119 | +- **x**: input array. |
| 120 | +- **out**: output array. |
| 121 | + |
| 122 | +</section> |
| 123 | + |
| 124 | +<!-- /.usage --> |
| 125 | + |
| 126 | +<section class="notes"> |
| 127 | + |
| 128 | +## Notes |
| 129 | + |
| 130 | +- The output data type policy only applies to the `apply` method. For the `assign` method, the output array is allowed to be any array-like object, including [accessor arrays][@stdlib/array/base/accessor]. |
| 131 | + |
| 132 | +</section> |
| 133 | + |
| 134 | +<!-- /.notes --> |
| 135 | + |
| 136 | +<section class="examples"> |
| 137 | + |
| 138 | +## Examples |
| 139 | + |
| 140 | +<!-- eslint no-undef: "error" --> |
| 141 | + |
| 142 | +```javascript |
| 143 | +var base = require( '@stdlib/math/base/special/sin' ); |
| 144 | +var uniform = require( '@stdlib/random/array/uniform' ); |
| 145 | +var dtypes = require( '@stdlib/array/dtypes' ); |
| 146 | +var dtype = require( '@stdlib/array/dtype' ); |
| 147 | +var logEach = require( '@stdlib/console/log-each' ); |
| 148 | +var Unary = require( '@stdlib/math/array/tools/unary' ); |
| 149 | + |
| 150 | +// Define the supported input and output data types: |
| 151 | +var idt = dtypes( 'real_and_generic' ); |
| 152 | +var odt = dtypes( 'real_floating_point_and_generic' ); |
| 153 | + |
| 154 | +// Define the policy mapping an input data type to an output data type: |
| 155 | +var policy = 'real_floating_point_and_generic'; |
| 156 | + |
| 157 | +// Create an interface for computing the element-wise sine: |
| 158 | +var sin = new Unary( base, idt, odt, policy ); |
| 159 | + |
| 160 | +// Generate an array of random numbers: |
| 161 | +var x = uniform( 10, -1.0, 1.0, { |
| 162 | + 'dtype': 'generic' |
| 163 | +}); |
| 164 | + |
| 165 | +// Compute the element-wise sine: |
| 166 | +var y = sin.apply( x ); |
| 167 | + |
| 168 | +// Resolve the output array data type: |
| 169 | +var dt = dtype( y ); |
| 170 | +console.log( dt ); |
| 171 | + |
| 172 | +// Print the results: |
| 173 | +``` |
| 174 | + |
| 175 | +</section> |
| 176 | + |
| 177 | +<!-- /.examples --> |
| 178 | + |
| 179 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 180 | + |
| 181 | +<section class="related"> |
| 182 | + |
| 183 | +</section> |
| 184 | + |
| 185 | +<!-- /.related --> |
| 186 | + |
| 187 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 188 | + |
| 189 | +<section class="links"> |
| 190 | + |
| 191 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 192 | + |
| 193 | +</section> |
| 194 | + |
| 195 | +<!-- /.links --> |
0 commit comments