@stdlib/math-tools-unary 0.0.6 → 0.2.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/lib/main.js CHANGED
@@ -28,6 +28,7 @@ var isCollection = require( '@stdlib/assert-is-collection' );
28
28
  var dtype = require( '@stdlib/ndarray-base-buffer-dtype' );
29
29
  var buffer = require( '@stdlib/ndarray-base-buffer' );
30
30
  var broadcast = require( '@stdlib/ndarray-base-broadcast-array' );
31
+ var format = require( '@stdlib/string-format' );
31
32
  var ndarrayfcn = require( './ndarray.js' );
32
33
  var odtype = require( './resolve_output_dtype.js' );
33
34
  var defaults = require( './defaults.json' );
@@ -47,7 +48,7 @@ var validate = require( './validate.js' );
47
48
  * @param {(Function|null)} [table.array] - function to invoke upon receiving an array-like object
48
49
  * @param {(Function|null)} [table.ndarray] - function to invoke upon receiving an ndarray-like object
49
50
  * @param {Options} [options] - options
50
- * @param {string} [options.output_dtype_policy='float'] - policy for determining the output array data type
51
+ * @param {string} [options.output_dtype_policy='floating-point'] - policy for determining the output array data type
51
52
  * @throws {TypeError} first argument must be an object
52
53
  * @throws {TypeError} first argument must have valid table fields
53
54
  * @throws {Error} each table field value must be either a function or `null`
@@ -132,7 +133,7 @@ function dispatch( table, options ) {
132
133
  * @throws {TypeError} must provide valid options
133
134
  * @returns {(ndarray|Collection|number|Complex)} results
134
135
  */
135
- function dispatcher( x, options ) {
136
+ function dispatcher( x ) {
136
137
  var xdtype;
137
138
  var ydtype;
138
139
  var opts;
@@ -152,7 +153,7 @@ function dispatch( table, options ) {
152
153
  }
153
154
  opts = {};
154
155
  if ( arguments.length > 1 ) {
155
- err = validate( opts, options );
156
+ err = validate( opts, arguments[ 1 ] );
156
157
  if ( err ) {
157
158
  throw err;
158
159
  }
@@ -171,12 +172,10 @@ function dispatch( table, options ) {
171
172
  xdtype = dtype( x ) || 'generic';
172
173
  ydtype = opts.dtype || odtype( xdtype, OPTS.policy );
173
174
  y = buffer( ydtype, x.length );
174
-
175
- // FIXME: need to supply dtype enum argument for each array argument...
176
- t.array( x.length, x, 1, y, 1 );
175
+ t.array( x.length, xdtype, x, 1, ydtype, y, 1 );
177
176
  return y;
178
177
  }
179
- throw new TypeError( 'invalid argument. Must provide an argument having a supported data type. Value: `' + x + '`.' );
178
+ throw new TypeError( format( 'invalid argument. Must provide an argument having a supported data type. Value: `%s`.', x ) );
180
179
  }
181
180
 
182
181
  /**
@@ -225,8 +224,7 @@ function dispatch( table, options ) {
225
224
  if ( y.length !== x.length ) {
226
225
  throw new RangeError( 'invalid argument. Output array must have the same number of elements (i.e., length) as the input array.' );
227
226
  }
228
- // FIXME: need to supply dtype enum argument for each array argument...
229
- t.array( x.length, x, 1, y, 1 );
227
+ t.array( x.length, dtype( x ) || 'generic', x, 1, dtype( y ) || 'generic', y, 1 );
230
228
  return y;
231
229
  }
232
230
  throw new TypeError( 'invalid argument. If the first argument is an array-like object, the second argument must be an array-like object.' );
@@ -237,7 +235,7 @@ function dispatch( table, options ) {
237
235
  if ( isComplexLike( x ) ) {
238
236
  throw new TypeError( 'invalid argument. Providing a complex number is not supported. Consider providing a zero-dimensional ndarray containing the complex number value.' );
239
237
  }
240
- throw new TypeError( 'invalid argument. Must provide an argument having a supported data type. Value: `' + x + '`.' );
238
+ throw new TypeError( format( 'invalid argument. Must provide an argument having a supported data type. Value: `%s`.', x ) );
241
239
  }
242
240
  }
243
241
 
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
@@ -1,4 +1,6 @@
1
1
  [
2
2
  "same",
3
- "float"
3
+ "floating-point",
4
+ "real floating-point",
5
+ "complex floating-point"
4
6
  ]
@@ -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 'float':
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: `' + policy + '`.' );
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 a plain object. Value: `' + options + '`.' );
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. `dtype` option must be a recognized/supported data type. Option: `' + opts.dtype + '`.' );
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. `order` option must be a recognized/supported data type. Option: `' + opts.order + '`.' );
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;
@@ -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 a plain object. Value: `' + options + '`.' );
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. `output_dtype_policy` option must be a recognized/supported output array data type policy. Option: `' + opts.policy + '`.' );
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;
@@ -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 a plain object. Value: `' + table + '`.' );
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 `' + key + '` field value must be either a function or `null`. Value: `' + tmp + '`.' );
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.6",
3
+ "version": "0.2.0",
4
4
  "description": "Multiple dispatch for unary mathematical functions.",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
@@ -37,46 +37,47 @@
37
37
  "url": "https://github.com/stdlib-js/stdlib/issues"
38
38
  },
39
39
  "dependencies": {
40
- "@stdlib/assert-contains": "^0.0.x",
41
- "@stdlib/assert-has-own-property": "^0.0.x",
42
- "@stdlib/assert-is-collection": "^0.0.x",
43
- "@stdlib/assert-is-complex-like": "^0.0.x",
44
- "@stdlib/assert-is-function": "^0.0.x",
45
- "@stdlib/assert-is-ndarray-like": "^0.0.x",
46
- "@stdlib/assert-is-null": "^0.0.x",
47
- "@stdlib/assert-is-number": "^0.0.x",
48
- "@stdlib/assert-is-plain-object": "^0.0.x",
49
- "@stdlib/ndarray-base-broadcast-array": "^0.0.x",
50
- "@stdlib/ndarray-base-buffer": "^0.0.x",
51
- "@stdlib/ndarray-base-buffer-dtype": "^0.0.x",
52
- "@stdlib/ndarray-base-numel": "^0.0.x",
53
- "@stdlib/ndarray-base-shape2strides": "^0.0.x",
54
- "@stdlib/ndarray-ctor": "^0.0.x",
55
- "@stdlib/ndarray-dtypes": "^0.0.x",
56
- "@stdlib/ndarray-orders": "^0.0.x",
57
- "@stdlib/types": "^0.0.x",
58
- "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x",
59
- "@stdlib/utils-keys": "^0.0.x"
40
+ "@stdlib/array-base-copy-indexed": "^0.2.0",
41
+ "@stdlib/assert-contains": "^0.2.0",
42
+ "@stdlib/assert-has-own-property": "^0.2.0",
43
+ "@stdlib/assert-is-collection": "^0.2.0",
44
+ "@stdlib/assert-is-complex-like": "^0.2.0",
45
+ "@stdlib/assert-is-function": "^0.2.0",
46
+ "@stdlib/assert-is-ndarray-like": "^0.2.0",
47
+ "@stdlib/assert-is-null": "^0.2.0",
48
+ "@stdlib/assert-is-number": "^0.2.0",
49
+ "@stdlib/assert-is-plain-object": "^0.2.0",
50
+ "@stdlib/ndarray-base-broadcast-array": "^0.2.0",
51
+ "@stdlib/ndarray-base-buffer": "^0.2.0",
52
+ "@stdlib/ndarray-base-buffer-dtype": "^0.2.0",
53
+ "@stdlib/ndarray-base-numel": "^0.2.0",
54
+ "@stdlib/ndarray-base-shape2strides": "^0.2.0",
55
+ "@stdlib/ndarray-ctor": "^0.2.0",
56
+ "@stdlib/ndarray-dtypes": "^0.2.0",
57
+ "@stdlib/ndarray-orders": "^0.2.0",
58
+ "@stdlib/string-format": "^0.2.0",
59
+ "@stdlib/types": "^0.3.1",
60
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.0",
61
+ "@stdlib/utils-keys": "^0.2.0",
62
+ "@stdlib/error-tools-fmtprodmsg": "^0.2.0"
60
63
  },
61
64
  "devDependencies": {
62
- "@stdlib/array-float32": "^0.0.x",
63
- "@stdlib/array-float64": "^0.0.x",
64
- "@stdlib/array-int32": "^0.0.x",
65
- "@stdlib/bench": "^0.0.x",
66
- "@stdlib/math-base-assert-is-nan": "^0.0.x",
67
- "@stdlib/math-base-special-abs": "^0.0.x",
68
- "@stdlib/math-base-special-pow": "^0.0.x",
69
- "@stdlib/math-strided-special-abs": "^0.0.x",
70
- "@stdlib/math-strided-special-dabs": "^0.0.x",
71
- "@stdlib/math-strided-special-sabs": "^0.0.x",
72
- "@stdlib/ndarray-array": "^0.0.x",
73
- "@stdlib/ndarray-base-unary": "^0.0.x",
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.2.0",
66
+ "@stdlib/array-float64": "^0.2.0",
67
+ "@stdlib/math-base-assert-is-nan": "^0.2.0",
68
+ "@stdlib/math-base-special-abs": "^0.2.0",
69
+ "@stdlib/math-base-special-pow": "^0.2.0",
70
+ "@stdlib/math-strided-special-abs": "^0.2.0",
71
+ "@stdlib/ndarray-array": "^0.1.0",
72
+ "@stdlib/ndarray-base-unary": "^0.1.0",
73
+ "@stdlib/ndarray-dispatch": "^0.2.0",
74
+ "@stdlib/ndarray-ind2sub": "^0.1.1",
75
+ "@stdlib/random-base-uniform": "^0.1.0",
77
76
  "tape": "git+https://github.com/kgryte/tape.git#fix/globby",
78
77
  "istanbul": "^0.4.1",
79
- "tap-spec": "5.x.x"
78
+ "tap-min": "git+https://github.com/Planeshifter/tap-min.git",
79
+ "@stdlib/bench-harness": "^0.2.0",
80
+ "@stdlib/bench": "^0.3.1"
80
81
  },
81
82
  "engines": {
82
83
  "node": ">=0.10.0",
@@ -108,7 +109,7 @@
108
109
  ],
109
110
  "__stdlib__": {},
110
111
  "funding": {
111
- "type": "patreon",
112
- "url": "https://www.patreon.com/athan"
112
+ "type": "opencollective",
113
+ "url": "https://opencollective.com/stdlib"
113
114
  }
114
115
  }
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
-