Skip to content

Commit

Permalink
higher ranked args in encode
Browse files Browse the repository at this point in the history
  • Loading branch information
xpqz committed Aug 5, 2022
1 parent cc8766a commit 392efe4
Showing 1 changed file with 253 additions and 2 deletions.
255 changes: 253 additions & 2 deletions contents/decode.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Going the other way, how many soldiers are there in a legion?"
"Going the other way, how many soldiers are there in a legion? "
]
},
{
Expand Down Expand Up @@ -290,7 +290,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"There are some less obvious uses, too. For example, we can use `1⊥` to sum vectors:"
"_Decode_ has some less obvious uses, too. For example, we can use `1⊥` to sum vectors:"
]
},
{
Expand Down Expand Up @@ -492,6 +492,257 @@
"source": [
"⎕ ← i j←(⍴m)⊤k ⍝ 1D to 2D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Higher ranks\n",
"\n",
"So far, we only used vectors as arguments to _Encode_ and _Decode_. However, they can take arguments of higher ranks, too. It may not be immediately obvious what this means, so let's look at that.\n",
"\n",
"_Encode_ returns an array where the shape is the combined shape of the left and right arguments:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre class=\"language-APL\"> 1 2 4 5\n",
"23 46 10 33\n",
"20 40 0 20\n",
"</pre>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"24 60 60⊤5000 10000 15000 20000 ⍝ Convert 5000 10000 15000 20000 to HMS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So the shape is `3 4`. If we apply the same operation to a matrix, the same relationship holds:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre class=\"language-APL\"> 5000 10000\n",
"15000 20000\n",
"</pre>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"<pre class=\"language-APL\"> 1 2\n",
" 4 5\n",
"\n",
"23 46\n",
"10 33\n",
"\n",
"20 40\n",
" 0 20\n",
"</pre>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"seconds ← ⎕ ← 2 2⍴5000 10000 15000 20000\n",
"24 60 60⊤seconds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Again, the shape is 3 layers (as `3=≢24 60 60`) of shape `2 2`. The leading axis holds the results which is easier to see with a transpose applied:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre class=\"language-APL\">1 23 20\n",
"2 46 40\n",
"\n",
"4 10 0\n",
"5 33 20\n",
"</pre>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"⎕IO←0\n",
"2 0 1⍉24 60 60⊤seconds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Perhaps more surprisingly, we can use a higher-ranked argument to the _left_, too. It then provides _multiple_ radix vectors into which to encode the right hand side. Let's say we want to convert a set of numbers to bases 10 and 16:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre class=\"language-APL\">10 16\n",
"10 16\n",
"10 16\n",
"</pre>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"<pre class=\"language-APL\">1 2\n",
"0 1\n",
"\n",
"2 5\n",
"8 0\n",
"\n",
"8 6\n",
"0 0\n",
"</pre>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"radix ← ⎕ ← 3 2⍴10 16\n",
"radix⊤128 256"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We expected the shape `3 2 2`, but perhaps it's tricky to untangle why the result looks like it does. It's essentially an outer product, with a different axis order:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre class=\"language-APL\">1 2\n",
"0 1\n",
"\n",
"2 5\n",
"8 0\n",
"\n",
"8 6\n",
"0 0\n",
"</pre>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"⍉↑⍉(10 10 10)(16 16 16)∘.⊤128 256"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre class=\"language-APL\">1 2 8\n",
"0 8 0\n",
"\n",
"2 5 6\n",
"1 0 0\n",
"</pre>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"⍉(3 2⍴10 16)⊤128 256"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"which is\n",
"\n",
" 128 = (1,2,8)₁₀\n",
" 128 = (0,8,0)₁₆\n",
"\n",
" 256 = (2,5,6)₁₀\n",
" 256 = (1,0,0)₁₆"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Higher ranks for both left and right left as an exercise for the interested reader."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 392efe4

Please sign in to comment.