From 240b20d72989b80b63d49b1bbdd5bb21444d7f4c Mon Sep 17 00:00:00 2001 From: chvmvd Date: Tue, 17 Jan 2023 09:48:44 +0900 Subject: [PATCH 1/2] Update gaussian_elimination scripts --- .../10gaussian-elimination/index.mdx | 2 + docs/02algorithms/11dp/index.mdx | 2 +- .../gaussian_elimination.ipynb | 85 ++++++++-------- .../gaussian_elimination_error.ipynb | 95 +++++++++--------- .../gaussian_elimination_revised.ipynb | 97 +++++++++---------- 5 files changed, 140 insertions(+), 141 deletions(-) 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..6a9850016 100644 --- a/static/gaussian-elimination/gaussian_elimination_revised.ipynb +++ b/static/gaussian-elimination/gaussian_elimination_revised.ipynb @@ -1,53 +1,52 @@ { - "nbformat": 4, - "nbformat_minor": 2, - "metadata": {}, - "cells": [ - { - "metadata": {}, - "source": [ - "def gaussian_elimination_revised(a):\n", - " # 前進消去\n", - " for i in range(len(a)):\n", - " # 部分ピボット選択\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", - " # 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_revised([[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": "stream", - "name": "stdout", - "text": [ - "[1.0, 2.0, 2.0]\n" - ] + "metadata": {}, + "source": [ + "def gaussian_elimination_revised(a):\n", + " # \u524d\u9032\u6d88\u53bb\n", + " for i in range(len(a)):\n", + " # \u90e8\u5206\u30d4\u30dc\u30c3\u30c8\u9078\u629e\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", + " # 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_revised([[0, -2, 3, 2], [-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 - } - ] + ] } From 5cdee651d9204da15662427d1208cf88be034868 Mon Sep 17 00:00:00 2001 From: chvmvd Date: Tue, 17 Jan 2023 09:49:49 +0900 Subject: [PATCH 2/2] Update gassian_elimination_revised script --- .../gaussian_elimination_revised.ipynb | 94 +++++++++---------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/static/gaussian-elimination/gaussian_elimination_revised.ipynb b/static/gaussian-elimination/gaussian_elimination_revised.ipynb index 6a9850016..304d289f2 100644 --- a/static/gaussian-elimination/gaussian_elimination_revised.ipynb +++ b/static/gaussian-elimination/gaussian_elimination_revised.ipynb @@ -1,52 +1,50 @@ { - "nbformat": 4, - "nbformat_minor": 2, - "metadata": {}, - "cells": [ + "nbformat": 4, + "nbformat_minor": 2, + "metadata": {}, + "cells": [ + { + "metadata": {}, + "source": [ + "def gaussian_elimination_revised(a):\n", + " # 前進消去\n", + " for i in range(len(a)):\n", + " # 部分ピボット選択\n", + " for j in range(i + 1, len(a)):\n", + " if abs(a[i][i]) < abs(a[j][i]):\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", + " 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 = [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_revised([[0, -2, 3, 2], [-1, 3, -2, 1], [1, -1, 6, 11]]))" + ], + "cell_type": "code", + "outputs": [ { - "metadata": {}, - "source": [ - "def gaussian_elimination_revised(a):\n", - " # \u524d\u9032\u6d88\u53bb\n", - " for i in range(len(a)):\n", - " # \u90e8\u5206\u30d4\u30dc\u30c3\u30c8\u9078\u629e\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", - " # 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_revised([[0, -2, 3, 2], [-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 + "output_type": "stream", + "name": "stdout", + "text": [ + "[1.0, 2.0, 2.0]\n" + ] } - ] + ], + "execution_count": null + } + ] }