-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
233 lines (212 loc) · 5.82 KB
/
main.js
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* @license Apache-2.0
*
* Copyright (c) 2021 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 valid-typeof */
'use strict';
// MODULES //
var isFunction = require( '@stdlib/assert/is-function' );
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
var format = require( '@stdlib/string/format' );
// VARIABLES //
var T = 'number';
// FUNCTIONS //
/**
* Wraps a function accepting complex number arguments to support providing both real and complex numbers.
*
* ## Notes
*
* - The returned function **assumes** that the wrapped function accepts **only** complex number input arguments (i.e., every argument must be a complex number).
* - The returned function **assumes** that, if an input argument is non-numeric (i.e., not of type `number`), then the input argument is a complex number. The returned function does **not** verify that non-numeric input arguments are, in fact, complex number objects. The returned function passes non-numeric input arguments to the wrapped function without modification.
*
* @param {Function} fcn - function to wrap
* @param {NonNegativeInteger} nargs - number of arguments
* @param {Function} ctor - complex number constructor
* @throws {TypeError} first argument must be a function
* @throws {TypeError} second argument must be a nonnegative integer
* @throws {TypeError} third argument must be a constructor function
* @returns {Function} wrapped function
*
* @example
* var Complex64 = require( '@stdlib/complex/float32/ctor' );
* var caddf = require( '@stdlib/complex/float32/base/add' );
* var realf = require( '@stdlib/complex/float32/real' );
* var imagf = require( '@stdlib/complex/float32/imag' );
*
* var f = wrap( caddf, 2, Complex64 );
*
* // ...
*
* var z = f( 3.0, 4.0 );
* // returns <Complex64>
*
* var re = realf( z );
* // returns 7.0
*
* var im = imagf( z );
* // returns 0.0
*/
function wrap( fcn, nargs, ctor ) {
var fcns;
if ( !isFunction( fcn ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
}
if ( !isNonNegativeInteger( nargs ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', nargs ) );
}
if ( !isFunction( ctor ) ) {
throw new TypeError( format( 'invalid argument. Third argument must be a constructor function. Value: `%s`.', ctor ) );
}
fcns = [ fcn0, fcn1, fcn2, fcn3, fcn4, fcn5 ];
return ( nargs <= 5 ) ? fcns[ nargs ] : fcnN;
/**
* Invokes a nullary function.
*
* @private
* @returns {*} result
*/
function fcn0() {
return fcn();
}
/**
* Invokes a unary function accepting complex numbers.
*
* @private
* @param {(number|Complex)} x - input value
* @returns {*} result
*/
function fcn1( x ) {
if ( typeof x === T ) {
x = new ctor( x, 0.0 );
}
return fcn( x );
}
/**
* Invokes a binary function accepting complex numbers.
*
* @private
* @param {(number|Complex)} x - input value
* @param {(number|Complex)} y - input value
* @returns {*} result
*/
function fcn2( x, y ) {
if ( typeof x === T ) {
x = new ctor( x, 0.0 );
}
if ( typeof y === T ) {
y = new ctor( y, 0.0 );
}
return fcn( x, y );
}
/**
* Invokes a ternary function accepting complex numbers.
*
* @private
* @param {(number|Complex)} x - input value
* @param {(number|Complex)} y - input value
* @param {(number|Complex)} z - input value
* @returns {*} result
*/
function fcn3( x, y, z ) {
if ( typeof x === T ) {
x = new ctor( x, 0.0 );
}
if ( typeof y === T ) {
y = new ctor( y, 0.0 );
}
if ( typeof z === T ) {
z = new ctor( z, 0.0 );
}
return fcn( x, y, z );
}
/**
* Invokes a quaternary function accepting complex numbers.
*
* @private
* @param {(number|Complex)} x - input value
* @param {(number|Complex)} y - input value
* @param {(number|Complex)} z - input value
* @param {(number|Complex)} w - input value
* @returns {*} result
*/
function fcn4( x, y, z, w ) {
if ( typeof x === T ) {
x = new ctor( x, 0.0 );
}
if ( typeof y === T ) {
y = new ctor( y, 0.0 );
}
if ( typeof z === T ) {
z = new ctor( z, 0.0 );
}
if ( typeof w === T ) {
w = new ctor( w, 0.0 );
}
return fcn( x, y, z, w );
}
/**
* Invokes a quinary function accepting complex numbers.
*
* @private
* @param {(number|Complex)} x - input value
* @param {(number|Complex)} y - input value
* @param {(number|Complex)} z - input value
* @param {(number|Complex)} w - input value
* @param {(number|Complex)} v - input value
* @returns {*} result
*/
function fcn5( x, y, z, w, v ) {
if ( typeof x === T ) {
x = new ctor( x, 0.0 );
}
if ( typeof y === T ) {
y = new ctor( y, 0.0 );
}
if ( typeof z === T ) {
z = new ctor( z, 0.0 );
}
if ( typeof w === T ) {
w = new ctor( w, 0.0 );
}
if ( typeof v === T ) {
v = new ctor( v, 0.0 );
}
return fcn( x, y, z, w, v );
}
/**
* Invokes a function accepting an arbitrary number of complex number input arguments.
*
* @private
* @param {...(number|Complex)} args - input values
* @returns {*} result
*/
function fcnN() {
var args;
var v;
var i;
args = [];
for ( i = 0; i < arguments.length; i++ ) {
v = arguments[ i ];
if ( typeof v === T ) {
v = new ctor( v, 0.0 );
}
args.push( v );
}
return fcn.apply( null, args );
}
}
// EXPORTS //
module.exports = wrap;