Search code examples
kdb

Relation between iasc and rank in q kdb


My understanding is that iasc and rank works internally in below order:
iasc: step by step procedure till rank

original: 2 7 3 2 5 / 0->2, 1->7, 2->3, 3->2, 4->5 //Index item mapping
asc original returns 2 2 3 5 7 / 0->2, 1->2, 2->3, 3->5, 4->7 // Index item mapping 
iasc original returns  0 3 2 4 1 / 0->0, 1->3, 2->2, 3->4, 5->1 // Index item mapping /// iasc using asc internally
asc iasc original returns 0 1 2 3 4 / 0->0, 1->1, 2->2, 3->3, 4->4 // Index item mapping
iasc iasc original returns 0 4 2 1 3 // hence it is equal to rank original

rank:

original: 2 7 3 2 5 / 0->2, 1->7, 2->3, 3->2, 4->5 //Index item mapping
asc original returns 2 2 3 5 7 / 0->2, 1->2, 2->3, 3->5, 4->7 // Index item mapping 
rank original -> 0 4 2 1 3 / rank of orignal items of list in sorted list /// rank using asc internally

But when I see the code of iasc and rank, both of them uses rank internally. Can you please tell me how iasc and rank works internally(is my understanding not correct)?


Solution

  • You are correct in saying iasc iasc is equivalent to using rank. If you look inside the function iasc and rank you can see that they are very similar:

    q)rank
    k){$[0h>@x;'`rank;<<x]}
    q)iasc
    k){$[0h>@x;'`rank;<x]}
    

    In this case the k code < will do the same job as iasc, so rank is essentially using iasc iasc internally, since << is the k code for iasc iasc.

    When you say that they both use rank internally, you may be referring to '`rank, which is used to throw an error if anything other than a list is used, since the function uses an if-else to determine if the input is a list.