Function axis: Difference between revisions
Miraheze>Adám Brudzewsky 
m (Text replacement  "<source" to "<syntaxhighlight") 

(17 intermediate revisions by 5 users not shown)  
Line 1:  Line 1:  
{{BuiltinFunction axis[ax]}} is a special syntax which modifies the behavior of a function, for example <syntaxhighlight lang=apl inline>⌽[2]</syntaxhighlight> to [[Rotate]] along the second [[axis]]. Axis specification was a feature of [[Iverson notation]] and was ubiquitous in early APLs; many newer APLs which adhere to [[leading axis theory]] reject the use of axis specification in favor of the [[Rank operator]] because it is a fully general operator while the behavior of functions with axis must be defined for each function separately. However, [[GNU APL]] implements the syntax and extends it so it applies to all userdefined functions.  
== Functions ==  
=== Monadic functions ===  
The following [[Monadic functionmonads]] may allow an axis:  The following [[Monadic functionmonads]] may allow an axis:  
* [[Mix]] accepts a list of axes to specify where the axes of argument elements will be placed in the result.  * [[Mix]] accepts a list of axes to specify where the axes of argument elements will be placed in the result.  
Line 9:  Line 9:  
* [[Split]] accepts a single axis, and encloses each [[vector]] along that axis.  * [[Split]] accepts a single axis, and encloses each [[vector]] along that axis.  
* [[Reverse]] reverses along the specified axis.  * [[Reverse]] reverses along the specified axis.  
=== Dyadic functions ===  
The following [[Dyadic functiondyads]] may allow one:  The following [[Dyadic functiondyads]] may allow one:  
* [[Scalar  * [[Scalar functionScalar]] [[dyadic]]s accept a list of axes to override [[conformability]] rules: it specifies , for each axis in the lowerrank (or left, in case of a tie) argument, which axis in the other argument it is paired with.  
* [[Catenate]] combines along the selected axis, adding a new axis if a noninteger axis is given.  * [[Catenate]] combines along the selected axis, adding a new axis if a noninteger axis is given.  
* [[Rotate]] rotates the right argument along the selected axis.  * [[Rotate]] rotates the right argument along the selected axis.  
* [[Replicate]] and [[Expand]] work on the specified right argument axis.  * [[Replicate]] and [[Expand]] work on the specified right argument axis.  
* [[Take]] and [[Drop]] modify the selected right argument axes.  * [[Take]] and [[Drop]] modify the selected right argument axes.  
* [[  * [[Squad indexing]] takes axes to specify which axis of the right argument corresponds to each left argument element.  
* [[Partition]] and [[Partitioned Enclose]] have complicated and different behavior.  * [[Partition]] and [[Partitioned Enclose]] have complicated and different behavior.  
== Operators ==  
The following [[operator]]s may admit axis specification:  
* [[Reduction]] removes the specified right argument axis.  
* [[Scan]] works on the specified right argument axis.  
In [[SHARP APL]], [[Replicate]] and [[Expand]] are also included in this category as they are operators and not functions.  
Axis specification always modifies the [[derived function]] obtained from an operator, and not the operator itself. The exact syntax may vary: in most cases a set of brackets is parsed like a monadic operator and applies directly to the derived function; sometimes brackets can be applied directly to an operator, in which case the derived function produced by this operator is modified. In [[Dyalog APL]], a slash with axis retains its [[functionoperator overloading]]: it can be applied as an operator or as a dyadic function ([[Replicate]] or [[Expand]]).  
== Generalisation ==  
GNU APL generalises function axis to all functions, and beyond cases that refer to one or more argument axes. The axis works like an additional argument, which the function can use in any way. For example, <syntaxhighlight lang=apl inline>⌹[ax] M</syntaxhighlight> computes the [[wikipedia:QR factorizationQR factorization]] of <syntaxhighlight lang=apl inline>M</syntaxhighlight> with the [[comparison tolerance]] of <syntaxhighlight lang=apl inline>ax</syntaxhighlight>. Here, <syntaxhighlight lang=apl inline>ax</syntaxhighlight> is not an axis at all, and there's no direct connection between this monadic usage of Domino and its nonaxis meaning of [[Matrix inverse]]. Thus there is no value for <syntaxhighlight lang=apl inline>ax</syntaxhighlight> such that <syntaxhighlight lang=apl inline>f[ax]</syntaxhighlight> is the same as <syntaxhighlight lang=apl inline>f</syntaxhighlight>, which is usually the case for existing axis definitions (not scalar functions). Much like the left argument of [[Circular]] (<syntaxhighlight lang=apl inline>○</syntaxhighlight>), The GNU APL specific File Input Output [[system function]] (<syntaxhighlight lang=apl inline>⎕FIO</syntaxhighlight>) uses the bracket axis syntax as a function selector, as does the [[wikipedia:SQLSQL]] interface function (<syntaxhighlight lang=apl inline>⎕SQL</syntaxhighlight>). Some of the options for <syntaxhighlight lang=apl inline>⎕SQL</syntaxhighlight> take additional parameters as subsequent elements of the array inside the brackets. Similarly, the [[wikipedia:regular expressionregular expression]] interface (<syntaxhighlight lang=apl inline>⎕RE</syntaxhighlight>) uses [[character]] "axes" as flags for the regular expression engine, where [[Dyalog APL]] uses a [[dyadic operator]], [[Variant]] (<syntaxhighlight lang=apl inline>⍠</syntaxhighlight>), for analogous functionality.  
GNU APL additionally gives userdefined functions, both [[dfn]]s and [[tradfn]]s, access to the bracket axis syntax, again while permitting its use for any purpose. For example, the [[root]] function could be implemented as a monadic function with axis specifying the degree, rather than as a dyadic function. In a dfn, the value given via axis notation is denoted <syntaxhighlight lang=apl inline>χ</syntaxhighlight>:  
<syntaxhighlight lang=apl>  
Root←{⍵*÷χ}  
Root[3]8  
2  
</syntaxhighlight>  
{{Works in[[GNU APL]]}}  
Or as a tradfn, indicating axis notation in the header line:  
<syntaxhighlight lang=apl>  
∇ r←Root[ax] y  
r←y*÷ax  
∇  
</syntaxhighlight>  
{{Works in[[GNU APL]]}}  
== External links ==  
=== Documentation ===  
* Dyalog: [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Operators/Operators%20Summarised.htm overview], [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Operators/Axis%20with%20Monadic%20Operand.htm monadic], [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Operators/Axis%20with%20Dyadic%20Operand.htm dyadic]  
* [http://microapl.com/apl_help/ch_020_010_020.htm APLX]  
* [https://www.gnu.org/software/apl/apl.html#Section2_002e2 GNU APL]  
=== Lesson ===  
* [https://chat.stackexchange.com/rooms/52405/conversation/lesson3someaploperators#message40898783 APL Cultivation]  
{{APL syntax}}[[Category:Function characteristics]] 
Revision as of 22:20, 10 September 2022
[ax]

Function axis ([ax]
) is a special syntax which modifies the behavior of a function, for example ⌽[2]
to Rotate along the second axis. Axis specification was a feature of Iverson notation and was ubiquitous in early APLs; many newer APLs which adhere to leading axis theory reject the use of axis specification in favor of the Rank operator because it is a fully general operator while the behavior of functions with axis must be defined for each function separately. However, GNU APL implements the syntax and extends it so it applies to all userdefined functions.
Functions
Monadic functions
The following monads may allow an axis:
 Mix accepts a list of axes to specify where the axes of argument elements will be placed in the result.
 Ravel accepts a list of axes which are combined, or a single fractional number to add a length1 axis.
 Enclose accepts a list of axes. Each subarray along these axes is enclosed.
 Split accepts a single axis, and encloses each vector along that axis.
 Reverse reverses along the specified axis.
Dyadic functions
The following dyads may allow one:
 Scalar dyadics accept a list of axes to override conformability rules: it specifies , for each axis in the lowerrank (or left, in case of a tie) argument, which axis in the other argument it is paired with.
 Catenate combines along the selected axis, adding a new axis if a noninteger axis is given.
 Rotate rotates the right argument along the selected axis.
 Replicate and Expand work on the specified right argument axis.
 Take and Drop modify the selected right argument axes.
 Squad indexing takes axes to specify which axis of the right argument corresponds to each left argument element.
 Partition and Partitioned Enclose have complicated and different behavior.
Operators
The following operators may admit axis specification:
 Reduction removes the specified right argument axis.
 Scan works on the specified right argument axis.
In SHARP APL, Replicate and Expand are also included in this category as they are operators and not functions.
Axis specification always modifies the derived function obtained from an operator, and not the operator itself. The exact syntax may vary: in most cases a set of brackets is parsed like a monadic operator and applies directly to the derived function; sometimes brackets can be applied directly to an operator, in which case the derived function produced by this operator is modified. In Dyalog APL, a slash with axis retains its functionoperator overloading: it can be applied as an operator or as a dyadic function (Replicate or Expand).
Generalisation
GNU APL generalises function axis to all functions, and beyond cases that refer to one or more argument axes. The axis works like an additional argument, which the function can use in any way. For example, ⌹[ax] M
computes the QR factorization of M
with the comparison tolerance of ax
. Here, ax
is not an axis at all, and there's no direct connection between this monadic usage of Domino and its nonaxis meaning of Matrix inverse. Thus there is no value for ax
such that f[ax]
is the same as f
, which is usually the case for existing axis definitions (not scalar functions). Much like the left argument of Circular (○
), The GNU APL specific File Input Output system function (⎕FIO
) uses the bracket axis syntax as a function selector, as does the SQL interface function (⎕SQL
). Some of the options for ⎕SQL
take additional parameters as subsequent elements of the array inside the brackets. Similarly, the regular expression interface (⎕RE
) uses character "axes" as flags for the regular expression engine, where Dyalog APL uses a dyadic operator, Variant (⍠
), for analogous functionality.
GNU APL additionally gives userdefined functions, both dfns and tradfns, access to the bracket axis syntax, again while permitting its use for any purpose. For example, the root function could be implemented as a monadic function with axis specifying the degree, rather than as a dyadic function. In a dfn, the value given via axis notation is denoted χ
:
Root←{⍵*÷χ}
Root[3]8
2
Or as a tradfn, indicating axis notation in the header line:
∇ r←Root[ax] y
r←y*÷ax
∇