-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.jsfx-inc
490 lines (409 loc) · 8.91 KB
/
complex.jsfx-inc
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
desc:Complex math operations
// Copyright (C) 2017 Theo Niessink <[email protected]>
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
/* Setting Functions
* cplx_set_real(a)
Example: z.cplx_set_real(1.0);
Sets the complex number to a + 0*i.
* cplx_set(a, b)
Example: z.cplx_set(1.0, -1.0);
Sets the complex number to a + b*i.
* cplx_set(z*)
Example: z2.cplx_set(z1); // z2 = z1
Sets the complex number to the value of another complex number.
Polar Functions
* cplx_polar(r, phi)
Example: z.cplx_polar(1.0, $pi/4);
Sets the complex number using polar coordinates, converting from polar
to trigonometric form.
* cplx_norm()
Example: norm = z.cplx_norm();
Returns the norm of the complex number i.e. Re(z)^2 + Im(z)^2.
* cplx_abs()
Example: mag = z.cplx_abs();
Returns the absolute value (or modulus or magnitude) of the complex
number.
* cplx_arg()
Example: phase = z.cplx_arg();
Returns the argument (or phase) of the complex number.
Equality Functions
* cplx_eq(z*)
Example: is_eq = z1.cplx_eq(z2); // z1 == z2
Returns 1 if the complex numbers are equal, or 0 otherwise.
* cplx_not_eq(z*)
Example: is_diff = z1.cplx_not_eq(z2); // z1 != z2
Returns 1 if the complex numbers are not equal, or 0 otherwise.
Elementary Functions
* cplx_add(x*[, y*]) -- Add
* cplx_sub(x*[, y*]) -- Subtract
* cplx_mul(x*[, y*]) -- Multiply
* cplx_div(x*[, y*]) -- Divide
Example: z.cplx_add(x, y); // z = x + y
Example: z.cplx_add(x); // z = z + x
Sets the complex number to the result of the operation on two complex
numbers.
* cplx_add_real([z*,] a) -- Add
* cplx_sub_real([z*,] a) -- Subtract
* cplx_mul_real([z*,] a) -- Multiply
* cplx_div_real([z*,] a) -- Divide
Example: z.cplx_add_real(x, a); // z = x + a
Example: z.cplx_add_real(a); // z = z + a
Sets the complex number to the result of the operation on a complex
number and a real number.
* cplx_conj(z*) -- Complex conjugate
* cplx_neg(z*) -- Negation (-z)
* cplx_recip(z*) -- Reciprocal (1/z)
* cplx_sqrt(z*) -- Square root
* cplx_sqr(z*) -- Square (z^2)
Example: z.cplx_recip(x); // z = 1/x
Sets the complex number to the result of the operation on a complex
number.
Exponentiation Functions
* cplx_exp(z*) -- Exponential function
* cplx_log(z*) -- Natural logarithm
* cplx_log10(z*) -- Base 10 logarithm
Example: z.cplx_log(x); // z = ln(x)
Sets the complex number to the result of the function of a complex
number.
* cplx_pow(x*, y*)
* cplx_pow_real(z*, a)
Example: z.cplx_pow(x, y); // z = x^y
Sets the complex number to a complex number raised to the power of
another complex or real number.
Trigonometric functions
* cplx_sin(z*) -- Sine
* cplx_cos(z*) -- Cosine
* cplx_tan(z*) -- Tangent
* cplx_sinh(z*) -- Hyperbolic sine
* cplx_cosh(z*) -- Hyperbolic cosine
* cplx_tanh(z*) -- Hyperbolic tangent
Example: z.cplx_sin(x);
Sets the complex number to the trigonometric function of a complex
number.
Instance Variables
* re
* im
Example: a = z.re;
Example: b = z.im;
The real/imaginary part of the complex number.
*/
@init
function cplx_set(a, b)
instance(re, im)
(
im = b;
re = a;
);
function cplx_set(z*)
instance(re, im)
(
im = z.im;
re = z.re;
);
function cplx_set_real(a)
instance(re, im)
(
im = 0;
re = a;
);
function cplx_polar(r, phi)
instance(re, im)
(
im = r * sin(phi);
re = r * cos(phi);
);
function cplx_norm()
instance(re, im)
(
sqr(re) + sqr(im);
);
function cplx_abs()
instance(re, im)
(
sqrt(this.cplx_norm());
);
function cplx_arg()
instance(re, im)
(
atan2(im, re);
);
function cplx_eq(z*)
instance(re, im)
(
re == z.re && im == z.im;
);
function cplx_not_eq(z*)
instance(re, im)
(
re != z.re || im != z.im;
);
function cplx_conj(z*)
instance(re, im)
(
im = -z.im;
re = z.re;
);
function cplx_neg(z*)
instance(re, im)
(
im = -z.im;
re = -z.re;
);
function cplx_add(x*, y*)
instance(re, im)
(
im = x.im + y.im;
re = x.re + y.re;
);
function cplx_add(z*)
instance(re, im)
(
im += z.im;
re += z.re;
);
function cplx_add_real(z*, a)
instance(re)
(
re = z.re + a;
);
function cplx_add_real(a)
instance(re)
(
re += a;
);
function cplx_sub(x*, y*)
instance(re, im)
(
im = x.im - y.im;
re = x.re - y.re;
);
function cplx_sub(z*)
instance(re, im)
(
im -= z.im;
re -= z.re;
);
function cplx_sub_real(z*, a)
instance(re)
(
re = z.re - a;
);
function cplx_sub_real(a)
instance(re)
(
re -= a;
);
function cplx_mul(x*, y*)
instance(re, im)
local(a)
(
a = x.re * y.re - x.im * y.im;
im = x.im * y.re + x.re * y.im;
re = a;
);
function cplx_mul(z*)
instance(re, im)
local(a)
(
a = re * z.re - im * z.im;
im = im * z.re + re * z.im;
re = a;
);
function cplx_mul_real(z*, a)
instance(re, im)
(
im = z.im * a;
re = z.re * a;
);
function cplx_mul_real(a)
instance(re, im)
(
im *= a;
re *= a;
);
function cplx_div(x*, y*)
instance(re, im)
local(norm, a)
(
norm = y.cplx_norm();
a = (x.re * y.re + x.im * y.im) / norm;
im = (x.im * y.re - x.re * y.im) / norm;
re = a;
);
function cplx_div(z*)
instance(re, im)
local(norm, a)
(
norm = z.cplx_norm();
a = (re * z.re + im * z.im) / norm;
im = (im * z.re - re * z.im) / norm;
re = a;
);
function cplx_div_real(z*, a)
instance(re, im)
(
im = z.im / a;
re = z.re / a;
);
function cplx_div_real(a)
instance(re, im)
(
im /= a;
re /= a;
);
function cplx_recip(z*)
instance(re, im)
local(norm)
(
norm = z.cplx_norm();
im = z.im / norm;
re = z.re / norm;
);
function cplx_sqrt(z*)
instance(re, im)
local(mod)
(
mod = z.cplx_abs();
im = sign(z.im) * sqrt((mod - z.re) * 0.5);
re = sqrt((mod + z.re) * 0.5);
);
function cplx_sqr(z*)
instance(re, im)
local(a)
(
a = sqr(z.re) - sqr(z.im);
im = 2 * z.re * z.im;
re = a;
);
function cplx_exp(z*)
instance(re, im)
local(e)
(
e = exp(z.re);
re = e * cos(z.im);
im = e * sin(z.im);
re;
);
function cplx_log(z*)
instance(re, im)
local(a)
(
a = log(z.cplx_abs());
im = z.cplx_arg();
re = a;
);
function cplx_log10(z*)
instance(re, im)
(
this.cplx_log(z);
// log(10) / sqr(log(10))
im *= 0.43429448190325183;
re *= 0.43429448190325183;
);
function cplx_pow(x*, y*)
instance(re, im)
local(a, b, c)
(
// z.cplx_log(x)
a = log(x.cplx_abs());
b = x.cplx_arg();
// z.cplx_mul(y)
c = a * y.re - b * y.im;
im = b * y.re + a * y.im;
re = c;
// this.cplx_exp(z)
this.cplx_exp(this);
);
function cplx_pow_real(z*, a)
instance(re, im)
local(b)
(
// y.cplx_log(z)
// y.cplx_mul_real(a)
b = z.cplx_arg() * a;
re = log(z.cplx_abs()) * a;
im = b;
// this.cplx_exp(y)
this.cplx_exp(this);
);
function cplx_cos(z*)
instance(re, im)
local(e)
(
e = exp(z.im);
im = (1/e - e) * 0.5 * sin(z.re);
re = (1/e + e) * 0.5 * cos(z.re);
);
function cplx_sin(z*)
instance(re, im)
local(e)
(
e = exp(z.im);
im = (e - 1/e) * 0.5 * cos(z.re);
re = (e + 1/e) * 0.5 * sin(z.re);
);
function cplx_tan(z*)
instance(re, im)
local(p, q, e, a, b, c, d, norm)
(
e = exp(z.im);
a = (e + 1/e) * 0.5;
b = (e - 1/e) * 0.5;
p = cos(z.re);
q = sin(z.re);
// x.cplx_sin(z)
c = a*q;
d = b*p;
// y.cplx_cos(z)
a *= p;
b *= q;
// this.cplx_div(x, y)
norm = sqr(a) + sqr(b);
im = (a*d + b*c) / norm;
re = (a*c - b*d) / norm;
);
function cplx_cosh(z*)
instance(re, im)
local(e)
(
e = exp(z.re);
re = (e + 1/e) * 0.5 * cos(z.im);
im = (e - 1/e) * 0.5 * sin(z.im);
re;
);
function cplx_sinh(z*)
instance(re, im)
local(e)
(
e = exp(z.re);
re = (e - 1/e) * 0.5 * cos(z.im);
im = (e + 1/e) * 0.5 * sin(z.im);
re;
);
function cplx_tanh(z*)
instance(re, im)
local(e, a, b, c, d, norm)
(
e = exp(z.re);
c = cos(z.im);
d = sin(z.im);
// x.cplx_exp(z)
a = c*e;
b = d*e;
// y.cplx_exp(-z)
c /= e;
d /= e;
// this.cplx_sub(x, y)
im = b + d;
re = a - c;
// x.cplx_add(y)
a += c;
b -= d;
// this.cplx_div(x)
norm = sqr(a) + sqr(b);
c = (re * a + im * b) / norm;
im = (im * a - re * b) / norm;
re = c;
);