aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2021-01-02 21:45:17 -0500
committerMarshall Lochbaum <mwlochbaum@gmail.com>2021-01-02 21:45:17 -0500
commit43459e8322548d742d116af5ee0527283fa540c5 (patch)
tree1860df0e059e67ee7b2464e584e6c6d4bd400cba /doc
parent2f01827355811e86350b4a0e433834163d07810a (diff)
Add documentation for Deshape and Reshape
Diffstat (limited to 'doc')
-rw-r--r--doc/README.md1
-rw-r--r--doc/leading.md2
-rw-r--r--doc/primitive.md2
-rw-r--r--doc/reshape.md100
4 files changed, 103 insertions, 2 deletions
diff --git a/doc/README.md b/doc/README.md
index 1dae6894..4b71be11 100644
--- a/doc/README.md
+++ b/doc/README.md
@@ -21,6 +21,7 @@ Concepts:
Primitives:
- [Array depth](depth.md) (`≡` and `⚇`)
+- [Deshape and Reshape](reshape.md) (`⥊`)
- [Group](group.md) (`⊔`)
- [Join](join.md) (`∾`)
- [Logical functions](logic.md) (`∧∨¬`)
diff --git a/doc/leading.md b/doc/leading.md
index 43024a3c..bbb58a4c 100644
--- a/doc/leading.md
+++ b/doc/leading.md
@@ -59,7 +59,7 @@ The Each (`¨`) and Table (`⌜`) modifiers return functions which are the same
## 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](couple.md) (`≍`), 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](couple.md) (`≍`), 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](reshape.md) (`⥊`) 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
diff --git a/doc/primitive.md b/doc/primitive.md
index 4b573db2..98ff2fa0 100644
--- a/doc/primitive.md
+++ b/doc/primitive.md
@@ -32,7 +32,7 @@ Functions that have significant differences from APL functions are marked with a
| `≢` | [Shape](https://aplwiki.com/wiki/Shape) | [Not Match](https://aplwiki.com/wiki/Not_Match)
| `⊣` | [Identity](https://aplwiki.com/wiki/Identity) | [Left](https://aplwiki.com/wiki/Identity)
| `⊢` | [Identity](https://aplwiki.com/wiki/Identity) | [Right](https://aplwiki.com/wiki/Identity)
-| `⥊` | [Deshape](https://aplwiki.com/wiki/Ravel) | [Reshape](https://aplwiki.com/wiki/Reshape)
+| `⥊` | [Deshape](reshape.md) | [Reshape](reshape.md)*
| `∾` | [Join](join.md)* | [Join to](https://aplwiki.com/wiki/Catenate)
| `≍` | [Solo](couple.md)* | [Couple](couple.md)*
| `↑` | [Prefixes](prefixes.md)* | [Take](https://aplwiki.com/wiki/Take)
diff --git a/doc/reshape.md b/doc/reshape.md
new file mode 100644
index 00000000..9f96fa26
--- /dev/null
+++ b/doc/reshape.md
@@ -0,0 +1,100 @@
+*View this file with results and syntax highlighting [here](https://mlochbaum.github.io/BQN/doc/reshape.html).*
+
+# Deshape and Reshape
+
+The glyph `⥊` indicates BQN's facilities to reflow the data in an array, giving it a different shape. Its monadic form, Deshape, simply removes all shape information, returning a list of all the elements from the array in reading order. With a left argument, `⥊` is called Reshape and is a more versatile tool for rearranging the data in an array into the desired shape.
+
+Because of its dependence on the reading order of an array, Reshape is less fundamental than other array operations. Using Reshape in the central computations of a program can be a sign of imperfect usage of arrays. For example, it may be useful to use Reshape to create a constant array or repeat a sequence of values several times, but the same task might also be accomplished more simply with Table `⌜`, or by taking advantage of leading axis agreement in arithmetic primitives.
+
+## Deshape
+
+The result of Deshape is a list containing the same elements as the argument.
+
+ ⊢ a ← +⌜´ ⟨100‿200, 30‿40, 5‿6‿7⟩
+
+ ⥊ a
+
+The elements are ordered in reading order—left to right, then top to bottom. This means that leading axes "matter more" for ordering: if one element comes earlier in the first axis but later in the second than some other element, it will come first in the result. In another view, elements are ordered according to their [indices](indices.md). In other words, deshaping the array of indices for an array will always give a sorted array.
+
+ ↕≢a
+
+ ⍋ ⥊ ↕≢a
+
+This ordering is also known as *row-major* order.
+
+Deshape turns a unit argument into a single-element list, automatically enclosing it if it's an atom. However, if you know the argument is a unit, a more principled way to turn it into a list is to apply [Solo](couple.md) (`≍`), which adds a length-1 axis before any other axes. If you ever add axes to the data format, Solo is more likely to continue working after this transition, unless there's a reason the result should always be a list.
+
+ ⥊ 2
+ ≍ 2
+
+## Reshape
+
+While Deshape removes all shape information from its argument array, Reshape adds shape information back based on the left argument. Reshape ignores the shape of its original argument, treating it like a list of elements as though it were deshaped initially.
+
+The left argument of Reshape gives the shape of the result, except that one entry can be left unspecified for BQN to fill in. We'll look at the cases where a full shape is given first.
+
+### Matching lengths
+
+If the number of elements implied by this shape—that is, `×´𝕨`—is equal to the number of elements in the argument, then the argument is simply rearranged to match that shape. The element list is kept the same, so that the deshaped result matches the deshaped argument.
+
+ a
+
+ 6‿2 ⥊ a
+
+ (⥊a) ≡ ⥊ 6‿2⥊a
+
+One common usage is to generate an array with a specified shape that counts up from 0 in reading order. The idiomatic phrase to do this is `⥊⟜(↕×´)`, since it doesn't require writing the shape and its product separately.
+
+ 2‿7 ⥊ ↕14
+ ⥊⟜(↕×´) 2‿7
+
+### Non-matching lengths
+
+If the left argument implies a smaller number of elements, then only the initial elements of the argument are used. Here the result stops at `237`, three-quarters of the way through `a`, because at that point the result is filled up.
+
+ 3‿3 ⥊ a
+
+If the left argument implies a larger number of elements, then the argument elements are reused cyclically. Below, we reach the last element `247` and start over at `135`. If the array doesn't have any elements to start with, its fill value is used instead, but it's probably best not to invoke this case!
+
+ 15 ⥊ a
+
+ 4 ⥊ ↕0 # Fill for ↕0 is 0
+
+Reshape is the idiomatic way to make an array filled with a constant value (that is, where all elements are the same). For an atom element, just reshape it directly; for an arbitrary element, first enclose it to create a unit, and then reshape it.
+
+ 3‿4 ⥊ 0
+
+ 5 ⥊ < "string"
+
+### Computed lengths
+
+What if you want to reshape an array into, say, rows of length 2, but don't want to have to write out the number of rows?
+
+ ∘‿2 ⥊ "aAeEiIoOuU"
+
+Above, the length given is `∘`, a special value that indicates that a length that fits the argument should be computed. In fact, BQN has four different special values that can be used. Every one works the same for a case like the above, where the rest of the shape divides the argument length evenly. They differ in how they handle uneven cases, where the required length would fall between two whole numbers.
+
+- `∘` says the length must be an exact fit, and gives an error in such a case.
+- `⌊` rounds the length down, so that some elements are discarded.
+- `⌽` rounds the length up, repeating elements to make up the difference.
+- `↑` rounds the length up, but uses the argument's fill values for the needed extra elements.
+
+These values are just BQN primitives of course. They're not called by Reshape or anything like that; the primitives are just chosen to suggest the corresponding functionality.
+
+Here's an example. If we try to turn five elements into two rows, `∘` gives an error, `⌊` drops the last element, `⌽` uses the first element again, and `↑` uses a fill element (like `5↑"abcde"` would).
+
+ 2‿∘ ⥊ "abcde"
+ 2‿⌊ ⥊ "abcde"
+ 2‿⌽ ⥊ "abcde"
+ 2‿↑ ⥊ "abcde"
+
+A computed length can be useful to input an array without using nested notation: for example, if you have a table with rows of three elements, you might write it as one long list, using `∘‿3⥊⟨…⟩` to get it back to the appropriate shape. `∘` is definitely the value to use here, as it will check that you haven't missed an element or something like that.
+
+Computed Reshape might also be used in actual data processing: for example, to sum a list in groups of four, you might first reshape it using `↑‿4` for the shape, then average the rows. Here the code `↑` is useful because added fill elements of `0` won't change the sum, so that if the last group doesn't have four elements (`9‿7` below), it will still be summed correctly.
+
+ +´˘ ↑‿4 ⥊ ⟨0,2,1,1, 5,9,6,4, 3,3,3,3, 9,7⟩
+
+Computed Reshape can even be used with structural Under. Only the `∘` case really makes sense, although `⌊`, which leaves trailing elements unchanged, could conceivably be useful. Below, we either split the argument into three groups and reverse their order or reverse it in groups of three.
+
+ ⌽⌾(3‿∘⊸⥊) ↕15
+ ⌽⌾(∘‿3⊸⥊) "nolyricshere"