Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/02algorithms/10gaussian-elimination/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ $x_n = d_n$ になります。求まった $x_n$ をそれよりも上の式す

<ViewSource path="/gaussian-elimination/gaussian_elimination.ipynb" />

冒頭の `x = [0 for _ in range(len(a[0]) - 1)]` は、リストの内包表記と呼ばれ、配列 `x` を `0` で埋める操作です。この操作で `x` は `[0, 0, ..., 0]` となっています。

`reversed` 関数は配列の要素を反転します。

## 部分ピボット選択
Expand Down
2 changes: 1 addition & 1 deletion docs/02algorithms/11dp/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ DP には大きく分けて、二種類あります。トップダウン方式

<ViewSource path="/dp/fib_memoization.ipynb" />

冒頭の `memo = [-1 for _ in range(10000)]` は、リストの内包表記と呼ばれ、配列 `memo` を `-1` で埋める操作です。この操作で `memo` は `[-1, -1, ..., -1]` となっています
冒頭の `memo = [-1 for _ in range(10000)]` は、配列 `memo` を `-1` で埋めています

#### ボトムアップ方式

Expand Down
85 changes: 42 additions & 43 deletions static/gaussian-elimination/gaussian_elimination.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
]
]
}
95 changes: 47 additions & 48 deletions static/gaussian-elimination/gaussian_elimination_error.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
]
]
}
11 changes: 4 additions & 7 deletions static/gaussian-elimination/gaussian_elimination_revised.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down