Leading axis agreement: Difference between revisions

Jump to navigation Jump to search
m
Text replacement - "<source" to "<syntaxhighlight"
m (Text replacement - "</source>" to "</syntaxhighlight>")
m (Text replacement - "<source" to "<syntaxhighlight")
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:


<source lang=j>
<syntaxhighlight lang=j>
   ]x =: 2 3 $ 10
   ]x =: 2 3 $ 10
10 10 10
10 10 10
Line 22: Line 22:
as well as when one is a [[scalar]]:
as well as when one is a [[scalar]]:


<source lang=j>
<syntaxhighlight lang=j>
   ]x =: 10
   ]x =: 10
10
10
Line 36: Line 36:
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:


<source lang=j>
<syntaxhighlight lang=j>
   ]x =: 10 20
   ]x =: 10 20
10 20
10 20
Line 48: Line 48:
{{Works in|[[J]]}}
{{Works in|[[J]]}}


In this case, <source lang=j inline>x</syntaxhighlight> has shape <source lang=j inline>2</syntaxhighlight> and <source lang=j inline>y</syntaxhighlight> has shape <source lang=j inline>2 3</syntaxhighlight>. Since the leading axes agree and the rank difference is 1, each atom (or 0-[[cell]]) of <source lang=j inline>x</syntaxhighlight> is matched with each row (or 1-cell) of <source lang=j inline>y</syntaxhighlight>, and the two rows in the result are the results of <source lang=j inline>10 + 0 1 2</syntaxhighlight> and <source lang=j inline>20 + 3 4 5</syntaxhighlight>, respectively.
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 ==
== Model ==


In dialects that do not feature leading axis agreement, it can nevertheless be utilised by the introduction of an explicit operator:
In dialects that do not feature leading axis agreement, it can nevertheless be utilised by the introduction of an explicit operator:
<source lang=apl>
<syntaxhighlight lang=apl>
       _LA←{⍺ ⍺⍺⍤(-⍺⌊⍥(≢⍴)⍵)⊢⍵}
       _LA←{⍺ ⍺⍺⍤(-⍺⌊⍥(≢⍴)⍵)⊢⍵}
       ⊢x ← 10 20
       ⊢x ← 10 20
Line 68: Line 68:
== Aligning axes using the Rank operator ==
== Aligning axes using the Rank operator ==


When using the [[Rank (operator)|Rank operator]] for dyadic functions as in <source lang=apl inline>X (f⍤m n) Y</syntaxhighlight>, the [[Frame|frames]] of <source lang=apl inline>X</syntaxhighlight> and <source 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.
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.


<source lang=j>
<syntaxhighlight lang=j>
   NB. $x        : 2|3
   NB. $x        : 2|3
   NB. $y        :  |3 2
   NB. $y        :  |3 2

Navigation menu