-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcomplex.cr
344 lines (289 loc) · 6.67 KB
/
complex.cr
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# A complex number is a number represented in the form a + bi. In this form,
# a and b are real numbers, and i is an imaginary number such as i² = -1.
# The a is the real part of the number, and the b is the imaginary part of
# the number.
#
# ```
# require "complex"
#
# Complex.new(1, 0) # => 1.0 + 0.0i
# Complex.new(5, -12) # => 5.0 - 12.0i
#
# 1.to_c # => 1.0 + 0.0i
# 1.i # => 0.0 + 1.0i
# ```
struct Complex
# Returns the real part.
getter real : Float64
# Returns the imaginary part.
getter imag : Float64
def initialize(real : Number, imag : Number = 0)
@real = real.to_f
@imag = imag.to_f
end
def self.new(c : Complex)
c
end
# Determines whether `self` equals *other* or not.
def ==(other : Complex)
@real == other.real && @imag == other.imag
end
# :ditto:
def ==(other : Number)
self == other.to_c
end
# :ditto:
def ==(other)
false
end
# Returns `self`.
def to_c
self
end
# Returns the value as a `Float64` if possible (the imaginary part should be exactly zero),
# raises otherwise.
def to_f64
unless @imag.zero?
raise Exception.new "Complex number with non-zero imaginary part can't be converted to real number"
end
@real
end
# Returns the value as a `Float32` if possible (the imaginary part should be exactly zero),
# raises otherwise.
def to_f32
to_f64.to_f32
end
# See `#to_f64`.
def to_f
to_f64
end
# Returns the value as an `Int64` if possible (the imaginary part should be exactly zero),
# raises otherwise.
def to_i64
to_f64.to_i64
end
delegate to_i32, to_i16, to_i8, to: to_i64
# Returns the value as an `UInt64` if possible (the imaginary part should be exactly zero),
# raises otherwise.
def to_u64
to_f64.to_u64
end
delegate to_u32, to_u16, to_u8, to: to_u64
# See `#to_i32`.
def to_i
to_i32
end
# Writes this complex object to an *io*.
#
# ```
# require "complex"
#
# Complex.new(42, 2).to_s # => "42.0 + 2.0i"
# ```
def to_s(io : IO) : Nil
io << @real
io << (@imag >= 0 ? " + " : " - ")
io << @imag.abs
io << 'i'
end
# Writes this complex object to an *io*, surrounded by parentheses.
#
# ```
# require "complex"
#
# Complex.new(42, 2).inspect # => "(42.0 + 2.0i)"
# ```
def inspect(io : IO) : Nil
io << '('
to_s(io)
io << ')'
end
# Returns the absolute value of this complex number in a
# number form, using the Pythagorean theorem.
#
# ```
# require "complex"
#
# Complex.new(42, 2).abs # => 42.04759208325728
# Complex.new(-42, 2).abs # => 42.04759208325728
# ```
def abs
Math.hypot(@real, @imag)
end
# Returns the square of absolute value in a number form.
#
# ```
# require "complex"
#
# Complex.new(42, 2).abs2 # => 1768
# ```
def abs2
@real * @real + @imag * @imag
end
def sign
self / abs
end
# Returns the phase of `self`.
def phase
Math.atan2(@imag, @real)
end
# Returns a `Tuple` with the `abs` value and the `phase`.
#
# ```
# require "complex"
#
# Complex.new(42, 2).polar # => {42.047592083257278, 0.047583103276983396}
# ```
def polar
{abs, phase}
end
# Returns the conjugate of `self`.
#
# ```
# require "complex"
#
# Complex.new(42, 2).conj # => 42.0 - 2.0i
# Complex.new(42, -2).conj # => 42.0 + 2.0i
# ```
def conj
Complex.new(@real, -@imag)
end
# Returns the inverse of `self`.
def inv
conj / abs2
end
# `Complex#sqrt` was inspired by the [following blog post](https://pavpanchekha.com/casio/)
# of Pavel Panchekha on floating point precision.
#
def sqrt
r = abs
re = if @real >= 0
0.5 * Math.sqrt(2.0 * (r + @real))
else
@imag.abs / Math.sqrt(2 * (r - @real))
end
im = if @real <= 0
0.5 * Math.sqrt(2.0 * (r - @real))
else
@imag.abs / Math.sqrt(2 * (r + @real))
end
Complex.new(re, @imag >= 0 ? im : -im)
end
# Calculates the exp of `self`.
#
# ```
# require "complex"
#
# Complex.new(4, 2).exp # => -22.720847417619233 + 49.645957334580565i
# ```
def exp
r = Math.exp(@real)
Complex.new(r * Math.cos(@imag), r * Math.sin(@imag))
end
# Calculates the log of `self`.
def log
Complex.new(Math.log(abs), phase)
end
# Calculates the log2 of `self`.
def log2
log / Math::LOG2
end
# Calculates the log10 of `self`.
def log10
log / Math::LOG10
end
# Returns absolute value of `self`.
def +
Complex.new(@real.abs, @imag.abs)
end
# Adds the value of `self` to *other*.
def +(other : Complex)
Complex.new(@real + other.real, @imag + other.imag)
end
# :ditto:
def +(other : Number)
Complex.new(@real + other, @imag)
end
# Returns the opposite of `self`.
def -
Complex.new(-@real, -@imag)
end
# Removes the value of *other* from `self`.
def -(other : Complex)
Complex.new(@real - other.real, @imag - other.imag)
end
# :ditto:
def -(other : Number)
Complex.new(@real - other, @imag)
end
# Multiplies `self` by *other*.
def *(other : Complex)
Complex.new(@real * other.real - @imag * other.imag, @real * other.imag + @imag * other.real)
end
# :ditto:
def *(other : Number)
Complex.new(@real * other, @imag * other)
end
# Divides `self` by *other*.
def /(other : Complex)
if other.real <= other.imag
r = other.real / other.imag
d = other.imag + r * other.real
Complex.new((@real * r + @imag) / d, (@imag * r - @real) / d)
else
r = other.imag / other.real
d = other.real + r * other.imag
Complex.new((@real + @imag * r) / d, (@imag - @real * r) / d)
end
end
# :ditto:
def /(other : Number)
Complex.new(@real / other, @imag / other)
end
def clone
self
end
# See `Object#hash(hasher)`
def hash(hasher)
hasher = real.hash(hasher)
hasher = imag.hash(hasher) unless imag.zero?
hasher
end
# Returns the number `0` in complex form.
def self.zero : Complex
new 0, 0
end
def zero? : Bool
@real == 0 && @imag == 0
end
# Rounds to the nearest *digits*.
def round(digits = 0)
Complex.new(@real.round(digits), @imag.round(digits))
end
end
struct Number
def to_c
Complex.new(self, 0)
end
def i
Complex.new(0, self)
end
def ==(other : Complex)
to_c == other
end
def cis
Complex.new(Math.cos(self), Math.sin(self))
end
def +(other : Complex)
Complex.new(self + other.real, other.imag)
end
def -(other : Complex)
Complex.new(self - other.real, -other.imag)
end
def *(other : Complex)
Complex.new(self * other.real, self * other.imag)
end
def /(other : Complex)
self * other.inv
end
end