{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vectors from Shakespeare's plays\n",
    "We look at the examples from J&M, chapter 6, Shakespeare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "AYLI = np.array([1, 114, 36, 20])\n",
    "TwNi = np.array([0, 80, 58, 15])\n",
    "JuCa = np.array([7, 62, 1, 2])\n",
    "HenV = np.array([13, 89, 4, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cos(a, b):\n",
    "    return a@b /(np.sqrt(a@a) * np.sqrt(b@b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9999999999999999"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cos(AYLI, AYLI)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        AYLI   TwNi   JuCa   HenV   \n",
      "AYLI    1.000  0.950  0.945  0.949\n",
      "TwNi    0.950  1.000  0.809  0.822\n",
      "JuCa    0.945  0.809  1.000  0.999\n",
      "HenV    0.949  0.822  0.999  1.000\n"
     ]
    }
   ],
   "source": [
    "plays = ['AYLI', 'TwNi', 'JuCa', 'HenV']\n",
    "print(8*\" \"+\"{d[0]:7}{d[1]:7}{d[2]:7}{d[3]:7}\".format(d=plays))\n",
    "for p1 in plays:\n",
    "    coss = [cos(eval(p1), eval(p2)) for p2 in plays]\n",
    "    print(\"{p:6}{d[0]:7.3f}{d[1]:7.3f}{d[2]:7.3f}{d[3]:7.3f}\".format(p=p1, d=coss))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Only battles and fools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "bf = np.array([0,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        AYLI   TwNi   JuCa   HenV   \n",
      "AYLI    1.000  1.000  0.169  0.321\n",
      "TwNi    1.000  1.000  0.141  0.294\n",
      "JuCa    0.169  0.141  1.000  0.988\n",
      "HenV    0.321  0.294  0.988  1.000\n"
     ]
    }
   ],
   "source": [
    "plays = ['AYLI', 'TwNi', 'JuCa', 'HenV']\n",
    "print(8*\" \"+\"{d[0]:7}{d[1]:7}{d[2]:7}{d[3]:7}\".format(d=plays))\n",
    "for p1 in plays:\n",
    "    coss = [cos(eval(p1)[bf], eval(p2)[bf]) for p2 in plays]\n",
    "    print(\"{p:6}{d[0]:7.3f}{d[1]:7.3f}{d[2]:7.3f}{d[3]:7.3f}\".format(p=p1, d=coss))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9499125103160965"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cos(AYLI, TwNi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1, 36])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AYLI[bf]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 58])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TwNi[bf]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9996144206527183"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cos(AYLI[bf],TwNi[bf])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
