From dc9cdd8a54e0057524c5ac69a21c52969a8e8cda Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Mon, 18 Apr 2022 10:52:26 -0400 Subject: Diagrams for Grade and Ordinals --- docs/doc/order.html | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 2 deletions(-) (limited to 'docs/doc') diff --git a/docs/doc/order.html b/docs/doc/order.html index 1916167d..3d5047ae 100644 --- a/docs/doc/order.html +++ b/docs/doc/order.html @@ -23,7 +23,51 @@

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

-

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

+ + + + + + + + + + + + 's' + 'o' + 'r' + 't' + + + 1 + 2 + 0 + 3 + + + 'o' + 'r' + 's' + 't' + + + + + 0 + 1 + 2 + 3 + + + + 𝕩 + 𝕩 + 𝕩 + + + +

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"
 ⟨ "planet" "moon" "star" "asteroid" ⟩
@@ -39,6 +83,67 @@
 ⟨ "asteroid" "moon" "planet" "star" ⟩
 

Ordinals

+ + + + + + + + + + + + + + + + 's' + 'o' + 'r' + 't' + + + 1 + 2 + 0 + 3 + + + 2 + 0 + 1 + 3 + + + + + 0 + 1 + 2 + 3 + + + 0 + 1 + 2 + 3 + + + 0 + 1 + 2 + 3 + + + + 𝕩 + 𝕩 + ⍋⍋𝕩 + + + +

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
 ┌─                                   
@@ -87,8 +192,9 @@
 

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.

-↗️
    56241  3
+↗️
    56241  3
 Error: ⍋: 𝕨 must be sorted
+
     03479  3
 Error: ⍒: 𝕨 must be sorted in descending order
 
-- cgit v1.2.3