Sha256: f12a292418c66ea8fb1e7ec1952501e844f120001cec7eee179efeff9febc03d

Contents?: true

Size: 920 Bytes

Versions: 129

Compression:

Stored size: 920 Bytes

Contents

class ComplexNumber
  attr_reader :real, :imaginary

  def initialize(real, imaginary = 0)
    @real = real
    @imaginary = imaginary
  end

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

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

  def -(other)
    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)
  end

  def /(other)
    self*other.inv
  end

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

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

  def inv
   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))
  end
end

module BookKeeping
  VERSION = 1
end

Version data entries

129 entries across 129 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.179 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.178 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.177 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.176 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.175 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.174 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.173 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.172 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.171 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.170 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.169 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.167 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.166 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.165 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.164 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.163 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.162 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.161 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb
trackler-2.2.1.160 tracks/ruby/exercises/complex-numbers/.meta/solutions/complex_numbers.rb