Does applying the Rank operator to a dyadic function preserve its argument order in Dyalog APL?
0 reputation · 15 Jul 2025, 20:46 UTC
0 reputation · 15 Jul 2025, 20:46 UTC
When using the Rank operator (⍤) to apply a dyadic primitive such as − to a specific axis of a higher‑dimensional array, the goal is to determine whether the function receives its left and right arguments in the same order as when the function is used without Rank.
APL’s right‑to‑left evaluation order and the way Rank reshapes the argument arrays raise uncertainty about whether the operator might swap the arguments or otherwise alter their binding.
Does the Rank operator preserve the original argument order for dyadic functions? If it does not, under what conditions does the order change? How can one verify the actual argument ordering in a Dyalog APL session?
31775 reputation · 16 Jul 2025, 00:19 UTC
Yes. Applying the Rank operator (⍤) to a dyadic function does not swap or reorder its arguments. The derived function receives the original left argument as its left argument (⍺) and the original right argument as its right argument (⍵), exactly as if the function had been called without Rank.
The confusion is understandable because Rank does reshape the data. Given a rank specification, ⍤ partitions each argument into subarrays (cells) of the requested rank and applies the operand function cell-by-cell, with the usual conformability rules between the left and right cell frames. What it does not do is alter the left/right binding: whatever array appears to the left of the derived function remains the left argument of every cell-wise invocation, and likewise for the right.
APL's right-to-left evaluation order is a separate concern. It governs how an expression is parsed, not how a dyadic function binds its two arguments. Rank sits on top of that unchanged binding.
This behavior is long-standing and stable across Dyalog APL versions (certainly from 14.0 onward), since argument order is fundamental to dyadic evaluation rather than something the Rank machinery reorders. If you are on a much older or non-Dyalog interpreter, verify locally before relying on this.
The clearest check is a non-commutative primitive like minus. Compare the plain call with the ranked call:
10 20 - 1 2
9 18
10 20 (-⍤0) 1 2
9 18Both give 9 18, i.e. left cell minus right cell. If Rank had swapped the arguments, the ranked result would be ¯9 ¯18.
For an explicit trace, use a dyadic dfn that exposes both arguments:
f←{⍺,'|'⍵}
(2 2⍴⍳4) (f⍤1) 2 2⍴'ABCD'
1 2|AB
3 4|CDEach row of the left argument appears before the separator, confirming the left argument is still ⍺ at every cell.
The only situations where results look "swapped" are ones you create yourself:
If your ranked expression produces values consistent with swapped operands, check first for a stray ⍨ or an unintended rank vector before suspecting the operator itself.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.