diff --git a/docs/02algorithms/10gaussian-elimination/index.mdx b/docs/02algorithms/10gaussian-elimination/index.mdx index 6401d4de1..9f9819fb5 100644 --- a/docs/02algorithms/10gaussian-elimination/index.mdx +++ b/docs/02algorithms/10gaussian-elimination/index.mdx @@ -469,6 +469,8 @@ $x_n = d_n$ になります。求まった $x_n$ をそれよりも上の式す +冒頭の `x = [0 for _ in range(len(a[0]) - 1)]` は、リストの内包表記と呼ばれ、配列 `x` を `0` で埋める操作です。この操作で `x` は `[0, 0, ..., 0]` となっています。 + `reversed` 関数は配列の要素を反転します。 ## 部分ピボット選択 diff --git a/docs/02algorithms/11dp/index.mdx b/docs/02algorithms/11dp/index.mdx index cf5c78057..f36197903 100644 --- a/docs/02algorithms/11dp/index.mdx +++ b/docs/02algorithms/11dp/index.mdx @@ -69,7 +69,7 @@ DP には大きく分けて、二種類あります。トップダウン方式 -冒頭の `memo = [-1 for _ in range(10000)]` は、リストの内包表記と呼ばれ、配列 `memo` を `-1` で埋める操作です。この操作で `memo` は `[-1, -1, ..., -1]` となっています。 +冒頭の `memo = [-1 for _ in range(10000)]` は、配列 `memo` を `-1` で埋めています。 #### ボトムアップ方式 diff --git a/static/gaussian-elimination/gaussian_elimination.ipynb b/static/gaussian-elimination/gaussian_elimination.ipynb index 2a009554b..461f5b71b 100644 --- a/static/gaussian-elimination/gaussian_elimination.ipynb +++ b/static/gaussian-elimination/gaussian_elimination.ipynb @@ -1,47 +1,46 @@ { - "nbformat": 4, - "nbformat_minor": 2, - "metadata": {}, - "cells": [ - { - "metadata": {}, - "source": [ - "def gaussian_elimination(a):\n", - " # 前進消去\n", - " for i in range(len(a)):\n", - " # pivot倍で行を割る\n", - " pivot = a[i][i]\n", - " for j in range(i, len(a[i])):\n", - " a[i][j] /= pivot\n", - "\n", - " # i+1行目以降を掃き出す\n", - " for j in range(i + 1, len(a)):\n", - " factor = a[j][i]\n", - " for k in range(i, len(a[i])):\n", - " a[j][k] -= factor * a[i][k]\n", - " # 後退代入\n", - " x = []\n", - " for i in reversed(range(len(a))):\n", - " tmp = a[i][len(a[i]) - 1] / a[i][i]\n", - " for j in range(i):\n", - " a[j][len(a[i]) - 1] -= a[j][i] * tmp\n", - " x = [tmp] + x\n", - " return x\n", - "\n", - "\n", - "print(gaussian_elimination([[1, -2, 3, 3], [-1, 3, -2, 1], [1, -1, 6, 11]]))" - ], - "cell_type": "code", - "outputs": [ + "nbformat": 4, + "nbformat_minor": 2, + "metadata": {}, + "cells": [ { - "output_type": "stream", - "name": "stdout", - "text": [ - "[1.0, 2.0, 2.0]\n" - ] + "metadata": {}, + "source": [ + "def gaussian_elimination(a):\n", + " # \u524d\u9032\u6d88\u53bb\n", + " for i in range(len(a)):\n", + " # pivot\u500d\u3067\u884c\u3092\u5272\u308b\n", + " pivot = a[i][i]\n", + " for j in range(i, len(a[i])):\n", + " a[i][j] /= pivot\n", + "\n", + " # i+1\u884c\u76ee\u4ee5\u964d\u3092\u6383\u304d\u51fa\u3059\n", + " for j in range(i + 1, len(a)):\n", + " factor = a[j][i]\n", + " for k in range(i, len(a[i])):\n", + " a[j][k] -= factor * a[i][k]\n", + " # \u5f8c\u9000\u4ee3\u5165\n", + " x = [0 for _ in range(len(a[0]) - 1)]\n", + " for i in reversed(range(len(a))):\n", + " x[i] = a[i][len(a[i]) - 1]\n", + " for j in range(i):\n", + " a[j][len(a[i]) - 1] -= a[j][i] * x[i]\n", + " return x\n", + "\n", + "\n", + "print(gaussian_elimination([[1, -2, 3, 3], [-1, 3, -2, 1], [1, -1, 6, 11]]))" + ], + "cell_type": "code", + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1.0, 2.0, 2.0]\n" + ] + } + ], + "execution_count": null } - ], - "execution_count": null - } - ] + ] } diff --git a/static/gaussian-elimination/gaussian_elimination_error.ipynb b/static/gaussian-elimination/gaussian_elimination_error.ipynb index 4d7d0a35b..3a60067c9 100644 --- a/static/gaussian-elimination/gaussian_elimination_error.ipynb +++ b/static/gaussian-elimination/gaussian_elimination_error.ipynb @@ -1,52 +1,51 @@ { - "nbformat": 4, - "nbformat_minor": 2, - "metadata": {}, - "cells": [ - { - "metadata": {}, - "source": [ - "def gaussian_elimination(a):\n", - " # 前進消去\n", - " for i in range(len(a)):\n", - " # pivot倍で行を割る\n", - " pivot = a[i][i]\n", - " for j in range(i, len(a[i])):\n", - " a[i][j] /= pivot\n", - "\n", - " # i+1行目以降を掃き出す\n", - " for j in range(i + 1, len(a)):\n", - " factor = a[j][i]\n", - " for k in range(i, len(a[i])):\n", - " a[j][k] -= factor * a[i][k]\n", - " # 後退代入\n", - " x = []\n", - " for i in reversed(range(len(a))):\n", - " tmp = a[i][len(a[i]) - 1] / a[i][i]\n", - " for j in range(i):\n", - " a[j][len(a[i]) - 1] -= a[j][i] * tmp\n", - " x = [tmp] + x\n", - " return x\n", - "\n", - "\n", - "print(gaussian_elimination([[0, -2, 3, 2], [-1, 3, -2, 1], [1, -1, 6, 11]]))" - ], - "cell_type": "code", - "outputs": [ + "nbformat": 4, + "nbformat_minor": 2, + "metadata": {}, + "cells": [ { - "output_type": "error", - "ename": "ZeroDivisionError", - "evalue": "division by zero", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn [1], line 23\u001b[0m\n\u001b[1;32m 20\u001b[0m x\u001b[39m=\u001b[39m[tmp]\u001b[39m+\u001b[39mx\n\u001b[1;32m 21\u001b[0m \u001b[39mreturn\u001b[39;00m x\n\u001b[0;32m---> 23\u001b[0m \u001b[39mprint\u001b[39m(gaussian_elimination([[\u001b[39m0\u001b[39m,\u001b[39m-\u001b[39m\u001b[39m2\u001b[39m,\u001b[39m3\u001b[39m,\u001b[39m2\u001b[39m],[\u001b[39m-\u001b[39m\u001b[39m1\u001b[39m,\u001b[39m3\u001b[39m,\u001b[39m-\u001b[39m\u001b[39m2\u001b[39m,\u001b[39m1\u001b[39m],[\u001b[39m1\u001b[39m,\u001b[39m-\u001b[39m\u001b[39m1\u001b[39m,\u001b[39m6\u001b[39m,\u001b[39m11\u001b[39m]]))\n", - "Cell \u001b[0;32mIn [1], line 7\u001b[0m, in \u001b[0;36mgaussian_elimination\u001b[0;34m(a)\u001b[0m\n\u001b[1;32m 5\u001b[0m pivot\u001b[39m=\u001b[39ma[i][i]\n\u001b[1;32m 6\u001b[0m \u001b[39mfor\u001b[39;00m j \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(i,\u001b[39mlen\u001b[39m(a[i])):\n\u001b[0;32m----> 7\u001b[0m a[i][j]\u001b[39m/\u001b[39m\u001b[39m=\u001b[39mpivot\n\u001b[1;32m 9\u001b[0m \u001b[39m# i+1行目以降を掃き出す\u001b[39;00m\n\u001b[1;32m 10\u001b[0m \u001b[39mfor\u001b[39;00m j \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(i\u001b[39m+\u001b[39m\u001b[39m1\u001b[39m,\u001b[39mlen\u001b[39m(a)):\n", - "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" - ] + "metadata": {}, + "source": [ + "def gaussian_elimination(a):\n", + " # \u524d\u9032\u6d88\u53bb\n", + " for i in range(len(a)):\n", + " # pivot\u500d\u3067\u884c\u3092\u5272\u308b\n", + " pivot = a[i][i]\n", + " for j in range(i, len(a[i])):\n", + " a[i][j] /= pivot\n", + "\n", + " # i+1\u884c\u76ee\u4ee5\u964d\u3092\u6383\u304d\u51fa\u3059\n", + " for j in range(i + 1, len(a)):\n", + " factor = a[j][i]\n", + " for k in range(i, len(a[i])):\n", + " a[j][k] -= factor * a[i][k]\n", + " # \u5f8c\u9000\u4ee3\u5165\n", + " x = [0 for _ in range(len(a[0]) - 1)]\n", + " for i in reversed(range(len(a))):\n", + " x[i] = a[i][len(a[i]) - 1]\n", + " for j in range(i):\n", + " a[j][len(a[i]) - 1] -= a[j][i] * x[i]\n", + " return x\n", + "\n", + "\n", + "print(gaussian_elimination([[0, -2, 3, 2], [-1, 3, -2, 1], [1, -1, 6, 11]]))" + ], + "cell_type": "code", + "outputs": [ + { + "output_type": "error", + "ename": "ZeroDivisionError", + "evalue": "division by zero", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn [1], line 23\u001b[0m\n\u001b[1;32m 19\u001b[0m a[j][\u001b[39mlen\u001b[39m(a[i]) \u001b[39m-\u001b[39m \u001b[39m1\u001b[39m] \u001b[39m-\u001b[39m\u001b[39m=\u001b[39m a[j][i] \u001b[39m*\u001b[39m x[i]\n\u001b[1;32m 20\u001b[0m \u001b[39mreturn\u001b[39;00m x\n\u001b[0;32m---> 23\u001b[0m \u001b[39mprint\u001b[39m(gaussian_elimination([[\u001b[39m0\u001b[39m, \u001b[39m-\u001b[39m\u001b[39m2\u001b[39m, \u001b[39m3\u001b[39m, \u001b[39m2\u001b[39m], [\u001b[39m-\u001b[39m\u001b[39m1\u001b[39m, \u001b[39m3\u001b[39m, \u001b[39m-\u001b[39m\u001b[39m2\u001b[39m, \u001b[39m1\u001b[39m], [\u001b[39m1\u001b[39m, \u001b[39m-\u001b[39m\u001b[39m1\u001b[39m, \u001b[39m6\u001b[39m, \u001b[39m11\u001b[39m]]))\n", + "Cell \u001b[0;32mIn [1], line 7\u001b[0m, in \u001b[0;36mgaussian_elimination\u001b[0;34m(a)\u001b[0m\n\u001b[1;32m 5\u001b[0m pivot \u001b[39m=\u001b[39m a[i][i]\n\u001b[1;32m 6\u001b[0m \u001b[39mfor\u001b[39;00m j \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(i, \u001b[39mlen\u001b[39m(a[i])):\n\u001b[0;32m----> 7\u001b[0m a[i][j] \u001b[39m/\u001b[39m\u001b[39m=\u001b[39m pivot\n\u001b[1;32m 9\u001b[0m \u001b[39m# i+1\u884c\u76ee\u4ee5\u964d\u3092\u6383\u304d\u51fa\u3059\u001b[39;00m\n\u001b[1;32m 10\u001b[0m \u001b[39mfor\u001b[39;00m j \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(i \u001b[39m+\u001b[39m \u001b[39m1\u001b[39m, \u001b[39mlen\u001b[39m(a)):\n", + "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" + ] + } + ], + "execution_count": null } - ], - "execution_count": null - } - ] + ] } diff --git a/static/gaussian-elimination/gaussian_elimination_revised.ipynb b/static/gaussian-elimination/gaussian_elimination_revised.ipynb index 4e394b839..304d289f2 100644 --- a/static/gaussian-elimination/gaussian_elimination_revised.ipynb +++ b/static/gaussian-elimination/gaussian_elimination_revised.ipynb @@ -12,9 +12,7 @@ " # 部分ピボット選択\n", " for j in range(i + 1, len(a)):\n", " if abs(a[i][i]) < abs(a[j][i]):\n", - " tmp = a[i]\n", - " a[i] = a[j]\n", - " a[j] = tmp\n", + " a[i], a[j] = a[j], a[i]\n", " # pivot倍で行を割る\n", " pivot = a[i][i]\n", " for j in range(i, len(a[i])):\n", @@ -26,12 +24,11 @@ " for k in range(i, len(a[i])):\n", " a[j][k] -= factor * a[i][k]\n", " # 後退代入\n", - " x = []\n", + " x = [0 for _ in range(len(a[0]) - 1)]\n", " for i in reversed(range(len(a))):\n", - " tmp = a[i][len(a[i]) - 1] / a[i][i]\n", + " x[i] = a[i][len(a[i]) - 1]\n", " for j in range(i):\n", - " a[j][len(a[i]) - 1] -= a[j][i] * tmp\n", - " x = [tmp] + x\n", + " a[j][len(a[i]) - 1] -= a[j][i] * x[i]\n", " return x\n", "\n", "\n",