From df68a1636ddad4b5a68e274acc0c04c9f31e62d7 Mon Sep 17 00:00:00 2001 From: Tiziano Zito Date: Wed, 21 Aug 2024 12:34:31 +0200 Subject: [PATCH 1/4] my solution to the final exercise --- exercise-my-solution.ipynb | 172 +++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 exercise-my-solution.ipynb diff --git a/exercise-my-solution.ipynb b/exercise-my-solution.ipynb new file mode 100644 index 0000000..bb98c7e --- /dev/null +++ b/exercise-my-solution.ipynb @@ -0,0 +1,172 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "execution": { + "iopub.execute_input": "2024-03-04T09:40:28.904Z", + "iopub.status.busy": "2024-03-04T09:40:28.896Z", + "iopub.status.idle": "2024-03-04T09:40:28.978Z", + "shell.execute_reply": "2024-03-04T09:40:28.967Z" + } + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "execution": { + "iopub.execute_input": "2024-03-04T10:02:39.062Z", + "iopub.status.busy": "2024-03-04T10:02:39.057Z", + "iopub.status.idle": "2024-03-04T10:02:39.068Z", + "shell.execute_reply": "2024-03-04T10:02:39.071Z" + } + }, + "outputs": [], + "source": [ + "n_series = 32\n", + "len_one_series = 5*2**20\n", + "time_series = np.random.rand(n_series, len_one_series)\n", + "gap = 16*2**10" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "execution": { + "iopub.execute_input": "2024-03-04T10:02:41.027Z", + "iopub.status.busy": "2024-03-04T10:02:41.020Z", + "iopub.status.idle": "2024-03-04T10:02:41.036Z", + "shell.execute_reply": "2024-03-04T10:02:41.040Z" + }, + "scrolled": true + }, + "outputs": [], + "source": [ + "print(f'Size of one time series: {int(time_series[0].nbytes/2**20)} M')\n", + "print(f'Size of collection: {int(time_series.nbytes/2**20)} M')\n", + "print(f'Gap size: {int(gap*8/2**10)} K')\n", + "print(f'Gapped series size: {int(time_series[0, ::gap].nbytes/2**10)} K')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "execution": { + "iopub.execute_input": "2024-03-04T10:06:08.461Z", + "iopub.status.busy": "2024-03-04T10:06:08.459Z", + "iopub.status.idle": "2024-03-04T10:06:08.466Z", + "shell.execute_reply": "2024-03-04T10:06:08.468Z" + } + }, + "outputs": [], + "source": [ + "# compute a Taylor-like series\n", + "def taylor(time_series, mean, gap):\n", + " for row, ts in enumerate(time_series):\n", + " for pwr in range(1,20):\n", + " mean[row] += (ts[::gap]**pwr).sum()\n", + " return mean\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "execution": { + "iopub.execute_input": "2024-03-04T10:06:08.461Z", + "iopub.status.busy": "2024-03-04T10:06:08.459Z", + "iopub.status.idle": "2024-03-04T10:06:08.466Z", + "shell.execute_reply": "2024-03-04T10:06:08.468Z" + } + }, + "outputs": [], + "source": [ + "def taylor_improved(time_series, mean, gap):\n", + " y = time_series[:,::gap].copy()\n", + " for row, ts in enumerate(y):\n", + " for pwr in range(1,20):\n", + " mean[row] += (ts**pwr).sum()\n", + " return mean" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# verify that they yield the same results\n", + "out1 = taylor(time_series, np.zeros(n_series), gap)\n", + "out2 = taylor_improved(time_series, np.zeros(n_series), gap)\n", + "np.testing.assert_allclose(out1, out2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "execution": { + "iopub.execute_input": "2024-03-04T10:06:14.959Z", + "iopub.status.busy": "2024-03-04T10:06:14.956Z", + "iopub.status.idle": "2024-03-04T10:06:17.437Z", + "shell.execute_reply": "2024-03-04T10:06:17.443Z" + } + }, + "outputs": [], + "source": [ + "mean = np.zeros(n_series, dtype='float64')\n", + "%timeit taylor(time_series, mean, gap)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "execution": { + "iopub.execute_input": "2024-03-04T10:06:20.056Z", + "iopub.status.busy": "2024-03-04T10:06:20.053Z", + "iopub.status.idle": "2024-03-04T10:06:21.695Z", + "shell.execute_reply": "2024-03-04T10:06:21.700Z" + } + }, + "outputs": [], + "source": [ + "mean = np.zeros(n_series, dtype='float64')\n", + "%timeit taylor_improved(time_series, mean, gap)" + ] + } + ], + "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.5" + }, + "nteract": { + "version": "0.28.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 165239e433071f694c279870c1dae1e16b46f8d1 Mon Sep 17 00:00:00 2001 From: otizonaizit Date: Mon, 26 Aug 2024 22:38:23 +0200 Subject: [PATCH 2/4] typo --- architecture/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/architecture/README.md b/architecture/README.md index eefadfc..de9596b 100644 --- a/architecture/README.md +++ b/architecture/README.md @@ -11,7 +11,7 @@ - `bin(14)` ➔ `'0b1110'` - `np.iinfo(np.int32)` ➔ `iinfo(min=-2147483648, max=2147483647, dtype=int32)` - Python integers, as opposed to numpy integer types, are represented with a flexible number of bits: `sys.int_info` ➔ `bits_per_digit=30, sizeof_digit=4, default_max_str_digits=4300, str_digits_check_threshold=640` - - they are called "long" or "sperlong" integers, because they can have arbitrary size. Low level implementation explained: + - they are called "long" or "superlong" integers, because they can have arbitrary size. Low level implementation explained: - [Arpit Bhayani's blog](https://arpitbhayani.me/blogs/long-integers-python/) - [Artem Golubin's blog](https://rushter.com/blog/python-integer-implementation/) From 8f67903169545db1781247a50ecc816878b37f44 Mon Sep 17 00:00:00 2001 From: Tiziano Zito Date: Thu, 29 Aug 2024 10:12:18 +0300 Subject: [PATCH 3/4] add reference to tarot cards meaning --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 35f4a1c..002c62c 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,9 @@ Two exercises to activate the body and the mind Common goal of both exercises is to sort a deck of tarot cards by value + +Before starting, make yourself acquainted with the meanining and the power of the tarot cards. Carefully read [this booklet](https://aspp.school/wiki/_media/tarot-runic.pdf) + ### First experiment: human sorting Setup: - 1 volunteer to keep the time spent sorting From 8d8bab72425575dee9aac34507ea7bcdee55738c Mon Sep 17 00:00:00 2001 From: Tiziano Zito Date: Sat, 31 Aug 2024 10:39:50 +0300 Subject: [PATCH 4/4] add notes about low-level representation of strings Fixes #2 --- architecture/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/architecture/README.md b/architecture/README.md index eefadfc..699660a 100644 --- a/architecture/README.md +++ b/architecture/README.md @@ -37,6 +37,7 @@ - UTF8 encoded, flexible width from 1B (byte) to 4B (bytes): 1,112,064 Unicode characters (code points) - ASCII: 7 bits (fits in one byte), 127 characters ➔ [ASCII table](https://upload.wikimedia.org/wikipedia/commons/2/26/ASCII_Table_%28suitable_for_printing%29.svg) - [visualization](https://sonarsource.github.io/utf8-visualizer/) + - actually in Python strings (more precisely: unicode objects) are stored in different formats depending on which characters are stored for memory efficiency. Look at the gory details [here](https://docs.python.org/3.14/c-api/unicode.html) ➔ not for the faint-hearted! - **hexadecimal notation**: - base16 ➔ '0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f'