import numpy as np


def table4_2():
    # power method

    # number of iterations
    K = 9

    # matrix
    n = 2
    A = np.zeros((n, n))
    A[0, 0] = 2
    A[0, 1] = 1
    A[1, 0] = 1
    A[1, 1] = 2
    evalue = 3

    # start off guess
    # y=np.random.rand(n,1)
    y = np.array([2.0, 0.0])
    # y[0]=-3; y[1]=2

    k = 0
    vv = 100
    z = A @ y
    v = np.dot(y, z) / np.dot(y, y)
    err = abs(v - evalue)
    errv = 100
    print(f"{k:d} & {v:15.12f} & {np.divide(err, errv):5.2e}   ")

    # power method
    dv = 1.0
    dvv = 1.0
    for k in range(1, K + 1):
        vv = v
        dvv = dv
        errv = err
        y = z / np.linalg.norm(z, 2)
        z = A @ y
        v = np.dot(y, z)
        dv = v - vv
        err = abs(v - evalue)
        print(
            f"{k:d} & {v:15.12f} & {np.divide(err, errv):5.2e}  & "
            f"{np.divide(dv, dvv):5.2e} "
        )

    # y
    # z


if __name__ == "__main__":
    table4_2()
