@stdlib/math-tools-unary 0.0.5 → 0.1.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/CITATION.cff +30 -0
- package/LICENSE +0 -304
- package/NOTICE +1 -1
- package/README.md +36 -5
- package/dist/index.d.ts +3 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +1 -1
- package/lib/defaults.json +1 -1
- package/lib/main.js +8 -10
- package/lib/ndarray.js +1 -22
- package/lib/policies.json +3 -1
- package/lib/resolve_output_dtype.js +29 -2
- package/lib/validate.js +4 -3
- package/lib/validate_options.js +3 -2
- package/lib/validate_table.js +3 -2
- package/package.json +39 -39
- package/docs/repl.txt +0 -119
- package/docs/types/test.ts +0 -288
package/lib/ndarray.js
CHANGED
|
@@ -24,27 +24,7 @@ var ndarray = require( '@stdlib/ndarray-ctor' );
|
|
|
24
24
|
var buffer = require( '@stdlib/ndarray-base-buffer' );
|
|
25
25
|
var shape2strides = require( '@stdlib/ndarray-base-shape2strides' );
|
|
26
26
|
var numel = require( '@stdlib/ndarray-base-numel' );
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
// FUNCTIONS //
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Copies an array-like object to a generic array.
|
|
33
|
-
*
|
|
34
|
-
* @private
|
|
35
|
-
* @param {ArrayLikeObject} x - input array
|
|
36
|
-
* @returns {Array} output array
|
|
37
|
-
*/
|
|
38
|
-
function copy( x ) {
|
|
39
|
-
var out;
|
|
40
|
-
var i;
|
|
41
|
-
|
|
42
|
-
out = [];
|
|
43
|
-
for ( i = 0; i < x.length; i++ ) {
|
|
44
|
-
out.push( x[ i ] );
|
|
45
|
-
}
|
|
46
|
-
return out;
|
|
47
|
-
}
|
|
27
|
+
var copy = require( '@stdlib/array-base-copy-indexed' );
|
|
48
28
|
|
|
49
29
|
|
|
50
30
|
// MAIN //
|
|
@@ -57,7 +37,6 @@ function copy( x ) {
|
|
|
57
37
|
* @param {ndarray} x - input array
|
|
58
38
|
* @param {string} ydtype - output array data type
|
|
59
39
|
* @param {string} yorder - output array order
|
|
60
|
-
* @throws {TypeError} must provide an input array argument with a supported data type
|
|
61
40
|
* @returns {ndarray} output array
|
|
62
41
|
*/
|
|
63
42
|
function ndarrayfcn( fcn, x, ydtype, yorder ) {
|
package/lib/policies.json
CHANGED
|
@@ -18,6 +18,13 @@
|
|
|
18
18
|
|
|
19
19
|
'use strict';
|
|
20
20
|
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var format = require( '@stdlib/string-format' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
21
28
|
/**
|
|
22
29
|
* Resolves an output array data type.
|
|
23
30
|
*
|
|
@@ -31,7 +38,19 @@ function resolve( xdtype, policy ) {
|
|
|
31
38
|
switch ( policy ) {
|
|
32
39
|
case 'same':
|
|
33
40
|
return xdtype;
|
|
34
|
-
case '
|
|
41
|
+
case 'floating-point':
|
|
42
|
+
// TODO: we may want to delegate checking for a floating-point dtype to a utility function/package (e.g., isFloatDtype), in order to centralize logic for testing whether a dtype is "floating-point". Otherwise, this will be yet another place to update logic should we ever add, e.g., a `float128` or `float16` dtype.
|
|
43
|
+
if (
|
|
44
|
+
xdtype === 'float64' ||
|
|
45
|
+
xdtype === 'float32' ||
|
|
46
|
+
xdtype === 'generic' ||
|
|
47
|
+
xdtype === 'complex128' ||
|
|
48
|
+
xdtype === 'complex64'
|
|
49
|
+
) {
|
|
50
|
+
return xdtype;
|
|
51
|
+
}
|
|
52
|
+
return 'float64'; // TODO: constants/math/default-real-floating-point-dtype?
|
|
53
|
+
case 'real floating-point':
|
|
35
54
|
if (
|
|
36
55
|
xdtype === 'float64' ||
|
|
37
56
|
xdtype === 'float32' ||
|
|
@@ -40,8 +59,16 @@ function resolve( xdtype, policy ) {
|
|
|
40
59
|
return xdtype;
|
|
41
60
|
}
|
|
42
61
|
return 'float64';
|
|
62
|
+
case 'complex floating-point':
|
|
63
|
+
if (
|
|
64
|
+
xdtype === 'complex128' ||
|
|
65
|
+
xdtype === 'complex64'
|
|
66
|
+
) {
|
|
67
|
+
return xdtype;
|
|
68
|
+
}
|
|
69
|
+
return 'complex128'; // TODO: constants/math/default-complex-floating-point-dtype?
|
|
43
70
|
default:
|
|
44
|
-
throw new Error( 'invalid option. Unsupported policy for determining an output array data type. Option:
|
|
71
|
+
throw new Error( format( 'invalid option. Unsupported policy for determining an output array data type. Option: `%s`.', policy ) );
|
|
45
72
|
}
|
|
46
73
|
}
|
|
47
74
|
|
package/lib/validate.js
CHANGED
|
@@ -25,6 +25,7 @@ var hasOwnProp = require( '@stdlib/assert-has-own-property' );
|
|
|
25
25
|
var contains = require( '@stdlib/assert-contains' );
|
|
26
26
|
var orders = require( '@stdlib/ndarray-orders' );
|
|
27
27
|
var dtypes = require( '@stdlib/ndarray-dtypes' );
|
|
28
|
+
var format = require( '@stdlib/string-format' );
|
|
28
29
|
|
|
29
30
|
|
|
30
31
|
// VARIABLES //
|
|
@@ -57,18 +58,18 @@ var DTYPES = dtypes();
|
|
|
57
58
|
*/
|
|
58
59
|
function validate( opts, options ) {
|
|
59
60
|
if ( !isPlainObject( options ) ) {
|
|
60
|
-
return new TypeError( 'invalid argument. Options argument must be
|
|
61
|
+
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
|
|
61
62
|
}
|
|
62
63
|
if ( hasOwnProp( options, 'dtype' ) ) {
|
|
63
64
|
opts.dtype = options.dtype;
|
|
64
65
|
if ( !contains( DTYPES, opts.dtype ) ) {
|
|
65
|
-
return new TypeError( 'invalid option. `
|
|
66
|
+
return new TypeError( format( 'invalid option. `%s` option must be a recognized/supported data type. Option: `%s`.', 'dtype', opts.dtype ) );
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
if ( hasOwnProp( options, 'order' ) ) {
|
|
69
70
|
opts.order = options.order;
|
|
70
71
|
if ( !contains( ORDERS, opts.order ) ) {
|
|
71
|
-
return new TypeError( 'invalid option. `
|
|
72
|
+
return new TypeError( format( 'invalid option. `%s` option must be a recognized/supported data type. Option: `%s`.', 'order', opts.order ) );
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
75
|
return null;
|
package/lib/validate_options.js
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
var isPlainObject = require( '@stdlib/assert-is-plain-object' );
|
|
24
24
|
var hasOwnProp = require( '@stdlib/assert-has-own-property' );
|
|
25
25
|
var contains = require( '@stdlib/assert-contains' );
|
|
26
|
+
var format = require( '@stdlib/string-format' );
|
|
26
27
|
var POLICIES = require( './policies.json' );
|
|
27
28
|
|
|
28
29
|
|
|
@@ -49,12 +50,12 @@ var POLICIES = require( './policies.json' );
|
|
|
49
50
|
*/
|
|
50
51
|
function validate( opts, options ) {
|
|
51
52
|
if ( !isPlainObject( options ) ) {
|
|
52
|
-
return new TypeError( 'invalid argument. Options argument must be
|
|
53
|
+
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
|
|
53
54
|
}
|
|
54
55
|
if ( hasOwnProp( options, 'output_dtype_policy' ) ) {
|
|
55
56
|
opts.policy = options.output_dtype_policy;
|
|
56
57
|
if ( !contains( POLICIES, opts.policy ) ) {
|
|
57
|
-
return new TypeError( 'invalid option. `
|
|
58
|
+
return new TypeError( format( 'invalid option. `%s` option must be a recognized/supported output array data type policy. Option: `%s`.', 'output_dtype_policy', opts.policy ) );
|
|
58
59
|
}
|
|
59
60
|
}
|
|
60
61
|
return null;
|
package/lib/validate_table.js
CHANGED
|
@@ -25,6 +25,7 @@ var hasOwnProp = require( '@stdlib/assert-has-own-property' );
|
|
|
25
25
|
var isFunction = require( '@stdlib/assert-is-function' );
|
|
26
26
|
var isNull = require( '@stdlib/assert-is-null' );
|
|
27
27
|
var objectKeys = require( '@stdlib/utils-keys' );
|
|
28
|
+
var format = require( '@stdlib/string-format' );
|
|
28
29
|
|
|
29
30
|
|
|
30
31
|
// MAIN //
|
|
@@ -61,7 +62,7 @@ function validate( out, table ) {
|
|
|
61
62
|
var i;
|
|
62
63
|
|
|
63
64
|
if ( !isPlainObject( table ) ) {
|
|
64
|
-
return new TypeError( 'invalid argument. Resolution table must be
|
|
65
|
+
return new TypeError( format( 'invalid argument. Resolution table must be an object. Value: `%s`.', table ) );
|
|
65
66
|
}
|
|
66
67
|
fields = objectKeys( out );
|
|
67
68
|
for ( i = 0; i < fields.length; i++ ) {
|
|
@@ -69,7 +70,7 @@ function validate( out, table ) {
|
|
|
69
70
|
if ( hasOwnProp( table, key ) ) {
|
|
70
71
|
tmp = table[ key ];
|
|
71
72
|
if ( !isFunction( tmp ) && !isNull( tmp ) ) {
|
|
72
|
-
return new TypeError( 'invalid argument. Resolution table `
|
|
73
|
+
return new TypeError( format( 'invalid argument. Resolution table `%s` field value must be either a function or null. Value: `%s`.', key, tmp ) );
|
|
73
74
|
}
|
|
74
75
|
out[ key ] = tmp;
|
|
75
76
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/math-tools-unary",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Multiple dispatch for unary mathematical functions.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -37,46 +37,46 @@
|
|
|
37
37
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@stdlib/
|
|
41
|
-
"@stdlib/assert-
|
|
42
|
-
"@stdlib/assert-
|
|
43
|
-
"@stdlib/assert-is-
|
|
44
|
-
"@stdlib/assert-is-
|
|
45
|
-
"@stdlib/assert-is-
|
|
46
|
-
"@stdlib/assert-is-
|
|
47
|
-
"@stdlib/assert-is-
|
|
48
|
-
"@stdlib/assert-is-
|
|
49
|
-
"@stdlib/
|
|
50
|
-
"@stdlib/ndarray-base-
|
|
51
|
-
"@stdlib/ndarray-base-buffer
|
|
52
|
-
"@stdlib/ndarray-base-
|
|
53
|
-
"@stdlib/ndarray-base-
|
|
54
|
-
"@stdlib/ndarray-
|
|
55
|
-
"@stdlib/ndarray-
|
|
56
|
-
"@stdlib/ndarray-
|
|
57
|
-
"@stdlib/
|
|
58
|
-
"@stdlib/
|
|
59
|
-
"@stdlib/
|
|
40
|
+
"@stdlib/array-base-copy-indexed": "^0.1.0",
|
|
41
|
+
"@stdlib/assert-contains": "^0.1.0",
|
|
42
|
+
"@stdlib/assert-has-own-property": "^0.1.0",
|
|
43
|
+
"@stdlib/assert-is-collection": "^0.1.0",
|
|
44
|
+
"@stdlib/assert-is-complex-like": "^0.1.0",
|
|
45
|
+
"@stdlib/assert-is-function": "^0.1.0",
|
|
46
|
+
"@stdlib/assert-is-ndarray-like": "^0.1.0",
|
|
47
|
+
"@stdlib/assert-is-null": "^0.1.0",
|
|
48
|
+
"@stdlib/assert-is-number": "^0.1.0",
|
|
49
|
+
"@stdlib/assert-is-plain-object": "^0.1.0",
|
|
50
|
+
"@stdlib/ndarray-base-broadcast-array": "^0.1.0",
|
|
51
|
+
"@stdlib/ndarray-base-buffer": "^0.1.0",
|
|
52
|
+
"@stdlib/ndarray-base-buffer-dtype": "^0.1.0",
|
|
53
|
+
"@stdlib/ndarray-base-numel": "^0.1.0",
|
|
54
|
+
"@stdlib/ndarray-base-shape2strides": "^0.1.0",
|
|
55
|
+
"@stdlib/ndarray-ctor": "^0.1.0",
|
|
56
|
+
"@stdlib/ndarray-dtypes": "^0.1.0",
|
|
57
|
+
"@stdlib/ndarray-orders": "^0.1.0",
|
|
58
|
+
"@stdlib/string-format": "^0.1.0",
|
|
59
|
+
"@stdlib/types": "^0.1.0",
|
|
60
|
+
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.0",
|
|
61
|
+
"@stdlib/utils-keys": "^0.1.0",
|
|
62
|
+
"@stdlib/error-tools-fmtprodmsg": "^0.1.0"
|
|
60
63
|
},
|
|
61
64
|
"devDependencies": {
|
|
62
|
-
"@stdlib/array-float32": "^0.0
|
|
63
|
-
"@stdlib/array-float64": "^0.0
|
|
64
|
-
"@stdlib/
|
|
65
|
-
"@stdlib/
|
|
66
|
-
"@stdlib/math-base-
|
|
67
|
-
"@stdlib/math-base-special-
|
|
68
|
-
"@stdlib/math-
|
|
69
|
-
"@stdlib/
|
|
70
|
-
"@stdlib/
|
|
71
|
-
"@stdlib/
|
|
72
|
-
"@stdlib/ndarray-
|
|
73
|
-
"@stdlib/
|
|
74
|
-
"@stdlib/ndarray-dispatch": "^0.0.x",
|
|
75
|
-
"@stdlib/ndarray-ind2sub": "^0.0.x",
|
|
76
|
-
"@stdlib/random-base-uniform": "^0.0.x",
|
|
65
|
+
"@stdlib/array-float32": "^0.1.0",
|
|
66
|
+
"@stdlib/array-float64": "^0.1.0",
|
|
67
|
+
"@stdlib/bench": "^0.1.0",
|
|
68
|
+
"@stdlib/math-base-assert-is-nan": "^0.1.0",
|
|
69
|
+
"@stdlib/math-base-special-abs": "^0.1.0",
|
|
70
|
+
"@stdlib/math-base-special-pow": "^0.1.0",
|
|
71
|
+
"@stdlib/math-strided-special-abs": "^0.1.0",
|
|
72
|
+
"@stdlib/ndarray-array": "^0.0.9",
|
|
73
|
+
"@stdlib/ndarray-base-unary": "^0.1.0",
|
|
74
|
+
"@stdlib/ndarray-dispatch": "^0.1.0",
|
|
75
|
+
"@stdlib/ndarray-ind2sub": "^0.1.0",
|
|
76
|
+
"@stdlib/random-base-uniform": "^0.0.6",
|
|
77
77
|
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
|
|
78
78
|
"istanbul": "^0.4.1",
|
|
79
|
-
"tap-
|
|
79
|
+
"tap-min": "git+https://github.com/Planeshifter/tap-min.git"
|
|
80
80
|
},
|
|
81
81
|
"engines": {
|
|
82
82
|
"node": ">=0.10.0",
|
|
@@ -108,7 +108,7 @@
|
|
|
108
108
|
],
|
|
109
109
|
"__stdlib__": {},
|
|
110
110
|
"funding": {
|
|
111
|
-
"type": "
|
|
112
|
-
"url": "https://
|
|
111
|
+
"type": "opencollective",
|
|
112
|
+
"url": "https://opencollective.com/stdlib"
|
|
113
113
|
}
|
|
114
114
|
}
|
package/docs/repl.txt
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
{{alias}}( table )
|
|
3
|
-
Returns a function which dispatches to specified functions based on input
|
|
4
|
-
argument types.
|
|
5
|
-
|
|
6
|
-
A `table` resolution object may contain one or more of the following fields:
|
|
7
|
-
|
|
8
|
-
- scalar: strided look-up table for scalar arguments.
|
|
9
|
-
- array: strided look-up table for array-like object arguments.
|
|
10
|
-
- ndarray: strided look-up table for ndarray arguments.
|
|
11
|
-
|
|
12
|
-
Each strided look-up table should be comprised as follows:
|
|
13
|
-
|
|
14
|
-
[ <dtype>, <fcn>, <dtype>, <fcn>, ... ]
|
|
15
|
-
|
|
16
|
-
If an argument's data type is *not* found in the argument's corresponding
|
|
17
|
-
look-up table and if a 'generic' data type is present in that same table,
|
|
18
|
-
the returned dispatch function will resolve the "generic" implementation. In
|
|
19
|
-
other words, an implementation associated with a 'generic' data type will be
|
|
20
|
-
treated as the default implementation.
|
|
21
|
-
|
|
22
|
-
If unable to resolve an implementation for a provided argument data type,
|
|
23
|
-
the returned function throws an error.
|
|
24
|
-
|
|
25
|
-
If provided a number, the returned function returns a number.
|
|
26
|
-
|
|
27
|
-
If provided an ndarray or array-like object, the returned function performs
|
|
28
|
-
element-wise computation.
|
|
29
|
-
|
|
30
|
-
If provided an array-like object, the returned function returns an array-
|
|
31
|
-
like object having the same length and data type as the provided input
|
|
32
|
-
argument.
|
|
33
|
-
|
|
34
|
-
If provided an ndarray, the returned function returns an ndarray having the
|
|
35
|
-
same shape and data type as the provided input argument.
|
|
36
|
-
|
|
37
|
-
Parameters
|
|
38
|
-
----------
|
|
39
|
-
table: Object
|
|
40
|
-
Table resolution object.
|
|
41
|
-
|
|
42
|
-
table.scalar: ArrayLikeObject (optional)
|
|
43
|
-
Strided look-up table for scalar arguments. Supported data types:
|
|
44
|
-
'number' and 'complex'.
|
|
45
|
-
|
|
46
|
-
table.array: ArrayLikeObject (optional)
|
|
47
|
-
Strided look-up table for array-like object arguments. Implementation
|
|
48
|
-
functions must follow strided array interface argument conventions:
|
|
49
|
-
|
|
50
|
-
fcn( N, x, strideX, y, strideY )
|
|
51
|
-
|
|
52
|
-
where
|
|
53
|
-
|
|
54
|
-
- N: number of indexed elements.
|
|
55
|
-
- x: input strided array.
|
|
56
|
-
- strideX: index increment for `x`.
|
|
57
|
-
- y: destination strided array.
|
|
58
|
-
- strideY: index increment for `y`.
|
|
59
|
-
|
|
60
|
-
Supported array data types consist of all supported ndarray data types.
|
|
61
|
-
|
|
62
|
-
table.ndarray: ArrayLikeObject (optional)
|
|
63
|
-
Strided look-up table for ndarray arguments. Implementation functions
|
|
64
|
-
must follow strided array ndarray interface argument conventions:
|
|
65
|
-
|
|
66
|
-
fcn( N, x, strideX, offsetX, y, strideY, offsetY )
|
|
67
|
-
|
|
68
|
-
where
|
|
69
|
-
|
|
70
|
-
- N: number of indexed elements.
|
|
71
|
-
- x: input strided array (i.e., underlying input ndarray buffer).
|
|
72
|
-
- strideX: index increment for `x`.
|
|
73
|
-
- offsetX: starting index for `x`.
|
|
74
|
-
- y: destination strided array (i.e., underlying output ndarray buffer).
|
|
75
|
-
- strideY: index increment for `y`.
|
|
76
|
-
- offsetY: starting index for `y`.
|
|
77
|
-
|
|
78
|
-
Supported data types consist of all supported ndarray data types.
|
|
79
|
-
|
|
80
|
-
Returns
|
|
81
|
-
-------
|
|
82
|
-
fcn: Function
|
|
83
|
-
Dispatch function.
|
|
84
|
-
|
|
85
|
-
Examples
|
|
86
|
-
--------
|
|
87
|
-
> var t = {};
|
|
88
|
-
> t.scalar = [ 'number', {{alias:@stdlib/math/base/special/abs}} ];
|
|
89
|
-
> t.array = [
|
|
90
|
-
... 'float64', {{alias:@stdlib/math/strided/special/dabs}},
|
|
91
|
-
... 'float32', {{alias:@stdlib/math/strided/special/sabs}},
|
|
92
|
-
... 'generic', {{alias:@stdlib/math/strided/special/abs}}
|
|
93
|
-
... ];
|
|
94
|
-
> t.ndarray = [
|
|
95
|
-
... 'float64', {{alias:@stdlib/math/strided/special/dabs}}.ndarray,
|
|
96
|
-
... 'float32', {{alias:@stdlib/math/strided/special/sabs}}.ndarray,
|
|
97
|
-
... 'generic', {{alias:@stdlib/math/strided/special/abs}}.ndarray
|
|
98
|
-
... ];
|
|
99
|
-
> var fcn = {{alias}}( t );
|
|
100
|
-
|
|
101
|
-
// Provide a number:
|
|
102
|
-
> var y = fcn( -1.0 )
|
|
103
|
-
1.0
|
|
104
|
-
|
|
105
|
-
// Provide an array-like object:
|
|
106
|
-
> var x = new {{alias:@stdlib/array/float64}}( [ -1.0, -2.0 ] );
|
|
107
|
-
> y = fcn( x )
|
|
108
|
-
<Float64Array>[ 1.0, 2.0 ]
|
|
109
|
-
|
|
110
|
-
// Provide an ndarray:
|
|
111
|
-
> x = {{alias:@stdlib/ndarray/array}}( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
|
|
112
|
-
> y = fcn( x )
|
|
113
|
-
<ndarray>
|
|
114
|
-
> y.get( 0, 1 )
|
|
115
|
-
2.0
|
|
116
|
-
|
|
117
|
-
See Also
|
|
118
|
-
--------
|
|
119
|
-
|