1  数値解の収束性:25点
2  丸め誤差:25点
3  Newtonの差分商公式:25点
4  ページランク:25点
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "
\n", "数値計算プレ試験解答例(2015)\n", "
\n", "
\n", "
\n", " 18/12/14 関西学院大学 西谷滋人　\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 数値解の収束性:25点\n", "\n", "次の関数\n", "$$\n", "f(x)=-4\\,\\exp(-x)+2\\,\\exp(-2\\, x)\n", "$$\n", "は$x=-1..0$に解$-\\ln(2)$を持つ．二分法によって数値解を求めよ．繰り返しは１０回程度でいい．また，収束の様子を片対数(logplot)でプロットせよ．" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xd4FXXe/vH3NyEkIaGFcqRIUzorssEA8lvKonRE3FWExayKBiyAS6gLSMQWMPBQlgdFRUCRsEtRaYKuQcBCswBS3NBBiooIgaXm+/sjkcdCCTmTM6fcr+uai4TMzLk/Jt5M5syZY6y1iIhI8AhzO4CIiDhLxS4iEmRU7CIiQUbFLiISZFTsIiJBRsUuIhJkVOwiIkFGxS4iEmRU7CIiQaaQGw9aunRpW6VKlXxte/LkSWJiYpwN5Oc0c2jQzKHBm5k3bNjwnbW2zNXWc6XYq1Spwvr16/O17YoVK2jRooWzgfycZg4Nmjk0eDOzMWZPXtbTqRgRkSDjWLEbY8KNMZ8bYxY5tU8REbl2Th6x9wO2Org/ERHJB0eK3RhTEegAvOLE/kREJP+cOmIfDwwCsh3an4iI5JPx9o02jDEdgfbW2keNMS2AAdbajpdYLwlIAvB4PPHp6en5erysrCxiY2O9SBx4NHNo0MyhwZuZW7ZsucFa2/CqK1prvVqA54H9wG7gEHAKeONK28THx9v8ysjIyPe2gUozhwbNHBq8m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Lb+IViLzMPNRaW9FaW4Wcn+sP/LnU8+CqM+f+PL8KbLXWjvNhNqfkpY/eARJzr45pTM6p1IO/3lG+WWv9egG6kHOkdgY4DCzL/fvywJKfrdce+BrYQc4pHNezezFzKXKuGPgP8D4Qd5mZnwK2kXPl0OtApNvZfTDzzcB6YCPwFlDS7ewFPfPP1m8BLHI7d0HPDPw/wOZ+j7/IXdq7nf0a5/xNHwG9gd65HxtyrpzZAWwi5+o2xx5frzwVEQkywXIqRkREcqnYRUSCjIpdRCTIqNhFRIKMil1EJMio2EVEgoyKXUQkyKjYRUSCzP8Hl/ZXsrspAhQAAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "def func(x):\n", " return -4*np.exp(-x)+2*np.exp(-2*x)\n", "\n", "x1, x2 = -1.0, 0.0\n", "x = np.linspace(x1,x2, 101)\n", "y = func(x)\n", "plt.plot(x, y, color = 'k')\n", "plt.plot([x1,x2],[0,0])\n", "plt.grid()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " x1 x2 f1 f2\n", " -1.0000000000 +0.0000000000 +3.9049848840 -2.0000000000\n", " -1.0000000000 -0.5000000000 +3.9049848840 -1.1583214259\n", " -0.7500000000 -0.5000000000 +0.4953780742 -1.1583214259\n", " -0.7500000000 -0.6250000000 +0.4953780742 -0.4922979148\n", " -0.7500000000 -0.6875000000 +0.4953780742 -0.0447964325\n", " -0.7187500000 -0.6875000000 +0.2128474183 -0.0447964325\n", " -0.7031250000 -0.6875000000 +0.0810265592 -0.0447964325\n", " -0.6953125000 -0.6875000000 +0.0173789137 -0.0447964325\n", " -0.6953125000 -0.6914062500 +0.0173789137 -0.0138911236\n", " -0.6933593750 -0.6914062500 +0.0016980959 -0.0138911236\n", " -0.6933593750 -0.6923828125 +0.0016980959 -0.0061079375\n", " -0.6933593750 -0.6928710938 +0.0016980959 -0.0022077800\n", " -0.6933593750 -0.6931152344 +0.0016980959 -0.0002555572\n", " -0.6932373047 -0.6931152344 +0.0007210905 -0.0002555572\n", " -0.6931762695 -0.6931152344 +0.0002327219 -0.0002555572\n", " -0.6931762695 -0.6931457520 +0.0002327219 -0.0000114288\n", " -0.6931610107 -0.6931457520 +0.0001106438 -0.0000114288\n", " -0.6931533813 -0.6931457520 +0.0000496068 -0.0000114288\n", " -0.6931495667 -0.6931457520 +0.0000190888 -0.0000114288\n", " -0.6931476593 -0.6931457520 +0.0000038299 -0.0000114288\n", " -0.6931476593 -0.6931467056 +0.0000038299 -0.0000037995\n", "\n" ] } ], "source": [ "x1, x2 = -1.0, 0.0\n", "f1, f2 = func(x1), func(x2)\n", "print('%+15s %+15s %+15s %+15s' % ('x1','x2','f1','f2'))\n", "print('%+15.10f %+15.10f %+15.10f %+15.10f' % (x1,x2,f1,f2))\n", "x0 = -np.log(2.0)\n", "list_bisec = [[0],[abs(x1-x0)]]\n", "for i in range(0, 20):\n", " x = (x1 + x2)/2\n", " f = func(x)\n", " if (f*f1>=0.0):\n", " x1, f1 = x, f\n", " list_bisec[0].append(i)\n", " list_bisec[1].append(abs(x1-x0))\n", " else:\n", " x2, f2 = x, f\n", " list_bisec[0].append(i)\n", " list_bisec[1].append(abs(x2-x0))\n", "\n", " print('%+15.10f %+15.10f %+15.10f %+15.10f' % (x1,x2,f1,f2))\n", "\n", "list_bisec\n", "print()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD8CAYAAAB+UHOxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xd4VGXa+PHvPSkEkhBKSAgTIEBCCYQakWJBQQUBAUWFtaAi2MXd9d3Vn7u+23x33XVdy9pAEFQEwYKoiK4VpBp6hxhaAiShJpT05/dHhjWGB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0xtbwz0CmPMEuBopc2jgFmun2cBo6t4qTvvVZ/EZ4z5whhT4nq6Eoj3drnuqub6ucOy63eWiAhwEzDH2+Vapa4nACewv8LzTM79gHXnGJ8TkQSgF7Cqit0DXNXzz0Skq18DAwN8KSJrRGRyFfttcf2AcVT/H8/K6wcQa4w56Pr5EBBbxTF2uY53UV6jq8qF3gu+9JDrbzijmiY0O1y/S4FsY8yuavZbef1qpa4ngDpBRCKA94FHjDF5lXavBdoYY7oDLwIL/BzeJcaYnsAw4AERuczP5V+QiIQC1wHzq9ht9fX7GVPeFmDLoXUi8gRQAsyu5hCr3guvUN600xM4SHkzix2N5/zf/m3/f6myup4AsoDWFZ7Hu7bV9BifEZEQyj/8ZxtjPqi83xiTZ4w56fp5ERAiItH+is8Yk+X6Nwf4kPKqdkWWXj+XYcBaY0x25R1WXz+X7LPNYq5/c6o4xur34R3ACOAWV5I6hxvvBZ8wxmQbY0qNMWXAtGrKtfr6BQPXA+9Wd4xV188TdT0B/AAkiUg717fEccDCSscsBG53jWbpB5yoUF33KVeb4XRgmzHm2WqOaek6DhHpS/nf5Iif4gsXkcizP1PeWbi50mGWXb8Kqv3mZeX1q2AhMMH18wTgoyqOcee96hMiMhT4DXCdMeZ0Nce4817wVXwV+5TGVFOuZdfPZQiw3RiTWdVOK6+fR6zuhfb0QfkolZ2UjxB4wrXtXuBe188CvOTavwlI9WNsl1DeHLARWO96XFspvgeBLZSPalgJDPBjfO1d5W5wxWCr6+cqP5zyD/SoCtssu36UJ6KDQDHl7dATgebAV8Au4EugmevYVsCi871X/RRfOuXt52ffg69Wjq+694Kf4nvL9d7aSPmHepydrp9r+8yz77kKx/r9+nn7oXcCK6VUPVXXm4CUUkrVkiYApZSqpzQBKKVUPaUJQCml6ilNAEopVU9pAlBKqXpKE4BSStVTmgCUUqqe+v/YXqNtW3TMMwAAAABJRU5ErkJggg==\n", "text/plain": [ "" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "\n", "X = list_bisec[0]\n", "Y = list_bisec[1]\n", "plt.plot(X, Y)\n", "\n", "plt.yscale(\"log\") # y軸を対数目盛に\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 丸め誤差:25点\n", "\n", "大きな数どおしのわずかな差は，丸め誤差にとくに影響を受ける．\n", "1. 23.173-23.094を有効数字がそれぞれ５桁，４桁，３桁，２桁で計算した結果を示せ．\n", "1. 同様に，0.81321/(23.173-23.094)を有効数字がそれぞれ５桁，４桁，３桁，２桁で計算した結果を示せ．\n", "\n", "(E.クライツィグ著「数値解析」(培風館,2003), p.10, 問題1.1-3改)\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "from decimal import *\n", "\n", "def pretty_p(result,a,b,operator):\n", " print('context.prec:{}'.format(getcontext().prec))\n", " print(' %20.14f' % (a))\n", " print( '%1s%20.14f' % (operator, b))\n", " print('-----------')\n", " print( ' %20.14f' % (result))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "context.prec:5\n", " 23.17300000000000\n", "- 23.09400000000000\n", "-----------\n", " 0.07900000000000\n", "0.079\n", "10.294\n" ] } ], "source": [ "getcontext().prec = 5\n", "\n", "a=Decimal('0.81321')\n", "b=Decimal('23.173')\n", "c=Decimal('23.094')\n", "pretty_p(b-c,b,c,'-')\n", "\n", "print(b-c)\n", "print(a/(b-c))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "context.prec:4\n", " 23.17000000000000\n", "- 23.09000000000000\n", "-----------\n", " 0.08000000000000\n", "0.08\n", "10.16\n" ] } ], "source": [ "TWOPLACES = Decimal(10) ** -2 \n", "getcontext().prec = 4\n", "a=Decimal('0.81321').quantize(Decimal(10) ** -4)\n", "b=Decimal('23.173').quantize(Decimal('0.01'))\n", "c=Decimal('23.094').quantize(Decimal('0.01'))\n", "\n", "pretty_p(b-c,b,c,'-')\n", "\n", "print(b-c)\n", "print(a/(b-c))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "context.prec:3\n", " 23.20000000000000\n", "- 23.10000000000000\n", "-----------\n", " 0.10000000000000\n", "0.1\n", "8.13\n" ] } ], "source": [ "ONEPLACES = Decimal(10) ** -1\n", "getcontext().prec = 3\n", "a=Decimal('0.81321').quantize(Decimal(10) ** -3)\n", "b=Decimal('23.173').quantize(ONEPLACES)\n", "c=Decimal('23.094').quantize(ONEPLACES)\n", "\n", "pretty_p(b-c,b,c,'-')\n", "\n", "print(b-c)\n", "print(a/(b-c))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "context.prec:2\n", " 23.00000000000000\n", "- 23.00000000000000\n", "-----------\n", " 0.00000000000000\n", "0\n" ] }, { "ename": "DivisionByZero", "evalue": "[]", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mDivisionByZero\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mpretty_p\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'-'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mDivisionByZero\u001b[0m: []" ] } ], "source": [ "ZEROPLACES = Decimal(10) ** 0\n", "getcontext().prec = 2\n", "a=Decimal('0.81321').quantize(Decimal(10) ** -2)\n", "b=Decimal('23.173').quantize(ZEROPLACES)\n", "c=Decimal('23.094').quantize(ZEROPLACES)\n", "\n", "pretty_p(b-c,b,c,'-')\n", "\n", "print(b-c)\n", "print(a/(b-c))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2桁の時には0割となっている．また，3桁でも相当大きなズレが出ていることが確認できる．似た同士の数値の引き算には注意が必要．" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Newtonの差分商公式:25点\n", "\n", "次の4点の内挿式をNewtonの差分商公式を用いて求める．\n", " python\n", "X:=[-1, 0, 1, 2];\n", "Y:=[4., -2., -1.2, -.52];\n", "\n", "最初の３点を用いて求めた2次の補間式は，\n", "\n", "$$\n", "- 2.00 - 6.00\\,x+ 3.40\\, \\left( x+1 \\right) x\n", "$$\n", "\n", "である．４点目を取り入れて３次の補間式を求めよ．" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# https://stackoverflow.com/questions/14823891/newton-s-interpolating-polynomial-python\n", "# by Khalil Al Hooti (stackoverflow)\n", "\n", "\n", "def _poly_newton_coefficient(x,y):\n", " \"\"\"\n", " x: list or np array contanining x data points\n", " y: list or np array contanining y data points\n", " \"\"\"\n", "\n", " m = len(x)\n", "\n", " x = np.copy(x)\n", " a = np.copy(y)\n", " for k in range(1,m):\n", " a[k:m] = (a[k:m] - a[k-1])/(x[k:m] - x[k-1])\n", "\n", " return a\n", "\n", "def newton_polynomial(x_data, y_data, x):\n", " \"\"\"\n", " x_data: data points at x\n", " y_data: data points at y\n", " x: evaluation point(s)\n", " \"\"\"\n", " a = _poly_newton_coefficient(x_data, y_data)\n", " n = len(x_data) - 1 # Degree of polynomial\n", " p = a[n]\n", " for k in range(1,n+1):\n", " p = a[n-k] + (x -x_data[n-k])*p\n", " return p" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 4. -6. 3.4 -1.15333333]\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl4VdW9xvHvYhJCZFAwKFNUBkURMBQRBwhQRUSsszRUsUq0jvVqHUpbr49yHapWsVLF4daWIVerKHJVBAnigDLJIKKIlEEEJ0QNUab87h8rXhASEnJ2ss7Z5/08z35ycs4++/xWdnjZWXvttZ2ZISIi8VErdAEiIhItBbuISMwo2EVEYkbBLiISMwp2EZGYUbCLiMSMgl1EJGYU7CIiMaNgFxGJmTohPrRZs2aWnZ1dpfdu2rSJhg0bRltQIGpL8olLO0BtSVaJtGXevHlfmlnzitYLEuzZ2dnMnTu3Su+dMWMGffr0ibagQNSW5BOXdoDakqwSaYtzblVl1lNXjIhIzCjYRURiRsEuIhIzCnYRkZhRsIuIxExkwe6cq+2ce9c5Nzmqbf7EuHGQnU3vvn0hO9t/LyIiu4lyuOM1wFKgUYTb9MaNg/x8KC7GAaxa5b8HyMuL/ONERFJZJEfszrlWwKnAY1FsbzcjRkBxMS9zMndyo3+uuNg/LyIiP+GiuOepc+5fwB3AvsD1ZjaojHXygXyArKysnIKCgkpvv3ffvjgzfsfdjOJqNrAfDSnGnOO16dMTrj+UoqIiMjMzQ5cRibi0JS7tALUlWSXSltzc3Hlm1r3CFc0soQUYBIwuf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3/W0n6I3YzW2pmH1awWg9guZmtMLMtQAFwevVXt9dOB54sffwk8IuAteytyvyMTwf+Yd7bQBPn3IE1XWglpMrvS4XMbCawYQ+rpMo+qUxbUoKZrTOz+aWPvwOWAi13Wa1a90vSB3sltQTW7PT9J+z+g0wGWWa2rvTxeiCrnPUMmOacm+ecy6+Z0ipUmZ9xquyHytbZq/TP5Jecc0fUTGmRS5V9UlkptU+cc9lAN+CdXV6q1v2SFPc8dc5NA1qU8dIIM3u+putJxJ7asvM3ZmbOufKGJB1vZmudcwcAU51zH5QezUjNmQ+0MbMi59xA4DmgfeCa0l1K7RPnXCbwDPBbM/u2Jj87KYLdzPonuIm1QOudvm9V+lyN21NbnHOfOecONLN1pX92fV7ONtaWfv3cOTcR33UQOtgr8zM2EWJKAAABUElEQVROmv1QgQrr3Pkfopm96Jwb7ZxrZmapNl9JquyTCqXSPnHO1cWH+jgze7aMVap1v8SlK2YO0N45d7Bzrh5wPjApcE1lmQRcWPr4QmC3v0accw2dc/v++Bg4CShzlEANq8zPeBJwQekZ/57ANzt1PSWTCtvinGvhnHOlj3vg/618VeOVJi5V9kmFUmWflNb4OLDUzO4rZ7Xq3S+hzyBX4gzzGfj+p83AZ8CU0ucPAl7c5SzzMvxohxGh6y6nLfsDrwIfAdOA/XZtC36kxsLSZUkytaWsnzFwGXBZ6WMHPFT6+mLKGcWUDEsl2nJl6c9/IfA20Ct0zeW0YwKwDtha+u/k4hTeJxW1JVX2yfH482SLgAWly8Ca3C+68lREJGbi0hUjIiKlFOwiIjGjYBcRiRkFu4hIzCjYRURiRsEuIhIzCnYRkZhRsIuIxMz/ATlv2JnTkJHKAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "from scipy import interpolate\n", "import matplotlib.pyplot as plt\n", "\n", "# もとの点\n", "x = np.array([-1,0,1,2])\n", "y = np.array([4,-2,-1.2,-0.52])\n", "for i in range(0,4):\n", " plt.plot(x[i],y[i],'o',color='r')\n", "\n", "\n", "print(_poly_newton_coefficient(x,y))\n", "\n", "xx = np.linspace(-1,2, 100)\n", "yy = newton_polynomial(x, y, xx)\n", "plt.plot(xx, yy, color = 'b')\n", "\n", "plt.grid()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Newtonの差分商公式の利点であるデータ点の追加に伴う煩雑さの回避は，このコードでは実感できない．pythonは，やっぱり便利なのかな．" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ページランク:25点\n", "\n", "次のようなリンクが張られたページ群のページランクを求めよ．" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([[ 0.000, 0.500, 0.000, 0.000, 0.500],\n", " [ 0.333, 0.000, 0.000, 0.000, 0.500],\n", " [ 0.333, 0.500, 0.000, 0.500, 0.000],\n", " [ 0.333, 0.000, 1.000, 0.000, 0.000],\n", " [ 0.000, 0.000, 0.000, 0.500, 0.000]])\n" ] } ], "source": [ "from pprint import pprint\n", "from numpy import array, zeros, diagflat, dot, transpose, set_printoptions\n", "from scipy.linalg import eig\n", "\n", "A = array([[0,1,1,1,0],\n", " [1,0,1,0,0],\n", " [0,0,0,1,0],\n", " [0,0,1,0,1],\n", " [1,1,0,0,0]])\n", "\n", "n = 5\n", "diag = []\n", "for i in range(0,n):\n", " tmp = 0.0\n", " for j in range(0,n):\n", " tmp += A[i,j]\n", " diag.append(1.0/tmp)\n", "\n", "D = diagflat(diag)\n", "tA = dot(transpose(A),D)\n", "\n", "set_printoptions(formatter={'float': '{: 0.3f}'.format}) \n", "pprint(tA)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "初期ベクトルを等分の値にして，3度ほどホップさせた結果．" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([ 0.125, 0.111, 0.269, 0.328, 0.167])\n" ] } ], "source": [ "x = array([1/5,1/5,1/5,1/5,1/5])\n", "pprint(dot(tA,dot(tA,dot(tA,x))))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "固有値を求める．固有値がソートされているか自信がないので，表示させてみた．" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([ 1.00000000+0.j , 0.10242347+0.50115386j,\n", " 0.10242347-0.50115386j, -0.81317748+0.j , -0.39166946+0.j ])\n" ] } ], "source": [ "l, V = eig(tA)\n", "pprint(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "l[0]に対応する最大固有値のベクトルを取り出す．\n", "さらに，初期ベクトルからのホップと比較するために値を揃えている．\n", "だいたい一致しているが，ホップ数が少ないので一致はそれほど高くない．" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([ 0.29497881-0.j, 0.26220338-0.j, 0.55718219-0.j, 0.65550846-0.j,\n", " 0.32775423-0.j])\n" ] } ], "source": [ "v0 = V[:,0]\n", "pprint(v0[0]/0.294*v0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "これより，ページランクは，\n", "[4, 3, 5, 1, 2]\n", "の順になる．" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "?eig" ] }, { "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.6.1" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "12px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": true, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }