From c618ade174cc2b4e428457751ad8dd01130c2239 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sat, 25 Jun 2022 22:20:19 -0400 Subject: Back to editing docs --- docs/doc/under.html | 12 ++--- docs/doc/undo.html | 11 +++-- docs/doc/windows.html | 119 ++++++++++++++++++++++++++------------------------ 3 files changed, 77 insertions(+), 65 deletions(-) (limited to 'docs') diff --git a/docs/doc/under.html b/docs/doc/under.html index ae931d47..2a06e458 100644 --- a/docs/doc/under.html +++ b/docs/doc/under.html @@ -57,15 +57,15 @@ 0 10 11 β”˜ -

When used with Under, the function 1⊸⌽ applies to the first column, rotating it. The result of 𝔽 needs to be compatible with the selection function, so Rotate works but trying to remove an element is no good:

+

When used with Under, the function 1⊸⌽ applies to the first column, rotating it. The result of 𝔽 needs to be compatible with the selection function, so Rotate works but trying to drop an element is no good:

↗️
    1βŠΈβ†“βŒΎ(⊏˘) a
 Error: ⁼: Inverse not found
 
-

BQN can detect lots of structural functions when written in tacit form; see the list of functions in the spec. You can also include computations on the shape. For example, here's a function to reverse the first half of a list.

+

BQN can detect lots of structural functions when written tacitly; see the list of recognized forms in the spec. You can also include computations on the shape. For example, here's a function to reverse the first half of a list.

↗️
    ⌽⌾(βŠ’β†‘Λœβ‰ Γ·2Λ™) "abcdef"
 "cbadef"
 
-

But you can't use a computation that uses array values, such as 10⊸+⌾((<⊸5)⊸/) to add 10 to each element below 5. This is because Under can change the array values, so that the function 𝔾 doesn't select the same elements before and after applying it (at the same time, Under can't change array structure, or at least not the parts that matter to 𝔾). To use a dynamic selection function, compute the mask or indices based on a copy of the argument and use those as part of 𝔾.

+

But you can't use a computation that uses array values, such as 10⊸+⌾((<⊸5)⊸/) to add 10 to each element below 5. This is because Under can change the array values, so that the function 𝔾 doesn't select the same elements before and after applying it (contrarily, Under can't change array structure, or at least not the parts that matter to 𝔾). To use a dynamic selection function, compute the mask or indices based on a copy of the argument and use those as part of 𝔾.

↗️
    {10⊸+⌾((𝕩<5)⊸/)𝕩} 3β€Ώ8β€Ώ2β€Ώ2β€Ώ6
 ⟨ 13 8 12 12 6 ⟩
 
@@ -89,14 +89,14 @@
 ↗️
    ⌊⌾(10βŠΈΓ—) 3.524β€Ώ6.799β€Ώ2.031
 ⟨ 3.5 6.7 2 ⟩
 
-

See how it works? ⌊ rounds down to an integer, but we can get it to round down to a decimal by first multiplying by 10 (single decimals are now integers), then rounding, then undoing that multiplication. A related idea is to not just round but produce a range. Suppose I want the arithmetic progression 4, 7, 10, ... <20. If I had the right range ↕n, then it would be 4+3×↕n, or (4+3Γ—βŠ’)↕n. By using the inverse of this transformation function on the desired endpoint, I can make sure it's applied on the way out, and BQN figures out what to do on the way in as if by magic.

+

See how it works? ⌊ rounds down to an integer, but we can get it to round down to a decimal by first multiplying by 10 (so that single decimals become integers), then rounding, then undoing that multiplication. A related idea is to not just round but produce a range. Suppose I want the arithmetic progression 4, 7, 10, ... <20. If I had the right range ↕n, then it would be 4+3×↕n, or (4+3Γ—βŠ’)↕n. By using the inverse of this transformation function on the desired endpoint, I can make sure it's applied on the way out, and BQN figures out what to do on the way in as if by magic.

↗️
    β†•βˆ˜βŒˆβŒΎ((4+3Γ—βŠ’)⁼) 20
 ⟨ 4 7 10 13 16 19 ⟩
 
-

Well, really it's a bit of simple algebra, but if it wants to wear a pointy hat and wave a wand around I won't judge.

+

Well, really it's some simple algebra, but if it wants to wear a pointy hat and wave a wand around I won't judge.

Left argument

When called dyadically, Under applies 𝔽 dyadically, like Over. This doesn't affect the undoing part of Under, which still tries to put the result of 𝔽 back into 𝕩 for structural Under or invert 𝔾 for computational. In fact, 𝕨 π”½βŒΎπ”Ύ 𝕩 is equivalent to (𝔾𝕨)Λ™βŠΈπ”½βŒΎπ”Ύ 𝕩 so no exciting language stuff is happening here at all.

-

But you can still do some cool stuff with it! One pattern is simply to set 𝔽 to ⊣, the identity function that just returns its left argument. Now structural Under will replace everything that 𝔾 selects from 𝕩 with the corresponding values in 𝕨. Here's an example that replaces elements with indices 1 and 2.

+

But you can still do cool things with it! One pattern is simply to set 𝔽 to ⊣, the identity function that just returns its left argument. Now structural Under will replace everything that 𝔾 selects from 𝕩 with the corresponding values in 𝕨. Here's an example that replaces elements with indices 1 and 2.

↗️
    "abcd" ⊣⌾(1β€Ώ2⊸⊏) "0123"
 "0bc3"
 
diff --git a/docs/doc/undo.html b/docs/doc/undo.html index daf1b60d..7e498365 100644 --- a/docs/doc/undo.html +++ b/docs/doc/undo.html @@ -21,7 +21,7 @@

Here it undoes a function to decrement the last character by incrementing that character. In part this is enabled by the clean design of BQN primitives, because better-behaved functions like those using structural Under are easier to invert.

The rules

-

If 𝔽 can be inverted exactly, then Undo just does that. However, there are also some other functions that BQN inverts. For example, the squaring function Γ—Λœ has both a positive and a negative inverse, and yet:

+

If 𝔽 can be inverted exactly, then Undo just does that (or tries). However, there are also some other functions that BQN inverts. For example, the squaring function Γ—Λœ has both a positive and a negative inverse, and yet:

↗️
    Γ—Λœ Β―3
 9
     Γ—ΛœβΌ Γ—Λœ Β―3  # It's not the same!
@@ -37,8 +37,9 @@
 

Arithmetic and simple combinators are usually invertible. A compound function that refers to its argument just once, like 6+βŒ½βˆ˜β‰, can typically be undone, but one that uses the argument in two different ways, such as ⊒+⋆, probably can't.

A few notable inverses are the logarithm ⋆⁼, un-Transpose ⍉⁼, and Indices inverse /⁼. Enclose inverse, <⁼, is an alternative to First that requires its argument to be a unit array.

Structural functions like Take and shifts that remove elements from 𝕩 can't be inverted, because given the result there's no way to know what the elements should be. However, there are two special cases that have inverses defined despite losing data: these are ⊣⁼ and k⁼ where k is a constant (a data type, or kΛ™). For these, 𝕩 is required to match the always returned value 𝕨 or k, and this value is also used for the resultβ€”even though any result would be valid, as these functions ignore 𝕩.

-↗️
    3 ⊣⁼ 4
+↗️
    3 ⊣⁼ 4
 Error: ⁼: Inverse does not exist
+
     3 ⊣⁼ 3
 3
 
@@ -49,4 +50,8 @@ π•ŠβΌπ•©: 𝕩÷1-𝕩 }
-

The above function could also be defined with the automatically invertible 1⊸+⌾÷, but maybe there's a numerical reason to use the definition above. Like a normal header, an undo header reflects the normal use, but it includes ⁼ and possibly ˜ addition to the function and arguments.

+

The above function could also be defined with the automatically invertible 1⊸+⌾÷, but maybe there's a numerical reason to use the definition above. Like a normal header, an undo header reflects the normal use, but it includes ⁼ and possibly ˜ addition to the function and arguments. Any header that includes a function can have these modifiers addedβ€”even a fancy modifier header like 𝔽_m1⁼aβ€Ώb: or plain label like π•ŠβΌ:. The three forms are π•ŠβΌπ•©:, π•¨π•ŠβΌπ•©:, and π•¨π•ŠΛœβΌπ•©:, and the two dyadic forms are also used to derive k⊸F⁼𝕩 and F⟜k⁼𝕩 with a constant k.

+↗️
    G ← { π•ŠΛœβΌ:𝕩-2×𝕨 ; 𝕨+2×𝕩 }
+    G⟜2⁼ 7
+3
+
diff --git a/docs/doc/windows.html b/docs/doc/windows.html index 77c40f19..67e6a9f8 100644 --- a/docs/doc/windows.html +++ b/docs/doc/windows.html @@ -5,10 +5,10 @@

Windows

-

In BQN, it's strongly preferred to use functions, and not modifiers, for array manipulation. Functions are simpler as they have fewer moving parts. They are more concrete, since the array results can always be viewed right away. They are easier to implement with reasonable performance as well, since there is no need to recognize many possible function operands as special cases.

-

The Window function replaces APL's Windowed Reduction, J's more general Infix operator, and Dyalog's Stencil, which is adapted from one case of J's Cut operator.

-

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.

+

The Windows function returns all slices, or contiguous subarrays, with shape (well, shape prefix) 𝕨 from 𝕩. It might also be seen as sliding a moving window along 𝕩.

+

This function replaces APL's Windowed Reduction, J's more general Infix operator, and Dyalog APL's Stencil, which is adapted from one case of J's Cut operator. In BQN, it's strongly preferred to use functions, and not modifiers, for array manipulation. Functions are simpler with fewer moving parts, and more concrete, since the array results can always be viewed right away.

+

Basic case

+

We'll start with the one-axis case. Here 𝕨 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  
@@ -17,44 +17,33 @@
         β”˜
 

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 Take with Drop or Rotate: 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"
+

A single slice of an array 𝕩 with length l and starting index i is l↑i↓𝕩, using Take and Drop. 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 the slice starting at that cell's index:

+↗️
    2⊏5↕"abcdefg"
 "cdefg"
+
     5↑2↓"abcdefg"
 "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"   
-         β”˜        β”˜  
-                    β”˜
+

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, so they fit together as cells of an array.

+

Windowed reduction

+

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

+↗️
    +˝˘3↕ ⟨2,6,0,1,4,3⟩
+⟨ 8 7 5 8 ⟩
 
-

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"   
-       β”˜      β”˜      β”˜  
-                       β”˜
+

A common task is to act on windows with an initial or final element so the total length stays the same. When using windows of length 2, the best way to accomplish this is with a shift Β« or Β». If the window length is longer or variable, then a trick with Windows works better: add the elements, and then use windows matching the original length. Here we invert Plus Scan +`, which requires we take pairwise differences starting at initial value 0.

+↗️
    -⟜(0»⊒) +` 3β€Ώ2β€Ώ1β€Ώ1
+⟨ 3 2 1 1 ⟩
+
+    (-ΛœΛβ‰ β†•0∾⊒) +` 3β€Ώ2β€Ώ1β€Ώ1
+⟨ 3 2 1 1 ⟩
+
+

With Windows, we can modify the 3-element running sum from before to keep the length constant by starting with two zeros.

+↗️
    (+˝≠↕(2β₯Š0)⊸∾) ⟨2,6,0,1,4,3⟩
+⟨ 2 8 8 7 5 8 ⟩
 
-

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 𝕨 has length 0, then 𝕩 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 𝕩, unchanged.

-

More formally

-

𝕩 is an array. 𝕨 is a number, or numeric list or unit, with 𝕨≀○≠≒𝕩. The result z has shape π•¨βˆΎΒ¬βŸœπ•¨βŒΎ((≠𝕨)βŠΈβ†‘)≒𝕩, and element iβŠ‘z is π•©βŠ‘Λœ(≠𝕨)(↑+⌾((≠𝕨)βŠΈβ†‘)↓)i.

-

Using Group we could also write iβŠ‘z ←→ π•©βŠ‘Λœ(π•¨βˆΎβ—‹(β†•βˆ˜β‰ )≒𝕩) +Β΄Β¨βˆ˜βŠ” i.

Symmetry

-

Let's look at an earlier example, along with its Transpose (⍉).

-↗️
    {βŸ¨π•©,β‰π•©βŸ©}5↕"abcdefg"
+

Let's look at the first example, paired with its Transpose (⍉).

+↗️
    β‹ˆβŸœβ‰ 5↕"abcdefg"
 β”Œβ”€                   
 Β· β”Œβ”€        β”Œβ”€       
   β•΅"abcde   β•΅"abc    
@@ -65,31 +54,49 @@
                   β”˜  
                     β”˜
 
-

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

-↗️
    ≑○(3β€Ώ3βŠΈβ†‘)βŸœβ‰5↕"abcdefg"
+

Although the two arrays have different shapes, they're identical in the 3Γ—3 region where they overlap.

+↗️
    ≑○(3β€Ώ3βŠΈβ†‘)βŸœβ‰ 5↕"abcdefg"
 1
 
-

In other words, the i'th element of slice j is the same as the j'th element of slice i: it is the i+j'th element of the argument. So transposing still gives a possible result of Windows, but with a different slice length.

-↗️
    {(5↕𝕩)≑⍉(3↕𝕩)}"abcdefg"
+

More concretely, the ith element of slice j is the same as the jth element of slice i: it's the i+jth element of the argument. So transposing still gives a possible result of Windows, but with a different slice length. The two lengths are related by Span, which converts between length and number of slices.

+↗️
    {(5↕𝕩)≑⍉(3↕𝕩)}"abcdefg"
 1
+
+    (β‰ "abcdefg") Β¬ 3
+5
 
-

In general, we need a more complicated transposeβ€”swapping the first set of ≠𝕨 axes with the second set. Note again the use of Span, our slice-length to slice-number converter.

-↗️
    {((5β€Ώ6Β¬2β€Ώ2)↕𝕩) ≑ 2β€Ώ3⍉(2β€Ώ2↕𝕩)} ↕5β€Ώ6β€Ώ7
-1
-
-

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 ⟩
-
-

A common task is to act on windows with an initial or final element so the total length stays the same. When using windows of length 2, the best way to accomplish this is with a shift Β« or Β». If the window length is longer or variable, then a trick with Windows works better: add the elements, and then use windows matching the original length. Here we invert Plus Scan +`, which requires we take pairwise differences starting at initial value 0.

-↗️
    -⟜(0»⊒) +` 3β€Ώ2β€Ώ1β€Ώ1
-⟨ 3 2 1 1 ⟩
+

Multiple dimensions

+

The right argument can have rank more than 1, and it's viewed as a list of major cells following leading axis principles. As an example, Windows can take two-row slices of a shape 3β€Ώ4 array.

+↗️
        2↕["0123","abcd","ABCD"]
+β”Œβ”€      
+β•Ž"0123  
+  abcd  
+        
+ Β·abcd  
+  ABCD" 
+       β”˜
 
-    (-ΛœΛβ‰ β†•0∾⊒) +` 3β€Ώ2β€Ώ1β€Ώ1
-⟨ 3 2 1 1 ⟩
+    <βŽ‰2 2↕["0123","abcd","ABCD"]
+β”Œβ”€                   
+Β· β”Œβ”€       β”Œβ”€        
+  β•΅"0123   β•΅"abcd    
+    abcd"    ABCD"   
+         β”˜        β”˜  
+                    β”˜
 
-

With Windows, we can modify the 3-element running sum from before to keep the length constant by starting with two zeros.

-↗️
    (+˝≠↕(2β₯Š0)⊸∾) ⟨2,6,0,1,4,3⟩
-⟨ 2 8 8 7 5 8 ⟩
+

In the second version we've enclosed each slice with <βŽ‰2 for viewingβ€”a slice has rank 2, the same as 𝕩. 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"   
+       β”˜      β”˜      β”˜  
+                       β”˜
 
+

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 𝕨 has length 0, then 𝕩 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 𝕩, unchanged.

+

Here's a more formal definition: 𝕩 is an array. 𝕨 is a number, or numeric list or unit, with 𝕨≀○≠≒𝕩. The result z has shape π•¨βˆΎΒ¬βŸœπ•¨βŒΎ((≠𝕨)βŠΈβ†‘)≒𝕩, and element iβŠ‘z is iβŠ‘z ←→ π•©βŠ‘Λœ+´¨(π•¨βˆΎβ—‹(β†•βˆ˜β‰ )≒𝕩)βŠ”i.

-- cgit v1.2.3