{ "cells": [ { "cell_type": "raw", "id": "2990f581", "metadata": {}, "source": [ "---\n", "title: \"First functions\"\n", "author: \"Jeremy Howard\"\n", "date: \"2022-07-05\"\n", "categories: [Dyalog, APL, Glyphs]\n", "---" ] }, { "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": "2dbbce67", "metadata": {}, "source": [ "## Boolean" ] }, { "cell_type": "markdown", "id": "71f403e7", "metadata": {}, "source": [ "### `=` (Equal sign)" ] }, { "cell_type": "markdown", "id": "2df02fe7", "metadata": {}, "source": [ "#### dyadic `=` (Equal to)" ] }, { "cell_type": "markdown", "id": "22de5339-feeb-4050-8fd2-c49604d38546", "metadata": {}, "source": [ "`=` is equal and returns a boolean (true/false). 1 means true, 0 means false." ] }, { "cell_type": "code", "execution_count": null, "id": "f9a1f4c0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "1\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 = 1" ] }, { "cell_type": "markdown", "id": "2dbd9b1e-f7dd-43ef-9198-42210c33f7f6", "metadata": {}, "source": [ "`=` works elementwise and will broadcast as needed." ] }, { "cell_type": "code", "execution_count": null, "id": "e494be50", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n", "│1 0│\n", "└~──┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 = 1 2" ] }, { "cell_type": "code", "execution_count": null, "id": "d31b5778", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n", "│1 1│\n", "└~──┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 = 1 1" ] }, { "cell_type": "markdown", "id": "2fbb3372-ec47-45a7-9c0f-0c5264207eaf", "metadata": {}, "source": [ "We can also compare characters" ] }, { "cell_type": "code", "execution_count": null, "id": "8d9ff9d7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────┐\n", "│0 1 0 1 0 1│\n", "└~──────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Banana' = 'aaaaaa'" ] }, { "cell_type": "code", "execution_count": null, "id": "0077e614", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────┐\n", "│0 1 0 1 0 1│\n", "└~──────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Banana' = 'a'" ] }, { "cell_type": "code", "execution_count": null, "id": "aa551f28", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────────────────────────────────┐\n", "│ ┌→──────────┐ ┌→────────┐ ┌→────────┐ │\n", "│ │0 1 0 1 0 1│ │0 0 0 0 0│ │0 1 0 0 0│ │\n", "│ └~──────────┘ └~────────┘ └~────────┘ │\n", "└∊──────────────────────────────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Banana' 'Apple' 'Candy' = 'a'" ] }, { "cell_type": "markdown", "id": "12b33beb", "metadata": {}, "source": [ "### `≠` (Not equal)" ] }, { "cell_type": "markdown", "id": "3b0b6323", "metadata": {}, "source": [ "#### monadic `≠` (Unique Mask)" ] }, { "cell_type": "markdown", "id": "5a19caf7-f061-44da-99c0-7495223a1812", "metadata": {}, "source": [ "Monadic `≠` returns 1 on the first occurrence of an item in an array." ] }, { "cell_type": "code", "execution_count": null, "id": "0d283400", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────────┐\n", "│1 1 0 0 1 0 1 0│\n", "└~──────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "≠22 10 22 22 21 10 5 10" ] }, { "cell_type": "code", "execution_count": null, "id": "1b52166b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────┐\n", "│1 1 1 0 0 0│\n", "└~──────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "≠ 'Banana'" ] }, { "cell_type": "code", "execution_count": null, "id": "84ed720d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────────────┐\n", "│1 1 1 0 0 0 0 0 1 0 0│\n", "└~────────────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "≠ 'Mississippi'" ] }, { "cell_type": "markdown", "id": "ec2830de", "metadata": {}, "source": [ "#### dyadic `≠` (Not Equal To)" ] }, { "cell_type": "markdown", "id": "a062070b-2923-4a0b-b964-df59052f23ed", "metadata": {}, "source": [ "Dyadic `≠` returns true (1) if elements are not equal, and false (0) if elements are equal. " ] }, { "cell_type": "code", "execution_count": null, "id": "21b37853", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│1 0 1│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3 ≠ 4 2 ¯1" ] }, { "cell_type": "markdown", "id": "5e01edbc-b54f-458b-a3ac-2b6ad344ae54", "metadata": {}, "source": [ "The number 7 and the character 7 are not equal." ] }, { "cell_type": "code", "execution_count": null, "id": "7b1f2164", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "1\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 ≠ '7'" ] }, { "cell_type": "markdown", "id": "2d5dfcd5", "metadata": {}, "source": [ "### `<` (Less than sign)" ] }, { "cell_type": "markdown", "id": "4277bc63", "metadata": {}, "source": [ "#### dyadic `<` (Less than)" ] }, { "cell_type": "code", "execution_count": null, "id": "b3c6be5e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│1 0 0│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3 < 4 2 ¯1" ] }, { "cell_type": "code", "execution_count": null, "id": "61f6c499", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│1 0 0│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3 < 2" ] }, { "cell_type": "markdown", "id": "3bd3a0ea", "metadata": {}, "source": [ "### `>` (Greater than sign)" ] }, { "cell_type": "markdown", "id": "b487022a", "metadata": {}, "source": [ "#### dyadic `>` (Greater than)" ] }, { "cell_type": "code", "execution_count": null, "id": "ed7bc01e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│0 0 1│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3 > 4 2 ¯1" ] }, { "cell_type": "code", "execution_count": null, "id": "b68ae705", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│0 0 1│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3 > 2" ] }, { "cell_type": "markdown", "id": "903c506f", "metadata": {}, "source": [ "### `≤` (Less than or equal to sign)" ] }, { "cell_type": "markdown", "id": "21b33cbf", "metadata": {}, "source": [ "#### dyadic `≤` (Less than or equal to)" ] }, { "cell_type": "code", "execution_count": null, "id": "985c9324", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│1 1 0│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3 ≤ 4 2 ¯1" ] }, { "cell_type": "code", "execution_count": null, "id": "ebb03a6a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│1 1 0│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3 ≤ 2" ] }, { "cell_type": "markdown", "id": "b9409575", "metadata": {}, "source": [ "### `≥` (Greater than or equal to sign)" ] }, { "cell_type": "markdown", "id": "8dbc6595", "metadata": {}, "source": [ "#### dyadic `≥` (Greater than or equal to)" ] }, { "cell_type": "code", "execution_count": null, "id": "c2db65ca", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│0 1 1│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3 ≥ 4 2 ¯1" ] }, { "cell_type": "code", "execution_count": null, "id": "3c4673b1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│0 1 1│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 2 3 ≥ 2" ] }, { "cell_type": "markdown", "id": "af629c33", "metadata": {}, "source": [ "### `≡` (Equal underbar)" ] }, { "cell_type": "markdown", "id": "04409fab", "metadata": {}, "source": [ "#### monadic `≡` (Depth)" ] }, { "cell_type": "markdown", "id": "9d6a0db6-2820-4e30-a53e-65e6010d8324", "metadata": {}, "source": [ "Depth shows how nested an array is. A simple scalar is an array with no nesting." ] }, { "cell_type": "code", "execution_count": null, "id": "fcb156fe", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "0\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "≡ 7" ] }, { "cell_type": "markdown", "id": "3b3f7079-1edd-4d36-8142-f2fad11bf1db", "metadata": {}, "source": [ "An array of simple scalars has depth 1." ] }, { "cell_type": "code", "execution_count": null, "id": "53200fb6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "1\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "≡ 'abc'" ] }, { "cell_type": "markdown", "id": "a9b106d5-a182-44fc-b3d9-c660344bc186", "metadata": {}, "source": [ "An array that is an array of simple arrays has depth 2." ] }, { "cell_type": "code", "execution_count": null, "id": "35407dd6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────┐\n", "│ ┌→──┐ ┌→──┐ │\n", "│ │1 2│ │3 4│ │\n", "│ └~──┘ └~──┘ │\n", "└∊────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1 2)(3 4)" ] }, { "cell_type": "code", "execution_count": null, "id": "f64d2e8e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "2\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "≡ (1 2)(3 4)" ] }, { "cell_type": "markdown", "id": "36ec6b8f-882d-483f-a42b-1b2c6f35b559", "metadata": {}, "source": [ "If the depth is not consistent, then it returns the max depth as a negative number." ] }, { "cell_type": "code", "execution_count": null, "id": "0650e6e6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────────────────┐\n", "│ ┌→──┐ ┌→──┐ ┌→────────┐ │\n", "│ │1 2│ │3 4│ │ ┌→──┐ │ │\n", "│ └~──┘ └~──┘ │ 5 │6 7│ │ │\n", "│ │ └~──┘ │ │\n", "│ └∊────────┘ │\n", "└∊────────────────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1 2)(3 4) (5 (6 7))" ] }, { "cell_type": "code", "execution_count": null, "id": "ede6ebf7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "¯3\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "≡ (1 2)(3 4) (5 (6 7))" ] }, { "cell_type": "markdown", "id": "d5e95d1d", "metadata": {}, "source": [ "#### dyadic `≡` (Match)" ] }, { "cell_type": "markdown", "id": "0819ac30-7d8f-446a-b4b9-e6113514cf1e", "metadata": {}, "source": [ "Match does a comparison to see if 2 objects are equal. It is similar to `=`, but it works on entire objects rather than elementwise. In other words it is equal with a wider scope." ] }, { "cell_type": "code", "execution_count": null, "id": "8f55c331", "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": "ee929e76", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "0\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 ≡ 0" ] }, { "cell_type": "markdown", "id": "1f48a33b-e717-47d3-a6f8-3395c759759e", "metadata": {}, "source": [ "`≡` works on whole objects and **not** elementwise with broadcasting." ] }, { "cell_type": "code", "execution_count": null, "id": "1419197c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "0\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 ≡ 1 1" ] }, { "cell_type": "markdown", "id": "284ae899", "metadata": {}, "source": [ "### `≢` (Equal Underbar Slash)" ] }, { "cell_type": "markdown", "id": "f6f859db", "metadata": {}, "source": [ "#### monadic `≢` (Tally)" ] }, { "cell_type": "markdown", "id": "026eab0b-68de-46c8-874e-f07035768242", "metadata": {}, "source": [ "Tally counts the major cells in an array. This means it gives the length of the leading axis. For a vector, or rank 1 array, this is the same as it's shape but as a scalar." ] }, { "cell_type": "code", "execution_count": null, "id": "10011239", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "3\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "≢ 1 2 3" ] }, { "cell_type": "code", "execution_count": null, "id": "6f6449ea", "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": "9adbc6a0-e791-46df-83ef-dcf6787d0ed4", "metadata": {}, "source": [ "If there are multiple dimensions, Tally returns the number of major cells, which is the size of the first dimension" ] }, { "cell_type": "code", "execution_count": null, "id": "7d2d2315", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "2\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "≢ 2 3 ⍴ ⍳6" ] }, { "cell_type": "code", "execution_count": null, "id": "b5c6f177-ce42-4718-8c3d-710febac94c2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "3\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "≢ 3 2 ⍴ ⍳6" ] }, { "cell_type": "markdown", "id": "63bf5a7c", "metadata": {}, "source": [ "#### dyadic `≢` (Not match)" ] }, { "cell_type": "markdown", "id": "b22c9e10-a63b-48c0-b811-96f416e76289", "metadata": {}, "source": [ "Not match does a comparison to see if 2 objects are not equal. It is similar to `≠`, but it works on entire objects rather than elementwise. In other words, it is equal with a wider scope." ] }, { "cell_type": "code", "execution_count": null, "id": "762603e0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "0\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 ≢ 1" ] }, { "cell_type": "code", "execution_count": null, "id": "87ebb14d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "1\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 ≢ 0" ] }, { "cell_type": "markdown", "id": "0846d742-8ba9-4a7e-b141-bc8cad0171c6", "metadata": {}, "source": [ "``≢`` works on whole objects and **not** elementwise with broadcasting." ] }, { "cell_type": "code", "execution_count": null, "id": "9f3fb7c0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "1\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 ≢ 1 1" ] }, { "cell_type": "markdown", "id": "fa5c0d84", "metadata": {}, "source": [ "### `∨` (Logical or)" ] }, { "cell_type": "markdown", "id": "42407b65", "metadata": {}, "source": [ "#### dyadic `∨` (Greatest Common Divisor (Or))" ] }, { "cell_type": "markdown", "id": "37035211-ea5a-424d-8e3f-cd1735dab5a8", "metadata": {}, "source": [ "Standard `or` operator when applied to booleans." ] }, { "cell_type": "code", "execution_count": null, "id": "439d7541", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n", "│0 1 1 1│\n", "└~──────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0 1 0 1 ∨ 0 0 1 1 ⍝ Truth table for *or*" ] }, { "cell_type": "markdown", "id": "ceae61c8-16d3-4c50-83f6-655f20aaeaad", "metadata": {}, "source": [ "5 is the largest number that divides 15 and 35 cleanly (meaning no remainder). Therefore, 5 is the greatest common divisor of 15 and 35." ] }, { "cell_type": "code", "execution_count": null, "id": "47abd9d1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n", "│5 1 2 7│\n", "└~──────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "15 1 2 7 ∨ 35 1 4 0 ⍝ GCD" ] }, { "cell_type": "markdown", "id": "fc3e5b03", "metadata": {}, "source": [ "### `⍱` (Logical NOR)" ] }, { "cell_type": "markdown", "id": "47ff8cd5", "metadata": {}, "source": [ "#### dyadic `⍱` (Nor)" ] }, { "cell_type": "markdown", "id": "f067a891-a383-4794-9e2e-731f6d750c69", "metadata": {}, "source": [ "For more details on logical nor, [see this page](https://en.wikipedia.org/wiki/Logical_NOR#Truth_table)" ] }, { "cell_type": "code", "execution_count": null, "id": "dc80c88d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n", "│1 0 0 0│\n", "└~──────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ " 0 1 0 1 ⍱ 0 0 1 1 ⍝ Truth table for *nor*" ] }, { "cell_type": "markdown", "id": "27932e1b", "metadata": {}, "source": [ "### `∧` (Logical AND)" ] }, { "cell_type": "markdown", "id": "31a3b8b5", "metadata": {}, "source": [ "#### dyadic `∧` (Lowest Common Multiple (And))" ] }, { "cell_type": "markdown", "id": "65f4a4d0-59ed-4e72-914e-c2ef482d8291", "metadata": {}, "source": [ "Standard `and` operator when applied to booleans." ] }, { "cell_type": "code", "execution_count": null, "id": "ac4309ef", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n", "│0 0 0 1│\n", "└~──────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0 1 0 1 ∧ 0 0 1 1 ⍝ Truth table for *and*" ] }, { "cell_type": "markdown", "id": "71b974e9-6f78-4e29-937f-ffb31a62af74", "metadata": {}, "source": [ "105 is the smallest multiple of both 15 and 35, therefore 105 is the lowest common multiple of 15 and 35." ] }, { "cell_type": "code", "execution_count": null, "id": "fbfaebf4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────┐\n", "│105 1 4 0│\n", "└~────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "15 1 2 7 ∧ 35 1 4 0 ⍝ LCM" ] }, { "cell_type": "markdown", "id": "04987504-6a54-4e01-b38f-3e69b99c1abe", "metadata": {}, "source": [ "### `~` (Logical NOT)" ] }, { "cell_type": "markdown", "id": "09ab7565-f35c-4072-ab77-3e1cd58e17ee", "metadata": {}, "source": [ "#### monadic `~` (Not)" ] }, { "cell_type": "code", "execution_count": null, "id": "b0abb3ae", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n", "│1 0│\n", "└~──┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "~ 0 1 ⍝ Truth table for *not*" ] }, { "cell_type": "markdown", "id": "190b5d68-57b5-4f04-a21e-3864fe8d8122", "metadata": {}, "source": [ "#### dyadic `~` (Without;Excluding)" ] }, { "cell_type": "markdown", "id": "d41d8180-bfaa-46f2-808e-d451673f4e5f", "metadata": {}, "source": [ "Gives the elements from the left argument that are not in the right." ] }, { "cell_type": "code", "execution_count": null, "id": "f6b788cd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n", "│3 4│\n", "└~──┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 1 4 1 5 ~ 5 1" ] }, { "cell_type": "markdown", "id": "82641989-2b43-4e01-a3d1-0489c417cae5", "metadata": {}, "source": [ "Also works on the character vectors" ] }, { "cell_type": "code", "execution_count": null, "id": "bfdf7e89", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────┐\n", "│ ┌→─┐ ┌→─┐ │\n", "│ │aa│ │cc│ │\n", "│ └──┘ └──┘ │\n", "└∊──────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'aa' 'bb' 'cc' 'bb' ~ 'bb' 'xx'" ] }, { "cell_type": "markdown", "id": "51a97128-c2ee-499a-a5e7-2f2e84897496", "metadata": {}, "source": [ "Also works on nested arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "9085478e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→┐\n", "│3│\n", "└~┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1 2) 3 ~ ⊂1 2" ] }, { "cell_type": "markdown", "id": "5f8cab64", "metadata": {}, "source": [ "### `⍲` (Logical NAND)" ] }, { "cell_type": "markdown", "id": "8b59feac", "metadata": {}, "source": [ "#### dyadic `⍲` (Nand)" ] }, { "cell_type": "markdown", "id": "abf07f68-d2d1-4784-b9f3-2fce4665a498", "metadata": {}, "source": [ "Nand is \"Not and\"" ] }, { "cell_type": "code", "execution_count": null, "id": "0ee272ec", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n", "│1 1 1 0│\n", "└~──────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ " 0 1 0 1 ⍲ 0 0 1 1 ⍝ Truth table for *nand*" ] }, { "cell_type": "markdown", "id": "327836f3-8acd-474d-a594-2fa525763bc6", "metadata": {}, "source": [ "We could use and (`^`) apply not (`~`) afterward to get the same result. As you can see it truly is \"Not and\"" ] }, { "cell_type": "code", "execution_count": null, "id": "113f86fa-9645-4c8e-b559-6b5cf7c822f8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n", "│1 1 1 0│\n", "└~──────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "~ 0 1 0 1 ∧ 0 0 1 1 ⍝ Truth table for *nand*" ] }, { "cell_type": "markdown", "id": "ebf0e3e4", "metadata": {}, "source": [ "### `/` (Slash)" ] }, { "cell_type": "markdown", "id": "81223d6f", "metadata": {}, "source": [ "#### monadic `/` (Replicate)" ] }, { "cell_type": "code", "execution_count": null, "id": "1e6ccb42", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────────┐\n", "│1 1 0 0 1 0 1 0│\n", "└~──────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v←22 10 22 22 21 10 5 10\n", "≠v" ] }, { "cell_type": "code", "execution_count": null, "id": "ed0370b6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→─────────┐\n", "│22 10 21 5│\n", "└~─────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(≠v)/v" ] }, { "cell_type": "code", "execution_count": null, "id": "4a7aa794", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───────────┐\n", "│BBBannnannna│\n", "└────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 1 3 1 3 1 / 'Banana'" ] }, { "cell_type": "markdown", "id": "95f0d135", "metadata": {}, "source": [ "## Min, max, index, concat" ] }, { "cell_type": "markdown", "id": "c184a076", "metadata": {}, "source": [ "### `⍳` (iota)" ] }, { "cell_type": "markdown", "id": "8485edff", "metadata": {}, "source": [ "#### monadic `⍳` (index generator)" ] }, { "cell_type": "markdown", "id": "6581d1bf-551f-42b5-a027-b080d72d4629", "metadata": {}, "source": [ "Creates index locations for array with specified shape." ] }, { "cell_type": "code", "execution_count": null, "id": "69cc99e0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────┐\n", "│1 2 3 4│\n", "└~──────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍳4" ] }, { "cell_type": "code", "execution_count": null, "id": "155302ac", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────────────┐\n", "↓ ┌→──┐ ┌→──┐ ┌→──┐ │\n", "│ │1 1│ │1 2│ │1 3│ │\n", "│ └~──┘ └~──┘ └~──┘ │\n", "│ ┌→──┐ ┌→──┐ ┌→──┐ │\n", "│ │2 1│ │2 2│ │2 3│ │\n", "│ └~──┘ └~──┘ └~──┘ │\n", "└∊──────────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍳2 3" ] }, { "cell_type": "markdown", "id": "841c9175-1100-41ed-8a7c-ec4bb7783504", "metadata": {}, "source": [ "`⍳` is often used with `⍴` to create arrays " ] }, { "cell_type": "code", "execution_count": null, "id": "73e93a04", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "↓1 2 3│\n", "│4 5 6│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 3 ⍴ ⍳6" ] }, { "cell_type": "markdown", "id": "22e8cdfd-5251-432f-b0a6-716bd803085d", "metadata": {}, "source": [ "To generate an array with various patterns you can combine with other functions." ] }, { "cell_type": "code", "execution_count": null, "id": "1744f807", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────┐\n", "│3 5 7 9 11 13│\n", "└~────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+2×⍳6" ] }, { "cell_type": "markdown", "id": "f92ecb85-02b0-46fe-9812-d4ed90ed9498", "metadata": {}, "source": [ "Array of shape 0 is nan empty vector and the index locations of a shape 0 array is and empty vector." ] }, { "cell_type": "code", "execution_count": null, "id": "3fd77f85", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌⊖┐\n", "│0│\n", "└~┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍳0" ] }, { "cell_type": "markdown", "id": "51f01e92", "metadata": {}, "source": [ "#### dyadic `⍳` (index of)" ] }, { "cell_type": "markdown", "id": "24713038-4260-48c3-aa7a-e79815cec6e8", "metadata": {}, "source": [ "Provides the index locations for where `⍵` (right argument) is in `⍺` (left argument)" ] }, { "cell_type": "code", "execution_count": null, "id": "db34fc0c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "2\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 3 6 5 4 ⍳ 3" ] }, { "cell_type": "code", "execution_count": null, "id": "1604dc20", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────┐\n", "│1 3 10 11│\n", "└~────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'ABCDABCDEF' ⍳ 'ACFG'" ] }, { "cell_type": "code", "execution_count": null, "id": "e6a697da", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n", "↓1 2│\n", "│3 4│\n", "│5 6│\n", "└~──┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⎕←mat←3 2 ⍴ ⍳6" ] }, { "cell_type": "markdown", "id": "4a0af1fb-7e47-4b68-8639-2d5e34d75dc2", "metadata": {}, "source": [ "`⍳` works on major cells, or along the first dimension. The third major cell of mat is `5 6`." ] }, { "cell_type": "code", "execution_count": null, "id": "e7e56d92", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "3\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mat←3 2 ⍴ ⍳6\n", "mat ⍳ 5 6" ] }, { "cell_type": "markdown", "id": "8dbfe270", "metadata": {}, "source": [ "### `⍸` (iota underbar)" ] }, { "cell_type": "markdown", "id": "c623312c", "metadata": {}, "source": [ "#### monadic `⍸` (Where)" ] }, { "cell_type": "markdown", "id": "6eb50df5-420e-456b-aebc-df8bcc377870", "metadata": {}, "source": [ "When applied to a boolean/binary array, where gives index locations of the true values." ] }, { "cell_type": "code", "execution_count": null, "id": "cd826c58", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│1 4 5│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍸ 1 0 0 1 1" ] }, { "cell_type": "markdown", "id": "80929a44-9b23-491a-98ef-d530ba1f3490", "metadata": {}, "source": [ "When applied to a natural number, where repeats the index locations if the number is greater than 1. For example `⍸1 2 3` would give `1 2 2 3 3 3`" ] }, { "cell_type": "code", "execution_count": null, "id": "1b6a928b-705a-48bc-89b9-0f48158568cc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────┐\n", "│1 2 2 3 3 3│\n", "└~──────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍸1 2 3" ] }, { "cell_type": "code", "execution_count": null, "id": "f6dd11af", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────┐\n", "│1 1 4 4 5│\n", "└~────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍸ 2 0 0 2 1" ] }, { "cell_type": "markdown", "id": "7be22017-4047-404f-a9fa-34c0a589c6ed", "metadata": {}, "source": [ "When applied to a higher rank array it still provides the index locations of the elements, but index locations have multiple values (in this case row and column)." ] }, { "cell_type": "code", "execution_count": null, "id": "a8a4bf88", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "↓0 1 0│\n", "│1 0 1│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "
┌→──────────────────┐\n", "│ ┌→──┐ ┌→──┐ ┌→──┐ │\n", "│ │1 2│ │2 1│ │2 3│ │\n", "│ └~──┘ └~──┘ └~──┘ │\n", "└∊──────────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⎕←bmat ← 2 3 ⍴ 0 1 0 1 0 1\n", "⍸ bmat" ] }, { "cell_type": "code", "execution_count": null, "id": "b3c0b9fa-98a8-4914-973e-57e01081c83b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "↓0 2 0│\n", "│2 0 2│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "
┌→────────────────────────────────────┐\n", "│ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ │\n", "│ │1 2│ │1 2│ │2 1│ │2 1│ │2 3│ │2 3│ │\n", "│ └~──┘ └~──┘ └~──┘ └~──┘ └~──┘ └~──┘ │\n", "└∊────────────────────────────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⎕←bmat ← 2 3 ⍴ 0 2 0 2 0 2\n", "⍸ bmat" ] }, { "cell_type": "markdown", "id": "1c86ab76", "metadata": {}, "source": [ "#### dyadic `⍸` (Interval Index)" ] }, { "cell_type": "markdown", "id": "057b305f-dd6e-4658-9c53-55576cef9585", "metadata": {}, "source": [ "Interval index creates intervals and returns an array that tells you which interval a value falls in.\n", "\n", "If the following example, `⍵` is `2 4 6`. Therefore the following intervals (sometimes called bins) are created.\n", "\n", "+ **0th interval:** Less than 2\n", "+ **1st interval:** 2-4 (excluding 4)\n", "+ **2nd interval:** 4-6 (excluding 6)\n", "+ **3rd interval:** Greater than or equal to 6\n", "\n", "`⍺` (right argument) is `1 2 3 4 5 6 7` so let's put those into intervals, which the first interval starting at 0.\n", "\n", "+ The first value of `⍺` is 1. 1 is Less than 2. Therefore it is in the 0th interval and interval index returns 0.\n", "+ The second value of `⍺` is 2. 2 is in the 2-4 range. Therefore it is in the 1st interval and interval index returns 1.\n", "+ The third value of `⍺` is 3. 3 is in the 2-4 range. Therefore it is in the 1st interval and interval index returns 1.\n", "+ The fourth value of `⍺` is 4. 4 is in the 4-6 range. Therefore it is in the 2nd interval and interval index returns 2.\n", "\n", "And so on and so forth." ] }, { "cell_type": "code", "execution_count": null, "id": "c402ce31", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────────────┐\n", "│0 1 1 2 2 3 3│\n", "└~────────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 4 6 ⍸ 1 2 3 4 5 6 7" ] }, { "cell_type": "markdown", "id": "46aea64b-c2d1-4ce9-ab66-de44e7cf5f52", "metadata": {}, "source": [ "Interval index works the same way with character vectors.\n", "\n", "+ `D` is in the first interval because it is between `A` and `E`.\n", "+ `Y` is in the last interval (fifth) because it is larger than all values\n", "+ `A` is in the first interval because it is between `A` and `E`.\n", "+ `L` is in the third interval because it is between `I` and `O`.\n", "\n", "etc." ] }, { "cell_type": "code", "execution_count": null, "id": "f66c7d8a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──────────┐\n", "│1 5 1 3 4 2│\n", "└~──────────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'AEIOU' ⍸ 'DYALOG'" ] }, { "cell_type": "markdown", "id": "88e6e4a7-6538-4372-b752-48c2cfc9dfb1", "metadata": {}, "source": [ "Interval index works across major cells." ] }, { "cell_type": "code", "execution_count": null, "id": "9c607eb4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n", "↓1 2│\n", "│3 4│\n", "│5 6│\n", "└~──┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⎕←mat←3 2⍴⍳6" ] }, { "cell_type": "code", "execution_count": null, "id": "d4e1c0b4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "1\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mat ⍸ 3 3" ] }, { "cell_type": "code", "execution_count": null, "id": "5af2f8d1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "2\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mat ⍸ 3 5" ] }, { "cell_type": "code", "execution_count": null, "id": "fc9be234", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n", "│1 2│\n", "└~──┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mat ⍸ 2 2 ⍴ 3 3 3 5" ] }, { "cell_type": "markdown", "id": "ad28d0b2", "metadata": {}, "source": [ "### `⌈` (Upstile)" ] }, { "cell_type": "markdown", "id": "3db6d6cb", "metadata": {}, "source": [ "#### monadic `⌈` (Ceiling)" ] }, { "cell_type": "markdown", "id": "f162a21e-b887-4a58-bb02-9c70e67100fa", "metadata": {}, "source": [ "Rounds up to nearest whole number." ] }, { "cell_type": "code", "execution_count": null, "id": "7223b897", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───────┐\n", "│4 ¯3 3 0│\n", "└~───────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⌈ 3.4 ¯3.4 3 0" ] }, { "cell_type": "markdown", "id": "0717d224", "metadata": {}, "source": [ "#### dyadic `⌈` (Maximum)" ] }, { "cell_type": "markdown", "id": "e97c8bc5-420c-44a4-850d-1f900c0f48e8", "metadata": {}, "source": [ "Takes maximum of 2 values" ] }, { "cell_type": "code", "execution_count": null, "id": "785a5a38", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "3\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3⌈2" ] }, { "cell_type": "code", "execution_count": null, "id": "7a410ee3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→──┐\n", "│3 3│\n", "└~──┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 2⌈2 3" ] }, { "cell_type": "code", "execution_count": null, "id": "f9f78ce3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "6\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 ⌈ 6 ⌈ 2" ] }, { "cell_type": "code", "execution_count": null, "id": "04ed6871", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→────┐\n", "│0 6 2│\n", "└~────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a ← ¯4 6 2\n", "0 ⌈ a" ] }, { "cell_type": "markdown", "id": "c9a8d1b5", "metadata": {}, "source": [ "### `⌊` (Downstile)" ] }, { "cell_type": "markdown", "id": "c054a3e9", "metadata": {}, "source": [ "#### monadic `⌊` (Floor)" ] }, { "cell_type": "markdown", "id": "8feeafed-2657-4651-b817-95a4e09075a6", "metadata": {}, "source": [ "Rounds down to nearest whole number" ] }, { "cell_type": "code", "execution_count": null, "id": "aa41f28e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌→───────┐\n", "│3 ¯4 3 0│\n", "└~───────┘\n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⌊ 3.4 ¯3.4 3 0" ] }, { "cell_type": "markdown", "id": "a40dd397", "metadata": {}, "source": [ "#### dyadic `⌊` (Minimum)" ] }, { "cell_type": "markdown", "id": "524a4b1b-3f65-4a2c-850a-37dd0deed965", "metadata": {}, "source": [ "Takes minimum of 2 values" ] }, { "cell_type": "code", "execution_count": null, "id": "d9a0b9a6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "2.5\n", " \n", "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 ⌊ 6 ⌊ 2.5" ] } ], "metadata": { "kernelspec": { "display_name": "Dyalog APL", "language": "apl", "name": "dyalog-kernel" } }, "nbformat": 4, "nbformat_minor": 5 }