{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Package Scipy and More Tools for Linear Algebra"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This work is licensed under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/)\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction\n",
    "\n",
    "The package package *Scipy* provides a a great array of funtions for scinetific computing;\n",
    "her we wil just exlore one part of it: some additional tools for linear algebra from module *linalg* within the package *Scipy*. \n",
    "This provides tools for solving simultaneous linear equations, for variations on the LU factorization seen in a numerical methods course, and much more.\n",
    "\n",
    "This module has the standard standard nickname `la`, so import it using that:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scipy.linalg as la"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "SciPy usually needs stuff from NumPy, so let's import that also:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name=Exercise-C></a>\n",
    "### Exercise C\n",
    "\n",
    "Use the Scipy function `solve` to solve $A x = b$ for $x$,\n",
    "where\n",
    "\n",
    "$$\n",
    "A = \\left[ \\begin{array}{ccc} 4. & 2. & 1. \\\\ 9. & 3. & 1. \\\\ 25. & 5. & 1. \\end{array} \\right]\n",
    "$$\n",
    "\n",
    "and\n",
    "\n",
    "$$b = [0.693147, 1.098612, 1.386294]$$\n",
    "\n",
    "as in Exercise A of section [Numpy Array Operations and Linear Algebra](array-operations-and-linear-algebra.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name=Exercise-D></a>\n",
    "### Exercise D\n",
    "Check this result by computing the *residual vector* $r = A x - b$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name=Exercise-D-bonus></a>\n",
    "#### Exercise D-bonus\n",
    "\n",
    "Check further by computing the maximum error, or *maximum norm*, or *infinity norm* of this: $\\|r\\|_\\infty = \\|A x - b\\|_\\infty$;\n",
    "\n",
    "that is, the maximum of the absolute values of the elements of $r$, $\\max_{i=1}^n |r_i|$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name=Exercise-E></a>\n",
    "### Exercise E\n",
    "\n",
    "Next use the Scipy function `lu` to compute the $PA = LU$ factorization of $A$.\n",
    "Check you work by verifying that:\n",
    "1. $P$ is a permutation matrix (a one in each row and column; the rest all zeros)\n",
    "1. $PA$ is got by permutating the rows of $A$\n",
    "1. $L$ is (unit) lower triangular (all zeros above the main diagonal; ones on the main diagonal)\n",
    "1. $U$ is upper triangular (all zeros below the main diagonal)\n",
    "1. The products $PA$ and $LU$ are the same (or very close; there might be rounding error).\n",
    "1. The \"residual matrix\" $R = PA - LU$ is zero (or very close; there might be rounding error)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name=Exercise-F></a>\n",
    "### Exercise F\n",
    "\n",
    "Use the above arrays $b$, $P$, $L$ and $U$ to solve $AX = b$ via $LUx = PAx = Pb$, as follows:\n",
    "1. Solve $Lc = Pb$ for $c$.\n",
    "2. Solve $Ux = c$ for $x$.\n",
    "\n",
    "At each step verify by looking at the errors, or *residuals,*: $Lc - Pb$, $Ux - c$, $Ax - b$.\n",
    "Better yet, compute the maximum norm of each of these errors."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name=Exercise-G></a>\n",
    "### Optional Exercise G (on the matrix norm, seen in a numerical methods course)\n",
    "\n",
    "Try to work out how to compute the matrix norm $\\| A \\|_\\infty$.\n",
    "\n",
    "Test your method on $A$, $L$ and $U$, and compare $\\| A \\|_\\infty$, $\\| PA \\|_\\infty$, and $\\| L \\|_\\infty \\| U \\|_\\infty$."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
