From bda30287098ab9002bc4d3e8290ed06c21abec28 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sun, 16 Aug 2020 22:14:53 -0400 Subject: Add document on Solo, Couple, and Merge --- docs/doc/couple.html | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ docs/doc/index.html | 1 + docs/doc/join.html | 2 +- docs/doc/leading.html | 4 +-- docs/index.html | 6 ++-- 5 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 docs/doc/couple.html (limited to 'docs') diff --git a/docs/doc/couple.html b/docs/doc/couple.html new file mode 100644 index 00000000..4c65743c --- /dev/null +++ b/docs/doc/couple.html @@ -0,0 +1,84 @@ + + +

Couple and Merge

+

Solo/Couple () and Merge (>) are functions that create a higher-rank array from lower-rank components. Each takes some number of inner arrays organized in an outer structure, and creates a single array combining all elements of those inner arrays. For example, let's couple two arrays of shape 23:

+
     p  35×3
+┌─        
+╵ 0 3  6  
+  0 5 10  
+         ┘
+     q  23"abcdef"
+┌─     
+╵"abc  
+  def" 
+      ┘
+    p  q   # p coupled to q
+┌─             
+╎ 0   3   6    
+  0   5   10   
+               
+  'a' 'b' 'c'  
+  'd' 'e' 'f'  
+              ┘
+     p  q
+⟨ 2 2 3 ⟩
+
+

The result has two inner axes that are shared by p and q, preceded by an outer axis: length 2 because there are two arguments. Calling with no left argument does something simpler: because there is one argument, it just adds a length-1 axis to the front. The argument goes solo, becoming the only major cell of the result.

+
     q
+┌─     
+╎"abc  
+  def" 
+      ┘
+      q
+⟨ 1 2 3 ⟩
+
+

Merge (>) also takes one argument, but a nested one. Its argument is an array of arrays, each with the same shape. The shape of the result is then the outer shape followed by this shared inner shape.

+
     a  "AB""CD"  "rst""uvw""xyz"
+┌─                         
+╵ "ABrst" "ABuvw" "ABxyz"  
+  "CDrst" "CDuvw" "CDxyz"  
+                          ┘
+    > a
+┌─       
+╎"ABrst  
+  ABuvw  
+  ABxyz  
+         
+  CDrst  
+  CDuvw  
+  CDxyz" 
+        ┘
+     > a
+⟨ 2 3 5 ⟩
+
+

Merge is effectively a generalization of Solo and Couple, since Solo is {>𝕩} and Couple is {>𝕨,𝕩}. Since works on the "list" of arguments, it can only add one dimension, but > can take any number of dimensions as its input.

+

Merge and array theory

+

In all cases what these functions do is more like reinterpreting existing data than creating new information. In fact, if we ignore the shape and look at the ravels of the arrays involved in a call to Merge, we find that it just joins them together. Essentially, Merge is a request to ensure that the inner arrays (which, being independent elements, could be any sort of "ragged" array) can fit together in an array, and then to consider them to be such an array. For this reason, Merge (or a virtual analogue) is used to combine the result cells when calling a function with Rank into a single array.

+
     > a
+"ABrstABuvwABxyzCDrstCDuvwCDxyz"
+     ¨ a
+⟨ "ABrst" "ABuvw" "ABxyz" "CDrst" "CDuvw" "CDxyz" ⟩
+      ¨ a
+"ABrstABuvwABxyzCDrstCDuvwCDxyz"
+
+

The way this happens, and the constraint that all inner arrays have the same shape, is closely connected to the concept of an array, and like Table , Merge might be considered a fundamental way to build up multidimensional arrays from lists. In both cases scalars are somewhat special. They are the identity element of a function with Table, and can be produced by Merge inverse, > on a list, which forces either the outer or inner shape to be empty (BQN chooses > to be <, but only on an array, as > cannot produce non-arrays). Merge has another catch as well: it cannot produce arrays with a 0 in the shape, except at the end, without some sort of prototype system.

+
     e  ⟨⟩¨ 3
+⟨ ⟨⟩ ⟨⟩ ⟨⟩ ⟩
+     > e
+⟨ 3 0 ⟩
+     > > e
+⟨ 3 0 ⟩
+
+

Above we start with a list of three empty arrays. After merging once we get a shape 30 array, sure, but what happens next? The shape added by another merge is the shared shape of that array's elements—and there aren't any! If the nested list kept some type information around then we might know, but extra type information is essentially how lists pretend to be arrays. True dynamic lists simply can't represent multidimensional arrays with a 0 in the middle of the shape. In this sense, arrays are a richer model than nested lists.

+

Coupling scalars

+

A note on the topic of Solo and Couple applied to scalars. As always, one axis will be added, so that the result is a list (strangely, J's laminate differs from Couple in this one case, as it will add an axis to get a shape 21 result). For Solo, this is interchangeable with Deshape (), and either primitive might be chosen for stylistic reasons. For Couple, it is equivalent to Join-to (), but this is an irregular form of Join-to because it is the only case where Join-to adds an axis to both arguments instead of just one. Couple should be preferred in this case.

+

The pair function, which creates a list from its arguments, can be written Pair <, while in either valence is >Pair. As an interesting consequence, ←→ ><, and the same relationship holds for Pair.

+
    2,3 < "abc"  # Pair two values
+⟨ ⟨ 2 3 ⟩ "abc" ⟩
+    < "abc"        # Pair one(?) value
+⟨ "abc" ⟩
+
+

Definitions

+

As discussed above, is equivalent to >{𝕩;𝕨,𝕩}. To complete the picture we should describe Merge fully. Merge is defined on an array argument 𝕩 such that there's some shape s satisfying ´(s≡≢)¨𝕩. If 𝕩 is empty then any shape satisfies this expression; s should be chosen based on known type information for 𝕩 or otherwise assumed to be ⟨⟩. If s is empty then 𝕩 is allowed to contain non-arrays as well as array scalars, and these will be implicitly promoted to arrays by the indexing used later. We construct the result by combining the outer and inner axes of the argument with Table; since the outer axes come first they must correspond to the left argument and the inner axes must correspond to the right argument. 𝕩 is a natural choice of left argument, and because no concrete array can be used, the right argument will be s, the array of indices into any element of 𝕩. To get the appropriate element corresponding to a particular choice of index and element of 𝕩 we should select using that index. The result of Merge is 𝕩˜⌜s.

+

Given this definition we can also describe Rank () in terms of Each (¨) and the simpler monadic function Enclose-Rank <k. We assume effective ranks j for 𝕨 (if present) and k for 𝕩 have been computed. Then the correspondence is 𝕨Fk𝕩 ←→ >(<j𝕨)F¨(<k𝕩).

+ diff --git a/docs/doc/index.html b/docs/doc/index.html index 2ad426ba..d70ee9b8 100644 --- a/docs/doc/index.html +++ b/docs/doc/index.html @@ -16,6 +16,7 @@
  • Join ()
  • Logical functions (∧∨¬)
  • Prefixes and Suffixes (↑↓)
  • +
  • Solo, Couple, and Merge (≍>)
  • Transpose ()
  • Windows ()
  • diff --git a/docs/doc/join.html b/docs/doc/join.html index f6edc533..6b595caf 100644 --- a/docs/doc/join.html +++ b/docs/doc/join.html @@ -1,7 +1,7 @@

    Join

    -

    Join () is an extension of the monadic function Raze from A+ and J to arbitrary argument ranks. It has the same relationship to Join to, the dyadic function sharing the same glyph, as Merge (>) does to Couple (): ab is >ab and ab is ab. While Merge and Couple combine arrays (the elements of Merge's argument, or the arguments themselves for Couple) along a new leading axis, Join and Join to combine them along the existing leading axis. Both Merge and Join can also be called on a higher-rank array, causing Merge to add multiple leading axes while Join combines elements along multiple existing axes.

    +

    Join () is an extension of the monadic function Raze from A+ and J to arbitrary argument ranks. It has the same relationship to Join to, the dyadic function sharing the same glyph, as Merge (>) does to Couple (): ab is >ab and ab is ab. While Merge and Couple combine arrays (the elements of Merge's argument, or the arguments themselves for Couple) along a new leading axis, Join and Join to combine them along the existing leading axis. Both Merge and Join can also be called on a higher-rank array, causing Merge to add multiple leading axes while Join combines elements along multiple existing axes.

    Join can be used to combine several strings into a single string, like array.join() in Javascript (but it doesn't force the result to be a string).

        "time""to""join""some""words"
     "timetojoinsomewords"
    diff --git a/docs/doc/leading.html b/docs/doc/leading.html
    index 0301ddd4..e86c57f7 100644
    --- a/docs/doc/leading.html
    +++ b/docs/doc/leading.html
    @@ -66,7 +66,7 @@
       ef" 
          ┘
     
    -

    Solo (), something of a maverick, manages to act on zero leading axes of its argument by creating the first axis of the result instead. Because it doesn't need any axis to work, it can go in front of either axis but also past the last one by working with rank 0, a case where most array functions would give an error.

    +

    Solo (), something of a maverick, manages to act on zero leading axes of its argument by creating the first axis of the result instead. Because it doesn't need any axis to work, it can go in front of either axis but also past the last one by working with rank 0, a case where most array functions would give an error.

          a                 # Solo adds a length-1 axis
     ⟨ 1 3 2 ⟩
         a    a             # First Cell undoes this
    @@ -107,7 +107,7 @@
     

    The other two monadic functions that work on high-rank arguments are Deshape () and First (). These treat the argument as one long list, ordered by its element indices. This ordering privileges leading axes (in fact, it is the reason for the choice of leading axes in the leading axis convention), but these functions can't really be said to work on leading axes: they apply to all axes.

    The Each (¨) and Table () modifiers return functions which are the same in the monadic case. These functions simply go through all elements of the argument array without regard for its multi-dimensional structure (the operand is applied to elements in index order, matching Deshape; this matters if it has side effects). Similarly, monadic scalar functions do not have any sort of leading axis dependence.

    Dyadic functions

    -

    For dyadic functions the pattern of working on only one argument axis is not so common. Only two functions can be said to follow it roughly: Join to () combines two arrays along one axis, using the first axis of both arguments if they have the same rank and of the higher-rank argument if they differ by one. Couple (), like Solo, does not manipulate the argument axes but adds a result axis. There are also some functions that can't be limited to leading axes: Reshape () treats the argument as one long list, and Pick () requires each index to be as long as the right argument's rank, because it selects elements and not cells from the right argument.

    +

    For dyadic functions the pattern of working on only one argument axis is not so common. Only two functions can be said to follow it roughly: Join to () combines two arrays along one axis, using the first axis of both arguments if they have the same rank and of the higher-rank argument if they differ by one. Couple (), like Solo, does not manipulate the argument axes but adds a result axis. There are also some functions that can't be limited to leading axes: Reshape () treats the argument as one long list, and Pick () requires each index to be as long as the right argument's rank, because it selects elements and not cells from the right argument.

    Multiple axes

    Instead of always working on a single axis, many dyadic functions work on one axis by default, but also allow a left argument with multiple elements corresponding to leading axes of the right argument. To decide which of the two possibilities applies, these functions test the left argument depth, a convention that is discussed in the depth documentation. A left argument that applies to one axis has a particular depth; the argument can also be a list of such arguments.

    diff --git a/docs/index.html b/docs/index.html index 4a9fd7a0..8671db3e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -200,7 +200,7 @@ - + @@ -250,8 +250,8 @@ - - + + -- cgit v1.2.3
    >MergeMerge* Greater Than
    Solo*Couple*Solo*Couple*