Rank (operator): Difference between revisions

Jump to navigation Jump to search
1,021 bytes added ,  23:54, 20 July 2023
Mention differing semantics of rank in NARS2000.
m (Text replacement - "<source" to "<syntaxhighlight")
(Mention differing semantics of rank in NARS2000.)
 
(3 intermediate revisions by 2 users not shown)
Line 5: Line 5:
== Rank specification ==
== Rank specification ==
The right operand specifies the [[rank]] of subarrays to which the left operand function is applied as follows:
The right operand specifies the [[rank]] of subarrays to which the left operand function is applied as follows:
For left argument <syntaxhighlight lang=apl inline>⍺</source> and right argument <syntaxhighlight lang=apl inline>⍵</source>,
For left argument <syntaxhighlight lang=apl inline>⍺</syntaxhighlight> and right argument <syntaxhighlight lang=apl inline>⍵</syntaxhighlight>,
<syntaxhighlight lang=apl>
<syntaxhighlight lang=apl>
   ⍤    c  ⍝ Rank-c cells of ⍵ (monadic) or both arguments (dyadic)
   ⍤    c  ⍝ Rank-c cells of ⍵ (monadic) or both arguments (dyadic)
   ⍤  b c  ⍝ Rank-b cells of ⍺ and rank-c cells of ⍵ (dyadic)
   ⍤  b c  ⍝ Rank-b cells of ⍺ and rank-c cells of ⍵ (dyadic)
   ⍤a b c  ⍝ Rank-a cells of ⍵ (monadic), b-cells of ⍺ and c-cells of ⍵ (dyadic)
   ⍤a b c  ⍝ Rank-a cells of ⍵ (monadic), b-cells of ⍺ and c-cells of ⍵ (dyadic)
</source>
</syntaxhighlight>


A non-negative right operand specifies the number of ''final'' axes to which the function applies. A negative right operand specifies ''complementary'' rank, i.e. the number of leading axes to be ''excluded''. Negative rank can also be thought of as rank specification ''relative to'' the overall rank of the argument array.
A non-negative right operand specifies the number of ''final'' axes to which the function applies. A negative right operand specifies ''complementary'' rank, i.e. the number of leading axes to be ''excluded''. Negative rank can also be thought of as rank specification ''relative to'' the overall rank of the argument array.


Since a rank specification greater than the rank of the argument array means to apply the function to the whole array, <syntaxhighlight lang=apl inline>99</source>, <syntaxhighlight lang=apl inline>(⌊/⍬)</source> or <syntaxhighlight lang=apl inline>∞</source>, depending on the implementation, is "rank infinity" and always specifies the whole argument array.
Since a rank specification greater than the rank of the argument array means to apply the function to the whole array, <syntaxhighlight lang=apl inline>99</syntaxhighlight>, <syntaxhighlight lang=apl inline>(⌊/⍬)</syntaxhighlight> or <syntaxhighlight lang=apl inline>∞</syntaxhighlight>, depending on the implementation, is "rank infinity" and always specifies the whole argument array.


== Examples ==
== Examples ==
Line 28: Line 28:
UVWX
UVWX
QRST
QRST
</source>
</syntaxhighlight>


Laminate scalars from arrays of differing ranks:
Laminate scalars from arrays of differing ranks:
Line 42: Line 42:
C 7
C 7
D 8
D 8
</source>
</syntaxhighlight>


Flat outer product:
Flat outer product:
Line 58: Line 58:
  1  3
  1  3
  0  0
  0  0
</source>
</syntaxhighlight>


== History ==
== History ==
Line 64: Line 64:


== Rank vs Axis ==
== Rank vs Axis ==
Due to its ability to apply functions to specified subarrays, rank is frequently contrasted with [https://aplwiki.com/wiki/Function_axis bracket-axis]. It provides nearly all of the functionality of the anomalous axis operator (<syntaxhighlight lang=apl inline>f[a]</source>) without its draw-backs.<ref name="intro2rank">[[Robert Bernecky]]. [https://doi.org/10.1145/55626.55632 An introduction to function rank] at [[APL88]]. ACM SIGAPL APL Quote Quad, 18(2), pp.39-43. 1987.</ref>
Due to its ability to apply functions to specified subarrays, rank is frequently contrasted with [https://aplwiki.com/wiki/Function_axis bracket-axis]. It provides nearly all of the functionality of the anomalous axis operator (<syntaxhighlight lang=apl inline>f[a]</syntaxhighlight>) without its draw-backs.<ref name="intro2rank">[[Robert Bernecky]]. [https://doi.org/10.1145/55626.55632 An introduction to function rank] at [[APL88]]. ACM SIGAPL APL Quote Quad, 18(2), pp.39-43. 1987.</ref>


One of these draw-backs is that bracket-axis is specified ad hoc for each of the specific primitives on which it applies. Rank benefits from consistent behaviour when applied to any function, including [[user-defined functions]]. The ad hoc nature of bracket-axis definitions means that a generalised axis operator which works on any function, but behaves just as bracket-axis on those particular primitives, is impossible to formulate.
One of these draw-backs is that bracket-axis is specified ad hoc for each of the specific primitives on which it applies. Rank benefits from consistent behaviour when applied to any function, including [[user-defined functions]]. The ad hoc nature of bracket-axis definitions means that a generalised axis operator which works on any function, but behaves just as bracket-axis on those particular primitives, is impossible to formulate.
Line 75: Line 75:
+/[1+⍺-⍨≢⍴⍵] ≡ +⌿⍤⍺            ⍝ For scalar ⍺
+/[1+⍺-⍨≢⍴⍵] ≡ +⌿⍤⍺            ⍝ For scalar ⍺
+/[⍺]        ≡ +⌿⍤(1+⍺-⍨≢⍴⍵)  ⍝ For scalar ⍺
+/[⍺]        ≡ +⌿⍤(1+⍺-⍨≢⍴⍵)  ⍝ For scalar ⍺
</source>
</syntaxhighlight>


===Enclose axes===
===Enclose axes===
Line 86: Line 86:
}
}
⊂[⍺] ≡ ⍺∘EncloseAxes
⊂[⍺] ≡ ⍺∘EncloseAxes
</source>
</syntaxhighlight>


===Merge axes===
===Merge axes===
Line 94: Line 94:
<syntaxhighlight lang=apl>
<syntaxhighlight lang=apl>
,[(-⍺)↑⍴⍵] ≡ ,⍤⍺
,[(-⍺)↑⍴⍵] ≡ ,⍤⍺
</source>
</syntaxhighlight>


Merging leading axes is more involved, but can be expressed in one line.
Merging leading axes is more involved, but can be expressed in one line.
<syntaxhighlight lang=apl>
<syntaxhighlight lang=apl>
,[⍳⍺] ←→ {(1⌽⍳≢⍴z)⍉z←,⍤⍺((-⍺)⌽⍳≢⍴⍵)⍉⍵}
,[⍳⍺] ←→ {(1⌽⍳≢⍴z)⍉z←,⍤⍺((-⍺)⌽⍳≢⍴⍵)⍉⍵}
</source>
</syntaxhighlight>


The general treatment benefits from being expanded.
The general treatment benefits from being expanded.
Line 112: Line 112:


,[⍺] ←→ ⍺∘MergeAxes  ⍝ for ∧/1=¯2-/⍺
,[⍺] ←→ ⍺∘MergeAxes  ⍝ for ∧/1=¯2-/⍺
</source>
</syntaxhighlight>
 
==Difference among dialects==
Uniquely to [[NARS2000]], negative rank k specifies taking the <syntaxhighlight lang=apl inline>∣k</syntaxhighlight>-cells from the array that results from permuting the k leading axes to the trailing end of the shape; the shape of the cells of argument <syntaxhighlight lang=apl inline>a</syntaxhighlight> specified by rank k is <syntaxhighlight lang=apl inline>(-k)↑⍴a</syntaxhighlight> whether k is negative or positive. It also allows axis specification for the rank operator, denoting what axis the results will go to in the frame; <syntaxhighlight lang=apl inline>x f⍤[ax] l r⊢y</syntaxhighlight> is equivalent to <syntaxhighlight lang=apl inline>⊃[ax] x f⍤l r⊢y</syntaxhighlight> (noting that <syntaxhighlight lang=apl inline>⊃</syntaxhighlight> in NARS2000 is [[Mix]]).


== External links ==
== External links ==
Line 119: Line 122:
* [http://wiki.nars2000.org/index.php?title=Rank NARS2000]
* [http://wiki.nars2000.org/index.php?title=Rank NARS2000]
* [https://mlochbaum.github.io/BQN/doc/rank.html#rank BQN]
* [https://mlochbaum.github.io/BQN/doc/rank.html#rank BQN]
* [https://code.jsoftware.com/wiki/Vocabulary/quote NuVoc]


=== Publications ===
=== Publications ===
trusted
83

edits

Navigation menu