Created
December 10, 2019 14:44
-
-
Save pfokin92/780ed5a281bfd83f99dad04eb7ca6423 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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Домашнее задание по лекции \"Библиотека numpy. Вычислительные задачи.\"\n", | |
"\n", | |
"## Задание 1 \n", | |
"Создайте numpy array с элементами от числа N до 0 (например, для N = 10 это будет array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]))." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"15\n", | |
"[14 13 12 11 10 9 8 7 6 5 4 3 2 1 0]\n" | |
] | |
} | |
], | |
"source": [ | |
"import numpy as np\n", | |
"N=int(input())\n", | |
"x=np.arange(N-1, -1, -1)\n", | |
"print(x)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Задание 2 \n", | |
"Создайте диагональную матрицу с элементами от N до 0. Посчитайте сумму ее значений на диагонали." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"5\n", | |
"[[4 0 0 0 0]\n", | |
" [0 3 0 0 0]\n", | |
" [0 0 2 0 0]\n", | |
" [0 0 0 1 0]\n", | |
" [0 0 0 0 0]]\n" | |
] | |
} | |
], | |
"source": [ | |
"N=int(input())\n", | |
"mat=np.diag(np.arange(N-1, -1, -1)) \n", | |
"print(mat)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Задание 3\n", | |
"Решите систему уравнений: \n", | |
"4x + 2y + z = 4 \n", | |
"x + 3y = 12 \n", | |
"5y + 4z = -3 " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from numpy import linalg" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ -0.22727273, 7.54545455, -10.18181818])" | |
] | |
}, | |
"execution_count": 43, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a=np.array([[4, 2, 1], [2, 3, 1], [0, 5, 4]])\n", | |
"b=np.array([4, 12, -3])\n", | |
"linalg.solve(a, b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.allclose( np.dot(a, linalg.solve(a, b)), b )" | |
] | |
} | |
], | |
"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.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment