11class ComplexNumber
2+
23 attr_reader :real , :imaginary
34
45 def initialize ( real , imaginary = 0 )
@@ -7,39 +8,39 @@ def initialize(real, imaginary = 0)
78 end
89
910 def ==( other )
10- ( self - other ) . abs < 1e-15
11+ ( self - other ) . abs < 1e-15
1112 end
1213
1314 def +( other )
14- self . class . new ( @ real + other . real , @ imaginary + other . imaginary )
15+ self . class . new ( real + other . real , imaginary + other . imaginary )
1516 end
1617
1718 def -( other )
18- self . class . new ( @ real - other . real , @ imaginary - other . imaginary )
19+ self . class . new ( real - other . real , imaginary - other . imaginary )
1920 end
2021
2122 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 )
2425 end
2526
2627 def /( other )
27- self * other . inv
28+ self * other . inv
2829 end
2930
3031 def abs
31- Math . sqrt ( ( self * self . conjugate ) . real )
32+ Math . sqrt ( ( self * conjugate ) . real )
3233 end
3334
3435 def conjugate
35- self . class . new ( @ real, -@ imaginary)
36+ self . class . new ( real , -imaginary )
3637 end
3738
3839 def inv
39- self . class . new ( @ real / abs **2 , -@ imaginary / abs **2 )
40+ self . class . new ( real / abs **2 , -imaginary / abs **2 )
4041 end
4142
4243 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 ) )
4445 end
4546end
0 commit comments