From 2010e8b2897a5741e211980c9f8ec9177299c939 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 16 Jul 2021 18:23:52 -0400 Subject: Finish links and editing documentation pass --- docs/doc/shift.html | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'docs/doc/shift.html') diff --git a/docs/doc/shift.html b/docs/doc/shift.html index 0d162721..c4b142c6 100644 --- a/docs/doc/shift.html +++ b/docs/doc/shift.html @@ -5,8 +5,8 @@

Shift functions

-

The shift functions « and » are two new primitives added to BQN based on a pattern used heavily in the compiler and a reasonable amount everywhere else. Shifts resemble but are more general than the bit-based shift operations used in low-level languages. They replace the APL pattern of a 2-wise reduction after appending or prepending an element (APL's 2≠/0,v translates to »v), one of the more common uses of 2-wise reduction.

-

The result of a shift function always has the same shape as its right argument. The function adds major cells to the beginning (») or end («) of 𝕩, moving the cells already in 𝕩 to accomodate them. Some cells on the opposite side from those added will "fall off" and not be included in the result.

+

The shift functions « and » add major cells to one side an array, displacing cells on the opposite side and moving those in between. Shifts resemble but are more general than the bit-based shift operations used in low-level languages. They replace the APL pattern of a 2-wise reduction after appending or prepending an element (APL's 2≠/0,v translates to »v), one of the more common uses of 2-wise reduction.

+

The result of a shift function always has the same shape as 𝕩. The function adds major cells to the beginning (») or end («) of 𝕩, moving the cells already in 𝕩 to accomodate them. Some cells on the opposite side from those added will "fall off" and not be included in the result.

↗️
    00 » 321             # Shift Before
 ⟨ 0 0 3 ⟩
     "end" « "add to the "   # Shift After
@@ -20,7 +20,7 @@
 

If 𝕨 is longer than 𝕩, some cells from 𝕨 will be discarded, as well as all of 𝕩. In this case 𝕨»𝕩 is (𝕩)𝕨 and 𝕨«𝕩 is (-≠𝕩)𝕨. For similar reasons, nudging an array of length 0 returns it unchanged.

Sequence processing with shifts

-

When working with a sequence of data such as text, daily measurements, or audio data, shift functions are generally the best way to handle the concept of "next" or "previous" in the data. In the following example s is shown alongside the shifted-right data »s, and each element is compared to the previous with -», which we see is the inverse of +`.

+

When working with a sequence of data such as text, daily measurements, or audio data, shift functions are generally the best way to handle the concept of "next" or "previous". In the following example s is shown alongside the shifted-right data »s, and each element is compared to the previous with -», which we see is the inverse of Plus Scan +`.

↗️
    s  1224356
     s  »s
 ┌─               
@@ -34,8 +34,9 @@
 ⟨ 1 2 2 4 3 5 6 ⟩
 

In this way » refers to a sequence containing the previous element at each position. By default the array's fill is used for the element before the first, and a right argument can be given to provide a different one.

-↗️
     » s
+↗️
     » s
 ⟨ ∞ 1 2 2 4 3 5 ⟩
+
     » s
 ⟨ 1 1 2 2 4 3 5 ⟩
 
@@ -76,17 +77,19 @@ ⟨ 1 1 1 1 0 0 1 1 ⟩

Other examples

-

In Take (), there's no way to specify the fill element when the result is longer than the argument. To take along the first axis with a specified, constant fill value, you can use Shift Before instead, where the right argument is an array of fills with the desired final shape.

+

In Take (), there's no way to specify the fill element when the result is longer than the argument. To take along the first axis with a specified, constant fill value, you can use Shift Before instead, where the right argument is an array of fills with the desired final shape.

↗️
    "abc" » 5'F'
 "abcFF"
 
-

When using Scan (`), the result at a particular index is obtained from values up to and including the one at that index. But it's common to want to use the values up to but not including that one instead. This can be done either by joining or shifting in that value before scanning. The difference is that with Join the result is longer than the argument. Either form might be wanted depending on how it will be used.

-↗️
    +` 1111
-⟨ 1 2 3 4 ⟩
-    2 +` 1111
-⟨ 2 3 4 5 6 ⟩
-    2 +`» 1111
-⟨ 2 3 4 5 ⟩
+

When using Scan (`), the result at a particular index is obtained from values up to and including the one at that index. But it's common to want to use the values up to but not including that one instead. This can be done either by joining or shifting in that value before scanning. The difference is that with Join the result is longer than the argument. Either form might be wanted depending on how it will be used.

+↗️
    2 +` 1010    # Initial value not retained
+⟨ 3 3 4 4 ⟩
+
+    2 +` 1010  # All values
+⟨ 2 3 3 4 4 ⟩
+
+    2 +`» 1010  # Final value not created
+⟨ 2 3 3 4 ⟩
 

The strides of an array are the distances between one element and the next along any given axis. It's the product of all axis lengths below that axis, since these are all the axes that have to be "skipped" to jump along the axis. The strides of an array 𝕩 are (×`1»⊢)𝕩.

↗️
    (×`1»⊢) 5243
@@ -94,13 +97,14 @@
 

Higher rank

Shifting always works on the first axis of 𝕩 (which must have rank 1 or more), and shifts in major cells. A left argument can have rank equal to 𝕩, or one less than it, in which case it becomes a single cell of the result. With no left argument, a cell of fills 10𝕩 is nudged in.

-↗️
     a  (↕×´) 43
+↗️
     a  (↕×´) 43
 ┌─         
 ╵ 0  1  2  
   3  4  5  
   6  7  8  
   9 10 11  
           ┘
+
     » a                # Nudge adds a cell of fills
 ┌─       
 ╵ 0 0 0  
@@ -108,6 +112,7 @@
   3 4 5  
   6 7 8  
         ┘
+
     "one" « a          # Shift in a cell
 ┌─             
 ╵ 3   4   5    
@@ -115,6 +120,7 @@
   9   10  11   
   'o' 'n' 'e'  
               ┘
+
     ("two""cel") « a  # Shift in multiple cells
 ┌─             
 ╵ 6   7   8    
@@ -125,5 +131,5 @@
 

Definition

In any instance of » or «, 𝕩 must have rank at least 1.

-

For a dyadic shift function, 𝕨 must be Join-compatible with 𝕩 (that is, 𝕨𝕩 completes without error) and cannot have greater rank than 𝕩. Then Shift Before (») is {(𝕩)𝕨𝕩} and Shift After («) is {(-≠𝕩)𝕩𝕨}

+

For a dyadic shift function, 𝕨 must be Join-compatible with 𝕩 (that is, 𝕨𝕩 completes without error) and cannot have greater rank than 𝕩. Then Shift Before (») is {(𝕩)𝕨𝕩} and Shift After («) is {(-≠𝕩)𝕩𝕨}

When called monadically, the default argument is a cell of fills 10𝕩. That is, Nudge (») is (10↑⊢)» and Nudge Back («) is (10↑⊢)«. This default argument always satisfies the compatibility requirement above and so the only conditions for nudge are that 𝕩 has rank at least 1 and has a fill element.

-- cgit v1.2.3