{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# MATH 4242 Python Lab 3: Determinants, row exchanges, and transposes\n",
        "\n",
        "**Course sections:** 1.8--2.1\n",
        "\n",
        "This notebook accompanies LEAP Assignment 3. 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:** Guided practice. Examples and function names are supplied; you complete short expressions marked `TODO`.\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. A triangular matrix\n",
        "\n",
        "Copy `a`, `b`, and `c` from LEAP. Because this matrix is triangular,\n",
        "its exact determinant is available without any elimination.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Replace these example values with your LEAP values.\n",
        "a = 2\n",
        "b = 3\n",
        "c = 4\n",
        "\n",
        "A = np.array([[a, 1, 0],\n",
        "              [0, b, 2],\n",
        "              [0, 0, c]], dtype=float)\n",
        "print(A)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# TODO: compute the exact determinant from the diagonal, then compare\n",
        "# with NumPy's floating-point computation.\n",
        "det_exact = 0  # TODO: replace 0 with the diagonal product\n",
        "det_numpy = 0  # TODO: replace 0 with np.linalg.det(A)\n",
        "print(\"exact determinant:\", det_exact)\n",
        "print(\"NumPy determinant:\", det_numpy)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2. Exchange two rows\n",
        "\n",
        "Advanced indexing creates the row-swapped matrix `B`. Predict the\n",
        "determinant before running the cell.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "B = A[[1, 0, 2], :]\n",
        "# TODO: compute det(B).\n",
        "det_B = 0  # TODO: replace 0 with np.linalg.det(B)\n",
        "print(\"B =\\n\", B)\n",
        "print(\"det(B) =\", det_B)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3. Test the transpose-of-a-product rule\n",
        "\n",
        "The order reverses when a product is transposed. Use a second matrix\n",
        "of compatible size to check the rule numerically.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "C = np.array([[1, 2],\n",
        "              [0, 1],\n",
        "              [-1, 3]], dtype=float)\n",
        "\n",
        "left = (A @ C).T\n",
        "# TODO: form the mathematically correct right-hand side.\n",
        "right = np.zeros_like(left)  # TODO: replace with C.T @ A.T\n",
        "\n",
        "print(\"left =\\n\", left)\n",
        "print(\"right =\\n\", right)\n",
        "print(\"identity verified:\", np.allclose(left, right))\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
}
