1
1
class ComplexNumber
2
+
2
3
attr_reader :real , :imaginary
3
4
4
5
def initialize ( real , imaginary = 0 )
@@ -7,39 +8,39 @@ def initialize(real, imaginary = 0)
7
8
end
8
9
9
10
def ==( other )
10
- ( self - other ) . abs < 1e-15
11
+ ( self - other ) . abs < 1e-15
11
12
end
12
13
13
14
def +( other )
14
- self . class . new ( @ real + other . real , @ imaginary + other . imaginary )
15
+ self . class . new ( real + other . real , imaginary + other . imaginary )
15
16
end
16
17
17
18
def -( other )
18
- self . class . new ( @ real - other . real , @ imaginary - other . imaginary )
19
+ self . class . new ( real - other . real , imaginary - other . imaginary )
19
20
end
20
21
21
22
def *( other )
22
- self . class . new ( @ real * other . real - @ imaginary * other . imaginary ,
23
- @ real * other . imaginary + @ imaginary * other . real )
23
+ self . class . new ( real * other . real - imaginary * other . imaginary ,
24
+ real * other . imaginary + imaginary * other . real )
24
25
end
25
26
26
27
def /( other )
27
- self * other . inv
28
+ self * other . inv
28
29
end
29
30
30
31
def abs
31
- Math . sqrt ( ( self * self . conjugate ) . real )
32
+ Math . sqrt ( ( self * conjugate ) . real )
32
33
end
33
34
34
35
def conjugate
35
- self . class . new ( @ real, -@ imaginary)
36
+ self . class . new ( real , -imaginary )
36
37
end
37
38
38
39
def inv
39
- self . class . new ( @ real / abs **2 , -@ imaginary / abs **2 )
40
+ self . class . new ( real / abs **2 , -imaginary / abs **2 )
40
41
end
41
42
42
43
def exp
43
- self . class . new ( Math . exp ( @ real) ) * self . class . new ( Math . cos ( @ imaginary) , Math . sin ( @ imaginary) )
44
+ self . class . new ( Math . exp ( real ) ) * self . class . new ( Math . cos ( imaginary ) , Math . sin ( imaginary ) )
44
45
end
45
46
end
0 commit comments