Skip to content

Commit 3e33005

Browse files
committed
initial commit
1 parent ccda2fc commit 3e33005

File tree

394 files changed

+183168
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

394 files changed

+183168
-0
lines changed

contents/.ipynb_checkpoints/aplway-checkpoint.ipynb

Lines changed: 1262 additions & 0 deletions
Large diffs are not rendered by default.

contents/.ipynb_checkpoints/array-checkpoint.ipynb

Lines changed: 879 additions & 0 deletions
Large diffs are not rendered by default.

contents/.ipynb_checkpoints/at-checkpoint.ipynb

Lines changed: 589 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Partitions\n",
8+
"\n",
9+
"> It's harder to read code than to write it. --_Joel Spolsky_"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 1,
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"data": {
19+
"text/html": [
20+
"<span style=\"white-space:pre; font-family: monospace\">done\n",
21+
"\n",
22+
"Rebuilding user command cache... done\n",
23+
"</span>"
24+
]
25+
},
26+
"execution_count": 1,
27+
"metadata": {},
28+
"output_type": "execute_result"
29+
}
30+
],
31+
"source": [
32+
"⎕IO ← 0\n",
33+
"{}⎕SE.UCMD'box on -s=min -t=tree -f=on'\n",
34+
"{}⎕SE.UCMD'rows on'"
35+
]
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"metadata": {},
40+
"source": [
41+
"We often need to group or select items in an array based on some criterion -- perhaps stretches of items that are equal, or perhaps we have a boolean mask indicating which stretches of elements should be joined up.\n",
42+
"\n",
43+
"Good partition primer on [APLWiki](https://aplwiki.com/wiki/Partition_representations)"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
49+
"source": [
50+
"## Partition `⊆`\n",
51+
"\n",
52+
"There's even a glyph called [Partition](http://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Partition.htm), `⊆`. It selects groups elements based on a vector where new selections are started where the corresponding element is larger than its predecessor. Ok... that sounds _very_ specific, and quite possibly not what you had in mind. In its simplest form it's like compress, but enclosing elements corresponding to stretches of 1s. Compare:"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 11,
58+
"metadata": {},
59+
"outputs": [
60+
{
61+
"data": {
62+
"text/html": [
63+
"<span style=\"white-space:pre; font-family: monospace\">┌───┬─┬────┬──┐\n",
64+
"│1 2│4│9 10│12│\n",
65+
"└───┴─┴────┴──┘\n",
66+
"</span>"
67+
]
68+
},
69+
"execution_count": 11,
70+
"metadata": {},
71+
"output_type": "execute_result"
72+
},
73+
{
74+
"data": {
75+
"text/html": [
76+
"<span style=\"white-space:pre; font-family: monospace\">1 2 4 9 10 12\n",
77+
"</span>"
78+
]
79+
},
80+
"execution_count": 11,
81+
"metadata": {},
82+
"output_type": "execute_result"
83+
}
84+
],
85+
"source": [
86+
"1 1 0 1 0 0 0 0 1 1 0 1 ⊆ ⍳12 ⍝ Partition\n",
87+
"1 1 0 1 0 0 0 0 1 1 0 1/⍳12 ⍝ Compress"
88+
]
89+
},
90+
{
91+
"cell_type": "markdown",
92+
"metadata": {},
93+
"source": [
94+
"## Partitioned enclose `A⊂B`\n",
95+
"\n",
96+
"Partitioned enclose, `A⊂B`, groups items based on a boolean mask where enclosures start on 1. For example:"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 5,
102+
"metadata": {},
103+
"outputs": [
104+
{
105+
"data": {
106+
"text/html": [
107+
"<span style=\"white-space:pre; font-family: monospace\">┌───┬───┬───┬───┬────┐\n",
108+
"│1 2│3 4│5 6│7 8│9 10│\n",
109+
"└───┴───┴───┴───┴────┘\n",
110+
"</span>"
111+
]
112+
},
113+
"execution_count": 5,
114+
"metadata": {},
115+
"output_type": "execute_result"
116+
}
117+
],
118+
"source": [
119+
"1 0 1 0 1 0 1 0 1 0⊂⍳10"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"We can use this to group stretches of elements which are equal:"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 6,
132+
"metadata": {},
133+
"outputs": [
134+
{
135+
"data": {
136+
"text/html": [
137+
"<span style=\"white-space:pre; font-family: monospace\">┌─────────┬───┬─┬───────┬───┬───┐\n",
138+
"│1 1 1 1 1│2 2│1│4 4 4 4│1 1│2 2│\n",
139+
"└─────────┴───┴─┴───────┴───┴───┘\n",
140+
"</span>"
141+
]
142+
},
143+
"execution_count": 6,
144+
"metadata": {},
145+
"output_type": "execute_result"
146+
}
147+
],
148+
"source": [
149+
"{⍵⊂⍨1,2≠/⍵} 1 1 1 1 1 2 2 1 4 4 4 4 1 1 2 2"
150+
]
151+
},
152+
{
153+
"cell_type": "markdown",
154+
"metadata": {},
155+
"source": [
156+
"We can apply the same technique to group based on sign:"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 7,
162+
"metadata": {},
163+
"outputs": [
164+
{
165+
"data": {
166+
"text/html": [
167+
"<span style=\"white-space:pre; font-family: monospace\">┌───┬─────┬─┬────────┬───┬─────┐\n",
168+
"│0 0│1 2 3│0│¯1 ¯7 ¯8│0 0│1 2 3│\n",
169+
"└───┴─────┴─┴────────┴───┴─────┘\n",
170+
"</span>"
171+
]
172+
},
173+
"execution_count": 7,
174+
"metadata": {},
175+
"output_type": "execute_result"
176+
}
177+
],
178+
"source": [
179+
"{⍵⊂⍨1,2≠/×⍵} 0 0 1 2 3 0 ¯1 ¯7 ¯8 0 0 1 2 3"
180+
]
181+
},
182+
{
183+
"cell_type": "markdown",
184+
"metadata": {},
185+
"source": [
186+
"In the above two examples we generate the mask by a windowed reduction of size 2:"
187+
]
188+
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": 8,
192+
"metadata": {},
193+
"outputs": [
194+
{
195+
"data": {
196+
"text/html": [
197+
"<span style=\"white-space:pre; font-family: monospace\">0 1 0 0 1 1 0 0 1 0 1 0 0\n",
198+
"</span>"
199+
]
200+
},
201+
"execution_count": 8,
202+
"metadata": {},
203+
"output_type": "execute_result"
204+
}
205+
],
206+
"source": [
207+
"{2≠/×⍵} 0 0 1 2 3 0 ¯1 ¯7 ¯8 0 0 1 2 3"
208+
]
209+
},
210+
{
211+
"cell_type": "markdown",
212+
"metadata": {},
213+
"source": [
214+
"but we also need to prepend a 1, as we want the first partition to start at the beginning."
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": null,
220+
"metadata": {},
221+
"outputs": [],
222+
"source": []
223+
}
224+
],
225+
"metadata": {
226+
"kernelspec": {
227+
"display_name": "Dyalog APL",
228+
"language": "apl",
229+
"name": "dyalog-kernel"
230+
},
231+
"language_info": {
232+
"file_extension": ".apl",
233+
"mimetype": "text/apl",
234+
"name": "APL"
235+
}
236+
},
237+
"nbformat": 4,
238+
"nbformat_minor": 4
239+
}

0 commit comments

Comments
 (0)