Skip to content

Commit

Permalink
Alignment, redundant self, space around operators
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
kotp committed Mar 19, 2024
1 parent 7e8509d commit 5e9b6b4
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions exercises/practice/complex-numbers/.meta/example.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ComplexNumber

attr_reader :real, :imaginary

def initialize(real, imaginary = 0)
Expand All @@ -7,39 +8,39 @@ def initialize(real, imaginary = 0)
end

def ==(other)
(self - other).abs < 1e-15
(self - other).abs < 1e-15
end

def +(other)
self.class.new(@real + other.real, @imaginary + other.imaginary)
self.class.new(real + other.real, imaginary + other.imaginary)
end

def -(other)
self.class.new(@real - other.real, @imaginary - other.imaginary)
self.class.new(real - other.real, imaginary - other.imaginary)
end

def *(other)
self.class.new(@real * other.real - @imaginary * other.imaginary,
@real * other.imaginary + @imaginary * other.real)
self.class.new(real * other.real - imaginary * other.imaginary,
real * other.imaginary + imaginary * other.real)
end

def /(other)
self*other.inv
self * other.inv
end

def abs
Math.sqrt((self*self.conjugate).real)
Math.sqrt((self * conjugate).real)
end

def conjugate
self.class.new(@real, -@imaginary)
self.class.new(real, -imaginary)
end

def inv
self.class.new(@real / abs**2, -@imaginary / abs**2)
self.class.new(real / abs**2, -imaginary / abs**2)
end

def exp
self.class.new(Math.exp(@real)) * self.class.new(Math.cos(@imaginary), Math.sin(@imaginary))
self.class.new(Math.exp(real)) * self.class.new(Math.cos(imaginary), Math.sin(imaginary))
end
end

0 comments on commit 5e9b6b4

Please sign in to comment.