This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
/
Complex.qs
323 lines (299 loc) · 8.42 KB
/
Complex.qs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Math {
/// # Summary
/// Represents a complex number in polar form.
///
/// The polar representation of a complex number is $c=r e^{i t}$.
///
/// # Named Items
/// ## Magnitude
/// The absolute value $r \ge 0$ of $c$.
/// ## Argument
/// The phase $t \in \mathbb R$ of $c$.
newtype ComplexPolar = (Magnitude: Double, Argument: Double);
/// # Summary
/// Returns the squared absolute value of a complex number of type
/// `Complex`.
///
/// # Input
/// ## input
/// Complex number $c = x + i y$.
///
/// # Output
/// Squared absolute value $|c|^2 = x^2 + y^2$.
function AbsSquaredComplex(input : Complex) : Double {
let (real, imaginary) = input!;
return real * real + imaginary * imaginary;
}
/// # Summary
/// Returns the absolute value of a complex number of type
/// `Complex`.
///
/// # Input
/// ## input
/// Complex number $c = x + i y$.
///
/// # Output
/// Absolute value $|c| = \sqrt{x^2 + y^2}$.
function AbsComplex(input : Complex) : Double {
return Sqrt(AbsSquaredComplex(input));
}
/// # Summary
/// Returns the phase of a complex number of type
/// `Complex`.
///
/// # Input
/// ## input
/// Complex number $c = x + i y$.
///
/// # Output
/// Phase $\text{Arg}[c] = \text{ArcTan}(y,x) \in (-\pi,\pi]$.
function ArgComplex(input : Complex) : Double {
let (real, imaginary) = input!;
return ArcTan2(imaginary, real);
}
/// # Summary
/// Returns the squared absolute value of a complex number of type
/// `ComplexPolar`.
///
/// # Input
/// ## input
/// Complex number $c = r e^{i t}$.
///
/// # Output
/// Squared absolute value $|c|^2 = r^2$.
function AbsSquaredComplexPolar(input : ComplexPolar) : Double {
let (abs, arg) = input!;
return abs * abs;
}
/// # Summary
/// Returns the absolute value of a complex number of type
/// `ComplexPolar`.
///
/// # Input
/// ## input
/// Complex number $c = r e^{i t}$.
///
/// # Output
/// Absolute value $|c| = r$.
function AbsComplexPolar(input : ComplexPolar) : Double {
return input::Magnitude;
}
/// # Summary
/// Returns the phase of a complex number of type
/// `ComplexPolar`.
///
/// # Input
/// ## input
/// Complex number $c = r e^{i t}$.
///
/// # Output
/// Phase $\text{Arg}[c] = t$.
function ArgComplexPolar (input : ComplexPolar) : Double {
return input::Argument;
}
/// # Summary
/// Returns the unary negation of an input.
///
/// # Input
/// ## input
/// A value whose negation is to be returned.
///
/// # Output
/// The unary negation of `input`.
function NegationC(input : Complex) : Complex {
let (re, im) = input!;
return Complex(-re, -im);
}
/// # Summary
/// Returns the unary negation of an input.
///
/// # Input
/// ## input
/// A value whose negation is to be returned.
///
/// # Output
/// The unary negation of `input`.
function NegationCP(input : ComplexPolar) : ComplexPolar {
return ComplexPolar(input::Magnitude, input::Argument + PI());
}
/// # Summary
/// Returns the sum of two inputs.
///
/// # Input
/// ## a
/// The first input $a$ to be summed.
/// ## b
/// The second input $b$ to be summed.
///
/// # Output
/// The sum $a + b$.
function PlusC(a : Complex, b : Complex) : Complex {
let ((reA, imA), (reB, imB)) = (a!, b!);
return Complex(reA + reB, imA + imB);
}
/// # Summary
/// Returns the sum of two inputs.
///
/// # Input
/// ## a
/// The first input $a$ to be summed.
/// ## b
/// The second input $b$ to be summed.
///
/// # Output
/// The sum $a + b$.
function PlusCP(a : ComplexPolar, b : ComplexPolar) : ComplexPolar {
return ComplexAsComplexPolar(
PlusC(
ComplexPolarAsComplex(a),
ComplexPolarAsComplex(b)
)
);
}
/// # Summary
/// Returns the difference between two inputs.
///
/// # Input
/// ## a
/// The first input $a$ to be subtracted.
/// ## b
/// The second input $b$ to be subtracted.
///
/// # Output
/// The difference $a - b$.
function MinusC(a : Complex, b : Complex) : Complex {
return PlusC(a, NegationC(b));
}
/// # Summary
/// Returns the difference between two inputs.
///
/// # Input
/// ## a
/// The first input $a$ to be subtracted.
/// ## b
/// The second input $b$ to be subtracted.
///
/// # Output
/// The difference $a - b$.
function MinusCP(a : ComplexPolar, b : ComplexPolar) : ComplexPolar {
return PlusCP(a, NegationCP(b));
}
/// # Summary
/// Returns the product of two inputs.
///
/// # Input
/// ## a
/// The first input $a$ to be multiplied.
/// ## b
/// The second input $b$ to be multiplied.
///
/// # Output
/// The product $a \times b$.
function TimesC(a : Complex, b : Complex) : Complex {
let ((reA, imA), (reB, imB)) = (a!, b!);
return Complex(
reA * reB - imA * imB,
reA * imB + imA * reB
);
}
/// # Summary
/// Returns the product of two inputs.
///
/// # Input
/// ## a
/// The first input $a$ to be multiplied.
/// ## b
/// The second input $b$ to be multiplied.
///
/// # Output
/// The product $a \times b$.
function TimesCP(a : ComplexPolar, b : ComplexPolar) : ComplexPolar {
return ComplexPolar(
a::Magnitude * b::Magnitude,
a::Argument + b::Argument
);
}
/// # Summary
/// Internal. Since it is easiest to define the power of two complex numbers
/// in cartesian form as returning in polar form, we define that here, then
/// convert as needed.
internal function PowCAsCP(base_ : Complex, power : Complex) : ComplexPolar {
let ((a, b), (c, d)) = (base_!, power!);
// Re: https://www.wolframalpha.com/input/?i=simplify+re+%28%28a+%2B+b+i%29%5E%28c+%2B+d+i%29%29
// Im: https://www.wolframalpha.com/input/?i=simplify+im+%28%28a+%2B+b+i%29%5E%28c+%2B+d+i%29%29
let norm = PNorm(2.0, [a, b]);
let sqNorm = PowD(norm, 2.0);
let baseArg = ArgComplex(base_);
let prefactor = PowD(norm, c) * ExpD(-d * baseArg);
let angle = 0.5 * d * Log(sqNorm) + c * baseArg;
return ComplexPolar(
prefactor, angle
);
}
/// # Summary
/// Returns a number raised to a given power.
///
/// # Input
/// ## a
/// The number $a$ that is to be raised.
/// ## power
/// The power $b$ to which $a$ should be raised.
///
/// # Output
/// The power $a^b$
function PowC(a : Complex, power : Complex) : Complex {
return ComplexPolarAsComplex(
PowCAsCP(a, power)
);
}
/// # Summary
/// Returns a number raised to a given power.
///
/// # Input
/// ## a
/// The number $a$ that is to be raised.
/// ## power
/// The power $b$ to which $a$ should be raised.
///
/// # Output
/// The power $a^b$
function PowCP(a : ComplexPolar, power : ComplexPolar) : ComplexPolar {
return PowCAsCP(
ComplexPolarAsComplex(a),
ComplexPolarAsComplex(power)
);
}
/// # Summary
/// Returns the quotient of two inputs.
///
/// # Input
/// ## a
/// The first input $a$ to be divided.
/// ## b
/// The second input $b$ to be divided.
///
/// # Output
/// The quotient $a / b$.
function DividedByC(a : Complex, b : Complex) : Complex {
return TimesC(
a,
PowC(b, Complex(-1.0, 0.0))
);
}
/// # Summary
/// Returns the quotient of two inputs.
///
/// # Input
/// ## a
/// The first input $a$ to be divided.
/// ## b
/// The second input $b$ to be divided.
///
/// # Output
/// The quotient $a / b$.
function DividedByCP(a : ComplexPolar, b : ComplexPolar) : ComplexPolar {
return TimesCP(a, PowCP(b, ComplexPolar(1.0, PI())));
}
}