Last active
October 6, 2018 06:25
-
-
Save pomo-mondreganto/fd193cb7276a8c390c7d4aaa10c4e800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_leader(row):\n", | |
" for i in range(len(row)):\n", | |
" if row[i] != 0:\n", | |
" return i\n", | |
" return 1e9\n", | |
"\n", | |
"def add_rows(r1, r2):\n", | |
" assert len(r1.shape) == 1\n", | |
" assert len(r2.shape) == 1\n", | |
" assert(len(r1) == len(r2))\n", | |
" return np.array(list(a + b for a, b in zip(r1, r2)))\n", | |
"\n", | |
"def mult_row(scalar, r1):\n", | |
" return np.array(list(x * scalar for x in r1))\n", | |
"\n", | |
"def gaussian_transform_forward(A):\n", | |
" current_leader = A.shape[1] + 1\n", | |
" for i in range(A.shape[0]):\n", | |
" if get_leader(A[i]) < current_leader:\n", | |
" current_leader = get_leader(A[i])\n", | |
" A[0], A[i] = A[i], A[0]\n", | |
" if current_leader == A.shape[1] + 1:\n", | |
" return A\n", | |
" A[0] = mult_row(1/A[0][current_leader], A[0])\n", | |
" for i in range(1, A.shape[0]):\n", | |
" A[i] = add_rows(mult_row(-A[i][current_leader], A[0]), A[i])\n", | |
" A[1:, 1:] = gaussian_transform_forward(A[1:, 1:])\n", | |
" return A\n", | |
"\n", | |
"def gaussian_transform_backward(A, cur_row):\n", | |
" if cur_row == 0:\n", | |
" return A\n", | |
" current_leader = get_leader(A[cur_row])\n", | |
" if current_leader == 1e9:\n", | |
" gaussian_transform_backward(A, cur_row - 1)\n", | |
" return A\n", | |
" for i in range(cur_row):\n", | |
" A[i] = add_rows(A[i], mult_row(-A[i][current_leader], A[cur_row]))\n", | |
" gaussian_transform_backward(A, cur_row - 1)\n", | |
" return A\n", | |
"\n", | |
"def gaussian_transform(A):\n", | |
" return gaussian_transform_backward(gaussian_transform_forward(A), A.shape[0] - 1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### СЛУ 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1. 0. 0. 2. 0.]\n", | |
" [0. 1. 2. 1. 0.]\n", | |
" [0. 0. 0. 0. 1.]\n", | |
" [0. 0. 0. 0. 0.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"A = np.array([\n", | |
" [2, 3, 6, 7, 6],\n", | |
" [-8, 5, 10, -11, 8],\n", | |
" [6, -6, -12, 6, 3],\n", | |
" [9, -7, -14, 11, 7]\n", | |
"], dtype='float64')\n", | |
"print(gaussian_transform(A))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### СЛУ 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 1. 0. 0. 2. -2.]\n", | |
" [ 0. 1. 2. 1. -1.]\n", | |
" [ 0. 0. 0. 0. 0.]\n", | |
" [ 0. 0. 0. 0. 0.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"A = np.array([\n", | |
" [2, 3, 6, 7, -7],\n", | |
" [-8, 5, 10, -11, 11],\n", | |
" [6, -6, -12, 6, -6],\n", | |
" [9, -7, -14, 11, -11]\n", | |
"], dtype='float64')\n", | |
"print(gaussian_transform(A))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment