Function composition is an operation in mathematics that combines two or more functions by applying them successively. The output of one function becomes the input of the next. If (f:X\to Y) and (g:Y\to Z), their composition is the function (g\circ f:X\to Z), defined by ((g\circ f)(x)=g(f(x))). Thus (g\circ f), read “(g) composed with (f),” applies (f) first and (g) second. (courses.maths.ox.ac.uk)
Definition and notation
Composition depends on compatible input and output sets. In the definition above, the codomain of (f) is the domain of (g). More generally, ordinary evaluation of (g(f(x))) is possible whenever every value produced by (f) belongs to the domain of (g), even if the declared intermediate sets are not identical. The functions need not involve numbers: they may act on any sets. (courses.maths.ox.ac.uk)
For example, take functions on the real numbers defined by (f(x)=2x+1) and (g(x)=x^2). Direct substitution gives
[ (g\circ f)(x)=(2x+1)^2, \qquad (f\circ g)(x)=2x^2+1. ]
At (x=1), the first expression equals (9), whereas the second equals (3). This illustrates that changing the order can change the result. Composition is also distinct from pointwise multiplication, which would give (f(x)g(x)=(2x+1)x^2). (openstax.org)
Domains and restrictions
For functions given by formulas, the natural domain of a composite is
[ \operatorname{Dom}(g\circ f)
{x\in\operatorname{Dom}(f): f(x)\in\operatorname{Dom}(g)}. ]
The input must therefore satisfy two conditions: the inner function must be defined, and its output must be an admissible input for the outer function. This is not generally the intersection of the two original domains. (openstax.org)
Consider (f(x)=x-1) and (g(x)=\sqrt{x}), interpreted as real-valued functions. Then (g\circ f=\sqrt{x-1}) has domain ([1,\infty)). Reversing the order gives (f\circ g=\sqrt{x}-1), with domain ([0,\infty)). Simplifying a composite expression does not remove restrictions inherited from its original evaluation. For instance, composing (x\mapsto1/x) with itself gives the expression (x), but only for (x\ne0); the composite remains undefined at zero. (openstax.org)
Algebraic properties
Composition is associative. For compatible functions (f:X\to Y), (g:Y\to Z), and (h:Z\to W),
[ h\circ(g\circ f)=(h\circ g)\circ f. ]
Both sides send (x) to (h(g(f(x)))). Parentheses may therefore be omitted from a chain of compositions, although the order of its functions must remain unchanged. Associativity is different from commutativity: as the earlier example shows, (g\circ f) need not equal (f\circ g). In some cases, only one of these two orders is defined. (courses.maths.ox.ac.uk)
The identity function on a set (X), denoted (\operatorname{id}_X), sends every element to itself. For (f:X\to Y),
[ f\circ\operatorname{id}_X=f, \qquad \operatorname{id}_Y\circ f=f. ]
These equations identify the neutral functions for composition. (dummit.cos.northeastern.edu)
Composition also preserves important mapping properties. A composite of two injective functions is injective, and a composite of two surjective functions is surjective. Consequently, composing two bijective functions produces another bijection. Conversely, if (g\circ f) is injective, then (f) must be injective; if it is surjective, then (g) must be surjective. (math.colorado.edu)
For bijections, inverse functions reverse the order:
[ (g\circ f)^{-1}=f^{-1}\circ g^{-1}. ]
Recovering the original input requires undoing the last transformation first. (dummit.cos.northeastern.edu)
Linear maps and differentiation
In linear algebra, composing two linear maps yields another linear map. If (T:U\to V) has matrix (A), and (S:V\to W) has matrix (B), relative to compatible choices of bases, then
[ [S\circ T]=BA. ]
Matrix multiplication thus represents successive linear transformations. With column-vector notation, the rightmost matrix acts first, matching the conventional order of function composition. (math.mit.edu)
In calculus, the chain rule describes the derivative of a composite. If (f) is differentiable at (x) and (g) is differentiable at (f(x)), then
[ (g\circ f)'(x)=g'(f(x))f'(x). ]
The outer derivative is evaluated at the inner function’s output, not generally at the original input. For differentiable maps between finite-dimensional Euclidean spaces, the corresponding Jacobian matrices satisfy
[ J_{g\circ f}(x)=J_g(f(x))J_f(x). ]
This is the multivariable form of the same principle: the derivative of the composite is the composition of the derivatives at the appropriate points. (jirilebl.github.io)
Computing and neural networks
In computer science, composition expresses the construction of larger functions from smaller ones. In functional programming, a composition operator can combine functions without explicitly naming their shared argument. The programming language Haskell defines (f . g) x = f (g x), with type
(.) :: (b -> c) -> (a -> b) -> a -> c
The intermediate type b records the compatibility between the output of g and the input of f. (haskell.org)
A feedforward artificial neural network can likewise be represented as a composition of layer functions,
[ F=f_L\circ f_{L-1}\circ\cdots\circ f_1. ]
In deep learning, these successive transformations provide a mathematical description of how an input is mapped through multiple layers to an output. Nonlinear layer functions allow the composite to represent relationships beyond those expressible by a single linear transformation. (arxiv.org)