From 0357fd578b6d9d79692b583292ececb876ce4923 Mon Sep 17 00:00:00 2001 From: chvmvd Date: Tue, 17 Jan 2023 17:26:30 +0900 Subject: [PATCH] Update gauss-jordan elimination script --- .../gauss_jordan_elimination.ipynb | 88 +++++++++---------- 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/static/gaussian-elimination/gauss_jordan_elimination.ipynb b/static/gaussian-elimination/gauss_jordan_elimination.ipynb index a12729fe8..9b7df2023 100644 --- a/static/gaussian-elimination/gauss_jordan_elimination.ipynb +++ b/static/gaussian-elimination/gauss_jordan_elimination.ipynb @@ -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 + } + ] }