Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 63x 63x 1x 1x 63x 9x 9x 53x 63x 63x 43x 63x 11x 11x 42x 63x 63x 32x 63x 11x 11x 63x 12x 12x 19x 19x 19x 19x 19x 63x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 53x 53x 53x 53x 53x 18x 18x 53x 53x 6x 6x 29x 53x 24x 24x 21x 21x 24x 53x 53x 53x 53x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 9x 9x 27x 9x 9x 9x 27x 27x 3x 3x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-restricted-syntax, no-invalid-this */
'use strict';
// MODULES //
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var isFunction = require( '@stdlib/assert/is-function' );
var isCollection = require( '@stdlib/assert/is-collection' );
var isDataType = require( '@stdlib/array/base/assert/is-data-type' );
var isOutputDataTypePolicy = require( '@stdlib/ndarray/base/assert/is-output-data-type-policy' );
var contains = require( '@stdlib/array/base/assert/contains' );
var unary = require( '@stdlib/strided/base/unary' );
var odtype = require( '@stdlib/ndarray/base/unary-output-dtype' );
var empty = require( '@stdlib/array/empty' );
var dtype = require( '@stdlib/array/dtype' );
var everyBy = require( '@stdlib/array/base/every-by' );
var copy = require( '@stdlib/array/base/copy' );
var format = require( '@stdlib/string/format' );
var validate = require( './validate.js' );
// VARIABLES //
var GENERIC_DTYPE = 'generic';
// MAIN //
/**
* Constructor for applying a unary function to each element in an input array.
*
* @constructor
* @param {Function} fcn - unary function to apply
* @param {StringArray} idtypes - list of supported input data types
* @param {StringArray} odtypes - list of supported output data types
* @param {string} policy - output data type policy
* @throws {TypeError} first argument must be a function
* @throws {TypeError} second argument must be an array of supported data types
* @throws {TypeError} third argument must be an array of supported data types
* @throws {TypeError} fourth argument must be a supported output data type policy
* @returns {Unary} instance
*
* @example
* var base = require( '@stdlib/math/base/special/abs' );
* var dtypes = require( '@stdlib/array/dtypes' );
*
* var idt = dtypes( 'real_and_generic' );
* var odt = idt;
* var policy = 'same';
*
* var abs = new Unary( base, idt, odt, policy );
*
* var x = [ -1.0, -2.0, -3.0 ];
*
* var y = abs.apply( x );
* // returns [ 1.0, 2.0, 3.0 ]
*/
function Unary( fcn, idtypes, odtypes, policy ) {
if ( !( this instanceof Unary ) ) {
return new Unary( fcn, idtypes, odtypes, policy );
}
if ( !isFunction( fcn ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
}
if (
!isCollection( idtypes ) ||
idtypes.length < 1 ||
!everyBy( idtypes, isDataType )
) {
throw new TypeError( format( 'invalid argument. Second argument must be an array of data types. Value: `%s`.', idtypes ) );
}
if (
!isCollection( odtypes ) ||
odtypes.length < 1 ||
!everyBy( odtypes, isDataType )
) {
throw new TypeError( format( 'invalid argument. Third argument must be an array of data types. Value: `%s`.', odtypes ) );
}
if ( !isOutputDataTypePolicy( policy ) ) {
throw new TypeError( format( 'invalid argument. Fourth argument must be a supported output data type policy. Value: `%s`.', policy ) );
}
this._fcn = fcn;
this._idtypes = copy( idtypes );
this._odtypes = copy( odtypes );
this._policy = policy;
return this;
}
/**
* Applies a unary function to each element in a provided input array.
*
* @name apply
* @memberof Unary.prototype
* @type {Function}
* @param {Collection} x - input array
* @param {Options} [options] - function options
* @param {string} [options.dtype] - output array data type
* @throws {TypeError} first argument must be a collection
* @throws {TypeError} first argument must have a supported data type
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {Collection} output array
*
* @example
* var base = require( '@stdlib/math/base/special/abs' );
* var dtypes = require( '@stdlib/array/dtypes' );
*
* var idt = dtypes( 'real_and_generic' );
* var odt = idt;
* var policy = 'same';
*
* var abs = new Unary( base, idt, odt, policy );
*
* var x = [ -1.0, -2.0, -3.0 ];
*
* var y = abs.apply( x );
* // returns [ 1.0, 2.0, 3.0 ]
*/
setReadOnly( Unary.prototype, 'apply', function apply( x, options ) {
var opts;
var err;
var out;
var dt;
if ( !isCollection( x ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );
}
dt = dtype( x ) || GENERIC_DTYPE;
if ( !contains( this._idtypes, dt ) ) {
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', this._idtypes.join( '", "' ), dt ) );
}
opts = {};
if ( arguments.length > 1 ) {
err = validate( opts, this._odtypes, options );
if ( err ) {
throw err;
}
}
dt = opts.dtype || odtype( dt, this._policy );
out = empty( x.length, dt );
unary( [ x, out ], [ x.length ], [ 1, 1 ], this._fcn );
return out;
});
/**
* Applies a unary function to each element in a provided input array and assigns results to a provided output array.
*
* @name assign
* @memberof Unary.prototype
* @type {Function}
* @param {Collection} x - input array
* @param {Collection} out - output array
* @throws {TypeError} first argument must be a collection
* @throws {TypeError} first argument must have a supported data type
* @throws {TypeError} second argument must be a collection
* @returns {Collection} output array
*
* @example
* var base = require( '@stdlib/math/base/special/abs' );
* var dtypes = require( '@stdlib/array/dtypes' );
*
* var idt = dtypes( 'real_and_generic' );
* var odt = idt;
* var policy = 'same';
*
* var abs = new Unary( base, idt, odt, policy );
*
* var x = [ -1.0, -2.0, -3.0 ];
* var y = [ 0.0, 0.0, 0.0 ];
*
* var out = abs.assign( x, y );
* // returns [ 1.0, 2.0, 3.0 ]
*
* var bool = ( out === y );
* // returns true
*/
setReadOnly( Unary.prototype, 'assign', function assign( x, out ) {
var dt;
if ( !isCollection( x ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );
}
if ( !isCollection( out ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', out ) );
}
// Validate the input array data type in order to maintain similar behavior to `apply` above...
dt = dtype( x ) || GENERIC_DTYPE;
if ( !contains( this._idtypes, dt ) ) {
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', this._idtypes.join( '", "' ), dt ) );
}
// Note: we intentionally don't validate the output array dtype because (1) maintains parity with `@stdlib/random/array/tools/unary` and (2) allowing any dtype provides an escape hatch for a user wanting to have an output array having a specific dtype that `apply` does not support
unary( [ x, out ], [ x.length ], [ 1, 1 ], this._fcn );
return out;
});
// EXPORTS //
module.exports = Unary;
|