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.139 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.138 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.137 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.136 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.135 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.134 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.133 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.132 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.131 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.130 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.129 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.128 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.127 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.126 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.125 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.124 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.123 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.122 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.121 tracks/python/exercises/complex-numbers/example.py
trackler-2.2.1.120 tracks/python/exercises/complex-numbers/example.py