-
Notifications
You must be signed in to change notification settings - Fork 8
/
complex.ts
420 lines (361 loc) · 13.4 KB
/
complex.ts
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import compile from './compiler/compile';
import parse from './compiler/parse';
import abs from './functions/abs';
import acos from './functions/acos';
import acosh from './functions/acosh';
import arg from './functions/arg';
import asin from './functions/asin';
import asinh from './functions/asinh';
import atan from './functions/atan';
import atanh from './functions/atanh';
import cbrt from './functions/cbrt';
import ceil from './functions/ceil';
import conj from './functions/conj';
import cos from './functions/cos';
import cosh from './functions/cosh';
import cube from './functions/cube';
import exp from './functions/exp';
import floor from './functions/floor';
import from from './functions/from';
import imag from './functions/imag';
import log from './functions/log';
import neg from './functions/neg';
import not from './functions/not';
import polar from './functions/polar';
import random from './functions/random';
import real from './functions/real';
import round from './functions/round';
import sign from './functions/sign';
import sin from './functions/sin';
import sinh from './functions/sinh';
import sqrt from './functions/sqrt';
import square from './functions/square';
import tan from './functions/tan';
import tanh from './functions/tanh';
import trunc from './functions/trunc';
import mask from './internal/mask';
import add from './methods/add';
import and from './methods/and';
import div from './methods/div';
import equals from './methods/equals';
import getAbs from './methods/getAbs';
import getArg from './methods/getArg';
import getImag from './methods/getImag';
import getReal from './methods/getReal';
import mod from './methods/mod';
import mul from './methods/mul';
import or from './methods/or';
import pow from './methods/pow';
import sal from './methods/sal';
import sar from './methods/sar';
import shr from './methods/shr';
import sub from './methods/sub';
import toString from './methods/toString';
import xor from './methods/xor';
export interface IContext {
[identifierName: string]: Complex;
}
export type IReviver<T extends any[]> = (...args: T) => IContext;
export default class Complex {
public static readonly '0' = Complex.from(0);
public static readonly '1' = Complex.from(1);
public static readonly 'I' = Complex.from(0, 1);
public static readonly 'E' = Complex.from(Math.E);
public static readonly 'LN2' = Complex.from(Math.LN2);
public static readonly 'LN10' = Complex.from(Math.LN10);
public static readonly 'LOG2E' = Complex.from(Math.LOG2E);
public static readonly 'LOG10E' = Complex.from(Math.LOG10E);
public static readonly 'PI' = Complex.from(Math.PI);
public static readonly 'SQRT1_2' = Complex.from(Math.SQRT1_2);
public static readonly 'SQRT2' = Complex.from(Math.SQRT2);
public static from (r: number, i?: number): Complex;
public static from (z: Complex | number): Complex;
public static from (z: Complex | number, i?: number): Complex {
return from(Complex, z, i);
}
public static cartesian (r: number, i = 0): Complex {
return new Complex(r, i, NaN, NaN, mask.HAS_CARTESIAN);
}
public static polar (abs: number, arg?: number): Complex {
return polar(Complex, abs, arg);
}
public static real (z: Complex | number): Complex;
public static real (r: number, i?: number): Complex;
public static real (z: Complex | number, i?: number): Complex {
return real(Complex, z, i);
}
public static imag (z: Complex | number): Complex;
public static imag (r: number, i?: number): Complex;
public static imag (z: Complex | number, i?: number): Complex {
return imag(Complex, z, i);
}
public static abs (z: Complex | number): Complex;
public static abs (r: number, i?: number): Complex;
public static abs (z: Complex | number, i?: number): Complex {
return abs(Complex, z, i);
}
public static arg (z: Complex | number): Complex;
public static arg (r: number, i?: number): Complex;
public static arg (z: Complex | number, i?: number): Complex {
return arg(Complex, z, i);
}
public static neg (z: Complex | number): Complex;
public static neg (r: number, i?: number): Complex;
public static neg (z: Complex | number, i?: number): Complex {
return neg(Complex, z, i);
}
public static conj (z: Complex | number): Complex;
public static conj (r: number, i?: number): Complex;
public static conj (z: Complex | number, i?: number): Complex {
return conj(Complex, z, i);
}
public static floor (z: Complex | number): Complex;
public static floor (r: number, i?: number): Complex;
public static floor (z: Complex | number, i?: number): Complex {
return floor(Complex, z, i);
}
public static ceil (z: Complex | number): Complex;
public static ceil (r: number, i?: number): Complex;
public static ceil (z: Complex | number, i?: number): Complex {
return ceil(Complex, z, i);
}
public static round (z: Complex | number): Complex;
public static round (r: number, i?: number): Complex;
public static round (z: Complex | number, i?: number): Complex {
return round(Complex, z, i);
}
public static sign (z: Complex | number): Complex;
public static sign (r: number, i?: number): Complex;
public static sign (z: Complex | number, i?: number): Complex {
return sign(Complex, z, i);
}
public static trunc (z: Complex | number): Complex;
public static trunc (r: number, i?: number): Complex;
public static trunc (z: Complex | number, i?: number): Complex {
return trunc(Complex, z, i);
}
public static not (z: Complex | number): Complex;
public static not (r: number, i?: number): Complex;
public static not (z: Complex | number, i?: number): Complex {
return not(Complex, z, i);
}
public static random (): Complex {
return random(Complex);
}
public static sqrt (z: Complex | number): Complex;
public static sqrt (r: number, i?: number): Complex;
public static sqrt (z: Complex | number, i?: number): Complex {
return sqrt(Complex, z, i);
}
public static cbrt (z: Complex | number): Complex;
public static cbrt (r: number, i?: number): Complex;
public static cbrt (z: Complex | number, i?: number): Complex {
return cbrt(Complex, z, i);
}
public static square (z: Complex | number): Complex;
public static square (r: number, i?: number): Complex;
public static square (z: Complex | number, i?: number): Complex {
return square(Complex, z, i);
}
public static cube (z: Complex | number): Complex;
public static cube (r: number, i?: number): Complex;
public static cube (z: Complex | number, i?: number): Complex {
return cube(Complex, z, i);
}
public static exp (z: Complex | number): Complex;
public static exp (r: number, i?: number): Complex;
public static exp (z: Complex | number, i?: number): Complex {
return exp(Complex, z, i);
}
public static log (z: Complex | number): Complex;
public static log (r: number, i?: number): Complex;
public static log (z: Complex | number, i?: number): Complex {
return log(Complex, z, i);
}
public static cos (z: Complex | number): Complex;
public static cos (r: number, i?: number): Complex;
public static cos (z: Complex | number, i?: number): Complex {
return cos(Complex, z, i);
}
public static sin (z: Complex | number): Complex;
public static sin (r: number, i?: number): Complex;
public static sin (z: Complex | number, i?: number): Complex {
return sin(Complex, z, i);
}
public static tan (z: Complex | number): Complex;
public static tan (r: number, i?: number): Complex;
public static tan (z: Complex | number, i?: number): Complex {
return tan(Complex, z, i);
}
public static acos (z: Complex | number): Complex;
public static acos (r: number, i?: number): Complex;
public static acos (z: Complex | number, i?: number): Complex {
return acos(Complex, z, i);
}
public static asin (z: Complex | number): Complex;
public static asin (r: number, i?: number): Complex;
public static asin (z: Complex | number, i?: number): Complex {
return asin(Complex, z, i);
}
public static atan (z: Complex | number): Complex;
public static atan (r: number, i?: number): Complex;
public static atan (z: Complex | number, i?: number): Complex {
return atan(Complex, z, i);
}
public static cosh (z: Complex | number): Complex;
public static cosh (r: number, i?: number): Complex;
public static cosh (z: Complex | number, i?: number): Complex {
return cosh(Complex, z, i);
}
public static sinh (z: Complex | number): Complex;
public static sinh (r: number, i?: number): Complex;
public static sinh (z: Complex | number, i?: number): Complex {
return sinh(Complex, z, i);
}
public static tanh (z: Complex | number): Complex;
public static tanh (r: number, i?: number): Complex;
public static tanh (z: Complex | number, i?: number): Complex {
return tanh(Complex, z, i);
}
public static acosh (z: Complex | number): Complex;
public static acosh (r: number, i?: number): Complex;
public static acosh (z: Complex | number, i?: number): Complex {
return acosh(Complex, z, i);
}
public static asinh (z: Complex | number): Complex;
public static asinh (r: number, i?: number): Complex;
public static asinh (z: Complex | number, i?: number): Complex {
return asinh(Complex, z, i);
}
public static atanh (z: Complex | number): Complex;
public static atanh (r: number, i?: number): Complex;
public static atanh (z: Complex | number, i?: number): Complex {
return atanh(Complex, z, i);
}
public static parse (text: string, context?: IContext): Complex {
return parse(Complex, text, context);
}
public static compile (text: string): () => Complex;
public static compile<T extends any[]> (text: string, reviver: IReviver<T>): (...args: T) => Complex;
public static compile<T extends any[]> (text: string, reviver?: IReviver<T>): (...args: T) => Complex {
return compile(Complex, text, reviver);
}
/**
* @internal
*/
public _real: number;
/**
* @internal
*/
public _imag: number;
/**
* @internal
*/
public _abs: number;
/**
* @internal
*/
public _arg: number;
/**
* @internal
*/
public _mask: mask;
/**
* @internal
*/
public constructor (_real: number, _imag: number, _abs: number, _arg: number, _mask: mask) {
// coerce -0 to +0
this._real = _real + 0;
this._imag = _imag + 0;
this._abs = _abs + 0;
// choose branch cut as the interval (-pi, pi]
const d = 2 * Math.PI;
this._arg = Math.PI - (3 * Math.PI - _arg % d) % d;
this._mask = _mask;
}
public get real (): number {
return getReal(this);
}
public get imag (): number {
return getImag(this);
}
public get abs (): number {
return getAbs(this);
}
public get arg (): number {
return getArg(this);
}
public toString (format?: string): string {
return toString(this, format);
}
public equals (rhs: Complex | number): boolean;
public equals (r: number, i?: number): boolean;
public equals (r: Complex | number, i?: number): boolean {
return equals(this, r, i);
}
public add (rhs: Complex | number): Complex;
public add (r: number, i?: number): Complex;
public add (r: Complex | number, i?: number): Complex {
return add(Complex, this, r, i);
}
public sub (rhs: Complex | number): Complex;
public sub (r: number, i?: number): Complex;
public sub (r: Complex | number, i?: number): Complex {
return sub(Complex, this, r, i);
}
public mul (rhs: Complex | number): Complex;
public mul (r: number, i?: number): Complex;
public mul (r: Complex | number, i?: number): Complex {
return mul(Complex, this, r, i);
}
public div (rhs: Complex | number): Complex;
public div (r: number, i?: number): Complex;
public div (r: Complex | number, i?: number): Complex {
return div(Complex, this, r, i);
}
public mod (rhs: Complex | number): Complex;
public mod (r: number, i?: number): Complex;
public mod (r: Complex | number, i?: number): Complex {
return mod(Complex, this, r, i);
}
public pow (rhs: Complex | number): Complex;
public pow (r: number, i?: number): Complex;
public pow (r: Complex | number, i?: number): Complex {
return pow(Complex, this, r, i);
}
public and (rhs: Complex | number): Complex;
public and (r: number, i?: number): Complex;
public and (r: Complex | number, i?: number): Complex {
return and(Complex, this, r, i);
}
public or (rhs: Complex | number): Complex;
public or (r: number, i?: number): Complex;
public or (r: Complex | number, i?: number): Complex {
return or(Complex, this, r, i);
}
public xor (rhs: Complex | number): Complex;
public xor (r: number, i?: number): Complex;
public xor (r: Complex | number, i?: number): Complex {
return xor(Complex, this, r, i);
}
public sal (rhs: Complex | number): Complex;
public sal (r: number, i?: number): Complex;
public sal (r: Complex | number, i?: number): Complex {
return sal(Complex, this, r, i);
}
public shl (rhs: Complex | number): Complex;
public shl (r: number, i?: number): Complex;
public shl (r: Complex | number, i?: number): Complex {
return sal(Complex, this, r, i);
}
public sar (rhs: Complex | number): Complex;
public sar (r: number, i?: number): Complex;
public sar (r: Complex | number, i?: number): Complex {
return sar(Complex, this, r, i);
}
public shr (rhs: Complex | number): Complex;
public shr (r: number, i?: number): Complex;
public shr (r: Complex | number, i?: number): Complex {
return shr(Complex, this, r, i);
}
}