"""Literal Python conversion of fig3_3.m."""

import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler


def _set_bold_ticks(ax, fontsize):
    ax.tick_params(labelsize=fontsize)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")


def fig3_3():
    A = np.zeros((2, 2))
    A[0, 0] = 1.2
    A[0, 1] = 2
    A[1, 0] = -1
    A[1, 1] = 1.8

    xL = 2.5

    plt.close("all")
    # get(gcf)
    # MATLAB Position [26 909 867 436], scaled to fit the plot window.
    fig, axes = plt.subplots(1, 2, figsize=(10.5, 5.3))
    # subaxis spacing is represented by subplots_adjust.
    fig.subplots_adjust(top=0.97, bottom=0.17, right=0.98, left=0.07, wspace=0.20)
    co = np.array([
        [0, 0, 1],
        [0, 0.5, 0],
        [1, 0, 0],
        [0, 0.75, 0.75],
        [0.75, 0, 0.75],
        [0.75, 0.75, 0],
        [0.25, 0.25, 0.25],
    ])
    for ax in axes:
        ax.set_prop_cycle(cycler(color=co.tolist()))

    nt = 200
    th = np.linspace(0, 2 * np.pi, nt)

    ax = axes[0]

    print("cond(A,inf) =", np.linalg.cond(A, np.inf))
    print("cond(A,2) =", np.linalg.cond(A, 2))
    x0 = np.zeros(nt)
    y0 = np.zeros(nt)
    x1 = np.zeros(nt)
    y1 = np.zeros(nt)
    for it in range(nt):
        x0[it] = np.cos(th[it])
        y0[it] = np.sin(th[it])
        x1[it] = A[0, 0] * x0[it] + A[0, 1] * y0[it]
        y1[it] = A[1, 0] * x0[it] + A[1, 1] * y0[it]
    ax.plot(x0, y0, "r", linewidth=2.5)
    ax.plot([-xL, xL], [0, 0], "k", linewidth=0.3)
    ax.plot([0, 0], [-xL, xL], "k", linewidth=0.3)
    ax.set_aspect("equal", adjustable="box")
    ax.set_frame_on(True)
    ax.grid(True)
    ax.set_xlabel("x_1-axis", fontsize=17, fontweight="bold")
    ax.set_ylabel("x_2-axis", fontsize=17, fontweight="bold")
    ax.axis([-xL, xL, -xL, xL])
    ax.set_xticks([-2, -1, 0, 1, 2])
    ax.set_yticks([-2, -1, 0, 1, 2])

    # say = r'$\kappa_\infty = 3$'
    # ax.text(0.5, -2.5, say, fontsize=20, fontweight='bold')
    _set_bold_ticks(ax, 17)

    ax = axes[1]

    ax.plot(x1, y1, "b", linewidth=2)
    ax.plot([-xL, xL], [0, 0], "k", linewidth=0.3)
    ax.plot([0, 0], [-xL, xL], "k", linewidth=0.3)
    ax.set_aspect("equal", adjustable="box")
    ax.axis([-xL, xL, -xL, xL])
    ax.set_xticks([-2, -1, 0, 1, 2])
    ax.set_yticks([-2, -1, 0, 1, 2])
    ax.set_frame_on(True)
    ax.grid(True)
    ax.set_xlabel("y_1-axis", fontsize=17, fontweight="bold")
    ax.set_ylabel("y_2-axis", fontsize=17, fontweight="bold")

    U, s, Vh = np.linalg.svd(A)
    S = np.diag(s)
    V = Vh.T
    ax.plot([0, S[0, 0] * U[0, 0]], [0, S[0, 0] * U[0, 1]], "k", linewidth=0.8)
    ax.plot([0, -S[1, 1] * U[0, 1]], [0, -S[1, 1] * U[1, 1]], "k", linewidth=0.8)
    ax.text(1.2, 0.72, r"$a$", fontsize=26)
    ax.text(-0.83, 0.55, r"$b$", fontsize=26)

    _set_bold_ticks(ax, 17)

    plt.show()
    return fig, axes

    # fig.savefig('/Users/mark/Desktop/norM.eps')


if __name__ == "__main__":
    fig3_3()
