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
|
#include "maths/complex.h"
#include <math.h>
struct PsiComplex psi_new_complex(double real, double imaginary)
{
return (struct PsiComplex){
real,
imaginary,
};
}
struct PsiComplex psi_new_complex_from_real(double real)
{
return (struct PsiComplex){
real,
0.0,
};
}
struct PsiComplex psi_conjugate_complex(struct PsiComplex z)
{
return (struct PsiComplex){
z.real,
-z.imaginary,
};
}
struct PsiComplex psi_neg_complex(struct PsiComplex z)
{
return (struct PsiComplex){
-z.real,
-z.imaginary,
};
}
double psi_phase_complex(struct PsiComplex z)
{
return atan2(z.imaginary, z.real);
}
double psi_norm2_complex(struct PsiComplex z)
{
return z.real * z.real + z.imaginary * z.imaginary;
}
double psi_abs_complex(struct PsiComplex z)
{
return sqrt(psi_norm2_complex(z));
}
struct PsiComplex psi_sqrt_complex(struct PsiComplex z)
{
double r = psi_abs_complex(z);
double half_theta = psi_phase_complex(z) / 2.0;
double sqrt_r = sqrt(r);
return (struct PsiComplex){
sqrt_r * cos(half_theta),
sqrt_r * sin(half_theta),
};
}
struct PsiComplex psi_add_complex(struct PsiComplex a, struct PsiComplex b)
{
return (struct PsiComplex){
a.real + b.real,
a.imaginary + b.imaginary,
};
}
struct PsiComplex psi_sub_complex(struct PsiComplex a, struct PsiComplex b)
{
return (struct PsiComplex){
a.real - b.real,
a.imaginary - b.imaginary,
};
}
struct PsiComplex psi_mul_complex(struct PsiComplex a, struct PsiComplex b)
{
// (a + bi)(c + di) = (ac - bd) + (ad + bc)i
return (struct PsiComplex){
a.real * b.real - a.imaginary * b.imaginary,
a.real * b.imaginary + a.imaginary * b.real,
};
}
struct PsiComplex psi_div_complex(struct PsiComplex a, struct PsiComplex b)
{
// (a + bi) / (c + di) = ((ac + bd) + (bc - ad)i) / (c² + d²)
double denom = b.real * b.real + b.imaginary * b.imaginary;
return (struct PsiComplex){
(a.real * b.real + a.imaginary * b.imaginary) / denom,
(a.imaginary * b.real - a.real * b.imaginary) / denom,
};
}
struct PsiComplex psi_add_complex_real(struct PsiComplex a, double b)
{
return (struct PsiComplex){
a.real + b,
a.imaginary,
};
}
struct PsiComplex psi_sub_complex_real(struct PsiComplex a, double b)
{
return (struct PsiComplex){
a.real - b,
a.imaginary,
};
}
struct PsiComplex psi_mul_complex_real(struct PsiComplex a, double b)
{
return (struct PsiComplex){
a.real * b,
a.imaginary * b,
};
}
struct PsiComplex psi_div_complex_real(struct PsiComplex a, double b)
{
return (struct PsiComplex){
a.real / b,
a.imaginary / b,
};
}
|