numerical-methods-homeworks.../HW1/Problem_6_sol.ipynb

438 lines
97 KiB
Plaintext
Raw Normal View History

2023-10-05 00:27:43 +03:00
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"from numba import njit\n",
"import time"
],
"metadata": {
"id": "Qaq--qm2_Qfm"
},
"execution_count": 19,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## The Hofstadter-Conway sequence $a(n)$: \n",
"\n",
"$a(1) = 1, a(2) = 1, ... , a(n) = a(a(n-1)) + a(n - a(n - 1))$"
],
"metadata": {
"id": "9A0VfT5yePm5"
}
},
{
"cell_type": "code",
"source": [
"n = int(1e3)\n",
"a = np.ones(n, dtype = int)"
],
"metadata": {
"id": "l_UpCyuqQBwt"
},
"execution_count": 20,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## **First implemination:** pre-allocating `numpy` array and filling it using `for` loop."
],
"metadata": {
"id": "GkeqAQzceyOD"
}
},
{
"cell_type": "code",
"source": [
"def HC_func1(array, n):\n",
" if n < 1:\n",
" raise Exception(\"n must be positive\")\n",
" elif n == 1:\n",
" array[0] = 1\n",
" else:\n",
" array[0], array[1] = 1, 1\n",
"\n",
" for idx in range(2, n):\n",
" array[idx] = array[array[idx - 1] - 1] + array[idx - array[idx - 1]]"
],
"metadata": {
"id": "i1B9t7PwPrB-"
},
"execution_count": 21,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%time HC_func1(a, n)\n",
"%time HC_func1(a, n)\n",
"%time HC_func1(a, n)"
],
"metadata": {
"id": "MlymyYQDPscK",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "c7f93f12-16f3-4b18-af01-b7c85ebcc5c9"
},
"execution_count": 22,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"CPU times: user 1.73 ms, sys: 0 ns, total: 1.73 ms\n",
"Wall time: 1.74 ms\n",
"CPU times: user 1.62 ms, sys: 104 µs, total: 1.73 ms\n",
"Wall time: 1.73 ms\n",
"CPU times: user 1.72 ms, sys: 0 ns, total: 1.72 ms\n",
"Wall time: 1.73 ms\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## **Second implemination:** cumulatively appending `python` list and converting it to `numpy` array."
],
"metadata": {
"id": "mtt236qrfDnI"
}
},
{
"cell_type": "code",
"source": [
"def HC_func2(n):\n",
" if n < 1:\n",
" raise Exception(\"n must be positive\")\n",
" elif n == 1:\n",
" result = [1]\n",
" else:\n",
" result = [1, 1]\n",
" for idx in range(2, n):\n",
" elem = result[result[idx - 1] - 1] + result[idx - result[idx - 1]]\n",
" result.append(elem)\n",
" return np.array(result)"
],
"metadata": {
"id": "LY9YUjFFNIs2"
},
"execution_count": 23,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%time HC_func2(n)\n",
"%time HC_func2(n)\n",
"%time HC_func2(n)\n",
"\"\""
],
"metadata": {
"id": "9X9tMCeYVYhm",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 140
},
"outputId": "957c3a66-7e73-4997-8f7c-aead3217beab"
},
"execution_count": 24,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"CPU times: user 779 µs, sys: 0 ns, total: 779 µs\n",
"Wall time: 785 µs\n",
"CPU times: user 720 µs, sys: 13 µs, total: 733 µs\n",
"Wall time: 737 µs\n",
"CPU times: user 736 µs, sys: 0 ns, total: 736 µs\n",
"Wall time: 740 µs\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"''"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 24
}
]
},
{
"cell_type": "markdown",
"source": [
"## **Second implemination:** compiled (`jit`) version."
],
"metadata": {
"id": "tKz26-9RfUex"
}
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"id": "7h5Woys4-Z3S"
},
"outputs": [],
"source": [
"@njit\n",
"def HC_func3(array, n):\n",
" if n < 1:\n",
" raise Exception(\"n must be positive\")\n",
" elif n == 1:\n",
" array[0] = 1\n",
" else:\n",
" array[0], array[1] = 1, 1\n",
"\n",
" for idx in range(2, n):\n",
" array[idx] = array[array[idx - 1] - 1] + array[idx - array[idx - 1]]"
]
},
{
"cell_type": "code",
"source": [
"%time HC_func3(a, n)\n",
"%time HC_func3(a, n)\n",
"%time HC_func3(a, n)"
],
"metadata": {
"id": "6rhqO950CzUg",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "06e1ac2d-11f2-49a2-e3a9-7b8b165e42b0"
},
"execution_count": 26,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"CPU times: user 235 ms, sys: 19.7 ms, total: 255 ms\n",
"Wall time: 298 ms\n",
"CPU times: user 23 µs, sys: 0 ns, total: 23 µs\n",
"Wall time: 26.7 µs\n",
"CPU times: user 19 µs, sys: 0 ns, total: 19 µs\n",
"Wall time: 22.6 µs\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Let's compare execution time"
],
"metadata": {
"id": "6WubSLEUfcLW"
}
},
{
"cell_type": "code",
"source": [
"def comp_time_res():\n",
" res1, res2, res3 = np.array([]), np.array([]), np.array([])\n",
" for k in list(map(int, [1e1, 1e2, 1e3, 1e4, 1e5])):\n",
" data = np.ones(k, dtype = int)\n",
"\n",
" start_time = time.time()\n",
" HC_func1(data, k)\n",
" end_time = time.time()\n",
" res1 = np.append(res1, end_time - start_time)\n",
"\n",
" start_time = time.time()\n",
" HC_func2(k)\n",
" end_time = time.time()\n",
" res2 = np.append(res2, end_time - start_time)\n",
"\n",
" start_time = time.time()\n",
" HC_func3(data, k)\n",
" end_time = time.time()\n",
" res3 = np.append(res3, end_time - start_time)\n",
" return (res1, res2, res3)"
],
"metadata": {
"id": "LELxb0HRoWvh"
},
"execution_count": 27,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import matplotlib.pyplot as plt\n",
"first_func, second_func, third_func = comp_time_res()\n",
"x = np.array([1e1, 1e2, 1e3, 1e4, 1e5], dtype=int)\n",
"\n",
"plt.plot(x, first_func)\n",
"plt.plot(x, second_func)\n",
"plt.plot(x, third_func)\n",
"\n",
"plt.xlabel(\"n\", fontsize=14)\n",
"plt.ylabel(\"comp time\", fontsize=14)\n",
"plt.legend([\"HC_func_1\", \"HC_func_2\", \"HC_func_3\"]);"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 455
},
"id": "4MPOWTsXtZhU",
"outputId": "7299eddf-166f-4e26-9058-716eaefe6168"
},
"execution_count": 28,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
],
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkgAAAG2CAYAAACEbnlbAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAABvbUlEQVR4nO3deVyVdfr/8dc57CCLKyiyuO+CuRBamY1FZSUtatZk2cy0NxpmppnZaouWTTljzu870yyZuaSWmtWYZim5g/uu4AKIGyjIds79+wM9eAAVj8A5wPs5wyPOfa77Ptd9H+Bcfu7PYjIMw0BEREREbMzOTkBERETE1ahAEhERESlFBZKIiIhIKSqQREREREpRgSQiIiJSigokERERkVJUIImIiIiUogJJREREpBR3ZydQU1itVo4ePYq/vz8mk8nZ6YiIiEgFGIbBmTNnaNasGWZzxduFVCBV0NGjRwkLC3N2GiIiIuKAQ4cO0bx58wrHu2SBNG3aND744APS09OJiorik08+oVevXuXGbtu2jQkTJrBhwwZSUlL46KOPGDlypF3MpEmT+Prrr9m5cyc+Pj707t2b9957j3bt2lU4J39/f6D4AgcEBDh8biIiIlJ9srOzCQsLs32OV5TLFUhfffUVCQkJTJ8+nZiYGKZOnUpcXBy7du2iSZMmZeJzc3Np2bIlgwYN4oUXXij3m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9dG9zd6k7ZUHfOVK7JI488wsiRIxk4cCDLly8nKSmJV199lYULF/LDD
},
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"source": [
"## The option with a `jit` compiler turned out to be the fastest. Let's use it to calculate $a(10^8)$:"
],
"metadata": {
"id": "KDEaQcaifhfn"
}
},
{
"cell_type": "code",
"source": [
"n = int(1e8)\n",
"a = np.ones(n, dtype = int)\n",
"\n",
"%time HC_func3(a, n)\n",
"%time HC_func3(a, n)\n",
"%time HC_func3(a, n)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dhgtJB8X0fkO",
"outputId": "e01fe169-8137-4a64-88f9-e08d57600fcd"
},
"execution_count": 29,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"CPU times: user 586 ms, sys: 4.05 ms, total: 590 ms\n",
"Wall time: 588 ms\n",
"CPU times: user 586 ms, sys: 4.75 ms, total: 590 ms\n",
"Wall time: 586 ms\n",
"CPU times: user 587 ms, sys: 2.11 ms, total: 589 ms\n",
"Wall time: 584 ms\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"x = np.arange(1, n + 1, 1, dtype=int)\n",
"plt.plot(x, a);\n",
"plt.title(\"$a(10^8)$\", fontsize=14);"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 476
},
"id": "fggJi6PlgQOv",
"outputId": "7a4d3f18-b3d8-4002-d4b5-38cae766af9f"
},
"execution_count": 30,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
],
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAhYAAAHLCAYAAACUD9G/AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAABEbklEQVR4nO3dZ3gU5cLG8XvTSYXQA6ETkBKqQCiiggIiAiogICBix4oNREUUQb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AAHIZiAQCAG8m12U3dPsUCAAA3kGbN1Zgfd+q+L9fLbjdMy1HsdzcFAACOtfnQWT37XbSOnsuUJK0/eEZt65QzJQvFAgAAF5Vrs+uLNQf1wfJY2Q2paplSeufuSNNKhUSxAADAJcWfTtez30Vr+5HzkqTezapobM+GCvbzNjUXxQIAABfzw9ajGr1gpzJzbAry89LrdzZQn5bhZseSRLEAAMBlZGTn6tWFO/XD1mOSpKhaZfVhvyaqHFLK5GT/j2IBAIALOHQ6XY9+u0WxSanysEhP3VpXT3eqK08Pi9nRLkGxAADAiRmGoe+3HtOb/92llKxclQv01aQBzdS6Vlmzo10RxQIAACd1PiNbo36I0ZKdiZKkpuGl9eWgFqoQ7GdysqujWAAA4IS2HD6np2dv07HzmfLysOi52yL06E215OXp3HNbUiwAAHAihmHoq7XxmrBkr2x2Q9VC/TVpQHM1rhpidrTrQrEAAMBJpFtzNfKHGP13+3FJUo8mYRrXq
},
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"source": [
"## Finally we display the graph $f(n) = a(n)/n$ for $n = 1, 2, ..., 2000$"
],
"metadata": {
"id": "pH6dsdzmhBmv"
}
},
{
"cell_type": "code",
"source": [
"n = int(2000)\n",
"a = np.ones(n, dtype = int)\n",
"HC_func3(a, n)\n",
"\n",
"x = np.arange(1, n + 1, 1, dtype=int)\n",
"y = a * np.array([1 / (i + 1) for i in range(n)])\n",
"plt.plot(x, y);\n",
"plt.title(\"a(n)/n\", fontsize=14);"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 453
},
"id": "tm9UvLrD9wfW",
"outputId": "e0b72afe-3cd1-444b-8849-744c827a7f75"
},
"execution_count": 31,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
],
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAG0CAYAAADgoSfXAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cH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M+swQkRERMpjGCEiIiJFMYwQERGRohhGiIiISFEMI+DMNEREREoy7zDCzjRERESKM+8wQkRERIpjGCEiIiJFMYwQERGRohhGiIiISFEMI+DcNEREREoy6zDCzjRERETKM+swQkRERMpjGCEiIiJFMYwQERGRohhGiIiISFEMIwAEZ6chIiJSjFmHERW70xARESnOrMMIERERKY9hhIiIiBTFMEJERESKYhgBh4MnIiJSklmHERUHhCciIlKcWYcRIiIiUh7DCBERESmKYYSIiIgUxTBCREREimIYATgYPBERkYLMOoxwOHgiIiLlmXUYISIiIuUxjBAREZGiGEaIiIhIUQwjREREpCiGEQCCk9MQEREpxqzDCHvTEBERKc+swwgREREpj2GEiIiIFMUwQkRERIpiGCEiIiJFMYwQERGRou4rjCxZsgQBAQGwtrZGaGgoDhw4YHTbwsJCzJkzBy1atIC1tTWCg4OxadOm+y5wTVKB3WmIiIiUZnIYWbVqFSIjIzF79mwcOXIEwcHBiIiIQHp6usHtZ86cia+++gqff/45Tp8+jYkTJ+Lxxx/H0aNHH7jwRERE1PCZHEYWLlyICRMmYNy4cWjXrh2WLl0KW1tbLF++3OD2P/zwA2bMmIGhQ4eie
},
"metadata": {}
}
]
}
]
}