Grade: Difference between revisions

Jump to navigation Jump to search
108 bytes added ,  21:08, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
m (→‎Other: Fix duplicated link to Roger's post)
m (Text replacement - "<source" to "<syntaxhighlight")
Line 1: Line 1:
{{Built-ins|Grade|⍋|⍒}} is a pair of [[ambivalent]] [[primitive function|primitive functions]] related to [[wikipedia:sorting#sorting information or data|sorting]]. Instead of sorting the given array directly, Grade returns a [[permutation]] [[vector]] whose length equals the number of [[major cell|major cells]], so that [[indexing]] into the argument gives the sorted array. The [[glyph]] <source lang=apl inline>⍋</source> is called '''Grade Up''' and gives the ascending sort order, while <source lang=apl inline>⍒</source> is called '''Grade Down''' and gives the descending sort order.
{{Built-ins|Grade|⍋|⍒}} is a pair of [[ambivalent]] [[primitive function|primitive functions]] related to [[wikipedia:sorting#sorting information or data|sorting]]. Instead of sorting the given array directly, Grade returns a [[permutation]] [[vector]] whose length equals the number of [[major cell|major cells]], so that [[indexing]] into the argument gives the sorted array. The [[glyph]] <syntaxhighlight lang=apl inline>⍋</source> is called '''Grade Up''' and gives the ascending sort order, while <syntaxhighlight lang=apl inline>⍒</source> is called '''Grade Down''' and gives the descending sort order.


== Monadic form ==
== Monadic form ==
Line 5: Line 5:
[[Monadic]] Grade returns the sorting order of the major cells of the [[argument]]. [[Indexing]] into the original argument gives the sorted array; indexing into another array gives the result commonly known as '''Sort By'''.
[[Monadic]] Grade returns the sorting order of the major cells of the [[argument]]. [[Indexing]] into the original argument gives the sorted array; indexing into another array gives the result commonly known as '''Sort By'''.


<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←A←↑'foo' 'bar' 'baz'  ⍝ A character matrix
       ⎕←A←↑'foo' 'bar' 'baz'  ⍝ A character matrix
foo
foo
Line 31: Line 31:
Grade performs [[wikipedia:sorting algorithm#stability|stable sorting]], so that the indices of the repeated major cells are sorted by the order of appearance. Because of this, Grade Down produces the reverse of Grade Up only if all the major cells are unique.
Grade performs [[wikipedia:sorting algorithm#stability|stable sorting]], so that the indices of the repeated major cells are sorted by the order of appearance. Because of this, Grade Down produces the reverse of Grade Up only if all the major cells are unique.


<source lang=apl>
<syntaxhighlight lang=apl>
       S←6 10 10 3 2 15 ⋄ T←S,¨⍳6
       S←6 10 10 3 2 15 ⋄ T←S,¨⍳6
       ⍝ Note that the order of two 10s are preserved in both cases
       ⍝ Note that the order of two 10s are preserved in both cases
Line 50: Line 50:
[[Dyalog APL]] supports [[total array ordering]] when comparing [[nested array|nested arrays]] of possibly different [[shape]] and [[type]]. Grade is the only built-in function which can perform comparisons between arbitrary arrays of previously unknown order ([[Interval Index]] requires the left argument to be sorted beforehand).
[[Dyalog APL]] supports [[total array ordering]] when comparing [[nested array|nested arrays]] of possibly different [[shape]] and [[type]]. Grade is the only built-in function which can perform comparisons between arbitrary arrays of previously unknown order ([[Interval Index]] requires the left argument to be sorted beforehand).


<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←A←(1(⍪2 3)) (1 3) (⊂0 1 2)
       ⎕←A←(1(⍪2 3)) (1 3) (⊂0 1 2)
┌─────┬───┬───────┐
┌─────┬───┬───────┐
Line 66: Line 66:
[[Dyadic]] Grade is limited to [[simple]] [[character]] array arguments. The left argument specifies the collation order to use when sorting the right argument.
[[Dyadic]] Grade is limited to [[simple]] [[character]] array arguments. The left argument specifies the collation order to use when sorting the right argument.


If the left argument is a [[vector]], the sorting order is simply the order of appearance in the left argument. In other words, <source lang=apl inline>X⍋Y</source> equals <source lang=apl inline>⍋X⍳Y</source>, and same for <source lang=apl inline>⍒</source>.
If the left argument is a [[vector]], the sorting order is simply the order of appearance in the left argument. In other words, <syntaxhighlight lang=apl inline>X⍋Y</source> equals <syntaxhighlight lang=apl inline>⍋X⍳Y</source>, and same for <syntaxhighlight lang=apl inline>⍒</source>.


<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕A⍋'ZAM,.BIA'  ⍝ Alphabetical order, garbage goes last
       ⎕A⍋'ZAM,.BIA'  ⍝ Alphabetical order, garbage goes last
2 8 6 7 3 1 4 5
2 8 6 7 3 1 4 5
Line 77: Line 77:
If the left argument is a higher-[[rank]] array, sorting is done in multiple stages. The last [[axis]] of X takes highest precedence, then the next-to-last, and so on, giving ties to all characters that appear at the same index over the current axis. A notable application is case-insensitive sorting.
If the left argument is a higher-[[rank]] array, sorting is done in multiple stages. The last [[axis]] of X takes highest precedence, then the next-to-last, and so on, giving ties to all characters that appear at the same index over the current axis. A notable application is case-insensitive sorting.


<source lang=apl>
<syntaxhighlight lang=apl>
       Data
       Data
ABLE
ABLE
Line 99: Line 99:
</source>
</source>


[[J]] does not implement dyadic grade, but provides '''Sort By''' as the dyadic counterparts of <source lang=j inline>/:</source> and <source lang=j inline>\:</source> instead.
[[J]] does not implement dyadic grade, but provides '''Sort By''' as the dyadic counterparts of <syntaxhighlight lang=j inline>/:</source> and <syntaxhighlight lang=j inline>\:</source> instead.


== External links ==
== External links ==

Navigation menu