From 16022acd45703772f62ca9ed9f8d8ef9288be9b5 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 21 Jul 2020 15:45:25 -0400 Subject: Custom array formatting in the markdown converter --- docs/doc/windows.html | 63 +++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 27 deletions(-) (limited to 'docs/doc/windows.html') diff --git a/docs/doc/windows.html b/docs/doc/windows.html index 79d18e77..c20d6591 100644 --- a/docs/doc/windows.html +++ b/docs/doc/windows.html @@ -6,35 +6,42 @@

Definition

We'll start with the one-axis case. Here Window's left argument is a number between 0 and 1+≠𝕩. The result is composed of slices of 𝕩 (contiguous sections of major cells) with length 𝕨, starting at each possible index in order.

    5↕"abcdefg"
-abcde
-bcdef
-cdefg
+β”Œβ”€       
+β•΅"abcde  
+  bcdef  
+  cdefg  
+        β”˜
 

There are 1+(≠𝕩)-𝕨, or (≠𝕩)¬𝕨, of these sections, because the starting index must be at least 0 and at most (≠𝕩)-𝕨. Another way to find this result is to look at the number of cells in or before a given slice: there are always 𝕨 in the slice and there are only ≠𝕩 in total, so the number of slices is the range spanned by these two endpoints.

You can take a slice of an array 𝕩 that has length l and starts at index i using l↑i↓𝕩 or l↑iβŒ½π•©. The Prefixes function returns all the slices that end at the end of the array ((≠𝕩)=i+l), and Suffixes gives the slices that start at the beginning (i=0). Windows gives yet another collection of slices: the ones that have a fixed length l=𝕨. Selecting one cell from its result gives you the slice starting at that cell's index:

    2⊏5↕"abcdefg"
-cdefg
+"cdefg"
     5↑2↓"abcdefg"
-cdefg
+"cdefg"
 

Windows differs from Prefixes and Suffixes in that it doesn't add a layer of nesting (it doesn't enclose each slice). This is possible because the slices have a fixed size.

Multiple dimensions

The above description applies to a higher-rank right argument. As an example, we'll look at two-row slices of a shape 3β€Ώ4 array. For convenience, we will enclose each slice. Note that slices always have the same rank as the argument array.

    <βŽ‰2 2↕"0123"∾"abcd"≍"ABCD"
-β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”
-β”‚0123β”‚abcdβ”‚
-β”‚abcdβ”‚ABCDβ”‚
-β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜
+β”Œβ”€                   
+Β· β”Œβ”€       β”Œβ”€        
+  β•΅"0123   β•΅"abcd    
+    abcd     ABCD    
+         β”˜        β”˜  
+                    β”˜
 

Passing a list as the left argument to Windows takes slices along any number of leading axes. Here are all the shape 2β€Ώ2 slices:

    <βŽ‰2 2β€Ώ2↕"0123"∾"abcd"≍"ABCD"
-β”Œβ”€β”€β”¬β”€β”€β”¬β”€β”€β”
-β”‚01β”‚12β”‚23β”‚
-β”‚abβ”‚bcβ”‚cdβ”‚
-β”œβ”€β”€β”Όβ”€β”€β”Όβ”€β”€β”€
-β”‚abβ”‚bcβ”‚cdβ”‚
-β”‚ABβ”‚BCβ”‚CDβ”‚
-β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜
+β”Œβ”€                      
+β•΅ β”Œβ”€     β”Œβ”€     β”Œβ”€      
+  β•΅"01   β•΅"12   β•΅"23    
+    ab     bc     cd    
+       β”˜      β”˜      β”˜  
+  β”Œβ”€     β”Œβ”€     β”Œβ”€      
+  β•΅"ab   β•΅"bc   β•΅"cd    
+    AB     BC     CD    
+       β”˜      β”˜      β”˜  
+                       β”˜
 

The slices are naturally arranged along multiple dimensions according to their starting index. Once again the equivalence i⊏l↕x ←→ l↑i↓x holds, provided i and l have the same length.

If the left argument has length 0, then the argument is not sliced along any dimensions. The only slice that resultsβ€”the entire argumentβ€”is then arranged along an additional zero dimensions. In the end, the result is the same as the argument.

@@ -44,13 +51,15 @@ cdefg

Symmetry

Let's look at an earlier example, along with its transpose.

    {βŸ¨π•©,β‰π•©βŸ©}5↕"abcdefg"
-β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”
-β”‚abcdeβ”‚abcβ”‚
-β”‚bcdefβ”‚bcdβ”‚
-β”‚cdefgβ”‚cdeβ”‚
-β”‚     β”‚defβ”‚
-β”‚     β”‚efgβ”‚
-β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”˜
+β”Œβ”€                   
+Β· β”Œβ”€        β”Œβ”€       
+  β•΅"abcde   β•΅"abc    
+    bcdef     bcd    
+    cdefg     cde    
+          β”˜   def    
+              efg    
+                  β”˜  
+                    β”˜
 

Although the two arrays have different shapes, they are identical where they overlap.

    ≑○(3β€Ώ3βŠΈβ†‘)βŸœβ‰5↕"abcdefg"
@@ -67,16 +76,16 @@ cdefg
 

Applications

Windows can be followed up with a reduction on each slice to give a windowed reduction. Here we take running sums of 3 values.

    +´˘3↕ ⟨2,6,0,1,4,3⟩
-8β€Ώ7β€Ώ5β€Ώ8
+⟨ 8 7 5 8 ⟩
 

A common task is to pair elements, with an initial or final element so the total length stays the same. This can also be done with a pairwise reduction, but another good way (and more performant without special support in the interpreter) is to add the element and then use windows matching the original length. Here both methods are used to invert +`, which requires we take pairwise differences starting at initial value 0.

    -˜´˘2↕0∾ +` 3β€Ώ2β€Ώ1β€Ώ1
-3β€Ώ2β€Ώ1β€Ώ1
+⟨ 3 2 1 1 ⟩
     ((-˜´<˘)≠↕0∾⊒) +` 3β€Ώ2β€Ώ1β€Ώ1
-3β€Ώ2β€Ώ1β€Ώ1
+⟨ 3 2 1 1 ⟩
 

This method extends to any number of initial elements. We can modify the running sum above to keep the length constant by starting with two zeros.

    ((+Β΄<˘)≠↕(2β₯Š0)⊸∾) ⟨2,6,0,1,4,3⟩
-2β€Ώ8β€Ώ8β€Ώ7β€Ώ5β€Ώ8
+⟨ 2 8 8 7 5 8 ⟩
 
-- cgit v1.2.3