{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# MATH 4242 Python Lab 8: Linear maps and change of basis\n",
        "\n",
        "**Course sections:** 7.1--7.2\n",
        "\n",
        "This notebook accompanies LEAP Assignment 8. LEAP supplies your\n",
        "parameter values and grades your mathematical responses. Copy those\n",
        "values into the indicated cell below. Run a cell with **Shift+Enter**.\n",
        "\n",
        "The notebook is a computational scratchpad: your graded answers are\n",
        "entered in LEAP unless your instructor says otherwise.\n",
        "\n",
        "**Programming support:** Supported independence. The mathematical goal and useful NumPy tools are supplied; you assemble several lines.\n",
        "\n",
        "A cell marked `TODO` is intentionally unfinished. Read the directions\n",
        "immediately above it before running it. Placeholders such as zero keep\n",
        "the notebook runnable, but their output is not an answer. If you are\n",
        "new to notebooks,\n",
        "complete `00_Getting_Started.ipynb` before this lab.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "np.set_printoptions(precision=6, suppress=True)\n",
        "print(\"NumPy version:\", np.__version__)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1. Build a change-of-basis matrix\n",
        "\n",
        "The columns of $P$ are the basis vectors\n",
        "$b_1=(1,0)^T$ and $b_2=(s,1)^T$ in standard coordinates. Thus\n",
        "$x=P[x]_B$ converts $B$-coordinates to standard coordinates.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Replace these example values with your LEAP values.\n",
        "s = 2\n",
        "k = 4\n",
        "p = 3\n",
        "q = -1\n",
        "\n",
        "P = np.array([[1, s],\n",
        "              [0, 1]], dtype=float)\n",
        "D = np.array([[2, 0],\n",
        "              [0, k]], dtype=float)  # matrix of T in basis B\n",
        "x_B = np.array([p, q], dtype=float)\n",
        "\n",
        "# TODO: convert x_B to standard coordinates.\n",
        "x_standard = np.zeros(2)  # TODO: replace with P @ x_B\n",
        "print(\"x in standard coordinates:\", x_standard)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2. Find the standard matrix of the transformation\n",
        "\n",
        "The coordinate chain is: standard $\\to B$, apply $D$, then\n",
        "$B\\to$ standard. Read compositions from right to left.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# TODO: form the standard matrix M = P D P^{-1}.\n",
        "M = np.zeros((2, 2))  # TODO: replace with P @ D @ np.linalg.inv(P)\n",
        "print(\"standard matrix M =\\n\", M)\n",
        "\n",
        "# Apply T in two coordinate systems and compare.\n",
        "Tx_via_standard = np.zeros(2)  # TODO: use M and x_standard\n",
        "Tx_via_basis = np.ones(2)      # TODO: use P, D, and x_B\n",
        "print(\"T(x), standard route:\", Tx_via_standard)\n",
        "print(\"T(x), basis route:   \", Tx_via_basis)\n",
        "print(\"routes agree:\", np.allclose(Tx_via_standard, Tx_via_basis))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3. Similarity preserves eigenvalues\n",
        "\n",
        "`D` and `M` represent the same linear transformation in different\n",
        "bases. Their entries differ, but their eigenvalues agree.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(\"eigenvalues of D:\", np.linalg.eigvals(D))\n",
        "print(\"eigenvalues of M:\", np.linalg.eigvals(M))\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}
