{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.linalg import hilbert, invhilbert\n",
    "from numpy.linalg import inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hilbert's matrix by definition:\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.5       , 0.33333333, 0.25      , 0.2       ],\n",
       "       [0.5       , 0.33333333, 0.25      , 0.2       , 0.16666667],\n",
       "       [0.33333333, 0.25      , 0.2       , 0.16666667, 0.14285714],\n",
       "       [0.25      , 0.2       , 0.16666667, 0.14285714, 0.125     ],\n",
       "       [0.2       , 0.16666667, 0.14285714, 0.125     , 0.11111111]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%precision 8\n",
    "\n",
    "print(\"Hilbert's matrix by definition:\")\n",
    "print()\n",
    "\n",
    "np.array([[1/(i + j - 1) for j in range(1, 6)] for i in range(1, 6)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hilbert's matrix using scipy.linalg.hilbert:\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.5       , 0.33333333, 0.25      , 0.2       ],\n",
       "       [0.5       , 0.33333333, 0.25      , 0.2       , 0.16666667],\n",
       "       [0.33333333, 0.25      , 0.2       , 0.16666667, 0.14285714],\n",
       "       [0.25      , 0.2       , 0.16666667, 0.14285714, 0.125     ],\n",
       "       [0.2       , 0.16666667, 0.14285714, 0.125     , 0.11111111]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%precision 8\n",
    "\n",
    "print(\"Hilbert's matrix using scipy.linalg.hilbert:\")\n",
    "print()\n",
    "\n",
    "H = hilbert(5)\n",
    "H"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hilbert's matrix inverted:\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 2.500e+01, -3.000e+02,  1.050e+03, -1.400e+03,  6.300e+02],\n",
       "       [-3.000e+02,  4.800e+03, -1.890e+04,  2.688e+04, -1.260e+04],\n",
       "       [ 1.050e+03, -1.890e+04,  7.938e+04, -1.176e+05,  5.670e+04],\n",
       "       [-1.400e+03,  2.688e+04, -1.176e+05,  1.792e+05, -8.820e+04],\n",
       "       [ 6.300e+02, -1.260e+04,  5.670e+04, -8.820e+04,  4.410e+04]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%precision 3\n",
    "\n",
    "print(\"Hilbert's matrix inverted:\")\n",
    "print()\n",
    "\n",
    "Hinv = invhilbert(5)\n",
    "Hinv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The identity matrix np.identity(5):\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%precision 3\n",
    "\n",
    "print(\"The identity matrix np.identity(5):\")\n",
    "print()\n",
    "\n",
    "np.identity(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hilbert's matrix multiplied by its inverse using H@Hinv:\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 1.000e+00,  0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00],\n",
       "       [ 0.000e+00,  1.000e+00,  0.000e+00,  0.000e+00,  0.000e+00],\n",
       "       [ 0.000e+00,  0.000e+00,  1.000e+00, -3.638e-12,  0.000e+00],\n",
       "       [ 0.000e+00,  0.000e+00,  0.000e+00,  1.000e+00,  0.000e+00],\n",
       "       [ 0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00,  1.000e+00]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%precision 3\n",
    "\n",
    "print(\"Hilbert's matrix multiplied by its inverse using H@Hinv:\")\n",
    "print()\n",
    "\n",
    "H@Hinv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hilbert's matrix multiplied by its inverse using H.dot(Hinv):\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 1.000e+00,  0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00],\n",
       "       [ 0.000e+00,  1.000e+00,  0.000e+00,  0.000e+00,  0.000e+00],\n",
       "       [ 0.000e+00,  0.000e+00,  1.000e+00, -3.638e-12,  0.000e+00],\n",
       "       [ 0.000e+00,  0.000e+00,  0.000e+00,  1.000e+00,  0.000e+00],\n",
       "       [ 0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00,  1.000e+00]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%precision 3\n",
    "\n",
    "print(\"Hilbert's matrix multiplied by its inverse using H.dot(Hinv):\")\n",
    "print()\n",
    "\n",
    "H.dot(Hinv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error matrix of H@Hinv - np.identity(5):\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00],\n",
       "       [ 0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00],\n",
       "       [ 0.000e+00,  0.000e+00,  0.000e+00, -3.638e-12,  0.000e+00],\n",
       "       [ 0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00],\n",
       "       [ 0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00,  0.000e+00]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%precision 3\n",
    "\n",
    "print(\"Error matrix of H@Hinv - np.identity(5):\")\n",
    "print()\n",
    "\n",
    "H@Hinv - np.identity(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The inverse of Hilbert's matrix inverted using numpy.linalg.inv(Hinv):\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.5       , 0.33333333, 0.25      , 0.2       ],\n",
       "       [0.5       , 0.33333333, 0.25      , 0.2       , 0.16666667],\n",
       "       [0.33333333, 0.25      , 0.2       , 0.16666667, 0.14285714],\n",
       "       [0.25      , 0.2       , 0.16666667, 0.14285714, 0.125     ],\n",
       "       [0.2       , 0.16666667, 0.14285714, 0.125     , 0.11111111]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%precision 8\n",
    "\n",
    "print(\"The inverse of Hilbert's matrix inverted using numpy.linalg.inv(Hinv):\")\n",
    "print()\n",
    "\n",
    "inv(Hinv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error matrix of H - inv(Hinv)\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[9.104e-15, 7.772e-15, 1.055e-15, 1.305e-15, 1.305e-15],\n",
       "       [1.443e-15, 1.221e-15, 9.159e-16, 5.551e-16, 7.216e-16],\n",
       "       [1.110e-15, 8.049e-16, 6.939e-16, 4.441e-16, 4.441e-16],\n",
       "       [9.992e-16, 6.939e-16, 5.274e-16, 3.886e-16, 3.469e-16],\n",
       "       [8.604e-16, 5.829e-16, 4.441e-16, 3.608e-16, 2.914e-16]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%precision 3\n",
    "\n",
    "print(\"Error matrix of H - inv(Hinv)\")\n",
    "print()\n",
    "\n",
    "H - inv(Hinv)"
   ]
  }
 ],
 "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
