# Function composition

In mathematics, the composition operator $\circ$ takes two functions, $f$ and $g$, and returns a new function $h(x):=(g\circ f)(x)=g(f(x))$. Thus, the function g is applied after applying f to x. $(g\circ f)$ is pronounced "the composition of g and f".
Reverse composition, sometimes denoted $f\mapsto g$ , applies the operation in the opposite order, applying $f$ first and $g$ second. Intuitively, reverse composition is a chaining process in which the output of function f feeds the input of function g.
The composition of functions is a special case of the composition of relations, sometimes also denoted by $\circ$. As a result, all properties of composition of relations are true of composition of functions, such as associativity.

## Examples

* Composition of functions on a finite set: If f = {(1, 1), (2, 3), (3, 1), (4, 2)}, and g = {(1, 2), (2, 3), (3, 1), (4, 2)}, then g ∘ f = {(1, 2), (2, 1), (3, 2), (4, 3)}, as shown in the figure.
* Composition of functions on an infinite set: If f: R → R (where R is the set of all real numbers) is given by f(x) = 2x + 4 and g: R → R is given by g(x) = x$^3$, then: (f ∘ g)(x) = f(g(x)) = f(x$^3$) = 2x$^3$ + 4, and (g ∘ f)(x) = g(f(x)) = g(2x + 4) = (2x + 4)$^3$.
* If an airplane's altitude at time t is a(t), and the air pressure at altitude x is p(x), then (p ∘ a)(t) is the pressure around the plane at time t.
* Function defined on finite sets which change the order of their elements such as permutations can be composed on the same set, this being composition of permutations.

## Properties

The composition of functions is always associative—a property inherited from the composition of relations. That is, if f, g, and h are composable, then f ∘ (g ∘ h) = (f ∘ g) ∘ h. Since the parentheses do not change the result, they are generally omitted.
In a strict sense, the composition g ∘ f is only meaningful if the codomain of f equals the domain of g; in a wider sense, it is sufficient that the former be an improper subset of the latter. Moreover, it is often convenient to tacitly restrict the domain of f, such that f produces only values in the domain of g. For example, the composition g ∘ f of the functions f : R → (−∞,+9] defined by f(x) = 9 − x$^2$ and g : [0,+∞) → R defined by $g(x)={\sqrt {x}}$ can be defined on the interval [−3,+3].
The functions g and f are said to commute with each other if g ∘ f = f ∘ g. Commutativity is a special property, attained only by particular functions, and often in special circumstances. For example, |x| + 3 = |x + 3| only when x ≥ 0. The picture shows another example.
The composition of one-to-one (injective) functions is always one-to-one. Similarly, the composition of onto (surjective) functions is always onto. It follows that the composition of two bijections is also a bijection. The inverse function of a composition (assumed invertible) has the property that (f ∘ g)$^{−1}$ = g$^{−1}$∘ f$^{−1}$.
Derivatives of compositions involving differentiable functions can be found using the chain rule. Higher derivatives of such functions are given by Faà di Bruno's formula.
Composition of functions is sometimes described as a kind of multiplication on a function space, but has very different properties from pointwise multiplication of functions (e.g. composition is not commutative).

## Composition monoids

Suppose one has two (or more) functions f: X → X, g: X → X having the same domain and codomain; these are often called transformations. Then one can form chains of transformations composed together, such as f ∘ f ∘ g ∘ f. Such chains have the algebraic structure of a monoid, called a transformation monoid or (much more seldom) a composition monoid. In general, transformation monoids can have remarkably complicated structure. One particular notable example is the de Rham curve. The set of all functions f: X → X is called the full transformation semigroup or symmetric semigroup on X. (One can actually define two semigroups depending how one defines the semigroup operation as the left or right composition of functions.)
If the given transformations are bijective (and thus invertible), then the set of all possible combinations of these functions forms a transformation group (also known as a permutation group); and one says that the group is generated by these functions.
The set of all bijective functions f: X → X (called permutations) forms a group with respect to function composition. This is the symmetric group, also sometimes called the composition group. A fundamental result in group theory, Cayley's theorem, essentially says that any group is in fact just a subgroup of a symmetric group (up to isomorphism).
In the symmetric semigroup (of all transformations) one also finds a weaker, non-unique notion of inverse (called a pseudoinverse) because the symmetric semigroup is a regular semigroup.

## Functional powers

If Y ⊆ X, then $f:X\to Y$ may compose with itself; this is sometimes denoted as $f^{2}$. That is:
$(f\circ f)(x)=f(f(x))=f^{2}(x)$
$(f\circ f\circ f)(x)=f(f(f(x)))=f^{3}(x)$
$(f\circ f\circ f\circ f)(x)=f(f(f(f(x))))=f^{4}(x)$
More generally, for any natural number n ≥ 2, the nth functional power can be defined inductively by f $^n$ = f ∘ f $^{n−1}$ = f $^{n−1}$ ∘ f, a notation introduced by Hans Heinrich Bürmann and John Frederick William Herschel. Repeated composition of such a function with itself is called function iteration.
* By convention, f $^0$ is defined as the identity map on f 's domain, id$_X$.
* If Y = X and f: X → X admits an inverse function f $^{−1}$, negative functional powers f $^{−n}$ are defined for n > 0 as the negated power of the inverse function: f $^{−n}$ = (f $^{−1}$)$^n$.
Note: If f takes its values in a ring (in particular for real or complex-valued f ), there is a risk of confusion, as f $^n$ could also stand for the n-fold product of f, e.g. f $^2$(x) = f(x) · f(x). For trigonometric functions, usually the latter is meant, at least for positive exponents. For example, in trigonometry, this superscript notation represents standard exponentiation when used with trigonometric functions:
sin$^2$(x) = sin(x) · sin(x).
However, for negative exponents (especially −1), it nevertheless usually refers to the inverse function, e.g., tan$^{−1}$ = arctan ≠ 1/tan.
In some cases, when, for a given function f, the equation g ∘ g = f has a unique solution g, that function can be defined as the functional square root of f, then written as g = f $^{1/2}$.
More generally, when g$^n$ = f has a unique solution for some natural number n > 0, then f $^{m/n}$ can be defined as g$^m$.
Under additional restrictions, this idea can be generalized so that the iteration count becomes a continuous parameter; in this case, such a system is called a flow, specified through solutions of Schröder's equation. Iterated functions and flows occur naturally in the study of fractals and dynamical systems.
To avoid ambiguity, some mathematicians choose to use ∘ to denote the compositional meaning, writing f$^{∘n}$(x) for the n-th iterate of the function f(x), as in, for example, f$^{∘3}$(x) meaning f(f(f(x))). For the same purpose, f$^{[n]}$(x) was used by Benjamin Peirce whereas Alfred Pringsheim and Jules Molk suggested $^n$f(x) instead.

## Alternative notations

Many mathematicians, particularly in group theory, omit the composition symbol, writing gf for g ∘ f.
During the mid-20th century, some mathematicians adopted postfix notation, writing xf  for f(x) and (xf)g for g(f(x)). This can be more natural than prefix notation in many cases, such as in linear algebra when x is a row vector and f and g denote matrices and the composition is by matrix multiplication. The order is important because function composition is not necessarily commutative. Having successive transformations applying and composing to the right agrees with the left-to-right reading sequence.
Mathematicians who use postfix notation may write "fg", meaning first apply f and then apply g, in keeping with the order the symbols occur in postfix notation, thus making the notation "fg" ambiguous. Computer scientists may write "f ; g" for this, thereby disambiguating the order of composition. To distinguish the left composition operator from a text semicolon, in the Z notation the ⨾ character is used for left relation composition. Since all functions are binary relations, it is correct to use the [fat] semicolon for function composition as well (see the article on composition of relations for further details on this notation).

## Composition operator

Given a function g, the composition operator C$_g$ is defined as that operator which maps functions to functions as $C_{g}f=f\circ g.$Composition operators are studied in the field of operator theory.

## In programming languages

Function composition appears in one form or another in numerous programming languages.

## Multivariate functions

Partial composition is possible for multivariate functions. The function resulting when some argument x$_i$ of the function f is replaced by the function g is called a composition of f and g in some computer engineering contexts, and is denoted f |$_{xi=g}$ $f|_{x_{i}=g}=f(x_{1},\ldots ,x_{i-1},g(x_{1},x_{2},\ldots ,x_{n}),x_{i+1},\ldots ,x_{n}).$
When g is a simple constant b, composition degenerates into a (partial) valuation, whose result is also known as restriction or co-factor.
$f|_{x_{i}=b}=f(x_{1},\ldots ,x_{i-1},b,x_{i+1},\ldots ,x_{n}).$
In general, the composition of multivariate functions may involve several other functions as arguments, as in the definition of primitive recursive function. Given f, a n-ary function, and n m-ary functions g$_1$, ..., g$_n$, the composition of f with g$_1$, ..., g$_n$, is the m-ary function $h(x_{1},\ldots ,x_{m})=f(g_{1}(x_{1},\ldots ,x_{m}),\ldots ,g_{n}(x_{1},\ldots ,x_{m})).$
This is sometimes called the generalized composite or superposition of f with g$_1$, ..., g$_n$. The partial composition in only one argument mentioned previously can be instantiated from this more general scheme by setting all argument functions except one to be suitably chosen projection functions. Here g$_1$, ..., g$_n$ can be seen as a single vector/tuple-valued function in this generalized scheme, in which case this is precisely the standard definition of function composition.
A set of finitary operations on some base set X is called a clone if it contains all projections and is closed under generalized composition. A clone generally contains operations of various arities. The notion of commutation also finds an interesting generalization in the multivariate case; a function f of arity n is said to commute with a function g of arity m if f is a homomorphism preserving g, and vice versa, that is: $f(g(a_{11},\ldots ,a_{1m}),\ldots ,g(a_{n1},\ldots ,a_{nm}))=g(f(a_{11},\ldots ,a_{n1}),\ldots ,f(a_{1m},\ldots ,a_{nm})).$
A unary operation always commutes with itself, but this is not necessarily the case for a binary (or higher arity) operation. A binary (or higher arity) operation that commutes with itself is called medial or entropic.

## Generalizations

Composition can be generalized to arbitrary binary relations. If R ⊆ X × Y and S ⊆ Y × Z are two binary relations, then their composition amounts to
$R\circ S=\{(x,z)\in X\times Z:(\exists y\in Y)((x,y)\in R\,\land \,(y,z)\in S)\}$.
Considering a function as a special case of a binary relation (namely functional relations), function composition satisfies the definition for relation composition. A small circle R∘S has been used for the infix notation of composition of relations, as well as functions. When used to represent composition of functions $(g\circ f)(x)\ =\ g(f(x))$ however, the text sequence is reversed to illustrate the different operation sequences accordingly.
The composition is defined in the same way for partial functions and Cayley's theorem has its analogue called the Wagner–Preston theorem.
The category of sets with functions as morphisms is the prototypical category. The axioms of a category are in fact inspired from the properties (and also the definition) of function composition. The structures given by composition are axiomatized and generalized in category theory with the concept of morphism as the category-theoretical replacement of functions. The reversed order of composition in the formula (f ∘ g)$^{−1}$ = (g$^{−1}$ ∘ f $^{−1}$) applies for composition of relations using converse relations, and thus in group theory. These structures form dagger categories.
   The standard "foundation" for mathematics starts with sets and their elements. It is possible to start differently, by axiomatising not elements of sets but functions between sets. This can be done by using the language of categories and universal constructions.
   . . . the membership relation for sets can often be replaced by the composition operation for functions. This leads to an alternative foundation for Mathematics upon categories -- specifically, on the category of all functions. Now much of Mathematics is dynamic, in that it deals with morphisms of an object into another object of the same kind. Such morphisms (like functions) form categories, and so the approach via categories fits well with the objective of organizing and understanding Mathematics. That, in truth, should be the goal of a proper philosophy of Mathematics.
   - Saunders Mac Lane, Mathematics: Form and Function

## Typography

The composition symbol ∘ is encoded as U+2218 ∘ RING OPERATOR (&compfn;, &SmallCircle;); see the Degree symbol article for similar-appearing Unicode characters. In TeX, it is written \circ.