Sha256: 866fb2ba3ae4e77285ecd1870e7d3d057c7198f5c640cff0ad188bfb63207141
Contents?: true
Size: 1.48 KB
Versions: 91
Compression:
Stored size: 1.48 KB
Contents
import math class ComplexNumber(object): def __init__(self, real, imaginary): self.real = real self.imaginary = imaginary def __eq__(self, other): return self.real == other.real and self.imaginary == other.imaginary def __add__(self, other): r = self.real + other.real i = self.imaginary + other.imaginary return ComplexNumber(r, i) def __mul__(self, other): r = self.real * other.real - self.imaginary * other.imaginary i = self.real * other.imaginary + self.imaginary * other.real return ComplexNumber(r, i) def __sub__(self, other): r = self.real - other.real i = self.imaginary - other.imaginary return ComplexNumber(r, i) def __truediv__(self, other): d = other.real * other.real + other.imaginary * other.imaginary r = (self.real * other.real + self.imaginary * other.imaginary) / float(d) i = (self.imaginary * other.real - self.real * self.real * other.imaginary) / float(d) return ComplexNumber(r, i) def __abs__(self): square_sum = self.real * self.real + self.imaginary * self.imaginary return math.sqrt(square_sum) def conjugate(self): return ComplexNumber(self.real, -1 * self.imaginary) def exp(self): r = round(math.cos(self.imaginary), 8) * math.exp(self.real) i = round(math.sin(self.imaginary), 8) * math.exp(self.real) return ComplexNumber(r, i)
Version data entries
91 entries across 91 versions & 1 rubygems