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