import numpy as np
import cmath


def action_p(z):
    sqrt_y = np.sqrt(z.imag)
    return np.array([[sqrt_y, z.real/sqrt_y], [0, 1.0/sqrt_y]])

def rotation(t):
    cos_t = np.cos(t)
    sin_t = np.sin(t)
    return np.array([[cos_t, sin_t], [-sin_t, cos_t]])

def main():
    z = complex(input("input half-plane point: "))
    if z.imag <= 0:
        print("input not in half-plane.")
        return

    w = complex(input("input unit tangent vector: "))
    if abs(w) != z.imag:
        print("input not a unit tangent vector.")
        return

    angle_of_rotation = (cmath.phase(w) - np.pi/2.)/2.

    print(f"angle of rotation: {angle_of_rotation}")

    iwasawa_action = action_p(z).dot(rotation(angle_of_rotation))

    print(f"matrix corresponding to the input (up to +-I): {iwasawa_action}")

if __name__ == "__main__":
    main()
