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

Version Path
trackler-2.2.1.180 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.179 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.178 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.177 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.176 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.175 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.174 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.173 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.172 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.171 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.170 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.169 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.167 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.166 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.165 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.164 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.163 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.162 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.161 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.160 tracks/python/exercises/complex-numbers/example.py