Leading axis agreement: Difference between revisions
m (Intro ⟶ mention that in J, LAA is a special case of frame agreement; link) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
'''Leading axis agreement''', sometimes called '''prefix agreement''', is a [[conformability]] rule designed for [[leading axis theory]]. It states that a [[dyadic]] [[scalar function]] can be applied between two [[array]]s only if one of their [[shape]]s is a [[prefix]] of the other. The shape of the result is that of the [[argument]] with higher [[rank]]. | '''Leading axis agreement''', sometimes called '''prefix agreement''', is a [[conformability]] rule designed for [[leading axis theory]] and used by [[J]] and [[BQN]]. It states that a [[dyadic]] [[scalar function]] can be applied between two [[array]]s only if one of their [[shape]]s is a [[prefix]] of the other. The shape of the result is that of the [[argument]] with higher [[rank]]. In J, since all functions, including dyadic scalar functions, have [[function rank|rank]], leading axis agreement is a special case of [[frame agreement]]. | ||
== Examples == | == Examples == | ||
Line 7: | Line 7: | ||
A scalar dyadic function works when the two arrays have the same shape: | A scalar dyadic function works when the two arrays have the same shape: | ||
< | <syntaxhighlight lang=j> | ||
]x =: 2 3 $ 10 | ]x =: 2 3 $ 10 | ||
10 10 10 | 10 10 10 | ||
Line 17: | Line 17: | ||
10 11 12 | 10 11 12 | ||
13 14 15 | 13 14 15 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[J]]}} | {{Works in|[[J]]}} | ||
as well as when one is a [[scalar]]: | as well as when one is a [[scalar]]: | ||
< | <syntaxhighlight lang=j> | ||
]x =: 10 | ]x =: 10 | ||
10 | 10 | ||
Line 31: | Line 31: | ||
10 11 12 | 10 11 12 | ||
13 14 15 | 13 14 15 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[J]]}} | {{Works in|[[J]]}} | ||
The two cases above are already supported in other APLs in the form of [[scalar extension]]. J goes one step further, allowing the lower-rank array argument to have nonzero rank, as long as the leading dimensions match: | The two cases above are already supported in other APLs in the form of [[scalar extension]]. J goes one step further, allowing the lower-rank array argument to have nonzero rank, as long as the leading dimensions match: | ||
< | <syntaxhighlight lang=j> | ||
]x =: 10 20 | ]x =: 10 20 | ||
10 20 | 10 20 | ||
Line 45: | Line 45: | ||
10 11 12 | 10 11 12 | ||
23 24 25 | 23 24 25 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[J]]}} | {{Works in|[[J]]}} | ||
In this case, < | In this case, <syntaxhighlight lang=j inline>x</syntaxhighlight> has shape <syntaxhighlight lang=j inline>2</syntaxhighlight> and <syntaxhighlight lang=j inline>y</syntaxhighlight> has shape <syntaxhighlight lang=j inline>2 3</syntaxhighlight>. Since the leading axes agree and the rank difference is 1, each atom (or 0-[[cell]]) of <syntaxhighlight lang=j inline>x</syntaxhighlight> is matched with each row (or 1-cell) of <syntaxhighlight lang=j inline>y</syntaxhighlight>, and the two rows in the result are the results of <syntaxhighlight lang=j inline>10 + 0 1 2</syntaxhighlight> and <syntaxhighlight lang=j inline>20 + 3 4 5</syntaxhighlight>, respectively. | ||
== Model == | |||
In dialects that do not feature leading axis agreement, it can nevertheless be utilised by the introduction of an explicit operator: | |||
<syntaxhighlight lang=apl> | |||
_LA←{⍺ ⍺⍺⍤(-⍺⌊⍥(≢⍴)⍵)⊢⍵} | |||
⊢x ← 10 20 | |||
10 20 | |||
⊢y ← 2 3 ⍴ ⍳ 6 | |||
0 1 2 | |||
3 4 5 | |||
x +_LA y | |||
10 11 12 | |||
23 24 25 | |||
</syntaxhighlight> | |||
{{Works in|Dyalog APL}} | |||
== Aligning axes using the Rank operator == | == Aligning axes using the Rank operator == | ||
When using the [[Rank (operator)|Rank operator]] for dyadic functions as in < | When using the [[Rank (operator)|Rank operator]] for dyadic functions as in <syntaxhighlight lang=apl inline>X (f⍤m n) Y</syntaxhighlight>, the [[Frame|frames]] of <syntaxhighlight lang=apl inline>X</syntaxhighlight> and <syntaxhighlight lang=apl inline>Y</syntaxhighlight> are checked for conformability. Combined with leading axis agreement, the Rank operator can be used to align the [[axis|axes]] to be matched. | ||
< | <syntaxhighlight lang=j> | ||
NB. $x : 2|3 | NB. $x : 2|3 | ||
NB. $y : |3 2 | NB. $y : |3 2 | ||
Line 74: | Line 90: | ||
53 54 | 53 54 | ||
65 66 | 65 66 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[J]]}} | {{Works in|[[J]]}} | ||
[[Category:Leading axis theory]][[Category:Function characteristics]][[Category:Conformability]]{{APL features}} | [[Category:Leading axis theory]][[Category:Function characteristics]][[Category:Conformability]]{{APL features}} |
Latest revision as of 18:58, 19 July 2023
Leading axis agreement, sometimes called prefix agreement, is a conformability rule designed for leading axis theory and used by J and BQN. It states that a dyadic scalar function can be applied between two arrays only if one of their shapes is a prefix of the other. The shape of the result is that of the argument with higher rank. In J, since all functions, including dyadic scalar functions, have rank, leading axis agreement is a special case of frame agreement.
Examples
The following examples use J for demonstration purposes.
A scalar dyadic function works when the two arrays have the same shape:
]x =: 2 3 $ 10 10 10 10 10 10 10 ]y =: 2 3 $ i.6 0 1 2 3 4 5 x + y 10 11 12 13 14 15
as well as when one is a scalar:
]x =: 10 10 ]y =: 2 3 $ i.6 0 1 2 3 4 5 x + y 10 11 12 13 14 15
The two cases above are already supported in other APLs in the form of scalar extension. J goes one step further, allowing the lower-rank array argument to have nonzero rank, as long as the leading dimensions match:
]x =: 10 20 10 20 ]y =: 2 3 $ i.6 0 1 2 3 4 5 x + y 10 11 12 23 24 25
In this case, x
has shape 2
and y
has shape 2 3
. Since the leading axes agree and the rank difference is 1, each atom (or 0-cell) of x
is matched with each row (or 1-cell) of y
, and the two rows in the result are the results of 10 + 0 1 2
and 20 + 3 4 5
, respectively.
Model
In dialects that do not feature leading axis agreement, it can nevertheless be utilised by the introduction of an explicit operator:
_LA←{⍺ ⍺⍺⍤(-⍺⌊⍥(≢⍴)⍵)⊢⍵} ⊢x ← 10 20 10 20 ⊢y ← 2 3 ⍴ ⍳ 6 0 1 2 3 4 5 x +_LA y 10 11 12 23 24 25
Aligning axes using the Rank operator
When using the Rank operator for dyadic functions as in X (f⍤m n) Y
, the frames of X
and Y
are checked for conformability. Combined with leading axis agreement, the Rank operator can be used to align the axes to be matched.
NB. $x : 2|3 NB. $y : |3 2 NB. ------------------ NB. $x +"1 2 y : 2 3 2 ]x =: 2 3 $ 10 20 30 40 50 60 10 20 30 40 50 60 ]y =: 3 2 $ 1 2 3 4 5 6 1 2 3 4 5 6 x +"1 2 y 11 12 23 24 35 36 41 42 53 54 65 66
APL features [edit] | |
---|---|
Built-ins | Primitives (functions, operators) ∙ Quad name |
Array model | Shape ∙ Rank ∙ Depth ∙ Bound ∙ Index (Indexing) ∙ Axis ∙ Ravel ∙ Ravel order ∙ Element ∙ Scalar ∙ Vector ∙ Matrix ∙ Simple scalar ∙ Simple array ∙ Nested array ∙ Cell ∙ Major cell ∙ Subarray ∙ Empty array ∙ Prototype |
Data types | Number (Boolean, Complex number) ∙ Character (String) ∙ Box ∙ Namespace ∙ Function array |
Concepts and paradigms | Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity element ∙ Complex floor ∙ Array ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ Glyph ∙ Leading axis theory ∙ Major cell search ∙ First-class function |
Errors | LIMIT ERROR ∙ RANK ERROR ∙ SYNTAX ERROR ∙ DOMAIN ERROR ∙ LENGTH ERROR ∙ INDEX ERROR ∙ VALUE ERROR ∙ EVOLUTION ERROR |