{ "cells": [ { "cell_type": "raw", "id": "2990f581", "metadata": {}, "source": [ "---\n", "title: \"APL Competition 2022\"\n", "author: \"Jeremy Howard\"\n", "date: \"2022-07-05\"\n", "categories: [Dyalog, APL, Glyphs]\n", "---" ] }, { "cell_type": "markdown", "id": "5d6af942", "metadata": {}, "source": [ "This notebook covers useful glyphs and concepts useful for the 2022 Dyalog APL competition. Be sure to read the [Basics](01_APL.ipynb) and [Operators](03_operators.ipynb) notebooks first." ] }, { "cell_type": "code", "execution_count": null, "id": "84e8fa35", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────────────────────────────────┐\n",
       "│Was ON -style=max -trains=tree -fns=on│\n",
       "└──────────────────────────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "]box on -style=max -trains=tree -fns=on" ] }, { "cell_type": "markdown", "id": "da2674b7", "metadata": {}, "source": [ "## Glpyhs" ] }, { "cell_type": "markdown", "id": "426ab86a", "metadata": {}, "source": [ "### `↑` (Up arrow)" ] }, { "cell_type": "markdown", "id": "d7c5a6e2", "metadata": {}, "source": [ "#### monadic `↑` (Mix)" ] }, { "cell_type": "markdown", "id": "4ddbcb70-667a-4885-91b4-ae34a82441e7", "metadata": {}, "source": [ "Mix takes elements of a complex array and stacks them along rows." ] }, { "cell_type": "code", "execution_count": null, "id": "3f02ff32", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────┐\n",
       "│ ┌→──┐ ┌→──┐ │\n",
       "│ │Hip│ │Hop│ │\n",
       "│ └───┘ └───┘ │\n",
       "└∊────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hip' 'Hop'" ] }, { "cell_type": "code", "execution_count": null, "id": "3f4cf518", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "↓Hip│\n",
       "│Hop│\n",
       "└───┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "↑ 'Hip' 'Hop'" ] }, { "cell_type": "code", "execution_count": null, "id": "c8195e4a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "│2 3│\n",
       "└~──┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍴↑ 'Hip' 'Hop'" ] }, { "cell_type": "markdown", "id": "3606c3d8-4edc-4163-bcb0-44c275b0f6ca", "metadata": {}, "source": [ "If elements are not all the same length, Mix will pad as needed" ] }, { "cell_type": "code", "execution_count": null, "id": "768e22de-e779-457e-9246-48159d63a2ab", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────────────┐\n",
       "│ ┌→──┐ ┌→──┐ ┌→────┐ │\n",
       "│ │Hip│ │Hop│ │Dance│ │\n",
       "│ └───┘ └───┘ └─────┘ │\n",
       "└∊────────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hip' 'Hop' 'Dance'" ] }, { "cell_type": "code", "execution_count": null, "id": "ec916625-a9af-4824-976f-987b695d158c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n",
       "↓Hip  │\n",
       "│Hop  │\n",
       "│Dance│\n",
       "└─────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "↑ 'Hip' 'Hop' 'Dance'" ] }, { "cell_type": "code", "execution_count": null, "id": "de720fe0-df52-47dc-9675-cefc4df54b0f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "│3 5│\n",
       "└~──┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍴↑ 'Hip' 'Hop' 'Dance'" ] }, { "cell_type": "markdown", "id": "a63eeb4e-b6e7-4e8b-ab8f-3ffa2e056e2b", "metadata": {}, "source": [ "Arrays can be mixed arrays containing both simple scalars and nested arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "cd23ab13", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────┐\n",
       "│ ┌→──┐     │\n",
       "│ │6 4│ 5 3 │\n",
       "│ └~──┘     │\n",
       "└∊──────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "
┌→──┐\n",
       "↓6 4│\n",
       "│5 0│\n",
       "│3 0│\n",
       "└~──┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(6 4) 5 3\n", "↑ (6 4) 5 3" ] }, { "cell_type": "code", "execution_count": null, "id": "b510d101", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────────┐\n",
       "│ ┌→──┐   ┌→────┐ │\n",
       "│ │1 2│ 3 │4 5 6│ │\n",
       "│ └~──┘   └~────┘ │\n",
       "└∊────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "
┌→────┐\n",
       "↓1 2 0│\n",
       "│3 0 0│\n",
       "│4 5 6│\n",
       "└~────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1 2) 3 (4 5 6)\n", "↑(1 2) 3 (4 5 6)" ] }, { "cell_type": "markdown", "id": "01db0c25", "metadata": {}, "source": [ "#### dyadic `↑` (Take)" ] }, { "cell_type": "markdown", "id": "8f3f149f-5ed7-4698-8371-31ce92b2c200", "metadata": {}, "source": [ "If `⍵` (right argument) is positive, returns the first `⍵` (right argument) elements of `⍺` (left argument)" ] }, { "cell_type": "code", "execution_count": null, "id": "b4cfe378", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───┐\n",
       "│Pine│\n",
       "└────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 ↑ 'Pineapple'" ] }, { "cell_type": "markdown", "id": "965b139e-f748-48ae-808a-4911514a4fe7", "metadata": {}, "source": [ "If `⍺` (left argument) is longer than the length of `⍵` (right argument), `⍵` is padded with blanks" ] }, { "cell_type": "code", "execution_count": null, "id": "032e52ce", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───────────┐\n",
       "│Pineapple   │\n",
       "└────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12 ↑ 'Pineapple'" ] }, { "cell_type": "markdown", "id": "eee3f026-d0ff-480a-9dff-a0378ee0bebc", "metadata": {}, "source": [ "If `⍵` (right argument) is negative, it returns the last elements rather than the first elements." ] }, { "cell_type": "code", "execution_count": null, "id": "83c4b6c7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n",
       "│apple│\n",
       "└─────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "¯5 ↑ 'Pineapple'" ] }, { "cell_type": "markdown", "id": "b35d6a60-a867-4b7b-9a17-4cf96dc13e47", "metadata": {}, "source": [ "When `⍺` (left argument) is a scalar `⍵` (right argument) indexes on the first axis. The same rules apply to padding and negative numbers with higher dimension arrays." ] }, { "cell_type": "code", "execution_count": null, "id": "359d90ed", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────┐\n",
       "↓1  2  3  4│\n",
       "│5  6  7  8│\n",
       "│9 10 11 12│\n",
       "└~─────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⎕←mat←3 4⍴⍳12" ] }, { "cell_type": "code", "execution_count": null, "id": "6f0d38ba", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n",
       "↓1 2 3 4│\n",
       "│5 6 7 8│\n",
       "└~──────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ↑ mat" ] }, { "cell_type": "code", "execution_count": null, "id": "71b7b051-1be6-4420-bafa-34889ce1eda1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────┐\n",
       "↓5  6  7  8│\n",
       "│9 10 11 12│\n",
       "└~─────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "¯2 ↑ mat" ] }, { "cell_type": "code", "execution_count": null, "id": "a67fd7b2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────┐\n",
       "↓1  2  3  4│\n",
       "│5  6  7  8│\n",
       "│9 10 11 12│\n",
       "│0  0  0  0│\n",
       "│0  0  0  0│\n",
       "└~─────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 ↑ mat" ] }, { "cell_type": "markdown", "id": "9f2068d5-1bd6-4e0d-a12e-8ccaf5c5310c", "metadata": {}, "source": [ "When an array is passed to `⍺` (left argument) you can slice on more than just the first axis. For example, `2 ¯3 ↑ mat` will give the first 2 rows (1st axis) and the last 3 columns (2nd axis) of mat." ] }, { "cell_type": "code", "execution_count": null, "id": "d25a75ba", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n",
       "↓2 3 4│\n",
       "│6 7 8│\n",
       "└~────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ¯3 ↑ mat" ] }, { "cell_type": "markdown", "id": "ccce31ab", "metadata": {}, "source": [ "### `↓` (Down arrow)" ] }, { "cell_type": "markdown", "id": "b13ca055", "metadata": {}, "source": [ "#### monadic `↓` (Split)" ] }, { "cell_type": "markdown", "id": "74e5497d-a008-4150-87e3-e51d982d67ce", "metadata": {}, "source": [ "Split takes an array and reshapes it to be all 1 row. That row will contain nested arrays that each contain the values in 1 of the rows." ] }, { "cell_type": "code", "execution_count": null, "id": "0a82ac8d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────┐\n",
       "↓1  2  3  4│\n",
       "│5  6  7  8│\n",
       "│9 10 11 12│\n",
       "└~─────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⎕←mat←3 4⍴⍳12" ] }, { "cell_type": "code", "execution_count": null, "id": "c37f5e72", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────────────────────────────┐\n",
       "│ ┌→──────┐ ┌→──────┐ ┌→─────────┐ │\n",
       "│ │1 2 3 4│ │5 6 7 8│ │9 10 11 12│ │\n",
       "│ └~──────┘ └~──────┘ └~─────────┘ │\n",
       "└∊─────────────────────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "↓mat" ] }, { "cell_type": "code", "execution_count": null, "id": "87d57b9b-2bd9-4f13-a6a9-ec41eb8a39ae", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────┐\n",
       "↓ ┌→──┐   │\n",
       "│ │Hip│   │\n",
       "│ └───┘   │\n",
       "│ ┌→──┐   │\n",
       "│ │Hop│   │\n",
       "│ └───┘   │\n",
       "│ ┌→────┐ │\n",
       "│ │Dance│ │\n",
       "│ └─────┘ │\n",
       "└∊────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⎕ ← mat ← 3 1⍴'Hip' 'Hop' 'Dance'" ] }, { "cell_type": "code", "execution_count": null, "id": "2a27177a-63cc-4997-aa9a-dc8a94dc967f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────────────────────────┐\n",
       "│ ┌→──────┐ ┌→──────┐ ┌→────────┐ │\n",
       "│ │ ┌→──┐ │ │ ┌→──┐ │ │ ┌→────┐ │ │\n",
       "│ │ │Hip│ │ │ │Hop│ │ │ │Dance│ │ │\n",
       "│ │ └───┘ │ │ └───┘ │ │ └─────┘ │ │\n",
       "│ └∊──────┘ └∊──────┘ └∊────────┘ │\n",
       "└∊────────────────────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "↓mat" ] }, { "cell_type": "markdown", "id": "17c9279a", "metadata": {}, "source": [ "#### dyadic `↓` (Drop)" ] }, { "cell_type": "markdown", "id": "6313de4d-63b9-4406-8e04-d4fcf1a08966", "metadata": {}, "source": [ "If `⍵` (right argument) is positive, removes the first `⍵` (right argument) elements of `⍺` (left argument)." ] }, { "cell_type": "code", "execution_count": null, "id": "07a1a03c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n",
       "│apple│\n",
       "└─────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 ↓ 'Pineapple'" ] }, { "cell_type": "code", "execution_count": null, "id": "17186dd4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌⊖┐\n",
       "│ │\n",
       "└─┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12 ↓ 'Pineapple'" ] }, { "cell_type": "markdown", "id": "a8233efd-8aef-4610-9956-7110caa943ff", "metadata": {}, "source": [ "If `⍵` is negative, it removes the last elements rather than the first elements." ] }, { "cell_type": "code", "execution_count": null, "id": "bd5cbaac", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───┐\n",
       "│Pine│\n",
       "└────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "¯5 ↓ 'Pineapple'" ] }, { "cell_type": "markdown", "id": "752c7060-7a79-4d54-8e62-8f0f7ff44108", "metadata": {}, "source": [ "When `⍺` (left argument) is a scalar `⍵` (right argument) indexes on the first axis. The same rules apply to padding and negative numbers with higher dimension arrays." ] }, { "cell_type": "code", "execution_count": null, "id": "0bbd6fb2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────┐\n",
       "↓1  2  3  4│\n",
       "│5  6  7  8│\n",
       "│9 10 11 12│\n",
       "└~─────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⎕←mat←3 4⍴⍳12" ] }, { "cell_type": "code", "execution_count": null, "id": "74e2fbcb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────┐\n",
       "↓9 10 11 12│\n",
       "└~─────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ↓ mat" ] }, { "cell_type": "markdown", "id": "fea13e80-85e0-4527-ab23-26a60d43db50", "metadata": {}, "source": [ "When an array is passed to `⍺` (left argument) you can slice on more than just the first axis. For example, `2 ¯3 ↑ mat` will remove the first 2 rows (1st axis) and the last 3 columns (2nd axis) of mat." ] }, { "cell_type": "code", "execution_count": null, "id": "6179ae17", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→┐\n",
       "↓9│\n",
       "└~┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ¯3 ↓ mat" ] }, { "cell_type": "markdown", "id": "b3a82aff", "metadata": {}, "source": [ "### `⌸` (Quad equal)" ] }, { "cell_type": "markdown", "id": "08eb109e", "metadata": {}, "source": [ "#### monadic `⌸` (Key operator)" ] }, { "cell_type": "markdown", "id": "92134238-c4d0-4374-93fa-882eac9d5311", "metadata": {}, "source": [ "The `key operator` gives the index locations of each unique element in a vector. For example `,⌸ 'banana'` has a `b` as the 1st element. `a` is the 2nd, 4th, and 6th elements. `n` is the 3rd and 5th elements." ] }, { "cell_type": "code", "execution_count": null, "id": "2ddcb1ea", "metadata": {}, "outputs": [], "source": [ "a ← 'banana'" ] }, { "cell_type": "code", "execution_count": null, "id": "548f40f2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n",
       "↓b 1    │\n",
       "│a 2 4 6│\n",
       "│n 3 5  │\n",
       "└+──────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ ",⌸ a" ] }, { "cell_type": "markdown", "id": "260f8205-ee48-487a-8ce8-4b00c59746f8", "metadata": {}, "source": [ "You can also combine this with other functions to do operations such as sum those index locations together." ] }, { "cell_type": "code", "execution_count": null, "id": "c4293cfd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───┐\n",
       "↓b  1│\n",
       "│a 12│\n",
       "│n  8│\n",
       "└+───┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a {⍺,+/⍵}⌸ ⍳6" ] }, { "cell_type": "code", "execution_count": null, "id": "d5b2cb64", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n",
       "↓b 1    │\n",
       "│a 2 4 6│\n",
       "│n 3 5  │\n",
       "└+──────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a ,⌸ ⍳6" ] }, { "cell_type": "markdown", "id": "d6e4c907", "metadata": {}, "source": [ "### `⊂` (Left shoe)" ] }, { "cell_type": "markdown", "id": "7148c091", "metadata": {}, "source": [ "#### monadic `⊂` (Enclose)" ] }, { "cell_type": "markdown", "id": "e8e8d8d0-42eb-45ef-8cf6-95db63922b33", "metadata": {}, "source": [ "Enclose is used to turn an array into a nested array with just one element. For example `1 2 3` is an array of shape 3. " ] }, { "cell_type": "code", "execution_count": null, "id": "5973fe9f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n",
       "│1 2 3│\n",
       "└~────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3" ] }, { "cell_type": "code", "execution_count": null, "id": "bd386b4c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→┐\n",
       "│3│\n",
       "└~┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍴1 2 3" ] }, { "cell_type": "markdown", "id": "cec54d4c-8487-4019-9153-71ad37bd1174", "metadata": {}, "source": [ "When we enclose `1 2 3` it becomes a scalar, meaning the shape is the empty list." ] }, { "cell_type": "code", "execution_count": null, "id": "c1bf4da8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌─────────┐\n",
       "│ ┌→────┐ │\n",
       "│ │1 2 3│ │\n",
       "│ └~────┘ │\n",
       "└∊────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊂1 2 3" ] }, { "cell_type": "code", "execution_count": null, "id": "ac73f91f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌⊖┐\n",
       "│0│\n",
       "└~┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍴⊂1 2 3" ] }, { "cell_type": "markdown", "id": "d40ca852-b6f1-4dae-9aec-5018b6c39d70", "metadata": {}, "source": [ "This can be used in conjunction with `,` in order to created arrays with nested elements." ] }, { "cell_type": "code", "execution_count": null, "id": "b3a8f422", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────┐\n",
       "│   ┌→──┐ │\n",
       "│ 1 │2 3│ │\n",
       "│   └~──┘ │\n",
       "└∊────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1(2 3)" ] }, { "cell_type": "code", "execution_count": null, "id": "5174b653", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→┐\n",
       "│2│\n",
       "└~┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍴ 1(2 3)" ] }, { "cell_type": "code", "execution_count": null, "id": "2a417898", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────┐\n",
       "│   ┌→──┐ │\n",
       "│ 1 │2 3│ │\n",
       "│   └~──┘ │\n",
       "└∊────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1,⊂2 3" ] }, { "cell_type": "code", "execution_count": null, "id": "8430caa1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌─────────────┐\n",
       "│ ┌→────────┐ │\n",
       "│ │   ┌→──┐ │ │\n",
       "│ │ 1 │2 3│ │ │\n",
       "│ │   └~──┘ │ │\n",
       "│ └∊────────┘ │\n",
       "└∊────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊂ 1(2 3)" ] }, { "cell_type": "code", "execution_count": null, "id": "97f5641a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌⊖┐\n",
       "│0│\n",
       "└~┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍴⊂ 1(2 3)" ] }, { "cell_type": "markdown", "id": "a11682e8-5df0-41a6-9ade-bcd15d9ad98f", "metadata": {}, "source": [ "A good way to see what enclose does is to do multiple of them, which continues to nest your original array deeper the more you add." ] }, { "cell_type": "code", "execution_count": null, "id": "f271d213", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌─────────────────┐\n",
       "│ ┌─────────────┐ │\n",
       "│ │ ┌→────────┐ │ │\n",
       "│ │ │   ┌→──┐ │ │ │\n",
       "│ │ │ 1 │2 3│ │ │ │\n",
       "│ │ │   └~──┘ │ │ │\n",
       "│ │ └∊────────┘ │ │\n",
       "│ └∊────────────┘ │\n",
       "└∊────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊂⊂ 1(2 3)" ] }, { "cell_type": "markdown", "id": "5be67cfa-da94-47b9-b691-da86d26ae32e", "metadata": {}, "source": [ "An enclosed simple scalar is still the original simple scalar." ] }, { "cell_type": "code", "execution_count": null, "id": "e55cbaac", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "1\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊂1" ] }, { "cell_type": "markdown", "id": "0510c31d", "metadata": {}, "source": [ "This adds `4 5 6` to each element of the LHS (1, 2, and 3):" ] }, { "cell_type": "code", "execution_count": null, "id": "90e97812", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────────────────┐\n",
       "│ ┌→────┐ ┌→────┐ ┌→────┐ │\n",
       "│ │5 6 7│ │6 7 8│ │7 8 9│ │\n",
       "│ └~────┘ └~────┘ └~────┘ │\n",
       "└∊────────────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3+⊂4 5 6" ] }, { "cell_type": "markdown", "id": "31548044", "metadata": {}, "source": [ "It's basically the same idea as this, since `(⊂ 4 5 6)` and `1` are both scalars:" ] }, { "cell_type": "code", "execution_count": null, "id": "fd7c7b48", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n",
       "│2 3 4│\n",
       "└~────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3+1" ] }, { "cell_type": "markdown", "id": "58d72f9c", "metadata": {}, "source": [ "#### dyadic `⊂` (Partitioned enclose)" ] }, { "cell_type": "markdown", "id": "adb07307-c5a4-463d-af3d-bccfc1d8e739", "metadata": {}, "source": [ "`partitioned enclose` is used to split an array into separate enclosures (created nested arrays). \n", "\n", "`⍺` (left argument) dictates where to split the array up. For example, `1 0 1 0 0 0 0` has a positive integer in the 1st and 3rd position and so we will split at the 1st and 3rd element" ] }, { "cell_type": "code", "execution_count": null, "id": "8272a0ec-7474-4355-8ee1-1c6e4963bcb9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────────────┐\n",
       "│ ┌→──┐ ┌→────────┐ │\n",
       "│ │1 2│ │3 4 5 6 7│ │\n",
       "│ └~──┘ └~────────┘ │\n",
       "└∊──────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 0 1 0 0 0 0⊂ 1 2 3 4 5 6 7" ] }, { "cell_type": "markdown", "id": "2b7eb786-0c9a-492d-99f7-1d12e301fb13", "metadata": {}, "source": [ "This can also be done with textual arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "b2e43612", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────────┐\n",
       "│ ┌→─┐ ┌→────┐ │\n",
       "│ │Hi│ │Earth│ │\n",
       "│ └──┘ └─────┘ │\n",
       "└∊─────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 0 1 0 0 0 0⊂'HiEarth'" ] }, { "cell_type": "markdown", "id": "e471002f-6e7f-4f41-afcb-d6593192cd26", "metadata": {}, "source": [ "The left argument of `partitioned enclose` controls the positioning of the newly created enclosures." ] }, { "cell_type": "code", "execution_count": null, "id": "2869efa5-9ea8-41ec-b5eb-2442b25f9a1a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────────────────────────┐\n",
       "│ ┌⊖┐ ┌→──┐ ┌→──┐ ┌⊖┐ ┌⊖┐ ┌→──┐ │\n",
       "│ │0│ │1 2│ │3 4│ │0│ │0│ │5 6│ │\n",
       "│ └~┘ └~──┘ └~──┘ └~┘ └~┘ └~──┘ │\n",
       "└∊──────────────────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 0 1 0 3 0 ⊂ 1 2 3 4 5 6" ] }, { "cell_type": "code", "execution_count": null, "id": "2c277ea0-ac6e-40a9-833d-8a9317c86d65", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────────────┐\n",
       "│ ┌→─┐ ┌⊖┐ ┌→────┐ │\n",
       "│ │Hi│ │ │ │Earth│ │\n",
       "│ └──┘ └─┘ └─────┘ │\n",
       "└∊─────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 0 2 0 0 0 0⊂'HiEarth'" ] }, { "cell_type": "markdown", "id": "129ede39", "metadata": {}, "source": [ "### `⊆` (Left shoe underbar)" ] }, { "cell_type": "markdown", "id": "83bd3a74", "metadata": {}, "source": [ "#### monadic `⊆` (Nest)" ] }, { "cell_type": "code", "execution_count": null, "id": "e05aaf18", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "1\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(⊆1) ≡ ⊂1" ] }, { "cell_type": "code", "execution_count": null, "id": "63181909", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "1\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(⊆1 2 3) ≡ ⊂1 2 3" ] }, { "cell_type": "code", "execution_count": null, "id": "f63a328c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "1\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(⊆ 1 (1 2 3)) ≡ 1 (1 2 3)" ] }, { "cell_type": "code", "execution_count": null, "id": "1cac6b3b-5649-453a-ba3d-bfa8487e678b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────┐\n",
       "│   ┌→────┐ │\n",
       "│ 1 │1 2 3│ │\n",
       "│   └~────┘ │\n",
       "└∊──────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "
┌→──────────┐\n",
       "│   ┌→────┐ │\n",
       "│ 1 │1 2 3│ │\n",
       "│   └~────┘ │\n",
       "└∊──────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊆ 1 (1 2 3)\n", "\n", "1 (1 2 3)" ] }, { "cell_type": "code", "execution_count": null, "id": "47b45de8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌───────────────┐\n",
       "│ ┌→──────────┐ │\n",
       "│ │   ┌→────┐ │ │\n",
       "│ │ 1 │1 2 3│ │ │\n",
       "│ │   └~────┘ │ │\n",
       "│ └∊──────────┘ │\n",
       "└∊──────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊂ 1 (1 2 3)" ] }, { "cell_type": "markdown", "id": "620edc7a", "metadata": {}, "source": [ "#### dyadic `⊆` (Partition)" ] }, { "cell_type": "markdown", "id": "235c21d5-0ad0-4570-a74e-bb005cc07630", "metadata": {}, "source": [ "Partition breaks an array (`⍵`) up based on the numbers provided in the left argument (`⍺`).\n", "\n", "A new partition is created anytime the integer in the left argument changes. 0 means that element is not included in the output. For example:\n", "\n", "`1 1 1 0 1 1 1 0 1 1 1 1⊆'How are you?'`:\n", "+ The first 3 elements are in the first partition because there are 3 of the same integers `1 1 1`. \n", "+ The first space is excluded because it is the fourth element in the right argument and the fourth element in the left argument is 0.\n", "+ The second partition starts on the 5th element because it's a non-zero integer that is not the same as the previous number." ] }, { "cell_type": "code", "execution_count": null, "id": "feca5be8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───────────────────┐\n",
       "│ ┌→──┐ ┌→──┐ ┌→───┐ │\n",
       "│ │How│ │are│ │you?│ │\n",
       "│ └───┘ └───┘ └────┘ │\n",
       "└∊───────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 1 1 0 1 1 1 0 1 1 1 1⊆'How are you?'" ] }, { "cell_type": "code", "execution_count": null, "id": "adfd2d1e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────────┐\n",
       "│ ┌→─┐ ┌→────┐ │\n",
       "│ │Hi│ │Earth│ │\n",
       "│ └──┘ └─────┘ │\n",
       "└∊─────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 1 2 2 2 2 2⊆'HiEarth'" ] }, { "cell_type": "code", "execution_count": null, "id": "ef923d85", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───────────┐\n",
       "│ ┌→─┐ ┌→──┐ │\n",
       "│ │Hi│ │Ear│ │\n",
       "│ └──┘ └───┘ │\n",
       "└∊───────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 1 2 2 2 0 0⊆'HiEarth'" ] }, { "cell_type": "markdown", "id": "f15d3d80", "metadata": {}, "source": [ "### `⊃` (Right shoe)" ] }, { "cell_type": "markdown", "id": "e288df04", "metadata": {}, "source": [ "#### monadic `⊃` (Disclose;First)" ] }, { "cell_type": "markdown", "id": "55141b18-a802-49c9-ba35-eb4a56048a10", "metadata": {}, "source": [ "Disclose removes the nesting around arrays. For example, in simple arrays disclose with reverse and enclose. This is because it is getting the first element in a list, and when an array is enclosed the first element is the whole original array." ] }, { "cell_type": "code", "execution_count": null, "id": "bec34bd9", "metadata": {}, "outputs": [], "source": [ "a←1 2" ] }, { "cell_type": "code", "execution_count": null, "id": "ab4fc081", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌───────┐\n",
       "│ ┌→──┐ │\n",
       "│ │1 2│ │\n",
       "│ └~──┘ │\n",
       "└∊──────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊂a" ] }, { "cell_type": "code", "execution_count": null, "id": "fd538116", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "│1 2│\n",
       "└~──┘\n",
       " \n",
       "1\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a ≡ ⎕ ← ⊃⊂1 2" ] }, { "cell_type": "markdown", "id": "063a8f05-4a94-43dc-8e41-50cd6358bccb", "metadata": {}, "source": [ "What disclose/first is doing is getting the first element in the array, regardless of whether it is nested or not." ] }, { "cell_type": "code", "execution_count": null, "id": "4b3c0639", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "W\n",
       "-\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊃ 'Word'" ] }, { "cell_type": "code", "execution_count": null, "id": "e5a4bb34-4bcd-4f59-af73-2d35788426f9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────┐\n",
       "│   ┌→──┐   │\n",
       "│ 1 │1 2│ 3 │\n",
       "│   └~──┘   │\n",
       "└∊──────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊃ (1 (1 2) 3) 4" ] }, { "cell_type": "code", "execution_count": null, "id": "38f38a51", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "│1 2│\n",
       "└~──┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊃ (1 2)(3 4 5)" ] }, { "cell_type": "markdown", "id": "1fd8817b", "metadata": {}, "source": [ "#### dyadic `⊃` (Pick)" ] }, { "cell_type": "markdown", "id": "247bbd4a-20cb-46c9-b58e-44daeeebcf40", "metadata": {}, "source": [ "Pick is a way of indexing into an array to get particular elements. For example `3 ⊃ 'Word'` will give the 3rd element in `Word`." ] }, { "cell_type": "code", "execution_count": null, "id": "37075d37", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "r\n",
       "-\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 ⊃ 'Word'" ] }, { "cell_type": "markdown", "id": "47eef0aa-64dc-4523-a8d6-4a9e6ea316b0", "metadata": {}, "source": [ "If the element you are selecting is an array, that array will be returned" ] }, { "cell_type": "code", "execution_count": null, "id": "e36eafb9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n",
       "│3 4 5│\n",
       "└~────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ⊃ (1 2)(3 4 5)" ] }, { "cell_type": "markdown", "id": "b2c5ff09-eaff-44fd-a02c-83e264c0edca", "metadata": {}, "source": [ "If you pass an array to the left argument it will index further. For example, `2 1 ⊃ (1 2)(3 4 5)` will first go to the 2nd element (`3 4 5`) and then output the first element inside of that (`3`)." ] }, { "cell_type": "code", "execution_count": null, "id": "092ff790", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "3\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 1 ⊃ (1 2)(3 4 5)" ] }, { "cell_type": "markdown", "id": "1a20f498-87d4-43d7-b0c3-32a634a42062", "metadata": {}, "source": [ "In order to index into a multi-dimensional array you need to specify the different dimensions as a scalar. For example if you are looking at a matrix (2d array), `(⊂2 1)` as the left argument will output the value in the 2nd row, 1st column." ] }, { "cell_type": "code", "execution_count": null, "id": "deae5a82", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n",
       "↓1 2 3│\n",
       "│4 5 6│\n",
       "└~────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⎕←mat ← 2 3⍴⍳6" ] }, { "cell_type": "code", "execution_count": null, "id": "267aa753", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "4\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(⊂2 1) ⊃ 2 3⍴⍳6" ] }, { "cell_type": "markdown", "id": "c8c2b52d-10fc-43c2-ab5a-4eff4caed036", "metadata": {}, "source": [ "These above concepts of indexing into subarrays and indexing into multi-dimensional arrays can be combined in one function call." ] }, { "cell_type": "code", "execution_count": null, "id": "a9e0b49e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────────────────────────────┐\n",
       "↓ ┌→────────┐ ┌→────────┐ ┌→────────┐ │\n",
       "│ │ ┌→──┐   │ │ ┌→──┐   │ │ ┌→──┐   │ │\n",
       "│ │ │ABC│ 1 │ │ │DEF│ 2 │ │ │GHI│ 3 │ │\n",
       "│ │ └───┘   │ │ └───┘   │ │ └───┘   │ │\n",
       "│ └∊────────┘ └∊────────┘ └∊────────┘ │\n",
       "│ ┌→────────┐ ┌→────────┐ ┌→────────┐ │\n",
       "│ │ ┌→──┐   │ │ ┌→──┐   │ │ ┌→──┐   │ │\n",
       "│ │ │JKL│ 4 │ │ │MNO│ 5 │ │ │PQR│ 6 │ │\n",
       "│ │ └───┘   │ │ └───┘   │ │ └───┘   │ │\n",
       "│ └∊────────┘ └∊────────┘ └∊────────┘ │\n",
       "└∊────────────────────────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "G←2 3⍴('ABC' 1)('DEF' 2)('GHI' 3)('JKL' 4),('MNO' 5)('PQR' 6)\n", "G" ] }, { "cell_type": "code", "execution_count": null, "id": "67b8a341", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "│JKL│\n",
       "└───┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "((⊂2 1),1) ⊃ G" ] }, { "cell_type": "markdown", "id": "8b0326cf", "metadata": {}, "source": [ "### `⊢` (Right tack)" ] }, { "cell_type": "markdown", "id": "0a34369a", "metadata": {}, "source": [ "#### monadic `⊢` (Same)" ] }, { "cell_type": "markdown", "id": "5b2a1f38-663e-4282-a644-1d6824c74ba2", "metadata": {}, "source": [ "Return the argument back unchanged" ] }, { "cell_type": "code", "execution_count": null, "id": "b81b6d0f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "1\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊢1" ] }, { "cell_type": "code", "execution_count": null, "id": "00691415", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "│abc│\n",
       "└───┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊢'abc'" ] }, { "cell_type": "markdown", "id": "97d0971d", "metadata": {}, "source": [ "#### dyadic `⊢` (Right)" ] }, { "cell_type": "markdown", "id": "5a0e1516-7b4b-4dc9-a169-38e4341ab45a", "metadata": {}, "source": [ "Returns the right argument back unchanged" ] }, { "cell_type": "code", "execution_count": null, "id": "461ac21e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "1\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'⊢1" ] }, { "cell_type": "code", "execution_count": null, "id": "bfe47293", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "│abc│\n",
       "└───┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1⊢'abc'" ] }, { "cell_type": "markdown", "id": "0c0ad5b4", "metadata": {}, "source": [ "### `⊣` (Left tack)" ] }, { "cell_type": "markdown", "id": "9813f4e1", "metadata": {}, "source": [ "#### monadic `⊣` (Same)" ] }, { "cell_type": "markdown", "id": "b825c9f3-bab7-495a-9330-bd2d1a73a804", "metadata": {}, "source": [ "Returns the argument back unchanged" ] }, { "cell_type": "code", "execution_count": null, "id": "de77f334", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "│abc│\n",
       "└───┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⊣'abc'" ] }, { "cell_type": "markdown", "id": "dff7a3c9", "metadata": {}, "source": [ "#### dyadic `⊣` (Left)" ] }, { "cell_type": "markdown", "id": "363c0077-700d-4c33-a509-c69ece47aa11", "metadata": {}, "source": [ "Returns the left argument back unchanged" ] }, { "cell_type": "code", "execution_count": null, "id": "89558f1d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "│abc│\n",
       "└───┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'⊣1" ] }, { "cell_type": "code", "execution_count": null, "id": "2d004ac1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "1\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1⊣'abc'" ] }, { "cell_type": "markdown", "id": "a44cd73e", "metadata": {}, "source": [ "### `⊥` (Up tack)" ] }, { "cell_type": "markdown", "id": "d51a72f8", "metadata": {}, "source": [ "#### dyadic `⊥` (Decode)" ] }, { "cell_type": "markdown", "id": "9a83c65a-76ad-4c1e-b180-52b0fb8f4133", "metadata": {}, "source": [ "Decode decodes the right argument using the encode definition on the left.\n", "\n", "In the example of `2 ⊥ 1 1 0 1`:\n", "\n", "+ 2 on the left means that all values are encoded with 2 values, in other words binary\n", "+ `1 1 0 1` in binary is equal to 13, so decode will return 13." ] }, { "cell_type": "code", "execution_count": null, "id": "c018adfc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
  \n",
       "13\n",
       "  \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ⊥ 1 1 0 1 ⍝ binary decode" ] }, { "cell_type": "markdown", "id": "1f268ef5-1519-451c-9a7e-557d3d1d2b92", "metadata": {}, "source": [ "An array can also be decoded if each element is encoded differently. For example, if you have an array of 3 elements representing hours, minutes, and seconds you can use decode to convert it into seconds.\n", "\n", "Hours would be decoded with 24, because there's 24 hours in a day. Minutes would be decoded with 60, because there's 60 minutes in an hour. Seconds would be decoded with 60 because there are 60 seconds in a minute." ] }, { "cell_type": "code", "execution_count": null, "id": "d9b3afe1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
     \n",
       "10000\n",
       "     \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍝ mixed radix: conversion of hours,\n", "⍝ minutes and seconds to seconds:\n", "24 60 60 ⊥ 2 46 40" ] }, { "cell_type": "code", "execution_count": null, "id": "2c76d681", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
     \n",
       "10000\n",
       "     \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2×60×60) + (46×60) + 40" ] }, { "cell_type": "markdown", "id": "0660fdf2-db3f-44fe-adf9-01f5ff0ba5a8", "metadata": {}, "source": [ "Below are a few examples of a few other examples of ways decode can be used.\n", "\n", "A decimal decode:" ] }, { "cell_type": "code", "execution_count": null, "id": "2bce48db", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n",
       "1101\n",
       "    \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 ⊥ 1 1 0 1 ⍝ decimal decode" ] }, { "cell_type": "code", "execution_count": null, "id": "6463996b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n",
       "3416\n",
       "    \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 ⊥ 3 4 1 6 ⍝ decimal decode" ] }, { "cell_type": "code", "execution_count": null, "id": "d5b1df93", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
   \n",
       "5J9\n",
       "   \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1j1⊥1 2 3 4" ] }, { "cell_type": "markdown", "id": "c6dfee67-faf5-48a6-b92d-1ad1cdaccf71", "metadata": {}, "source": [ "`1⊥ `is sum over the first axis (+⌿)" ] }, { "cell_type": "code", "execution_count": null, "id": "d75ca7b1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
  \n",
       "23\n",
       "  \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1⊥3 1 4 1 5 9" ] }, { "cell_type": "code", "execution_count": null, "id": "6ca32a66", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────┐\n",
       "↓1  2  3  4│\n",
       "│5  6  7  8│\n",
       "│9 10 11 12│\n",
       "└~─────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⎕←M←3 4⍴⍳12" ] }, { "cell_type": "code", "execution_count": null, "id": "c32aa91e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────┐\n",
       "│15 18 21 24│\n",
       "└~──────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1⊥M" ] }, { "cell_type": "markdown", "id": "154054f8", "metadata": {}, "source": [ "### `⊤` (Down tack)" ] }, { "cell_type": "markdown", "id": "144cf08f", "metadata": {}, "source": [ "#### dyadic `⊤` (Encode)" ] }, { "cell_type": "markdown", "id": "e7b22cb5-4a71-4b1e-a49e-c418ddcc6473", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "id": "e37a3491", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────┐\n",
       "│0 3 6 5 8│\n",
       "└~────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 10 10 10 10 ⊤ 3658 ⍝ decimal encode" ] }, { "cell_type": "code", "execution_count": null, "id": "e38e6791", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n",
       "│6 5 8│\n",
       "└~────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 10 10 ⊤ 3658 ⍝ truncated decimal encode" ] }, { "cell_type": "code", "execution_count": null, "id": "99af88f7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────┐\n",
       "│36 5 8│\n",
       "└~─────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0 10 10 ⊤ 3658" ] }, { "cell_type": "code", "execution_count": null, "id": "117dfea6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n",
       "│0 1 1 1│\n",
       "└~──────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 2 2 2 ⊤ 7 ⍝ binary encode" ] }, { "cell_type": "code", "execution_count": null, "id": "8930de81", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n",
       "│1 1│\n",
       "└~──┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 2 ⊤ 7 ⍝ truncated binary encode" ] }, { "cell_type": "code", "execution_count": null, "id": "0edabb74", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n",
       "↓0 0 1│\n",
       "│1 1 1│\n",
       "│0 1 0│\n",
       "│1 1 0│\n",
       "└~────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 2 2 2 ⊤ 5 7 12 ⍝ binary encode" ] }, { "cell_type": "code", "execution_count": null, "id": "77aac657", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n",
       "│2 46 40│\n",
       "└~──────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍝ mixed radix: encode of 10000 seconds\n", "⍝ to hours, minutes and seconds:\n", "24 60 60 ⊤ 10000" ] }, { "cell_type": "markdown", "id": "a4c28485", "metadata": {}, "source": [ "## Forks" ] }, { "cell_type": "markdown", "id": "51b3f84c", "metadata": {}, "source": [ "In traditional mathematical notation (TMN): `(f+g)(x)=f(x)+g(x)`. Forks (3-trains) are just a generalisation of this pattern to all functions (though the middle one has to be dyadic)." ] }, { "cell_type": "code", "execution_count": null, "id": "520f5e4e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
           \n",
       "20.41887026\n",
       "           \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(÷3)+(*3)" ] }, { "cell_type": "code", "execution_count": null, "id": "b2245603", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
           \n",
       "20.41887026\n",
       "           \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(÷+*)3" ] }, { "cell_type": "code", "execution_count": null, "id": "21fa36a1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "6\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(+/÷≢) 2 5 8 9" ] }, { "cell_type": "code", "execution_count": null, "id": "4b012428", "metadata": {}, "outputs": [], "source": [ "mean ← +/÷≢" ] }, { "cell_type": "code", "execution_count": null, "id": "2bf8d6db", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 \n",
       "6\n",
       " \n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean 2 5 8 9" ] }, { "cell_type": "markdown", "id": "0b876337", "metadata": {}, "source": [ "For a dyadic fork, each of `f` and `g` are passed the LHS and the RHS:" ] }, { "cell_type": "code", "execution_count": null, "id": "7a616a6d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────────────────┐\n",
       "│1 1 1 0 1 1 1 0 1 1 1 1│\n",
       "└~──────────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' '≠'How are you?'" ] }, { "cell_type": "code", "execution_count": null, "id": "52630585", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───────────┐\n",
       "│How are you?│\n",
       "└────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' '⊢'How are you?'" ] }, { "cell_type": "code", "execution_count": null, "id": "7e788e65", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───────────────────┐\n",
       "│ ┌→──┐ ┌→──┐ ┌→───┐ │\n",
       "│ │How│ │are│ │you?│ │\n",
       "│ └───┘ └───┘ └────┘ │\n",
       "└∊───────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(' '≠'How are you?') ⊆ (' '⊢'How are you?')" ] }, { "cell_type": "code", "execution_count": null, "id": "71bcd3d6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───────────────────┐\n",
       "│ ┌→──┐ ┌→──┐ ┌→───┐ │\n",
       "│ │How│ │are│ │you?│ │\n",
       "│ └───┘ └───┘ └────┘ │\n",
       "└∊───────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' '(≠⊆⊢)'How are you?'" ] }, { "cell_type": "code", "execution_count": null, "id": "8dc97773", "metadata": {}, "outputs": [], "source": [ "split ← {(⍺≠⍵) ⊆ (⍺⊢⍵)}" ] }, { "cell_type": "code", "execution_count": null, "id": "75a14063", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───────────────────┐\n",
       "│ ┌→──┐ ┌→──┐ ┌→───┐ │\n",
       "│ │How│ │are│ │you?│ │\n",
       "│ └───┘ └───┘ └────┘ │\n",
       "└∊───────────────────┘\n",
       "
" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' ' split 'How are you?'" ] }, { "cell_type": "code", "execution_count": null, "id": "7c8db425", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Dyalog APL", "language": "apl", "name": "dyalog-kernel" } }, "nbformat": 4, "nbformat_minor": 5 }