-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.c
72 lines (51 loc) · 1.1 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* complex.c
*
* @author: phdenzel
*
* A complex number struct with basic complex operations
*
*/
#include "complex.h"
double complex_real(complex_t* z) {
return z->real;
}
double complex_imag(complex_t* z) {
return z->imag;
}
void complex_setReal(complex_t* z, double r) {
z->real = r;
}
void complex_setImag(complex_t* z, double i) {
z->imag = i;
}
void complex_set(complex_t* z, double r, double i) {
complex_setReal(z, r);
complex_setImag(z, i);
}
void complex_add(complex_t* z2, complex_t* z1) {
z1->real += z2->real;
z1->imag += z2->real;
}
complex_t complex_addcpy(complex_t* z2, complex_t* z1) {
complex_t z;
z.real = z1->real + z2->real;
z.imag = z1->imag + z2->imag;
return z;
}
void complex_square(complex_t* z) {
double re, im;
re = z->real;
im = z->imag;
z->real = re*re - im*im;
z->imag = 2*re*im;
}
complex_t complex_squarecpy(complex_t* z) {
complex_t v;
v.real = z->real*z->real - z->imag*z->imag;
v.imag = 2*z->real*z->imag;
return v;
}
double complex_abs2(complex_t* z) {
return z->real*z->real+z->imag*z->imag;
}