-
Notifications
You must be signed in to change notification settings - Fork 1
/
complex.c
52 lines (44 loc) · 999 Bytes
/
complex.c
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
#include <math.h>
#include <float.h>
#include <stdbool.h>
#include "complex.h"
#include "constants.h"
Complex complex_add(Complex z1, Complex z2)
{
return (Complex) {
.real = z1.real + z2.real,
.imag = z1.imag + z2.imag
};
}
Complex complex_subtract(Complex z1, Complex z2)
{
return (Complex) {
.real = z1.real - z2.real,
.imag = z1.imag - z2.imag
};
}
Complex complex_multiply(Complex z1, Complex z2)
{
return (Complex) {
.real = z1.real * z2.real - z1.imag * z2.imag,
.imag = z1.imag * z2.real + z1.real * z2.imag
};
}
Complex complex_scale(Complex z, PREC scalar)
{
return (Complex) {
.real = z.real * scalar,
.imag = z.imag * scalar
};
}
Complex complex_conj_exp(PREC phase)
{
return (Complex) {
.real = PREC_COS(2.0 * PI * phase),
.imag = -PREC_SIN(2.0 * PI * phase)
};
}
PREC complex_magnitude(Complex z)
{
return PREC_SQRT(z.real * z.real + z.imag * z.imag);
}