Function composition: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 1: | Line 1: | ||
'''Function composition''' refers to the "gluing" together of two [[function]]s using a [[dyadic operator]] such that the functions are applied to the [[argument]](s) as normal, but in a particular pattern specific to the used [[operator]]. The term [[wikipedia:function composition|function composition]] comes from [[traditional mathematics]] where it is used for a function <math>h(x)=f(g(x))</math> when written as <math> h(x) = (f \circ g)(x)</math>. APL generalises this idea to [[dyadic function]]s, allowing various patterns of application in addition to the simple application of one [[monadic function]] to the result of another monadic function. The | '''Function composition''' refers to the "gluing" together of two or more [[function]]s using a [[dyadic operator]] or a [[train]] such that the functions are applied to the [[argument]](s) as normal, but in a particular pattern specific to the used [[operator]]. The term [[wikipedia:function composition|function composition]] comes from [[traditional mathematics]] where it is used for a function <math>h(x)=f(g(x))</math> when written as <math> h(x) = (f \circ g)(x)</math>. APL generalises this idea to [[dyadic function]]s, allowing various patterns of application in addition to the simple application of one [[monadic function]] to the result of another monadic function. The patterns represented by the operators [[Atop (operator)|Atop]], [[Beside]], and [[Over]] can be visualised as follows: | ||
:[[File: | {| class=wikitable | ||
! Atop !! Beside !! Over | |||
|- | |||
|[[File:F⍤g.png|frameless|200px]]||[[File:F∘g.png|frameless|200px]]||[[File:F⍥g.png|frameless|200px]] | |||
|} | |||
The patterns represented by [[Tacit_programming#trains|trains]], the 2-train Atop and the 3-train Fork, can be visualised as follows: | |||
{| class=wikitable | |||
! 2-train !! 3-train | |||
|- | |||
|[[File:Fg.png|frameless|200px]]||[[File:Fgh.png|frameless|200px]] | |||
|} | |||
The <source lang=apl inline>∘</source> operator (in this context called [[Bind]]) and the 3-train can also be used with constant arrays, then treating the arrays (<source lang=apl inline>A</source>) as constant functions, much as if they were used as operands to the [[Constant]] operator (<source lang=apl inline>A⍨</source>): | |||
{| class=wikitable | |||
! Bind !! 3-train | |||
|- | |||
|[[File:A∘g;g∘A.png|frameless|200px]]||[[File:Agh.png|frameless|200px]] | |||
|} | |||
=== Summary of rules === | |||
These are the rules applied in [[Dyalog APL]]: | |||
<source lang=apl inline> ( g∘h) ⍵</source> {{←→}} <source lang=apl inline> g ( h ⍵)</source><br> | |||
<source lang=apl inline>⍺ ( g∘h) ⍵</source> {{←→}} <source lang=apl inline> ⍺ g ( h ⍵)</source><br> | |||
<source lang=apl inline> (A∘g ) ⍵</source> {{←→}} <source lang=apl inline> A g ⍵ </source><br> | |||
<source lang=apl inline> ( g∘A) ⍵</source> {{←→}} <source lang=apl inline> ⍵ g A </source><br> | |||
<source lang=apl inline> ( g⍤h) ⍵</source> {{←→}} <source lang=apl inline> g ( h ⍵)</source><br> | |||
<source lang=apl inline>⍺ ( g⍤h) ⍵</source> {{←→}} <source lang=apl inline> g (⍺ h ⍵)</source><br> | |||
<source lang=apl inline> ( g⍥h) ⍵</source> {{←→}} <source lang=apl inline> g ( h ⍵)</source><br> | |||
<source lang=apl inline>⍺ ( g⍥h) ⍵</source> {{←→}} <source lang=apl inline>( h ⍺) g ( h ⍵)</source><br> | |||
<source lang=apl inline> (f g h) ⍵</source> {{←→}} <source lang=apl inline>( f ⍵) g ( h ⍵)</source><br> | |||
<source lang=apl inline>⍺ (f g h) ⍵</source> {{←→}} <source lang=apl inline>(⍺ f ⍵) g (⍺ h ⍵)</source><br> | |||
<source lang=apl inline> (A g h) ⍵</source> {{←→}} <source lang=apl inline> A g ( h ⍵)</source><br> | |||
<source lang=apl inline>⍺ (A g h) ⍵</source> {{←→}} <source lang=apl inline> A g (⍺ h ⍵)</source><br> | |||
<source lang=apl inline> (g h) ⍵</source> {{←→}} <source lang=apl inline> g ( h ⍵)</source><br> | |||
<source lang=apl inline>⍺ (g h) ⍵</source> {{←→}} <source lang=apl inline> g (⍺ h ⍵)</source> | |||
== Additional compositions == | == Additional compositions == |