Python Complex Number Library Code

Drclass ComplexNumber:
    def __init__(self, real_part, imaginary_part):
        self.real = real_part
        self.imaginary = imaginary_part

    def __str__(self):
        if self.imaginary >= 0:
            return f"{self.real} + {self.imaginary}i"
        else:
            return f"{self.real} - {-self.imaginary}i"

    def add(self, other):
        real_sum = self.real + other.real
        imaginary_sum = self.imaginary + other.imaginary
        return ComplexNumber(real_sum, imaginary_sum)

    def subtract(self, other):
        real_diff = self.real - other.real
        imaginary_diff = self.imaginary - other.imaginary
        return ComplexNumber(real_diff, imaginary_diff)

    def multiply(self, other):
        real_product = self.real * other.real - self.imaginary * other.imaginary
        imaginary_product = self.real * other.imaginary + self.imaginary * other.real
        return ComplexNumber(real_product, imaginary_product)

    def conjugate(self):
        return ComplexNumber(self.real, -self.imaginary)

    def modulus(self):
        return (self.real ** 2 + self.imaginary ** 2) ** 0.5

    def divide(self, other):
        real_dividend = (self.real * other.real + self.imaginary * other.imaginary) / (other.real ** 2 + other.imaginary ** 2)
        imaginary_dividend = (self.imaginary * other.real - self.real * other.imaginary) / (other.real ** 2 + other.imaginary ** 2)
        return ComplexNumber(real_dividend, imaginary_dividend)

    @staticmethod
    def real_to_complex(number):
        return ComplexNumber(number, 0)

    @staticmethod
    def imaginary_to_complex(number):
        return ComplexNumber(0, number)

    @staticmethod
    def from_polar(magnitude, phase):
        real = magnitude * math.cos(phase)
        imaginary = magnitude * math.sin(phase)
        return ComplexNumber(real, imaginary)

    def to_polar(self):
        magnitude = self.modulus()
        phase = math.atan2(self.imaginary, self.real)
        return magnitude, phase

This version of the code includes additional methods for dividing complex numbers, converting real and imaginary numbers to complex numbers, converting complex numbers from polar form to rectangular form (real and imaginary), and vice versa, converting complex numbers from rectangular form to polar form.

The division method calculates the division between two complex numbers.

The static methods `real_to_complex` and `imaginary_to_complex` convert a real or imaginary number to a complex number with the real or imaginary part set accordingly.

The static method `from_polar` converts a complex number from polar form, where `magnitude` is the distance from the origin and `phase` is the angle from the positive real axis, to rectangular form.

The `to_polar` method converts a complex number from rectangular form to polar form, returning the magnitude and phase.

These additions provide a more comprehensive range of operations and conversions for complex numbers.