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.119 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.118 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.117 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.116 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.115 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.114 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.113 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.111 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.110 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.109 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.108 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.107 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.106 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.105 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.104 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.103 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.102 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.101 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.100 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.99 tracks/python/exercises/complex-numbers/example.py