Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gauss-jordan elimination script #196

Merged
Merged
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
88 changes: 43 additions & 45 deletions static/gaussian-elimination/gauss_jordan_elimination.ipynb
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"def gauss_jordan_elimination(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",
" for j in range(len(a)):\n",
" if j != i:\n",
" factor = a[j][i]\n",
" for k in range(i, len(a[i])):\n",
" a[j][k] -= factor * a[i][k]\n",
" x = []\n",
" for i in range(len(a)):\n",
" x.append(a[i][len(a[i]) - 1])\n",
" return x\n",
"\n",
"\n",
"print(gauss_jordan_elimination([[0, -2, 3, 2], [-1, 3, -2, 1], [1, -1, 6, 11]]))"
],
"cell_type": "code",
"outputs": [
{
"metadata": {},
"source": [
"def gauss_jordan_elimination(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",
" # \u6383\u304d\u51fa\u3059\n",
" for j in range(len(a)):\n",
" if j != i:\n",
" factor = a[j][i]\n",
" for k in range(i, len(a[i])):\n",
" a[j][k] -= factor * a[i][k]\n",
" x = []\n",
" for i in range(len(a)):\n",
" x.append(a[i][len(a[i]) - 1])\n",
" return x\n",
"\n",
"\n",
"print(gauss_jordan_elimination([[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
}
]
}