-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.h
More file actions
215 lines (190 loc) · 8.12 KB
/
complex.h
File metadata and controls
215 lines (190 loc) · 8.12 KB
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
/**
* @file complex.h
* @brief Header file for complex number operations in Cartesian and Polar forms.
*/
#ifndef COMPLEX_NUMBER_H
#define COMPLEX_NUMBER_H
/**
* @struct cartesianComplexNumber
* @brief Represents a complex number in Cartesian form (real + imaginary * i).
* @var cartesianComplexNumber::real
* Member 'real' represents the real part of the complex number.
* @var cartesianComplexNumber::imaginary
* Member 'imaginary' represents the imaginary part of the complex number.
*/
typedef struct cartesianComplexNumber {
double real;
double imaginary;
} cartesianComplexNumber;
/**
* @struct polarComplexNumber
* @brief Represents a complex number in Polar form (modulus * e^(i * argument)).
* @var polarComplexNumber::modulus
* Member 'modulus' represents the magnitude of the complex number.
* @var polarComplexNumber::argument
* Member 'argument' represents the angle (in radians) of the complex number.
*/
typedef struct polarComplexNumber {
double modulus;
double argument;
} polarComplexNumber;
/**
* @brief Converts a complex number from Cartesian form to Polar form.
* @param a The complex number in Cartesian form.
* @return The complex number in Polar form.
*/
polarComplexNumber cartesianToPolar(cartesianComplexNumber a);
/**
* @brief Converts a complex number from Polar form to Cartesian form.
* @param a The complex number in Polar form.
* @return The complex number in Cartesian form.
*/
cartesianComplexNumber polarToCartesian(polarComplexNumber a);
/**
* @brief Multiplies two complex numbers in Cartesian form.
* @param a The first complex number in Cartesian form.
* @param b The second complex number in Cartesian form.
* @return The product of the two complex numbers in Cartesian form.
*/
cartesianComplexNumber multiplyComplexCartesian(cartesianComplexNumber a, cartesianComplexNumber b);
/**
* @brief Adds two complex numbers in Cartesian form.
* @param a The first complex number in Cartesian form.
* @param b The second complex number in Cartesian form.
* @return The sum of the two complex numbers in Cartesian form.
*/
cartesianComplexNumber addComplexCartesian(cartesianComplexNumber a, cartesianComplexNumber b);
/**
* @brief Subtracts one complex number from another in Cartesian form.
* @param a The complex number to be subtracted from (minuend) in Cartesian form.
* @param b The complex number to subtract (subtrahend) in Cartesian form.
* @return The difference of the two complex numbers in Cartesian form.
*/
cartesianComplexNumber subtractComplexCartesian(cartesianComplexNumber a, cartesianComplexNumber b);
/**
* @brief Divides one complex number by another in Cartesian form.
* @param a The complex number to be divided (dividend) in Cartesian form.
* @param b The complex number to divide by (divisor) in Cartesian form.
* @return The quotient of the two complex numbers in Cartesian form.
*/
cartesianComplexNumber divideComplexCartesian(cartesianComplexNumber a, cartesianComplexNumber b);
/**
* @brief Finds the conjugate of a complex number in Cartesian form.
* @param a The complex number in Cartesian form.
* @return The conjugate of the complex number in Cartesian form.
*/
cartesianComplexNumber conjugateComplexCartesian(cartesianComplexNumber a);
/**
* @brief Finds the conjugate of a complex number in Polar form.
* @param a The complex number in Polar form.
* @return The conjugate of the complex number in Polar form.
*/
polarComplexNumber conjugateComplexPolar(polarComplexNumber a);
/**
* @brief Finds the magnitude of a complex number in Cartesian form.
* @param a The complex number in Cartesian form.
* @return The magnitude of the complex number.
*/
double modulusComplexCartesian(cartesianComplexNumber a);
/**
* @brief Finds the value of a complex number raised to a power.
* @param a The complex number in Cartesian form.
* @param power The power to raise the complex number to.
* @return The complex number raised to the power in Cartesian form.
*/
cartesianComplexNumber powerComplexCartesian(cartesianComplexNumber a, double power);
#endif // COMPLEX_NUMBER_H
#pragma comment(lib, "m")
#include <math.h>
/**
* @brief Converts a complex number from Cartesian form to Polar form.
* @param a The complex number in Cartesian form.
* @return The complex number in Polar form.
*/
polarComplexNumber cartesianToPolar(cartesianComplexNumber a) {
polarComplexNumber result;
result.modulus = sqrt(a.real * a.real + a.imaginary * a.imaginary); // Calculate modulus
result.argument = atan2(a.imaginary, a.real); // Calculate argument
return result;
}
/**
* @brief Converts a complex number from Polar form to Cartesian form.
* @param a The complex number in Polar form.
* @return The complex number in Cartesian form.
*/
cartesianComplexNumber polarToCartesian(polarComplexNumber a) {
cartesianComplexNumber result;
result.real = a.modulus * cos(a.argument); // Calculate real part
result.imaginary = a.modulus * sin(a.argument); // Calculate imaginary part
return result;
}
/**
* @brief Multiplies two complex numbers in Cartesian form.
* @param a The first complex number in Cartesian form.
* @param b The second complex number in Cartesian form.
* @return The product of the two complex numbers in Cartesian form.
*/
cartesianComplexNumber multiplyComplexCartesian(cartesianComplexNumber a, cartesianComplexNumber b) {
cartesianComplexNumber result;
result.real = a.real * b.real - a.imaginary * b.imaginary; // Calculate real part
result.imaginary = a.real * b.imaginary + a.imaginary * b.real; // Calculate imaginary part
return result;
}
/**
* @brief Adds two complex numbers in Cartesian form.
* @param a The first complex number in Cartesian form.
* @param b The second complex number in Cartesian form.
* @return The sum of the two complex numbers in Cartesian form.
*/
cartesianComplexNumber addComplexCartesian(cartesianComplexNumber a, cartesianComplexNumber b) {
cartesianComplexNumber result;
result.real = a.real + b.real; // Calculate real part
result.imaginary = a.imaginary + b.imaginary; // Calculate imaginary part
return result;
}
/**
* @brief Subtracts one complex number from another in Cartesian form.
* @param a The complex number to be subtracted from (minuend) in Cartesian form.
* @param b The complex number to subtract (subtrahend) in Cartesian form.
* @return The difference of the two complex numbers in Cartesian form.
*/
cartesianComplexNumber subtractComplexCartesian(cartesianComplexNumber a, cartesianComplexNumber b) {
cartesianComplexNumber result;
result.real = a.real - b.real; // Calculate real part
result.imaginary = a.imaginary - b.imaginary; // Calculate imaginary part
return result;
}
/**
* @brief Divides one complex number by another in Cartesian form.
* @param a The complex number to be divided (dividend) in Cartesian form.
* @param b The complex number to divide by (divisor) in Cartesian form.
* @return The quotient of the two complex numbers in Cartesian form.
*/
cartesianComplexNumber divideComplexCartesian(cartesianComplexNumber a, cartesianComplexNumber b) {
cartesianComplexNumber result;
double denominator = b.real * b.real + b.imaginary * b.imaginary; // Calculate denominator
result.real = (a.real * b.real + a.imaginary * b.imaginary) / denominator; // Calculate real part
result.imaginary = (a.imaginary * b.real - a.real * b.imaginary) / denominator; // Calculate imaginary part
return result;
}
cartesianComplexNumber conjugateComplexCartesian(cartesianComplexNumber a) {
cartesianComplexNumber result;
result.real = a.real;
result.imaginary = -a.imaginary;
return result;
}
polarComplexNumber conjugateComplexPolar(polarComplexNumber a) {
polarComplexNumber result;
result.modulus = a.modulus;
result.argument = -a.argument;
return result;
}
double modulusComplexCartesian(cartesianComplexNumber a) {
return sqrt(a.real * a.real + a.imaginary * a.imaginary);
}
cartesianComplexNumber powerComplexCartesian(cartesianComplexNumber a, double power) {
polarComplexNumber polar = cartesianToPolar(a);
polar.modulus = pow(polar.modulus, power);
polar.argument *= power;
return polarToCartesian(polar);
}