"""Literal Python conversion of fig3_4A.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_4A():
    A = np.zeros((2, 2))
    A[0, 0] = 1
    A[0, 1] = -1
    A[1, 0] = -1
    A[1, 1] = 2

    AA = np.linalg.inv(A)
    print("AA =\n", AA)

    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.16, right=0.98, left=0.08, wspace=0.22)
    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)

    # the circle
    ax = axes[0]

    print("norm(AA,inf) =", np.linalg.norm(AA, np.inf))
    print("cond(AA,inf) =", np.linalg.cond(AA, np.inf))
    print("cond(AA,2) =", np.linalg.cond(AA, 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] = AA[0, 0] * x0[it] + AA[0, 1] * y0[it]
        y1[it] = AA[1, 0] * x0[it] + AA[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("b_1-axis", fontsize=17, fontweight="bold")
    ax.set_ylabel("b_2-axis", fontsize=17, fontweight="bold")
    ax.axis([-xL, xL, -xL, xL])
    ax.set_xticks([-2, -1, 0, 1, 2], labels=["-2r", "-r", "0", "r", "2r"])
    ax.set_yticks([-2, -1, 0, 1, 2], labels=["-2r", "-r", "0", "r", "2r"])
    # say = r'$\kappa_\infty = 3$'
    # ax.text(0.5, -2.5, say, fontsize=20, fontweight='bold')
    _set_bold_ticks(ax, 17)

    # the ellipse
    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], labels=["-2r", "-r", "0", "r", "2r"])
    ax.set_yticks([-2, -1, 0, 1, 2], labels=["-2r", "-r", "0", "r", "2r"])
    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")
    xM = round(np.linalg.norm(x1, np.inf) - 0.5)
    yM = round(np.linalg.norm(y1, np.inf) + 0.5)
    say2 = r"$\kappa_\infty = 9$"
    say = "d = 2"
    yy = ax.get_ylim()
    xA = xM / 6
    yA = yy[0] * 0.8
    ax.text(xA, yA, say2, fontsize=18, fontweight="bold")
    ax.text(xA, yA * 0.8, say, fontsize=18, fontweight="bold")
    _set_bold_ticks(ax, 17)

    plt.show()
    return fig, axes

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


if __name__ == "__main__":
    fig3_4A()
