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

Шаповалов Герман Ип215 #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
166 changes: 166 additions & 0 deletions Lec2_SGA.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "c4c50b56-c867-4597-80a0-cb7c9d023ab2",
"metadata": {},
"source": [
"Лекция №2"
]
},
{
"cell_type": "markdown",
"id": "ed045edc-6f10-488b-9ce8-5487d4a9a98e",
"metadata": {},
"source": [
"Задача 1"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "06bfb4fa-adae-4bcf-b121-cce946c7f7a8",
"metadata": {},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'swallow-speeds.txt'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m f \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mswallow-speeds.txt\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 2\u001b[0m speeds \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m line \u001b[38;5;129;01min\u001b[39;00m f\u001b[38;5;241m.\u001b[39mreadlines():\n",
"File \u001b[1;32mD:\\jupiter\\Lib\\site-packages\\IPython\\core\\interactiveshell.py:324\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;32m 317\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[0;32m 318\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 319\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 320\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 321\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 322\u001b[0m )\n\u001b[1;32m--> 324\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m io_open(file, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'swallow-speeds.txt'"
]
}
],
"source": [
"f = open('swallow-speeds.txt', 'r')\n",
"speeds = []\n",
"for line in f.readlines():\n",
" try:\n",
" speeds.append(float(line))\n",
" except (ValueError):\n",
" pass\n",
"f.close()\n",
"\n",
"average_speed = sum(speeds)/len(speeds)\n",
"print('The AVG speed of an unladen swallow is {:.1f} m/s' .format(average_speed))"
]
},
{
"cell_type": "markdown",
"id": "f0096f09-6155-45b3-b5d8-9b80c120ff4e",
"metadata": {},
"source": [
"Задача 2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c42e8469-37ce-49ea-b3b0-43aa629e6d3d",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "argument to str_vector() must be a list or tuple",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[2], line 18\u001b[0m\n\u001b[0;32m 16\u001b[0m str_vector((\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m0.5\u001b[39m,\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m2\u001b[39m))\n\u001b[0;32m 17\u001b[0m str_vector((\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3.5\u001b[39m))\n\u001b[1;32m---> 18\u001b[0m str_vector((\u001b[38;5;124m'\u001b[39m\u001b[38;5;124merunda\u001b[39m\u001b[38;5;124m'\u001b[39m))\n",
"Cell \u001b[1;32mIn[2], line 3\u001b[0m, in \u001b[0;36mstr_vector\u001b[1;34m(v)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mstr_vector\u001b[39m(v):\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(v) \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mlist\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(v) \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mtuple\u001b[39m:\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124margument to str_vector() must be a list or tuple\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(v) \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m (\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m):\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvector must be 2D or 3D in str_vector()\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
"\u001b[1;31mTypeError\u001b[0m: argument to str_vector() must be a list or tuple"
]
}
],
"source": [
"def str_vector(v):\n",
" if type(v) is not list and type(v) is not tuple:\n",
" raise TypeError('argument to str_vector() must be a list or tuple')\n",
" if len(v) not in (2,3):\n",
" raise ValueError('vector must be 2D or 3D in str_vector()')\n",
" unit_vectors = ['i','j','k']\n",
" s = []\n",
" for i, component in enumerate(v):\n",
" try:\n",
" s.append('{:s}{:1s}'.format(str(float(component)), unit_vectors[i]))\n",
" except(TypeError,ValueError):\n",
" raise ValueError('Invalid element in vector {}: {} is'\n",
" ' not a real number'.format(v,component))\n",
" return ' + '.join(s).replace('+ -', '- ')\n",
"\n",
"str_vector((4,0.5,-2))\n",
"str_vector((-2,3.5))\n",
"str_vector(('erunda'))"
]
},
{
"cell_type": "markdown",
"id": "a1b4ec40-9f9d-40bc-b581-38f28ed9f8ba",
"metadata": {},
"source": [
"Задача 3"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cb356066-ab7b-44e8-8c81-0aba8899a8a3",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Zero to the power zero",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[3], line 6\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mZero to the power zero\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mb\n\u001b[1;32m----> 6\u001b[0m powr(\u001b[38;5;241m0\u001b[39m,\u001b[38;5;241m0\u001b[39m)\n",
"Cell \u001b[1;32mIn[3], line 3\u001b[0m, in \u001b[0;36mpowr\u001b[1;34m(a, b)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpowr\u001b[39m(a,b):\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m a \u001b[38;5;241m==\u001b[39m b \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mZero to the power zero\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mb\n",
"\u001b[1;31mValueError\u001b[0m: Zero to the power zero"
]
}
],
"source": [
"def powr(a,b):\n",
" if a == b == 0:\n",
" raise ValueError('Zero to the power zero')\n",
" return a**b\n",
"\n",
"powr(0,0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "480d23ec-78b0-45d4-8506-d8ff900b01ad",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
153 changes: 153 additions & 0 deletions Pr07_SGA.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "b6e3a048-34fc-4647-9aba-f6fd8cc9f4ce",
"metadata": {},
"source": [
"Линейная алгебра"
]
},
{
"cell_type": "markdown",
"id": "862d2418-08c0-454e-9d54-af3303c7bbab",
"metadata": {},
"source": [
"Практическая работа №7"
]
},
{
"cell_type": "markdown",
"id": "ee2b7f56-9640-40ab-a643-25fc7e9317b9",
"metadata": {},
"source": [
"В1"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "648d601a-ae97-4f29-af3b-8d7a5a2604df",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (557334471.py, line 7)",
"output_type": "error",
"traceback": [
"\u001b[1;36m Cell \u001b[1;32mIn[1], line 7\u001b[1;36m\u001b[0m\n\u001b[1;33m In [x]: for sigma in pauli_matrices:\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"In [x]: pauli_matrices = np.array((\n",
"((0, 1), (1, 0)),\n",
"((0, -1j), (1j, 0)),\n",
"((1, 0), (0, -1))\n",
"))\n",
"In [x]: I2 - np.eye(2)\n",
"In [x]: for sigma in pauli_matrices:\n",
" ...: print(np.allclose(sigma.T.conj().dot(sigma), I2))\n",
"True\n",
"True\n",
"True"
]
},
{
"cell_type": "markdown",
"id": "0365c39b-85e4-40b1-85e3-a6dc99b88331",
"metadata": {},
"source": [
"B2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3a90e8d7-9089-465c-828c-a95b077577ef",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Gladipen\\AppData\\Local\\Temp\\ipykernel_17580\\361088777.py:11: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.\n",
"To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.\n",
" coefs, resid, _, _ = np.linalg.lstsq(A, x)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"x0 = 1.28 cm, v0 = -0.04 cm.s-1, g = 9.47 m.s-2\n"
]
},
{
"ename": "NameError",
"evalue": "name 'f' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[2], line 17\u001b[0m\n\u001b[0;32m 13\u001b[0m x0, v0, g \u001b[38;5;241m=\u001b[39m coefs[\u001b[38;5;241m0\u001b[39m], coefs[\u001b[38;5;241m1\u001b[39m], coefs[\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m*\u001b[39m \u001b[38;5;241m2\u001b[39m \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m100\u001b[39m\n\u001b[0;32m 15\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx0 = \u001b[39m\u001b[38;5;132;01m{:.2f}\u001b[39;00m\u001b[38;5;124m cm, v0 = \u001b[39m\u001b[38;5;132;01m{:.2f}\u001b[39;00m\u001b[38;5;124m cm.s-1, g = \u001b[39m\u001b[38;5;132;01m{:.2f}\u001b[39;00m\u001b[38;5;124m m.s-2\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mformat(x0, v0, g))\n\u001b[1;32m---> 17\u001b[0m xfit \u001b[38;5;241m=\u001b[39m Polynomial(coefs)(f)\n\u001b[0;32m 18\u001b[0m pylab\u001b[38;5;241m.\u001b[39mplot(t, x, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mko\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 19\u001b[0m pylab\u001b[38;5;241m.\u001b[39mplot(t, xfit, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
"\u001b[1;31mNameError\u001b[0m: name 'f' is not defined"
]
}
],
"source": [
"import numpy as np\n",
"import pylab\n",
"Polynomial = np.polynomial.Polynomial\n",
"\n",
"x = np.array([1.3, 6.0, 20.2, 43.9, 77.0, 119.6, 171.7, 233.2, 304.2, 384.7, 474.7, 574.1, 683.0, 801.3, 929.2, 1066.4, 1213.2, 1369.4, 1535.1, 1710.3, 1894.9])\n",
"dt, n = 0.1, len(x)\n",
"tmax = dt * (n-1)\n",
"t = np.linspace(0, tmax, n)\n",
"\n",
"A = np.vstack((np.ones(n), t, t**2)).T\n",
"coefs, resid, _, _ = np.linalg.lstsq(A, x)\n",
"\n",
"x0, v0, g = coefs[0], coefs[1], coefs[2] * 2 / 100\n",
"\n",
"print('x0 = {:.2f} cm, v0 = {:.2f} cm.s-1, g = {:.2f} m.s-2'.format(x0, v0, g))\n",
"\n",
"xfit = Polynomial(coefs)(f)\n",
"pylab.plot(t, x, 'ko')\n",
"pylab.plot(t, xfit, 'r')\n",
"pylab.xlabel('Time (sec)')\n",
"pylab.ylabel('Distance (cm)')\n",
"pylab.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e581bf81-fc6f-4271-be90-feb3604a1f46",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
72 changes: 72 additions & 0 deletions Pr08_SGA.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d111368e-6f53-4caf-a389-a298b85d0056",
"metadata": {},
"source": [
"Дискретные преобразования Фурье"
]
},
{
"cell_type": "markdown",
"id": "3d9a3a7c-fb79-4e27-adee-01ac061f4e4c",
"metadata": {},
"source": [
"Практическая работа №8"
]
},
{
"cell_type": "markdown",
"id": "6f59e641-cc5c-456c-9134-798088aa4bd9",
"metadata": {},
"source": [
"В1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15942eb9-eca8-462b-942b-c5c90e6e8b1f",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"n = 512\n",
"f = np.random.rand(n)\n",
"\n",
"%timeit np.fft.fft(f)\n",
"\n",
"k = np.arange(n)\n",
"m = k.reshape((n,1))\n",
"w = np.exp(-2j * np.pi * m * k / n)\n",
"%timeit np.dot(w,f)\n",
"\n",
"ftfast = np.fft.fft(f)\n",
"ftslow = np.dot(w,f)\n",
"np.allclose(ftfast, ftslow)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading