{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fyrirlestur 10\n",
    "\n",
    "Dagsetning: 16. mars\n",
    "\n",
    "Python uppsetning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "%matplotlib inline\n",
    "import matplotlib\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lausnir línulegra jöfnuhneppa\n",
    "\n",
    "Hægt er að fá lausnina beint með því að kalla á `np.linalg.solve`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "A = np.array([[3,1], [1,2]])\n",
    "print(A)\n",
    "b = np.array([9,8])\n",
    "x = np.linalg.solve(A, b)\n",
    "print(x)\n",
    "np.allclose(np.dot(A, x), b) # allclose bera saman tvo vigra, \n",
    "print(A.dot(x))\n",
    "print(A*x)\n",
    "print(A @ x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Andhverfur fylkja\n",
    "\n",
    "Fyrir ferningslaga fylki er andhverfan fundin með `np.linalg.inv`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Ainv = np.linalg.inv(A)\n",
    "print(A)\n",
    "print(Ainv)\n",
    "np.allclose(np.dot(A,Ainv),np.eye(2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## QR-þáttun\n",
    "\n",
    "Fallið `np.linalg.qr` gefur QR-þáttun"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "A = np.array([[1,1],[1,-1],[2,1]])\n",
    "Q,R = np.linalg.qr(A)\n",
    "print(Q)\n",
    "print(R)\n",
    "print(Q.dot(R))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Margliðubrúun\n",
    "\n",
    "Vandermonde fylkið er útfært í numpy með `vander` fallinu, setjum `increasing=True` til að fá veldin í vaxandi röð, en numpy vill frekar hafa það í minnkandi röð"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = np.array([-1.1,-0.4,0.1,0.8])\n",
    "A = np.vander(x,increasing=False)\n",
    "print(A)\n",
    "print(np.linalg.inv(A))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "b = np.array([1.0,-4,0,3])\n",
    "c = np.linalg.solve(A,b)\n",
    "c2 = np.polyfit(x,b,3) # polyfit er innbyggða numpy fallið sem gerir sama hlut\n",
    "p = np.poly1d(c)\n",
    "p2 = np.poly1d(c2)\n",
    "print(p)\n",
    "print(p2)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "t = np.arange(-1.1,0.8,0.01)\n",
    "_ = plt.plot(x,b,'ro',t,p(t),'-')\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gerviandhverfur\n",
    "Fást með `lingalg.pinv` fallinu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "A = np.array([[1,1],[1,-1],[2,1]])\n",
    "Ai = np.linalg.pinv(A)\n",
    "print(Ai)\n",
    "print(Ai.dot(A))\n",
    "np.allclose(Ai,np.linalg.inv(A.T.dot(A)).dot(A.T)) # venjulega formúlan\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
