From 2afb23928e1984d475cc460e1672e8f6fa0e4dbe Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Wed, 11 Aug 2021 17:21:31 -0400 Subject: Allow clicking on header to get fragment link --- docs/doc/order.html | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'docs/doc/order.html') diff --git a/docs/doc/order.html b/docs/doc/order.html index 1737ebd1..bd13f64a 100644 --- a/docs/doc/order.html +++ b/docs/doc/order.html @@ -4,7 +4,7 @@ BQN: Ordering functions -

Ordering functions

+

Ordering functions

BQN has six functions that order arrays as part of their operation (the comparison functions ≤<>≥ only order atoms, so they aren't included). These come in three pairs, where one of each pair uses an ascending ordering and the other uses a descending ordering.

The array ordering shared by all six is described last. For lists it's "dictionary ordering": two lists are compared one element at a time until one runs out, and the shorter one comes first in case of a tie. Operation values aren't ordered, so if an argument to an ordering function has a function or modifier somewhere in it then it will fail unless all the orderings can be decided without checking that value.

You can't provide a custom ordering function to Sort. The function would have to be called on one pair of cells at a time, which is contrary to the idea of array programming, and passing in a function with side effects could lead to implementation-specific behavior. Instead, build another array that will sort in the order you want (for example, by selecting or deriving the property you want to sort on). Then Grade it, and use the result to select from the original array.

-

Sort

+

Sort

You've probably seen it before. Sort Up () reorders the major cells of its argument to place them in ascending order, and Sort Down () puts them in descending order. Every ordering function follows this naming convention—there's an "Up" version pointing up and a "Down" version going the other way.

↗️
     "delta""alpha""beta""gamma"
 ⟨ "alpha" "beta" "delta" "gamma" ⟩
@@ -22,7 +22,7 @@
 "δγβα"
 

Sort Down always matches Sort Up reversed, . The reason for this is that BQN's array ordering is a total order, meaning that if one array doesn't come earlier or later that another array in the ordering then the two arrays match. Since any two non-matching argument cells are strictly ordered, they will have one ordering in and the opposite ordering in . With the reverse, any pair of non-matching cells are ordered the same way in and . Since these two results have the same major cells in the same order, they match. However, note that the results will not always behave identically because Match doesn't take fill elements into account (if you're curious, take a look at ¨0,"" versus ¨0,"").

-

Grade

+

Grade

See the APL Wiki page for a few more examples. BQN only has the monadic form.

Grade is more abstract than Sort. Rather than rearranging the argument's cells immediately, it returns a list of indices (more precisely, a permutation) giving the ordering that would sort them.

↗️
     l  "planet""moon""star""asteroid"
@@ -38,7 +38,7 @@
 ↗️
    (l)  l
 ⟨ "asteroid" "moon" "planet" "star" ⟩
 
-

Ordinals

+

Ordinals

So the elements of the Grade of an array correspond to the cells of that array after it's sorted. It's tempting if you don't have the sorted list handy to try to match them up with major cells of the original array, but this never makes sense—there's no relationship. However, applying Grade twice gives us a list that does correspond to the original argument quite usefully: it says, for each major cell of that argument, what rank it has relative to the others (smallest is 0, next is 1, and so on, breaking ties in favor of which cell comes earlier in the argument). Experienced APL programmers call this pattern the "ordinals" idiom.

↗️
    l  ⍋⍋ l
 ┌─                                   
@@ -49,7 +49,7 @@
 

How does it work? First, let's note that l is a permutation: it contains exactly the numbers ↕≠l, possibly in a different order. In other words, ∧⍋l is ↕≠l. Permuting an array rearranges the cells but doesn't remove or duplicate any. This implies it's always invertible: given a permutation p, some other permutation q will have 𝕩 qp𝕩 for every 𝕩 of the right length. This would mean that while l transforms l to l, the inverse of l transforms l back into l. That's what we want: for each cell of l, the corresponding number in the inverse of l is what index that cell has after sorting.

But what's the inverse q of a permutation p? Our requirement is that 𝕩 qp𝕩 for any 𝕩 with the same length as p. Setting 𝕩 to ↕≠p (the identity permutation), we have (↕≠p) qp, because p⊏↕≠p is just p. But if p is a permutation then p is ↕≠p, so our requirement could also be written (p) qp. Now it's all coming back around again. We know exactly how to get q! Defining qp, we have qp (p)p p ↕≠p, and qp𝕩 (qp)𝕩 (↕≠p)𝕩 𝕩.

The fact that Grade Up inverts a permutation is useful in itself. Note that this applies to Grade Up specifically, and not Grade Down. This is because the identity permutation is ordered in ascending order. Grade Down would actually invert the reverse of a permutation, which is unlikely to be useful. So the ordinals idiom that goes in the opposite direction is actually not ⍒⍒ but ⍋⍒. The initial grade is different, but the way to invert it is the same.

-

Stability

+

Stability

When sorting an array, we usually don't care how matching cells are ordered relative to each other (although it's possible to detect it by using fill elements carefully. They maintain their ordering). Grading is a different matter, because often the grade of one array is used to order another one.

↗️
     t  > "dog"4, "ant"6, "pigeon"2, "pig"4 
 ┌─            
@@ -83,7 +83,7 @@
 ↗️
    (⌽⍒/345)  "012abcdABCDE"
 "210dcbaEDCBA"
 
-

Bins

+

Bins

There's also an APL Wiki page on this function, but be careful as the Dyalog version has subtle differences.

The two Bins functions are written with the same symbols and as Grade, but take two arguments instead of one. More complicated? A little, but once you understand Bins you'll find that it's a basic concept that shows up in the real world all the time.

Bins behaves like a search function with respect to rank: it looks up cells from 𝕩 relative to major cells of 𝕨. However, there's an extra requirement: the left argument to Bins is already sorted according to whichever ordering is used. If it isn't, you'll get an error.

@@ -100,7 +100,7 @@ ERROR ⟨ 3 5 0 1 ⟩

A score of 565e7 sits between 578e7 and 553e7 at rank 3, 322e7 wouldn't make the list, 788e7 would beat everyone, and 627e7 would tie the high score but not beat it. The same principles apply to less spring-loaded things like character indices and line numbers (𝕨 is the index of the start of each line), or percentage scores and letter grades on a test (𝕨 is the minimum score possible for each grade). In each case, it's better to think of Bins not as a counting exercise but as finding "what bin" something fits into.

-

Array ordering

+

Array ordering

Most of the time you won't need to worry about the details of how BQN arrays are ordered. It's documented here because, well, that's what documentation does.

The array ordering defines some arrays to be smaller or larger than others. All of the "Up" ordering functions use this ordering directly, so that smaller arrays come earlier, and the "Down" ones use the opposite ordering, with larger arrays coming earlier. For arrays consisting only of characters and numbers, with arbitrary nesting, the ordering is always defined. If an array contains an operation, trying to order it relative to another array might give an error. If comparing two arrays succeeds, there are three possibilities: the first array is smaller, the second is smaller, or the two arrays match.

Comparing two atoms is defined to work the same way as the comparison functions ≤<>≥. Numbers come earlier than characters and otherwise these two types are ordered in the obvious way. To compare an atom to an array, the atom enclosing and then compared with the array ordering defined below. The result of this comparison is used except when the two arrays match: in that case, the atom is considered smaller.

-- cgit v1.2.3