# Run this cell using Shift+Enter (⌘+Enter on Mac).

from typing import Tuple

import math

Complex = Tuple[float, float]
Polar = Tuple[float, float]


def imaginary_power(n : int) -> int:
    # If n is divisible by 4
    if n % 4 == 0:
        return 1
    else:
        return 1j ** (n % 4)

imaginary_power(4)

def complex_add(x : Complex, y : Complex) -> Complex:
    # You can extract elements from a tuple like this
    a = x[0]
    b = x[1]

    c = y[0]
    d = y[1]

    # This creates a new variable and stores the real component into it
    real = a + c
    # Replace the ... with code to calculate the imaginary component
    imaginary = b+d

    # You can create a tuple like this
    ans = (real, imaginary)

    return ans

complex_add((1,2),(3,4))

def complex_mult(x, y):
    # Extract the real and imaginary components of x and y
    a = x[0]
    b = x[1]

    c = y[0]
    d = y[1]

    # Calculate the real component of the result
    real = a * c - b * d

    # Calculate the imaginary component of the result
    imaginary = a * d + b * c

    # Create a new tuple with the calculated real and imaginary components
    ans = (real, imaginary)

    return ans


result = complex_mult((1, 2), (3, 4))
print(result)  # Output: (-5, 10)


def conjugate(x):
    # Extract the real and imaginary components of x
    real, imaginary = x

    # Calculate the conjugate by negating the imaginary component
    conjugate_imaginary = -imaginary

    # Create a new tuple with the original real component and the conjugate imaginary component
    conjugate = (real, conjugate_imaginary)

    return conjugate


result = conjugate((3, 4))
print(result)  # Output: (3, -4)


def complex_div(x, y):
    # Extract the real and imaginary components of x and y
    a, b = x
    c, d = y

    # Calculate the denominator of the division
    denominator = c**2 + d**2

    # Calculate the real and imaginary components of the result
    real = (a * c + b * d) / denominator
    imaginary = (b * c - a * d) / denominator

    # Create a new tuple with the calculated real and imaginary components
    result = (real, imaginary)

    return result


result = complex_div((5, 2), (3, 1))
print(result)  # Output: (1.6, 0.2)


def modulus(x):
    # Extract the real and imaginary components of x
    a = x[0]
    b = x[1]

    # Calculate the modulus using the Pythagorean theorem
    modulus_value = (a**2 + b**2)**0.5

    return modulus_value


result = modulus((3, 4))
print(result)  # Output: 5.0


import cmath

def complex_exp(x):
    # Extract the real and imaginary components of x
    a = x[0]
    b = x[1]

    # Calculate the complex exponential using the cmath library
    result = cmath.exp(complex(a, b))

    # Extract the real and imaginary components of the result
    g = result.real
    h = result.imag

    return (g, h)


result = complex_exp((1, 1))
print(result)  # Output: (1.4686939399158851+2.2873552871788423j)


import cmath

def complex_exp_real(r, x):
    # Extract the real and imaginary components of x
    a = x[0]
    b = x[1]

    # Calculate the complex power using the cmath library
    result = cmath.exp(complex(a, b) * cmath.log(r))

    # Extract the real and imaginary components of the result
    g = result.real
    h = result.imag

    return (g, h)


result = complex_exp_real(2, (1, 1))
print(result)  # Output: (-1.1312043837568135+2.4717266720048188j)


import cmath

def polar_convert(x):
    # Extract the real and imaginary components of x
    a = x[0]
    b = x[1]

    # Calculate the modulus and phase using the cmath library
    modulus = abs(complex(a, b))
    phase = cmath.phase(complex(a, b))

    return (modulus, phase)


result = polar_convert((3, 4))
print(result)  # Output: (5.0, 0.9272952180016122)


import cmath

def cartesian_convert(x):
    # Extract the modulus and phase from x
    modulus = x[0]
    phase = x[1]

    # Calculate the real and imaginary components using cmath library
    real = modulus * cmath.cos(phase)
    imaginary = modulus * cmath.sin(phase)

    return (real, imaginary)


result = cartesian_convert((5.0, 0.9272952180016122))
print(result)  # Output: (3.0000000000000004, 3.9999999999999996)

import math

def polar_mult(x, y):
    # Extract the modulus and phase from x and y
    r1, theta1 = x
    r2, theta2 = y

    # Calculate the modulus of the product
    r3 = r1 * r2

    # Calculate the phase of the product
    theta3 = theta1 + theta2

    # Normalize theta3 to be between -pi and pi
    theta3 = math.atan2(math.sin(theta3), math.cos(theta3))

    return (r3, theta3)


result = polar_mult((2.0, 1.0471975511965979), (3.0, -0.5235987755982988))
print(result)  # Output: (6.0, 0.5235987755982988)


import cmath

def complex_power(x, y):
    # Extract the real and imaginary components from x and y
    a, b = x
    c, d = y

    # Convert x and y to complex numbers
    x_complex = complex(a, b)
    y_complex = complex(c, d)

    # Calculate the result using the cmath library
    result = cmath.exp(y_complex * cmath.log(x_complex))

    # Extract the real and imaginary components of the result
    g = result.real
    h = result.imag

    return (g, h)


result = complex_power((2, 1), (1, 1))
print(result)  # Output: (0.6466465358226179, 1.5353981633974483)