Ascending Cardinal Numbers in APL

前端 未结 1 516
清歌不尽
清歌不尽 2021-01-15 18:22

In the FinnAPL Idiom Library, the 19th item is described as “Ascending cardinal numbers (ranking, all different) ,” and the code is as follows:

⍋⍋X


        
相关标签:
1条回答
  • 2021-01-15 18:50

    Ascending Cardinal Numbers

    For the sake of shorthand, I'll call that little code snippet “rank.” It becomes evident what is happening with rank when you start applying it to binary numbers. For example:

    X←0 0 1 0 1
    ⍋⍋X             ⍝ output is 1 2 4 3 5
    

    The output indicates the position of the values after sorting. You can see from the output that the two 1s will end up in the last two slots, 4 and 5, and the 0s will end up at positions 1, 2 and 3. Thus, it is assigning rank to each value of the vector. Compare that to grade up:

    X←7 8 9 6
    ⍋X              ⍝ output is 4 1 2 3
    ⍋⍋X             ⍝ output is 2 3 4 1
    

    You can think of grade up as this position gets that number and, you can think of rank as this number gets that position:

    7 8 9 6         ⍝ values of X
    
    4 1 2 3         ⍝ position 1 gets the number at 4 (6)
                    ⍝ position 2 gets the number at 1 (7) etc.
    2 3 4 1         ⍝ 1st number (7) gets the position 2
                    ⍝ 2nd number (8) gets the position 3 etc.
    

    It's interesting to note that grade up and rank are like two sides of the same coin in that you can alternate between the two. In other words, we have the following identities:

    ⍋X = ⍋⍋⍋X = ⍋⍋⍋⍋⍋X = ...
    ⍋⍋X = ⍋⍋⍋⍋X = ⍋⍋⍋⍋⍋⍋X = ...
    

    Why?

    So far that doesn't really answer Mr Peschi's question as to why it has this effect. If you think in terms of key-value pairs, the answer lies in the fact that the original keys are a set of ascending cardinal numbers: 1 2 3 4. After applying grade up, a new vector is created, whose values are the original keys rearranged as they would be after a sort: 4 1 2 3. Applying grade up a second time is about restoring the original keys to a sequence of ascending cardinal numbers again. However, the values of this third vector aren't the ascending cardinal numbers themselves. Rather they correspond to the keys of the second vector.

    It's kind of hard to understand since it's a reference to a reference, but the values of the third vector are referencing the orginal set of numbers as they occurred in their original positions:

    7 8 9 6
    2 3 4 1
    

    In the example, 2 is referencing 7 from 7's original position. Since the value 2 also corresponds to the key of the second vector, which in turn is the second position, the final message is that after the sort, 7 will be in position 2. 8 will be in position 3, 9 in 4 and 6 in the 1st position.

    Ranking and Shareable

    In the FinnAPL Idiom Library, the 2nd item is described as “Ascending cardinal numbers (ranking, shareable) ,” and the code is as follows:

    ⌊.5×(⍋⍋X)+⌽⍋⍋⌽X
    

    The output of this code is the same as its brother, ascending cardinal numbers (ranking, all different) as long as all the values of the input vector are different. However, the shareable version doesn't assign new values for those that are equal:

    X←0 0 1 0 1
    ⌊.5×(⍋⍋X)+⌽⍋⍋⌽X         ⍝ output is 2 2 4 2 4
    

    The values of the output should generally be interpreted as relative, i.e. The 2s have a relatively lower rank than the 4s, so they will appear first in the array.

    0 讨论(0)
提交回复
热议问题