From f469c6f9bd4c9cf3c2b8ce93c3f2331cdcdd589a Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 10 Jun 2022 22:42:44 -0400 Subject: More editing --- docs/doc/shift.html | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'docs/doc/shift.html') diff --git a/docs/doc/shift.html b/docs/doc/shift.html index 5b72e865..8e8b3640 100644 --- a/docs/doc/shift.html +++ b/docs/doc/shift.html @@ -5,23 +5,25 @@

Shift functions

-

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 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); a nice version of this common pattern is one reason BQN is free to replace windowed reduction with the sometimes less convenient Windows.

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
+↗️
    00 » 321             # Shift Before
 ⟨ 0 0 3 ⟩
+
     "end" « "add to the "   # Shift After
 " to the end"
 

The cells to add come from 𝕨 if it's present, as shown above. Otherwise, a single cell of fill elements for 𝕩 is used. This kind of shift, which moves cells in 𝕩 over by just one, is called a "nudge".

-↗️
    » "abcd"   # Nudge
+↗️
    » "abcd"   # Nudge
 " abc"
+
     « 123    # Nudge Back
 ⟨ 2 3 0 ⟩
 
-

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.

+

If 𝕨 is longer than 𝕩, some cells from 𝕨 will be discarded, plus 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 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  1224356
     s  »s
 ┌─               
 ╵ 1 2 2 4 3 5 6  
@@ -30,8 +32,8 @@
     -» s
 ⟨ 1 1 0 2 ¯1 2 1 ⟩
 
-    +` -» s
-⟨ 1 2 2 4 3 5 6 ⟩
+    +` -» s   # Same as s
+⟨ 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
@@ -59,7 +61,7 @@
 

A feature these examples all share is that they maintain the length of s. This is a common condition in sequence processing, particularly when the processed sequence needs to be combined or compared with the original in some way. However, it's not always the case. In some instances, for example when searching s to see if there is any value less than the previous, the list should get shorter during processing. In these cases, Windows () is usually a better choice.

Arithmetic and logical shifts

-

The glyphs « and », suggesting movement, were chosen for the same reasons as the digraphs << and >>, and can be used to implement the same operations on boolean lists.

+

The glyphs « and », suggesting movement, were chosen for the same reasons as the digraphs << and >> in C-like languages, and can be used to implement the same bit-shift operations on boolean lists.

↗️
     i  "10011011"-'0'
 ⟨ 1 0 0 1 1 0 1 1 ⟩
 
@@ -77,7 +79,7 @@
 ⟨ 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 (a more general approach is Under).

↗️
    "abc" » 5'F'
 "abcFF"
 
@@ -91,12 +93,12 @@ 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»⊢)𝕩.

+

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
 ⟨ 24 12 3 1 ⟩
 

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.

+

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
 ┌─         
 ╵ 0  1  2  
-- 
cgit v1.2.3