|
1 | 1 | {
|
2 |
| - "nbformat": 4, |
3 |
| - "nbformat_minor": 2, |
4 |
| - "metadata": {}, |
5 |
| - "cells": [ |
| 2 | + "nbformat": 4, |
| 3 | + "nbformat_minor": 2, |
| 4 | + "metadata": {}, |
| 5 | + "cells": [ |
| 6 | + { |
| 7 | + "metadata": {}, |
| 8 | + "source": [ |
| 9 | + "def gauss_jordan_elimination(a):\n", |
| 10 | + " # 前進消去\n", |
| 11 | + " for i in range(len(a)):\n", |
| 12 | + " # 部分ピボット選択\n", |
| 13 | + " for j in range(i + 1, len(a)):\n", |
| 14 | + " if abs(a[i][i]) < abs(a[j][i]):\n", |
| 15 | + " a[i], a[j] = a[j], a[i]\n", |
| 16 | + " # pivot倍で行を割る\n", |
| 17 | + " pivot = a[i][i]\n", |
| 18 | + " for j in range(i, len(a[i])):\n", |
| 19 | + " a[i][j] /= pivot\n", |
| 20 | + " # 掃き出す\n", |
| 21 | + " for j in range(len(a)):\n", |
| 22 | + " if j != i:\n", |
| 23 | + " factor = a[j][i]\n", |
| 24 | + " for k in range(i, len(a[i])):\n", |
| 25 | + " a[j][k] -= factor * a[i][k]\n", |
| 26 | + " x = []\n", |
| 27 | + " for i in range(len(a)):\n", |
| 28 | + " x.append(a[i][len(a[i]) - 1])\n", |
| 29 | + " return x\n", |
| 30 | + "\n", |
| 31 | + "\n", |
| 32 | + "print(gauss_jordan_elimination([[0, -2, 3, 2], [-1, 3, -2, 1], [1, -1, 6, 11]]))" |
| 33 | + ], |
| 34 | + "cell_type": "code", |
| 35 | + "outputs": [ |
6 | 36 | {
|
7 |
| - "metadata": {}, |
8 |
| - "source": [ |
9 |
| - "def gauss_jordan_elimination(a):\n", |
10 |
| - " # \u524d\u9032\u6d88\u53bb\n", |
11 |
| - " for i in range(len(a)):\n", |
12 |
| - " # \u90e8\u5206\u30d4\u30dc\u30c3\u30c8\u9078\u629e\n", |
13 |
| - " for j in range(i + 1, len(a)):\n", |
14 |
| - " if abs(a[i][i]) < abs(a[j][i]):\n", |
15 |
| - " tmp = a[i]\n", |
16 |
| - " a[i] = a[j]\n", |
17 |
| - " a[j] = tmp\n", |
18 |
| - " # pivot\u500d\u3067\u884c\u3092\u5272\u308b\n", |
19 |
| - " pivot = a[i][i]\n", |
20 |
| - " for j in range(i, len(a[i])):\n", |
21 |
| - " a[i][j] /= pivot\n", |
22 |
| - " # \u6383\u304d\u51fa\u3059\n", |
23 |
| - " for j in range(len(a)):\n", |
24 |
| - " if j != i:\n", |
25 |
| - " factor = a[j][i]\n", |
26 |
| - " for k in range(i, len(a[i])):\n", |
27 |
| - " a[j][k] -= factor * a[i][k]\n", |
28 |
| - " x = []\n", |
29 |
| - " for i in range(len(a)):\n", |
30 |
| - " x.append(a[i][len(a[i]) - 1])\n", |
31 |
| - " return x\n", |
32 |
| - "\n", |
33 |
| - "\n", |
34 |
| - "print(gauss_jordan_elimination([[0, -2, 3, 2], [-1, 3, -2, 1], [1, -1, 6, 11]]))" |
35 |
| - ], |
36 |
| - "cell_type": "code", |
37 |
| - "outputs": [ |
38 |
| - { |
39 |
| - "output_type": "stream", |
40 |
| - "name": "stdout", |
41 |
| - "text": [ |
42 |
| - "[1.0, 2.0, 2.0]\n" |
43 |
| - ] |
44 |
| - } |
45 |
| - ], |
46 |
| - "execution_count": null |
| 37 | + "output_type": "stream", |
| 38 | + "name": "stdout", |
| 39 | + "text": [ |
| 40 | + "[1.0, 2.0, 2.0]\n" |
| 41 | + ] |
47 | 42 | }
|
48 |
| - ] |
| 43 | + ], |
| 44 | + "execution_count": null |
| 45 | + } |
| 46 | + ] |
49 | 47 | }
|
0 commit comments