@stdlib/math-tools-unary 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +1 -1
- package/README.md +220 -189
- package/dist/index.d.ts +2 -2
- package/dist/index.js +6 -18
- package/dist/index.js.map +4 -4
- package/docs/types/index.d.ts +80 -166
- package/lib/index.js +19 -14
- package/lib/main.js +153 -149
- package/lib/validate.js +13 -15
- package/package.json +21 -17
- package/lib/defaults.json +0 -3
- package/lib/ndarray.js +0 -63
- package/lib/policies.json +0 -6
- package/lib/resolve_output_dtype.js +0 -78
- package/lib/validate_options.js +0 -67
- package/lib/validate_table.js +0 -84
package/lib/main.js
CHANGED
|
@@ -21,47 +21,52 @@
|
|
|
21
21
|
// MODULES //
|
|
22
22
|
|
|
23
23
|
var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
24
|
-
var isNumber = require( '@stdlib/assert-is-number' ).isPrimitive;
|
|
25
|
-
var isComplexLike = require( '@stdlib/assert-is-complex-like' );
|
|
26
24
|
var isndarrayLike = require( '@stdlib/assert-is-ndarray-like' );
|
|
25
|
+
var isFunction = require( '@stdlib/assert-is-function' );
|
|
26
|
+
var isObject = require( '@stdlib/assert-is-object' );
|
|
27
27
|
var isCollection = require( '@stdlib/assert-is-collection' );
|
|
28
|
-
var
|
|
29
|
-
var
|
|
30
|
-
var
|
|
28
|
+
var isOutputDataTypePolicy = require( '@stdlib/ndarray-base-assert-is-output-data-type-policy' );
|
|
29
|
+
var isInputCastingPolicy = require( '@stdlib/ndarray-base-assert-is-input-casting-policy' );
|
|
30
|
+
var isDataType = require( '@stdlib/ndarray-base-assert-is-data-type' );
|
|
31
|
+
var contains = require( '@stdlib/array-base-assert-contains' );
|
|
32
|
+
var unaryOutputDataType = require( '@stdlib/ndarray-base-unary-output-dtype' );
|
|
33
|
+
var unaryInputCastingDataType = require( '@stdlib/ndarray-base-unary-input-casting-dtype' );
|
|
34
|
+
var baseAssign = require( '@stdlib/ndarray-base-assign' );
|
|
35
|
+
var baseEmpty = require( '@stdlib/ndarray-base-empty' );
|
|
36
|
+
var maybeBroadcastArray = require( '@stdlib/ndarray-maybe-broadcast-array' );
|
|
37
|
+
var getShape = require( '@stdlib/ndarray-shape' );
|
|
38
|
+
var getOrder = require( '@stdlib/ndarray-order' );
|
|
39
|
+
var getDType = require( '@stdlib/ndarray-dtype' );
|
|
40
|
+
var empty = require( '@stdlib/ndarray-empty' );
|
|
41
|
+
var everyBy = require( '@stdlib/array-base-every-by' );
|
|
42
|
+
var join = require( '@stdlib/array-base-join' );
|
|
31
43
|
var format = require( '@stdlib/string-format' );
|
|
32
|
-
var ndarrayfcn = require( './ndarray.js' );
|
|
33
|
-
var odtype = require( './resolve_output_dtype.js' );
|
|
34
|
-
var defaults = require( './defaults.json' );
|
|
35
|
-
var validateTable = require( './validate_table.js' );
|
|
36
|
-
var validateOptions = require( './validate_options.js' );
|
|
37
44
|
var validate = require( './validate.js' );
|
|
38
45
|
|
|
39
46
|
|
|
40
47
|
// MAIN //
|
|
41
48
|
|
|
42
49
|
/**
|
|
43
|
-
* Returns a function which
|
|
50
|
+
* Returns a function which performs element-wise computation.
|
|
44
51
|
*
|
|
45
|
-
* @param {
|
|
46
|
-
* @param {
|
|
47
|
-
* @param {
|
|
48
|
-
* @param {
|
|
49
|
-
* @param {
|
|
50
|
-
* @param {
|
|
51
|
-
* @
|
|
52
|
-
* @throws {TypeError}
|
|
53
|
-
* @throws {TypeError}
|
|
54
|
-
* @throws {
|
|
55
|
-
* @
|
|
56
|
-
* @throws {TypeError} must provide valid options
|
|
57
|
-
* @returns {Function} dispatch function
|
|
52
|
+
* @param {Function} fcn - function which applies a unary function to each element in an ndarray
|
|
53
|
+
* @param {ArrayLikeObject<StringArray>} idtypes - list containing lists of supported input data types for each ndarray argument
|
|
54
|
+
* @param {StringArray} odtypes - list of supported output data types
|
|
55
|
+
* @param {Object} policies - dispatch policies
|
|
56
|
+
* @param {string} policies.output - output data type policy
|
|
57
|
+
* @param {string} policies.casting - input ndarray casting policy
|
|
58
|
+
* @throws {TypeError} first argument must be a function
|
|
59
|
+
* @throws {TypeError} second argument must be an array containing arrays of supported data types
|
|
60
|
+
* @throws {TypeError} third argument must be an array of supported data types
|
|
61
|
+
* @throws {TypeError} fourth argument must be an object having supported policies
|
|
62
|
+
* @returns {Function} function which performs element-wise computation
|
|
58
63
|
*
|
|
59
64
|
* @example
|
|
60
65
|
* var base = require( '@stdlib/math-base-special-abs' );
|
|
61
|
-
* var
|
|
62
|
-
* var dispatcher = require( '@stdlib/ndarray-dispatch' );
|
|
66
|
+
* var dispatch = require( '@stdlib/ndarray-dispatch' );
|
|
63
67
|
* var unary = require( '@stdlib/ndarray-base-unary' );
|
|
64
|
-
* var
|
|
68
|
+
* var ndarray2array = require( '@stdlib/ndarray-to-array' );
|
|
69
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
65
70
|
*
|
|
66
71
|
* var types = [
|
|
67
72
|
* 'float64', 'float64',
|
|
@@ -73,173 +78,172 @@ var validate = require( './validate.js' );
|
|
|
73
78
|
* base,
|
|
74
79
|
* base
|
|
75
80
|
* ];
|
|
76
|
-
* var
|
|
81
|
+
* var dispatcher = dispatch( unary, types, data, 2, 1, 1 );
|
|
77
82
|
*
|
|
78
|
-
* var
|
|
79
|
-
*
|
|
80
|
-
* 'complex': null,
|
|
81
|
-
* 'array': strided,
|
|
82
|
-
* 'ndarray': nd
|
|
83
|
-
* };
|
|
83
|
+
* var idt = [ 'float64', 'float32', 'generic' ];
|
|
84
|
+
* var odt = idt;
|
|
84
85
|
*
|
|
85
|
-
* var
|
|
86
|
-
* '
|
|
87
|
-
*
|
|
86
|
+
* var policies = {
|
|
87
|
+
* 'output': 'real_and_generic',
|
|
88
|
+
* 'casting': 'none'
|
|
89
|
+
* };
|
|
90
|
+
* var abs = factory( dispatcher, [ idt ], odt, policies );
|
|
88
91
|
*
|
|
89
|
-
* var x =
|
|
92
|
+
* var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
|
|
93
|
+
* // returns <ndarray>
|
|
90
94
|
*
|
|
91
95
|
* var y = abs( x );
|
|
92
|
-
* // returns <
|
|
96
|
+
* // returns <ndarray>
|
|
97
|
+
*
|
|
98
|
+
* var arr = ndarray2array( y );
|
|
99
|
+
* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
|
|
93
100
|
*/
|
|
94
|
-
function
|
|
95
|
-
var
|
|
96
|
-
var
|
|
97
|
-
var
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
t = {
|
|
101
|
-
'number': null,
|
|
102
|
-
'complex': null,
|
|
103
|
-
'array': null,
|
|
104
|
-
'ndarray': null
|
|
105
|
-
};
|
|
106
|
-
err = validateTable( t, table );
|
|
107
|
-
if ( err ) {
|
|
108
|
-
throw err;
|
|
101
|
+
function factory( fcn, idtypes, odtypes, policies ) {
|
|
102
|
+
var POLICIES;
|
|
103
|
+
var dt;
|
|
104
|
+
var i;
|
|
105
|
+
if ( !isFunction( fcn ) ) {
|
|
106
|
+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
|
|
109
107
|
}
|
|
110
|
-
|
|
111
|
-
'
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (
|
|
116
|
-
|
|
108
|
+
if ( !isCollection( idtypes ) ) {
|
|
109
|
+
throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', idtypes ) );
|
|
110
|
+
}
|
|
111
|
+
for ( i = 0; i < idtypes.length; i++ ) {
|
|
112
|
+
dt = idtypes[ i ];
|
|
113
|
+
if (
|
|
114
|
+
!isCollection( dt ) ||
|
|
115
|
+
dt.length < 1 ||
|
|
116
|
+
!everyBy( dt, isDataType )
|
|
117
|
+
) {
|
|
118
|
+
throw new TypeError( format( 'invalid argument. Second argument must contain arrays of data types. Value: `%s`.', idtypes ) );
|
|
117
119
|
}
|
|
118
120
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
121
|
+
if (
|
|
122
|
+
!isCollection( odtypes ) ||
|
|
123
|
+
odtypes.length < 1 ||
|
|
124
|
+
!everyBy( odtypes, isDataType )
|
|
125
|
+
) {
|
|
126
|
+
throw new TypeError( format( 'invalid argument. Third argument must be an array of data types. Value: `%s`.', odtypes ) );
|
|
127
|
+
}
|
|
128
|
+
if ( !isObject( policies ) ) {
|
|
129
|
+
throw new TypeError( format( 'invalid argument. Fourth argument must be an object. Value: `%s`.', policies ) );
|
|
130
|
+
}
|
|
131
|
+
if ( !isOutputDataTypePolicy( policies.output ) ) {
|
|
132
|
+
throw new TypeError( format( 'invalid argument. Fourth argument must be an object having a supported output data type policy. Value: `%s`.', policies.output ) );
|
|
133
|
+
}
|
|
134
|
+
if ( !isInputCastingPolicy( policies.casting ) ) {
|
|
135
|
+
throw new TypeError( format( 'invalid argument. Fourth argument must be an object having a supported casting policy. Value: `%s`.', policies.casting ) );
|
|
136
|
+
}
|
|
137
|
+
POLICIES = {
|
|
138
|
+
'output': policies.output,
|
|
139
|
+
'casting': policies.casting
|
|
140
|
+
};
|
|
141
|
+
setReadOnly( unary, 'assign', assign );
|
|
142
|
+
return unary;
|
|
122
143
|
|
|
123
144
|
/**
|
|
124
|
-
*
|
|
145
|
+
* Performs element-wise computation.
|
|
125
146
|
*
|
|
126
147
|
* @private
|
|
127
|
-
* @param {
|
|
148
|
+
* @param {ndarray} x - input array
|
|
128
149
|
* @param {Options} [options] - options
|
|
129
|
-
* @param {
|
|
130
|
-
* @param {string} [options.order] - output array order
|
|
131
|
-
* @throws {TypeError} first argument must be
|
|
150
|
+
* @param {*} [options.dtype] - output array data type
|
|
151
|
+
* @param {string} [options.order] - output array order
|
|
152
|
+
* @throws {TypeError} first argument must be an ndarray
|
|
153
|
+
* @throws {TypeError} first argument must have a supported data type
|
|
132
154
|
* @throws {TypeError} options argument must be an object
|
|
133
155
|
* @throws {TypeError} must provide valid options
|
|
134
|
-
* @returns {
|
|
156
|
+
* @returns {ndarray} output array
|
|
135
157
|
*/
|
|
136
|
-
function
|
|
137
|
-
var xdtype;
|
|
138
|
-
var ydtype;
|
|
158
|
+
function unary( x ) {
|
|
139
159
|
var opts;
|
|
140
160
|
var err;
|
|
161
|
+
var xsh;
|
|
162
|
+
var ord;
|
|
163
|
+
var xdt;
|
|
164
|
+
var ydt;
|
|
165
|
+
var tmp;
|
|
166
|
+
var dt;
|
|
141
167
|
var y;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
throw new TypeError( 'invalid argument. Providing a number is not supported.' );
|
|
168
|
+
|
|
169
|
+
if ( !isndarrayLike( x ) ) {
|
|
170
|
+
throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) );
|
|
147
171
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
throw new TypeError( 'invalid argument. Providing a complex number is not supported.' );
|
|
172
|
+
xdt = getDType( x );
|
|
173
|
+
if ( !contains( idtypes[ 0 ], xdt ) ) {
|
|
174
|
+
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( idtypes[ 0 ], '", "' ), xdt ) );
|
|
153
175
|
}
|
|
154
176
|
opts = {};
|
|
155
177
|
if ( arguments.length > 1 ) {
|
|
156
|
-
err = validate( opts, arguments[ 1 ] );
|
|
178
|
+
err = validate( opts, odtypes, arguments[ 1 ] );
|
|
157
179
|
if ( err ) {
|
|
158
180
|
throw err;
|
|
159
181
|
}
|
|
160
182
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
183
|
+
xsh = getShape( x );
|
|
184
|
+
ord = getOrder( x );
|
|
185
|
+
|
|
186
|
+
// Initialize an output array:
|
|
187
|
+
ydt = opts.dtype || unaryOutputDataType( xdt, POLICIES.output );
|
|
188
|
+
y = empty( xsh, {
|
|
189
|
+
'dtype': ydt,
|
|
190
|
+
'order': opts.order || ord
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// Determine whether we need to cast the input ndarray...
|
|
194
|
+
dt = unaryInputCastingDataType( xdt, ydt, POLICIES.casting );
|
|
195
|
+
if ( xdt !== dt ) {
|
|
196
|
+
// TODO: replace the following logic with a call to `ndarray/base/(?maybe-)(cast|convert|copy)` or similar utility
|
|
197
|
+
tmp = baseEmpty( dt, xsh, ord );
|
|
198
|
+
baseAssign( [ x, tmp ] );
|
|
199
|
+
x = tmp;
|
|
177
200
|
}
|
|
178
|
-
|
|
201
|
+
fcn( x, y );
|
|
202
|
+
return y;
|
|
179
203
|
}
|
|
180
204
|
|
|
181
205
|
/**
|
|
182
|
-
*
|
|
206
|
+
* Performs element-wise computation and assigns results to a provided output ndarray.
|
|
183
207
|
*
|
|
184
208
|
* @private
|
|
185
|
-
* @param {
|
|
186
|
-
* @param {
|
|
187
|
-
* @throws {TypeError} first argument must be
|
|
188
|
-
* @throws {TypeError}
|
|
189
|
-
* @throws {TypeError}
|
|
190
|
-
* @throws {RangeError} output array must have sufficient elements
|
|
209
|
+
* @param {ndarray} x - input array
|
|
210
|
+
* @param {ndarray} y - output array
|
|
211
|
+
* @throws {TypeError} first argument must be an ndarray
|
|
212
|
+
* @throws {TypeError} first argument must have a supported data type
|
|
213
|
+
* @throws {TypeError} second argument must be an ndarray
|
|
191
214
|
* @throws {Error} unable to broadcast the input array against the output array
|
|
192
|
-
* @returns {
|
|
215
|
+
* @returns {ndarray} output array
|
|
193
216
|
*/
|
|
194
217
|
function assign( x, y ) {
|
|
195
|
-
var
|
|
196
|
-
var
|
|
197
|
-
var
|
|
198
|
-
if ( isndarrayLike( x ) ) {
|
|
199
|
-
if ( isndarrayLike( y ) ) {
|
|
200
|
-
xsh = x.shape;
|
|
201
|
-
ysh = y.shape;
|
|
218
|
+
var xdt;
|
|
219
|
+
var tmp;
|
|
220
|
+
var dt;
|
|
202
221
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
for ( i = 0; i < xsh.length; i++ ) {
|
|
206
|
-
// Check whether dimensions match...
|
|
207
|
-
if ( xsh[ i ] !== ysh[ i ] ) {
|
|
208
|
-
// We found a mismatched dimension; delegate to `broadcast` to ensure that `x` is broadcast compatible with the output array shape...
|
|
209
|
-
x = broadcast( x, ysh );
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
} else {
|
|
214
|
-
// If we are provided arrays with different ranks (i.e., number of dimensions), assume we need to broadcast, delegating to `broadcast` to ensure that `x` is broadcast compatible with the output array shape...
|
|
215
|
-
x = broadcast( x, ysh );
|
|
216
|
-
}
|
|
217
|
-
t.ndarray( x, y );
|
|
218
|
-
return y;
|
|
219
|
-
}
|
|
220
|
-
throw new TypeError( 'invalid argument. If the first argument is an ndarray, the second argument must be an ndarray.' );
|
|
222
|
+
if ( !isndarrayLike( x ) ) {
|
|
223
|
+
throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) );
|
|
221
224
|
}
|
|
222
|
-
if (
|
|
223
|
-
|
|
224
|
-
if ( y.length !== x.length ) {
|
|
225
|
-
throw new RangeError( 'invalid argument. Output array must have the same number of elements (i.e., length) as the input array.' );
|
|
226
|
-
}
|
|
227
|
-
t.array( x.length, dtype( x ) || 'generic', x, 1, dtype( y ) || 'generic', y, 1 );
|
|
228
|
-
return y;
|
|
229
|
-
}
|
|
230
|
-
throw new TypeError( 'invalid argument. If the first argument is an array-like object, the second argument must be an array-like object.' );
|
|
225
|
+
if ( !isndarrayLike( y ) ) {
|
|
226
|
+
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray. Value: `%s`.', y ) );
|
|
231
227
|
}
|
|
232
|
-
|
|
233
|
-
|
|
228
|
+
// Validate the input ndarray data type in order to maintain similar behavior to above...
|
|
229
|
+
xdt = getDType( x );
|
|
230
|
+
if ( !contains( idtypes[ 0 ], xdt ) ) {
|
|
231
|
+
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( idtypes[ 0 ], '", "' ), xdt ) );
|
|
234
232
|
}
|
|
235
|
-
|
|
236
|
-
|
|
233
|
+
// Determine whether we need to cast the input ndarray...
|
|
234
|
+
dt = unaryInputCastingDataType( xdt, getDType( y ), POLICIES.casting );
|
|
235
|
+
if ( xdt !== dt ) {
|
|
236
|
+
// TODO: replace the following logic with a call to `ndarray/base/(?maybe-)(cast|convert|copy)` or similar utility
|
|
237
|
+
tmp = baseEmpty( dt, getShape( x ), getOrder( x ) );
|
|
238
|
+
baseAssign( [ x, tmp ] );
|
|
239
|
+
x = tmp;
|
|
237
240
|
}
|
|
238
|
-
|
|
241
|
+
fcn( maybeBroadcastArray( x, getShape( y ) ), y );
|
|
242
|
+
return y;
|
|
239
243
|
}
|
|
240
244
|
}
|
|
241
245
|
|
|
242
246
|
|
|
243
247
|
// EXPORTS //
|
|
244
248
|
|
|
245
|
-
module.exports =
|
|
249
|
+
module.exports = factory;
|
package/lib/validate.js
CHANGED
|
@@ -22,18 +22,13 @@
|
|
|
22
22
|
|
|
23
23
|
var isPlainObject = require( '@stdlib/assert-is-plain-object' );
|
|
24
24
|
var hasOwnProp = require( '@stdlib/assert-has-own-property' );
|
|
25
|
-
var
|
|
25
|
+
var isOrder = require( '@stdlib/ndarray-base-assert-is-order' );
|
|
26
|
+
var contains = require( '@stdlib/array-base-assert-contains' );
|
|
26
27
|
var orders = require( '@stdlib/ndarray-orders' );
|
|
27
|
-
var
|
|
28
|
+
var join = require( '@stdlib/array-base-join' );
|
|
28
29
|
var format = require( '@stdlib/string-format' );
|
|
29
30
|
|
|
30
31
|
|
|
31
|
-
// VARIABLES //
|
|
32
|
-
|
|
33
|
-
var ORDERS = orders();
|
|
34
|
-
var DTYPES = dtypes();
|
|
35
|
-
|
|
36
|
-
|
|
37
32
|
// MAIN //
|
|
38
33
|
|
|
39
34
|
/**
|
|
@@ -41,35 +36,38 @@ var DTYPES = dtypes();
|
|
|
41
36
|
*
|
|
42
37
|
* @private
|
|
43
38
|
* @param {Object} opts - destination object
|
|
39
|
+
* @param {Array} dtypes - list of supported output data types
|
|
44
40
|
* @param {Object} options - options
|
|
45
|
-
* @param {
|
|
41
|
+
* @param {*} [options.dtype] - output array data type
|
|
46
42
|
* @param {string} [options.order] - output array order
|
|
47
43
|
* @returns {(Error|null)} null or an error object
|
|
48
44
|
*
|
|
49
45
|
* @example
|
|
46
|
+
* var dtypes = [ 'float64', 'float32', 'generic' ];
|
|
47
|
+
*
|
|
50
48
|
* var opts = {};
|
|
51
49
|
* var options = {
|
|
52
50
|
* 'order': 'row-major'
|
|
53
51
|
* };
|
|
54
|
-
* var err = validate( opts, options );
|
|
52
|
+
* var err = validate( opts, dtypes, options );
|
|
55
53
|
* if ( err ) {
|
|
56
54
|
* throw err;
|
|
57
55
|
* }
|
|
58
56
|
*/
|
|
59
|
-
function validate( opts, options ) {
|
|
57
|
+
function validate( opts, dtypes, options ) {
|
|
60
58
|
if ( !isPlainObject( options ) ) {
|
|
61
59
|
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
|
|
62
60
|
}
|
|
63
61
|
if ( hasOwnProp( options, 'dtype' ) ) {
|
|
64
62
|
opts.dtype = options.dtype;
|
|
65
|
-
if ( !contains(
|
|
66
|
-
return new TypeError( format( 'invalid option. `%s` option must be
|
|
63
|
+
if ( !contains( dtypes, String( opts.dtype ) ) ) {
|
|
64
|
+
return new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'dtype', join( dtypes, '", "' ), opts.dtype ) );
|
|
67
65
|
}
|
|
68
66
|
}
|
|
69
67
|
if ( hasOwnProp( options, 'order' ) ) {
|
|
70
68
|
opts.order = options.order;
|
|
71
|
-
if ( !
|
|
72
|
-
return new TypeError( format( 'invalid option. `%s` option must be
|
|
69
|
+
if ( !isOrder( opts.order ) ) {
|
|
70
|
+
return new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'order', join( orders(), '", "' ), opts.order ) );
|
|
73
71
|
}
|
|
74
72
|
}
|
|
75
73
|
return null;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/math-tools-unary",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Create a function which performs element-wise computation by applying a unary function to each element in an input ndarray.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "The Stdlib Authors",
|
|
@@ -30,27 +30,31 @@
|
|
|
30
30
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@stdlib/array-base-
|
|
34
|
-
"@stdlib/
|
|
33
|
+
"@stdlib/array-base-assert-contains": "^0.2.2",
|
|
34
|
+
"@stdlib/array-base-every-by": "^0.2.2",
|
|
35
|
+
"@stdlib/array-base-join": "^0.1.1",
|
|
35
36
|
"@stdlib/assert-has-own-property": "^0.2.2",
|
|
36
37
|
"@stdlib/assert-is-collection": "^0.2.2",
|
|
37
|
-
"@stdlib/assert-is-complex-like": "^0.2.2",
|
|
38
38
|
"@stdlib/assert-is-function": "^0.2.2",
|
|
39
39
|
"@stdlib/assert-is-ndarray-like": "^0.2.2",
|
|
40
|
-
"@stdlib/assert-is-
|
|
41
|
-
"@stdlib/assert-is-number": "^0.2.2",
|
|
40
|
+
"@stdlib/assert-is-object": "^0.2.2",
|
|
42
41
|
"@stdlib/assert-is-plain-object": "^0.2.2",
|
|
43
|
-
"@stdlib/ndarray-base-
|
|
44
|
-
"@stdlib/ndarray-base-
|
|
45
|
-
"@stdlib/ndarray-base-
|
|
46
|
-
"@stdlib/ndarray-base-
|
|
47
|
-
"@stdlib/ndarray-base-
|
|
48
|
-
"@stdlib/ndarray-
|
|
49
|
-
"@stdlib/ndarray-
|
|
42
|
+
"@stdlib/ndarray-base-assert-is-data-type": "^0.3.0",
|
|
43
|
+
"@stdlib/ndarray-base-assert-is-input-casting-policy": "^0.1.0",
|
|
44
|
+
"@stdlib/ndarray-base-assert-is-order": "^0.2.2",
|
|
45
|
+
"@stdlib/ndarray-base-assert-is-output-data-type-policy": "^0.1.0",
|
|
46
|
+
"@stdlib/ndarray-base-assign": "^0.1.1",
|
|
47
|
+
"@stdlib/ndarray-base-empty": "^0.3.0",
|
|
48
|
+
"@stdlib/ndarray-base-unary-input-casting-dtype": "^0.1.0",
|
|
49
|
+
"@stdlib/ndarray-base-unary-output-dtype": "^0.3.0",
|
|
50
|
+
"@stdlib/ndarray-dtype": "^0.2.2",
|
|
51
|
+
"@stdlib/ndarray-empty": "^0.3.0",
|
|
52
|
+
"@stdlib/ndarray-maybe-broadcast-array": "^0.2.2",
|
|
53
|
+
"@stdlib/ndarray-order": "^0.2.2",
|
|
50
54
|
"@stdlib/ndarray-orders": "^0.2.2",
|
|
55
|
+
"@stdlib/ndarray-shape": "^0.2.2",
|
|
51
56
|
"@stdlib/string-format": "^0.2.2",
|
|
52
57
|
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
|
|
53
|
-
"@stdlib/utils-keys": "^0.2.2",
|
|
54
58
|
"@stdlib/error-tools-fmtprodmsg": "^0.2.2"
|
|
55
59
|
},
|
|
56
60
|
"devDependencies": {},
|
|
@@ -80,9 +84,9 @@
|
|
|
80
84
|
"dispatch",
|
|
81
85
|
"multimethod",
|
|
82
86
|
"multimethods",
|
|
83
|
-
"ndarray"
|
|
87
|
+
"ndarray",
|
|
88
|
+
"ufunc"
|
|
84
89
|
],
|
|
85
|
-
"__stdlib__": {},
|
|
86
90
|
"funding": {
|
|
87
91
|
"type": "opencollective",
|
|
88
92
|
"url": "https://opencollective.com/stdlib"
|
package/lib/defaults.json
DELETED
package/lib/ndarray.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Apache-2.0
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
-
*
|
|
6
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
-
* you may not use this file except in compliance with the License.
|
|
8
|
-
* You may obtain a copy of the License at
|
|
9
|
-
*
|
|
10
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
-
*
|
|
12
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
-
* See the License for the specific language governing permissions and
|
|
16
|
-
* limitations under the License.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
'use strict';
|
|
20
|
-
|
|
21
|
-
// MODULES //
|
|
22
|
-
|
|
23
|
-
var ndarray = require( '@stdlib/ndarray-ctor' );
|
|
24
|
-
var buffer = require( '@stdlib/ndarray-base-buffer' );
|
|
25
|
-
var shape2strides = require( '@stdlib/ndarray-base-shape2strides' );
|
|
26
|
-
var numel = require( '@stdlib/ndarray-base-numel' );
|
|
27
|
-
var copy = require( '@stdlib/array-base-copy-indexed' );
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// MAIN //
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Applies a function to an ndarray.
|
|
34
|
-
*
|
|
35
|
-
* @private
|
|
36
|
-
* @param {Function} fcn - function to apply
|
|
37
|
-
* @param {ndarray} x - input array
|
|
38
|
-
* @param {string} ydtype - output array data type
|
|
39
|
-
* @param {string} yorder - output array order
|
|
40
|
-
* @returns {ndarray} output array
|
|
41
|
-
*/
|
|
42
|
-
function ndarrayfcn( fcn, x, ydtype, yorder ) {
|
|
43
|
-
var shape;
|
|
44
|
-
var buf;
|
|
45
|
-
var y;
|
|
46
|
-
|
|
47
|
-
// Check if we were provided a zero-dimensional array...
|
|
48
|
-
shape = copy( x.shape ); // Note: we need to copy the shape to avoid a shared shape object between `x` and `y` which could lead to unintended mutations (e.g., if either `x` or `y` is reshaped)
|
|
49
|
-
if ( shape.length === 0 ) {
|
|
50
|
-
buf = buffer( ydtype, 1 );
|
|
51
|
-
y = ndarray( ydtype, buf, [], [ 0 ], 0, yorder );
|
|
52
|
-
} else {
|
|
53
|
-
buf = buffer( ydtype, x.length || numel( shape ) ); // WARNING: `x.length` is a property found on ndarray instances, but not strictly necessary to describe an ndarray; accordingly, used here to avoid unnecessary computation, but a potential source of bugs if provided an ndarray-like object having a `length` property which is not equal to the product of the dimensions.
|
|
54
|
-
y = ndarray( ydtype, buf, shape, shape2strides( shape, yorder ), 0, yorder ); // eslint-disable-line max-len
|
|
55
|
-
}
|
|
56
|
-
fcn( x, y );
|
|
57
|
-
return y;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
// EXPORTS //
|
|
62
|
-
|
|
63
|
-
module.exports = ndarrayfcn;
|