Order theory

[summary: The study of binary relations that are reflexive, transitive, and anti-symmetric.]

Introduction
==========

Order pervades mathematics and the sciences. Often, a reader's intuitive notion of order is all that is necessary for understanding a particular invocation of the notion of order. However, a deeper examination of order reveals a rich taxonomy over the types of orderings that can arise and leads to powerful theorems with applications in mathematics, computer science, the natural sciences, and the social sciences.

The following are examples of orders that one might encounter in life:

- In a work place, employees may have a *rank* that determines which other employees they have authority over. IT workers report to the IT manager, marketers report to the marketing manager, managers report to the CEO, etc.

- In a grocery store, someone planning to buy corn flake cereal may consider the ordering on the *prices* of various brands of shredded wheat to decide which to purchase.

- At lunchtime, someone with a craving for burritos may order the nearby burrito restaurants by their *distance* from her workplace. She may also order these burrito joints by *quality*, and consider both orderings in her decision.

Now that we've seen some concrete examples of order, we can begin working toward a rigorous mathematical definition. In each of the above examples, we have some *underlying set* of objects that we are comparing: employees, corn flake brands, and burrito restaurants, respectively. We also have an *ordering*, which is a [3nt binary relation] determining whether or not one object is "less than" another. This suggests that order esentially a pair of a set and some binary relation defined on it. Is this all we need to capture the notion of order?

Here are a few examples of binary relations which may or may not be orderings. Do they agree with your intuitive notion of ordering?

* 1.) The relation which includes all pairs of people $(a,b)$ such that $a$ and $b$ are friends.
* 2.) The relation of pairs of people $(a,b)$ such that $a$ is a genetic descendant of $b$.
* 3.) The relation on days of the week which contains the exactly pairs $(a,b)$ such that a directly precedes b: $\{ (Monday, Tuesday), (Tuesday, Wednesday), ... \}$

It turns out that only item 2 agrees with the mathematical definition of ordering. Intuitively, item 1 is not an ordering because of its symmetry: a friendship between two people does not distinguish one friend as being "less than" the other (not a healthy friendship, at least). Friendship is actually an instance of another important class of binary relations called the [equivalence_relation_mathematics equivalence relations]. Item 3 is not an ordering because it lacks transitivity: Monday directly precedes Tuesday and Tuesday directly precedes Wednesday, but Monday does not directly precede Wednesday.

Posets
=====

We're now ready to provide a formal, mathematical definition, for a class of objects called *posets* (a contraction of partially ordered set), which captures the idea of ordering.

--

__Definition: Poset__

A *poset* is a pair $\langle P, \leq
angle$ of a set $P$ and a binary relation $\leq$ on $P$ such that for all $p,q,r \in P$, the following properties are satisfied:

- Reflexivity: $p \leq p$
- Transitivity: $p \leq q$ and  $q \leq r$ implies $p \leq r$
- Anti-Symmetry: $p \leq q$ and $q \leq p$ implies $p = q$

$P$ is referred to as the poset's *underlying set* and $\leq$
 is referred to as its *order relation*.

--

The above definition may strike some readers as more general than expected. Indeed, in both mathematics and conversational English, when someone states that a set of objects is ordered, they often mean that it is totally ordered. A total order is a poset for which any two elements of $a$ and $b$ of the underlying set are *comparable*; that is, $a \leq b$ or $b \leq a$. But our definition of a poset allows the possibility that two elements are *incomparable*. Recall our example of employees in a work place. In our *reports-to* relation, both an IT manager and a marketing manager report to the CEO; however, it would not make sense for an IT manager to report to a marketing manager or vice versa. The marketing and IT managers are thus incomparable. We write $a \parallel b$ to state that two poset elements $a$ and $b$ are incomparable.

Another important distinction must be made. Partial orders as we have described them are not *strict* orders. From any poset $\langle P, \leq
angle$, we can derive an strict order relation $<$, which includes exactly those pairs of $\leq$ relating two elements of $P$ that are distinct. It should be noted that, while strict orders are transitive and vacuously anti-symmetric, they are *not* partial orders because they are not reflexive. In everyday situations, strict orders seem to be more useful, but in mathematics non-strict orderings are of more use, and so non-strictness is built into the definition of poset.


Group theory

[summary: Group theory is the study of the [3gx algebraic structures] known as "[3gd groups]". A group $G$ is a collection of elements $X$ paired with an operator $ullet$ that combines elements of $X$ while obeying certain laws. Roughly speaking, $ullet$ treats elements of $X$ as composable, invertible actions. Group theory has many applications in pure and applied mathematics, as well as in the hard sciences like physics and chemistry. Groups are used as a building block in the formalization of many other mathematical structures, including [3w0 vector spaces] and [48l integers].]

Group theory is the study of the [3gx algebraic structures] known as "[3gd groups]". A group $G$ is a collection of elements $X$ paired with an operator $ullet$ that combines elements of $X$ while obeying certain laws. Roughly speaking, $ullet$ treats elements of $X$ as composable, invertible actions.

Group theory has many applications. Historically, groups first appeared in mathematics as groups of "substitutions" of mathematical functions; for example, the group of [48l integers] $\mathbb{Z}$ [3t9 acts] on the set of functions $f : \mathbb{R} 	o \mathbb{R}$ via the substitution $n : f(x) \mapsto f(x - n)$, which corresponds to translating the graph of $f$ $n$ units to the right. The functions which are invariant under this group action are precisely the functions which are periodic with period $1$, and group theory can be used to explain how this observation leads to the expansion of such a function as a [Fourier_series Fourier series] $f(x) = \sum \left( a_n \cos 2 \pi n x + b_n \sin 2 \pi n x
ight)$.

Groups are used as a building block in the formalization of many other mathematical structures, including [481 fields], [3w0 vector spaces], and [48l integers]. Group theory has various [group_theory_and_physics applications to physics]. For a list of example groups, see the [algebraic_group_examples examples page]. For a list of the key theorems in group theory, see the [algebraic_group_theorems main theorems page].

# Interpretations, visualizations, and uses

Group theory abstracts away from the elements in the [3gz underlying set] $X$: the group axioms do not care about what sort of thing is in $X$; they care only about the way that $ullet$ relates them. $X$ tells us how many elements $G$ has; all other information resides in the choices that $ullet$ makes to combine two group elements into a third group element. Thus, group theory can be seen as the study of possible relationships between objects that obey the group laws; regardless of what the objects themselves are. To visualize a group, we need only visualize the way that the elements relate to each other via $ullet$. This is the approach taken by [group_multiplication_table group multiplication tables] and [cayley_diagram group diagrams].

Group theory is interesting in part because the constraints on $ullet$ are at a "sweet spot" between "too lax" and "too restrictive." Group structure crops up in many areas of physics and mathematics, but the group axioms are still restrictive enough to make groups fairly easy to work with and reason about. For example, if the [3gg order] of $G$ is prime then there is only one possible group that $G$ can be (up to isomorphism). There are only 2, 2, 5, 2, and 2 groups of order 4, 6, 8, 9, and 10 (respectively). There are only 16 groups of order 100. If a group structure can be found in an object, this makes the behavior of the object fairly easy to analyze (especially if the order of the group is small). Group structure is relatively common in math and physics; for example, the solutions to a polynomial equation are acted on by a group called the [Galois_group Galois group] (a fact from which the [unsolvability_of_the_quintic unsolvability of quintic polynomials by radicals] was proven). Group theory is thus a useful tool for figuring out how various mathematical and physical objects behave. For more on this idea, see the page on [3t9 group actions].

Roughly speaking, the constraints on $ullet$ can be interpreted as saying that $ullet$ has to treat the elements like they are a "complete set of transformations" of a physical object. The axiom of identity then says "one of the elements has to be interpreted as a transformation that does nothing"; the axiom of inversion says "each transformation must be invertible;" and so on. In this light, group theory can be seen as the study of possible complete sets of transformations. Because the set of possible groups is relatively limited and well-behaved, group theory is a useful tool for studying the relationship between transformations of physical objects. For more discussion of this interpretation of group theory, see [groups_and_transformations].

Groups can also be seen as a tool for studying symmetry. Roughly speaking, given a set $X$ and some redundant structure atop $X$, a symmetry of $X$ is a function from $X$ to itself that preserves the given structure. For example, a physicist might build a model of two planets interacting in space, where an arbitrary point picked out as the origin. The behavior of the planets in relation to each other shouldn't depend on which point they label $(0, 0, 0)$, but many questions (such as "where is the planet now?") depend on the choice of origin. To study facts that are true regardless of where the origin is (rather than being artifacts of the representation), they might let $X$ be the possible states of the model, with then ask what properties of $X$ are preserved when the origin is shifted — i.e., they ask "what facts about this model would still be true if I had picked a different origin?" Translation of the origin is a "symmetry" of those properties: It takes one state of $X$ to another state of $X$, and preserves all properties of the physical system that are independent of the choice of origin. When a model has multiple symmetries (such as rotation invariance as well as translation invariance), those symmetries form a group under  composition. Group theory tells us about how those symmetries must relate to one another. For more on this topic, see [groups_and_symmetries].

In fact, laws of physics can be interpreted as "properties of the universe that are true at all places and all times, regardless of how you label things." Thus, the laws of physics can be studied by considering the properties of the universe that are preserved under certain transformations (such as changing labels). Group theory puts constraints on how these symmetries in the laws of physics relate to one another, and thus helps constrain the search for possible laws of physics. For more on this topic, see [groups_and_physics].

Finally, in a different vein, groups can be seen as a building block for other mathematical concepts. As an example, consider numbers. "Number" is a fairly fuzzy concept; the term includes the integers [48l $\mathbb Z$], the rational numbers [4zq $\mathbb Q$] (which include fractions), the real numbers [4bc $\mathbb R$] (which include numbers such as $\sqrt{2}$), and so on. The arrow from $\mathbb Z 	o \mathbb Q 	o \mathbb R$ points in the direction of _specialization:_ Each step adds constraints (such as "now you must include fractions") and thus narrows the scope of study (there are fewer things in the world that act like real numbers than there are that act like integers). Group theory is the result of following the arrow in the opposite direction: groups are like integers but _more permissive._ Groups have an identity element that corresponds to $0$ and an operation that corresponds to $+$, but they don't need to be infinitely large and they don't need to have anything corresponding to multiplication. From this point of view, "group" is a generalization of "integer", and from that generalization, we can build up many different mathematical objects, including [3gq rings], [481 fields], and [3w0 vector spaces]. Empirically, many interesting and useful mathematical objects use groups as one of their building blocks, and thus, some familiarity with group theory is helpful when learning about other mathematical structures. For more on this topic, see the [5dz tree of algebraic structures].


Group

[summary:
A group is an abstraction of a collection of symmetries of an object. The collection of symmetries of a triangle (rotating by $120^\circ$ or $240^\circ$ degrees and flipping), rearrangements of a collection of objects (permutations), or rotations of a sphere, are all examples of groups.

A group abstracts from these examples by forgetting what the symmetries are symmetries *of*, and only considers how symmetries behave.

- Any two symmetries can be composed. For the symmetries of flipping and rotating a triangle, the whole action of rotating, and then flipping a triangle at once is a symmetry. So there is an **[3h7 operation]** on a group which represents composition of symmetries.
- There is always a do-nothing symmetry. When composed with another symmetry, the do-nothing symmetry doesn't change that symmetry. So the operation has an **[-54p identity]**.
- Any symmetry can be reversed. That is, for any symmetry, there is another symmetry which when composed with the first, gives the do-nothing symmetry. Flipping a triangle, and then flipping it again, is the same as doing nothing. So the operation has an **[-inverse_mathematics inverse]** operation.
- Like any collection of functions, when composing a bunch of functions, it doesn't matter what order the individual compositions are computed in, as long as the *overall* composition ends up in the right order. When composing $f$, $g$, and $h$, we can compute $g \circ f$, and then compute $h \circ (g \circ f)$, or we can compute $h \circ g$ and then compute $(h \circ g) \circ f$, and we will get the same result. So the operation is **[-3h4 associative]**.

Note that it is not necessarily the case that the operation is [-3jb commutative]. Flipping and then rotating a triangle will give different symmetry than rotating and then flipping. If it is commutative, then the group is called [-3h2 abelian].
]

[summary(technical): A group $G$ is a pair $(X, ullet)$ where $X$ is a [3jz set] and $ullet$ is a [3h7 operation] obeying the following laws:

 1. **[3gy Closure]:** The operation is a function. For all $x, y$ in* $X$, $x ullet y$ is defined and in $X$. We abbreviate $x ullet y$ as $xy$.
 2. **[3h4 Associativity]:** $x(yz) = (xy)z$ for all $x, y, z \in X$.
 3. **[54p Identity]:** There is an element $e$ such that $xe=ex=x$ for all $x \in X$.
 4. **[-inverse_element Inverses]:** For each $x$ in $X$, there is an element $x^{-1} \in X$ such that $xx^{-1}=x^{-1}x=e$.

The operation need not be [-3jb commutative], but if it is then the group is called [-3h2 abelian].
]

A group is an abstraction of a collection of symmetries of an object. The collection of symmetries of a triangle (rotating by $120^\circ$ or $240^\circ$ degrees and flipping), rearrangements of a collection of objects (permutations), or rotations of a sphere, are all examples of groups. A group abstracts from these examples by forgetting what the symmetries are symmetries of, and only considers how symmetries behave.

A group $G$ is a pair $(X, ullet)$ where:

 - $X$ is a [3jz set], called the "underlying set." By abuse of notation, $X$ is usually denoted by the same symbol as the group $G$, which we will do for the rest of the article.
 - $ullet : G 	imes G 	o G$ is a binary [3h7 operation]. That is, a function that takes two elements of a set and returns a third. We will abbreviate $x ullet y$ by $xy$ when not ambiguous.  This operation is subject to the following axioms:
- **[3gy Closure]:** $ullet$ is a function. For all $x, y$ in $X$, $x ullet y$ is defined and in $X$. We abbreviate $x ullet y$ as $xy$.
- **[-54p Identity]:** There is an element $e$ such that $xe=ex=x$ for all $x \in X$.
- **[-inverse_element Inverses]:** For each $x$ in $X$, there is an element $x^{-1} \in X$ such that $xx^{-1}=x^{-1}x=e$.
- **[3h4 Associativity]:** $x(yz) = (xy)z$ for all $x, y, z \in X$.

1) The set X is the collection of abstract symmetries that this group represents. "Abstract," because these elements aren't necessarily symmetries *of* something, but almost all examples will be.

2) The operation $ullet$ is the abstract composition operation.

3) The axiom of closure is redundant, since $ullet$ is defined as a function $G 	imes G 	o G$, but it is useful to emphasize this, as sometimes one can forget to check that a given subsets of symmetries of an object is closed under composition.

4) The axiom of identity says that there is an element $e$ in $G$ that is a do-nothing symmetry: If you apply $ullet$ to $e$ and $x$, then $ullet$ simply returns $x$. The identity is unique: Given two elements $e$ and $z$ that satisfy axiom 2, we have $ze = ez = z.$ Thus, we can speak of "the identity" $e$ of $G$. This justifies the use of $e$ in the axiom of inversion: axioms 1 through 3 ensure that $e$ exists and is unique, so we can reference it in axiom 4.

$e$ is often written $1$ or $1_G$, because $ullet$ is often treated as an analog of multiplication on the set $X$, and $1$ is the multiplicative [54p identity]. (Sometimes, e.g. in the case of [3gq rings], $ullet$ is treated as an analog of addition, in which case the identity is often written $0$ or $0_G$.)

5) The axiom of inverses says that for every element $x$ in $X$, there is some other element $y$ that $ullet$ treats like the opposite of $x$, in the sense that $xy = e$ and vice versa. The inverse of $x$ is usually  written $x^{-1}$, or sometimes $(-x)$ in cases where $ullet$ is analogous to addition.

6) The axiom of associativity says that ullet behaves like composition of functions. When composing a bunch of functions, it doesn't matter what order the individual compositions are computed in. When composing $f$, $g$, and $h$, we can compute $g \circ f$, and then compute $h \circ (g \circ f)$, or we can compute $h \circ g$ and then compute $(h \circ g) \circ f$, and we will get the same result.

%%%knows-requisite([3h3]):
Equivalently, a group is a [3h3 monoid] which satisfies "every element has an inverse".
%%%

%%%knows-requisite([4c7]):
Equivalently, a group is a category with exactly one object, which satisfies "every arrow has an inverse"; the arrows are viewed as elements of the group. This justifies the intuition that groups are collections of symmetries. The object of this category can be thought of an abstract object that the isomorphisms are symmetries of. A functor from this category into the category of sets associates this object with a set, and each of the morphisms a permutation of that set.
%%%

# Examples

The most familiar example of a group is perhaps $(\mathbb{Z}, +)$, the integers under addition. To see that it satisfies the group axioms, note that:

1. (a) $\mathbb{Z}$ is a set, and (b) $+$ is a function of type $\mathbb Z 	imes \mathbb Z 	o \mathbb Z$
2. $(x+y)+z=x+(y+z)$
3. $0+x = x = x + 0$
4. Every element $x$ has an inverse $-x$, because $x + (-x) = 0$.

For more examples, see the [3t1 examples page].

# Notation

Given a group $G = (X, ullet)$, we say "$X$ forms a group under $ullet$." $X$ is called the [3gz underlying set] of $G$, and $ullet$ is called the _group operation_.

$x ullet y$ is usually abbreviated $xy$.

$G$ is generally allowed to substitute for $X$ when discussing the group. For example, we say that the elements $x, y \in X$ are "in $G$," and sometimes write "$x, y \in G$" or talk about the "elements of $G$."

The [3gg order of a group], written $|G|$, is the size $|X|$ of its underlying set: If $X$ has nine elements, then $|G|=9$ and we say that $G$ has order nine.

# Resources

Groups are a ubiquitous and useful algebraic structure. Whenever it makes sense to talk about symmetries of a mathematical object, or physical system, groups pop up. For a discussion of group theory and its various applications, refer to the [3g8 group theory] page.

A group is a [3h3 monoid] with inverses, and an associative [algebraic_loop loop]. For more on how groups relate to other [3gx algebraic structures], refer to the [5dz tree of algebraic structures].


Order of a group

The order $|G|$ of a [3gd group] $G$ is the size of its [3gz underlying set]. For example, if $G=(X,ullet)$ and $X$ has nine elements, we say that $G$ has order $9$. If $X$ is infinite, we say $G$ is infinite; if $X$ is finite, we say $G$ is finite.

The [4cq order of an element] $g \in G$ of a group is the smallest nonnegative integer $n$ such that $g^n = e$, or $\infty$ if there is no such integer. The relationship between this usage of order and the above usage of order is that the order of $g \in G$ in this sense is the order of the [subgroup] $\langle g
angle = \{ 1, g, g^2, \dots \}$ of $G$ [generating_set generated by] $g$ in the above sense.


Ring

[summary: A ring is a kind of [-3gx] which we obtain by considering [3gd groups] as being "things with addition" and then endowing them with a multiplication operation which must interact appropriately with the pre-existing addition. Terminology varies across sources;  we will take "ring" to refer to "commutative ring with $1$".]

[summary(Technical): A ring $R$ is a triple $(X, \oplus, \otimes)$ where $X$ is a [3jz set] and $\oplus$ and $\otimes$ are binary [set_theory_operation operations] subject to the ring axioms. We write $x \oplus y$ for the application of $\oplus$ to $x, y \in X$, which must be defined, and similarly for $\otimes$. Terminology varies across sources; our rings will have both operations [3jb commutative] and will have an [-54p] under multiplication, denoted $1$.]

A ring $R$ is a triple $(X, \oplus, \otimes)$ where $X$ is a [3jz set] and $\oplus$ and $\otimes$ are binary [set_theory_operation operations] subject to the ring axioms. We write $x \oplus y$ for the application of $\oplus$ to $x, y \in X$, which must be defined, and similarly for $\otimes$. It is standard to abbreviate $x \otimes y$ as $xy$ when $\otimes$ can be inferred from context. The ten ring axioms (which govern the behavior of $\oplus$ and $\otimes$) are as follows:

1. $X$ must be a [3h2 commutative group] under $\oplus$. That means:
   * $X$ must be [3gy closed] under $\oplus$.
   * $\oplus$ must be [associative_function associative].
   * $\oplus$ must be [commutative_function commutative].
   * $\oplus$ must have an identity, which is usually named $0$.
   * Every $x \in X$ must have an inverse $(-x) \in X$ such that $x \oplus (-x) = 0$.
2. $X$ must be a [3h3 monoid] under $\otimes$. That means:
   * $X$ must be [3gy closed] under $\otimes$.
   * $\otimes$ must be [associative_function associative].
   * $\otimes$ must have an identity, which is usually named $1$.
3. $\otimes$ must [distributive_property distribute] over $\oplus$. That means:
  * $a \otimes (x \oplus y) = (a\otimes x) \oplus (a\otimes y)$ for all $a, x, y \in X$.
  * $(x \oplus y)\otimes a = (x\otimes a) \oplus (y\otimes a)$ for all $a, x, y \in X$.

Though the axioms are many, the idea is simple: A ring is a [3h2 commutative group] equipped with an additional operation, under which the ring is a [3h3 monoid], and the two operations play nice together (the monoid operation [distributive_property distributes] over the group operation).

A ring is an [3gx algebraic structure].  To see how it relates to other algebraic structures, refer to the [5dz tree of algebraic structures].

# Examples

The integers $\mathbb{Z}$ form a ring under addition and multiplication.

[fixme: Add more example rings.]
\[work in progress.\]

# Notation

Given a ring $R = (X, \oplus, \otimes)$, we say "$R$ forms a ring under $\oplus$ and $\otimes$." $X$ is called the [3gz underlying set] of $R$. $\oplus$ is called the "additive operation," $0$ is called the "additive identity", $-x$ is called the "additive inverse" of $x$. $\otimes$ is called the "multiplicative operation," $1$ is called the "multiplicative identity", and a ring does not necessarily have multiplicative inverses.

# Basic properties

[fixme: Add the basic properties of rings.]
\[work in progress.\]

# Interpretations, Visualizations, and Applications

[fixme: Add (links to) interpretations, visualizations, and applications.]
\[work in progress.\]


Algebraic structure

Roughly speaking, an algebraic structure is a set $X$, known as the [3gz underlying set], paired with a collection of [3h7 operations] that obey a given set of laws. For example, a [3gd group] is a set paired with a single binary operation that satisfies the four group axioms, and a [3gq ring] is a set paired with two binary operations that satisfy the ten ring axioms.

In fact, algebraic structures can have more than one underlying set. Most have only one (including [3h3 monoids], [3gd groups], [3gq rings], [algebraic_field fields], [algebraic_lattice lattices], and [algebraic_arithmetic arithmetics]), and differ in how their associated operations work. More complex algebraic structures (such as  [algebraic_algebra algebras], [algebraic_module modules], and [3w0 vector spaces]) have two underlying sets. For example, vector spaces are defined using both an underlying [algebraic_field field] of scalars and an underlying [3h2 commutative group] of vectors.

For a map of algebraic structures and how they relate to each other, see the [algebraic_structure_tree tree of algebraic structures].


Closure

A [3jz set] $S$ is _closed_ under an [3h7 operation] $f$ if, whenever $f$ is fed elements of $S$, it produces another element of $S$. For example, if $f$ is a trinary operation (i.e., a function of three arguments) then "$S$ is closed under $f$" means "if $x, y, z \in S$ then $f(x, y, z) \in S$".

For example, the set [integer $\mathbb Z$] is closed under addition (because adding two integers yields another integer), but the set $\mathbb Z_5 = \{0, 1, 2, 3, 4, 5\}$ is not (because $1 + 5$ is not in $\mathbb Z_5$).


Underlying set

[summary: What do a [-3gd], a [-3rb], and a [ topological space] have in common? Each is a [-3jz] with some structure built on top of it, and in each case, we call the set the *underlying set*.]

What do a [-3gd], a [-3rb], and a [ topological space] have in common? Each is a [-3jz] with some structure built on top of it, and in each case, we call the set the *underlying set*. %%note: A group is a set with an [3h7 operation], a poset is a set with an [3dt ordering], and a topological space is a set with a collection of subsets that satisfy a [ certain property].%%

###Algebraic structures###

An [3gx algebraic structure] is a set equipped with operators that follow certain laws, such as a [3gd group], which is a pair $(X, ullet)$ where $X$ is a [3jz set] and $ullet$ is an operator that follows certain laws. Given an algebraic structure,  we can simply throw away the operators and recover the set ($X$, in this case), which is known as the "underlying set" of the structure.

The underlying set is sometimes known as the "carrier set." Some algebraic structures have more than one underlying set; for example, a vector space is an algebraic structure built out of a [481 field] of scalars and a [3h2 commutative group], in which case the term "underlying set" is ambiguous.


Abstract algebra

Abstract algebra is the study of [3gx algebraic structures], including [3gd groups], [3gq rings], [algebraic_field fields], [algebraic_module modules], [vector_space vector spaces], [algebraic_lattice lattices], [algebraic_arithmetic arithmetics], and [algebraic_algebra algebras].

The main idiom of abstract algebra is [abstract_over_objects abstracting away from the objects]: Abstract algebra concerns itself with the manipulation of objects, by focusing not on the objects themselves but on the relationships between them.

If you find any collection of objects that are related to each other in a manner that follows the laws of some algebraic structure, then those relationships are governed by the corresponding theorems, regardless of what the objects are. An abstract algebraist does not ask "what are numbers, really?"; rather, they say "I see that the operations of 'adding apples to the table' and 'removing apples from the table' follow the laws of numbers (in a limited domain), thus, theorems about numbers can tell what to expect as I add or remove apples (in that limited domain)."

For a map of algebraic structures and how they relate to each other, see the [algebraic_structures_tree tree of algebraic structures].


Abelian group

[summary: An abelian group is a [3gd group] where the operation is [3jb commutative]. That is, an abelian group $G$ is a pair $(X, ullet)$ where $X$ is a [3jz set] and $ullet$ is a binary [3h7 operation] obeying the four group axioms plus an axiom of commutativity:

1. [3gy Closure]: For all $x, y$ in $X$, $x ullet y$ is defined and in $X$. We abbreviate $x ullet y$ as $xy$.
2. [3h4 Associativity]: $x(yz) = (xy)z$ for all $x, y, z$ in $X$.
3. Identity: There is an element $e$ such that for all $x$ in $X$, $xe=ex=x$.
4. Inverses: For each $x$ in $X$ is an element $x^{-1}$ in $X$ such that $xx^{-1}=x^{-1}x=e$.
5. [3jb Commutativity]: For all $x, y$ in $X$, $xy=yx$.

Abelian groups are very "well-behaved" groups that are often easier to deal with than their non-commuting counterparts.]

An abelian group is a [3gd group] $G=(X, ullet)$ where $ullet$ is [3jb commutative]. In other words, the group operation satisfies the five axioms:

1. [3gy Closure]: For all $x, y$ in $X$, $x ullet y$ is defined and in $X$. We abbreviate $x ullet y$ as $xy$.
2. [3h4 Associativity]: $x(yz) = (xy)z$ for all $x, y, z$ in $X$.
3. Identity: There is an element $e$ such that for all $x$ in $X$, $xe=ex=x$.
4. Inverses: For each $x$ in $X$ is an element $x^{-1}$ in $X$ such that $xx^{-1}=x^{-1}x=e$.
5. [3jb Commutativity]: For all $x, y$ in $X$, $xy=yx$.

The first four are the standard [3gd group axioms]; the fifth is what distinguishes abelian groups from groups.

Commutativity gives us license to re-arrange chains of elements in formulas about commutative groups. For example, if in a commutative group with elements $\{1, a, a^{-1}, b, b^{-1}, c, c^{-1}, d\}$, we have the claim $aba^{-1}db^{-1}=d^{-1}$, we can shuffle the elements to get $aa^{-1}bb^{-1}d=d^{-1}$ and reduce this to the claim $d=d^{-1}$. This would be invalid for a nonabelian group, because $aba^{-1}$ doesn't necessarily equal $aa^{-1}b$ in general.

Abelian groups are very well-behaved groups, and they are often much easier to deal with than their non-commutative counterparts. For example, every [576] of an abelian group is [4h6 normal], and all finitely generated abelian groups are a [group_theory_direct_product direct product] of [47y cyclic groups] (the [structure_theorem_for_finitely_generated_abelian_groups structure theorem for finitely generated abelian groups]).


Monoid

A monoid $M$ is a pair $(X, \diamond)$ where $X$ is a [set_theory_set set] and $\diamond$ is an [associative_function associative] binary [3h7 operator] with an identity. $\diamond$ is often interpreted as concatenation; data structures that support concatenation and have an "empty element" (such as lists, strings, and the natural numbers under addition) are examples of monoids.

 Monoids are [3gx algebraic structures]. We write $x \diamond y$ for the application of $\diamond$ to $x, y \in X$, which must be defined. $x \diamond y$ is commonly abbreviated $xy$ when $\diamond$ can be inferred from context. The monoid axioms (which govern the behavior of $\diamond$) are as follows.

1. (Closure) For all $x, y$ in $X$, $xy$ is also in $X$.
1. (Associativity) For all $x, y, z$ in $X$, $x(yz) = (xy)z$.
2. (Identity) There is an $e$ in $X$ such that, for all $x$ in $X$, $xe = ex = x.$

The axiom of closure says that $x \diamond y \in X$, i.e. that combining two elements of $X$ using $\diamond$ yields another element of $X$. In other words, $X$ is [3gy closed] under $\diamond$.

The axiom of associativity says that $\diamond$ is an [3h4 associative] operation, which justifies omitting parenthesis when describing the application of $\diamond$ to many elements in sequence..

The axiom of identity says that there is some element $e$ in $X$ that $\diamond$ treats as "empty": If you apply $\diamond$ to $e$ and $x$, then $\diamond$ simply returns $x$. The identity is unique: Given two elements $e$ and $z$ that satisfy the axiom of identity, we have $ze = e = ez = z.$ Thus, we can speak of "the identity" $e$ of $M$. $e$ is often written $1$ or $1_M$.

%%%knows-requisite([4c7]):
Equivalently, a monoid is a category with exactly one object.
%%%

Monoids are [algebraic_semigroup semigroups] equipped with an identity. [3gd Groups] are monoids with inverses. For more on how monoids relate to other [algerbraic_structure algebraic structures], refer to the [algebraic_structure_tree tree of algebraic structures].

# Notation

Given a monoid $M = (X, \diamond)$, we say "$X$ forms a monoid under $\diamond$." For example, the set of finite bitstrings forms a monoid under concatenation: The set of finite bitstrings is closed under concatenation; concatenation is an associative operation; and the empty bitstring is a finite bitstring that acts like an identity under concatenation.

$X$ is called the [3gz underlying set] of $M$, and $\diamond$ is called the _monoid operation_. $x \diamond y$ is usually abbreviated $xy$. $M$ is generally allowed to substitute for $X$ when discussing the monoid. For example, we say that the elements $x, y \in X$ are "in $M$," and sometimes write "$x, y \in M$" or talk about the "elements of $M$."

# Examples

Bitstrings form a monoid under concatenation, with the empty string as the identity.

The set of finite lists of elements drawn from $Y$ (for any set $Y$) form a monoid under concatenation, with the empty list as the identity.

The natural numbers [45h $\mathbb N$] form a monid under addition, with $0$ as the identity.

Monoids have found some use in functional programming languages such as [https://en.wikipedia.org/wiki/Haskell_(programming_language) Haskell] and [https://en.wikipedia.org/wiki/Scala_(programming_language) Scala], where they are used to generalize over data types in which values can be "combined" (by some operation $\diamond$) and which include an "empty" value (the identity).


Associative operation

An **associative operation** $ullet : X 	imes X 	o X$ is a [3kb binary] [3h7 operation] such that for all $x, y, z$ in $X$, $x ullet (y ullet z) = (x ullet y) ullet z$. For example, $+$ is an associative function, because $(x + y) + z = x + (y + z)$ for all values of $x, y,$ and $z$. When an associative function is used to combine many elements in a row, parenthesis can be dropped, because the order of application is irrelevant.

Imagine that you're trying to use $f$ to combine 3 elements $x, y,$ and $z$ into one element, via two applications of $f$. $f$ is associative if $f(f(x, y), z) = f(x, f(y, z)),$ i.e., if the result is the same regardless of whether you apply $f$ to $x$ and $y$ first (and then apply that result to $z$), or whether you apply $f$ to $y$ and $z$ first (and then apply $x$ to that result).

Visualizing $f$ as a [3mb physical mechanism], there are two different ways to hook up two copies of $f$ together to create a function $f_3 : X 	imes X 	imes X 	o X,$ which takes three inputs and produces one output:

![Two ways of combining f](http://i.imgur.com/Ezs1P8l.png)

An associative function $f$ is one where the result is the same no matter which way the functions are hooked up, which means that the result of using $f$ twice to turn three inputs into one output yields the same output regardless of the order in which we combine adjacent inputs.

![3-arity f](http://i.imgur.com/WCT9HaA.png)

By similar argument, an associative operator $f$ also gives rise (unambiguously) to functions $f_4, f_5, \ldots,$ meaning that [3ms associative functions can be seen as a family of functions on lists].

This justifies the omission of parenthesis when writing expressions where an associative operator $ullet$ is applied to many inputs in turn, because the order of application does not matter. For example, multiplication is associative, so we can write expressions such as $2 \cdot 3 \cdot 4 \cdot 5$ without ambiguity. It makes no difference whether we compute the result by first multiplying 2 by 3, or 3 by 4, or 4 by 5.

By contrast, the function `prependx` that sticks its inputs together and puts an `x` on the front is not associative: `prependx(prependx("a","b"),"c") = "xxabc"`, but `prependx("a",prependx("b","c"))=xaxbc`.


Operator

An operation $f$ on a [3jz set] $S$ is a function that takes some values from $S$ and produces a new value. An operation can take any number of values from $S$, including zero (in which case $f$ is simply a constant) or infinitely many (in which case we call $f$ an "infinitary operation"). Common operations take a finite non-zero number of parameters. Operations often produce a value that is also in $S$ (in which case we say $S$ is [3gy closed] under $f$), but that is not always the case.

For example, the function $+$ is a binary operation on [45h $\mathbb N$], meaning it takes two values from $\mathbb N$ and produces another. Because $+$ produces a value that is also in $\mathbb N$, we say that $\mathbb N$ is closed under $+$.

The function $\operatorname{neg}$ that maps $x$ to $-x$ is a unary operation on [48l $\mathbb Z$]: It takes one value from $\mathbb Z$ as input, and produces an output in $\mathbb Z$ (namely, the negation of the input). $\operatorname{neg}$ is also a unary operation on $\mathbb N$, but $\mathbb N$ is not closed under $\operatorname{neg}$ (because $\operatorname{neg}(3)=-3$ is not in $\mathbb N$).

The number of values that the operator takes as input is called the [3h8 arity] of the operator. For example, the function $\operatorname{zero}$ which takes no inputs and returns $0$ is a zero-arity operator; and the operator $f(a, b, c, d) = ac - bd$ is a four-arity operator (which can be used on any [3gq ring], if we interpret multiplication and subtraction as ring operations).


Arity (of a function)

The arity of a [3jy function] is the number of parameters that it takes. For example, the function $f(a, b, c, d) = ac - bd$ is a function with arity 4, and $+$ is a function with arity 2; 2-arity functions are known as [3kb binary functions].

A function is said to take multiple parameters when its [3js domain] is the product of multiple sets. For example, consider the function `is_older_than` that takes (as input) a person and an age and returns `yes` if the person is older than that age, and `no` otherwise. The domain of `is_older_than` is the set of all pairs of people and ages, which we might write as $(\mathrm{People} 	imes \mathrm{Ages})$. Because this set is a product of two sets, we say that `is_older_than` is a function of two parameters, and that it has arity 2.


Commutative operation

A commutative function $f$ is a [3jy function] that takes multiple inputs from a [3jz set] $X$ and produces an output that does not depend on the ordering of the inputs. For example, the binary operation $+$ is commutative, because $3 + 4 = 4 + 3.$ The string concatenation function [3jv `concat`] is not commutative, because `concat("3","4")="34"` does not equal `concat("4","3")="43"`.


Commutativity: Intuition

[summary:We can think of commutativity either as an artifact of notation, or as a symmetry in the output of a function (with respect to the ordering of the inputs).]

# Commutativity as an artifact of notation

Instead of thinking of a commutative function $f(x, y)$ as a function that takes an ordered pair of inputs, we can think of $f$ as a function that takes an unordered [3jk bag] of inputs, and therefore _can't_ depend on their order. On this interpretation, the fact that functions are always given inputs in a particular order is an artifact of our definitions, not a fundamental property of functions themselves. If we had notation for functions applied to arguments in no particular order, then commutative functions would be the norm, and non-commutative functions would require additional structure imposed on their inputs.

In a world of linear left-to-right notation, where $f(x, y)$ means "$f$ applied to $x$ first and $y$ second", commutativity looks like a constraint. In an alternative world where functions are applied to their inputs in parallel, with none of them distinguished as "first" by default, commutativity is the natural state of affairs.

# Commutativity as symmetry in the output

Commutativity can be seen as a form of symmetry in the output of a [3kb binary] [3jy function]. Imagine a binary function as a physical mechanism of wheels and gears, that takes two inputs in along conveyer belts (one on the left, one on the right), manipulates those inputs (using mechanical sensors and manipulators and tools), and produces a result that is placed on an outgoing conveyer belt.

The output of a commutative function is symmetric in the way it relates to the inputs. Consider a function that takes two wooden blocks and glues them together. The function might _manipulate_ them in a symmetric fashion (if both blocks are picked up simultaneously, and have glue applied simultaneously, and are pushed together simultaneously), but the output is not symmetric: If a red block comes in on the left and a blue block comes in on the right, then the resulting block is red on the left and blue on the right, and the function is not commutative (though it is [3h4 associative]).

By contrast, a function that mixes the red and blue together — producing, for example, the uniform color purple or the unordered [3jz set] $\{b, d, e, l, u, r\}$ — would be commutative, because the way that the output relates to each input is independent of which side the input came in on.

A function is probably commutative if you can visualize the output itself as left/right symmetric, even if the left input was very different from the right input. For example, we can use the following visualization to show that multiplication is commutative. Imagine a function that takes in two stacks of poker chips, one on the left conveyer belt and one on the right conveyer belt. The function has a square of wet plaster in the middle. On each conveyer belt, an arm removes chips from the stack of poker chips until the stack only has one chip left, and for each chip that is removed, a perpendicular cut is put in the plaster (as in the diagram below). The plaster is then allowed to set, and the plaster pieces are then shaken out into a big bag. A third arm removes plaster pieces from the bag one at a time, and adds a poker chip to the outgoing conveyer belt for each one. To visualize this, see the diagram below:

![Why multiplication commutes](http://i.imgur.com/Q3QBUT6.png)

If the input conveyer belts had 4 and 6 poker chips on them (respectively), then the output belt will have 24 chips on it (because 24 different chunks of plaster will have fallen into the bag), but the chips don't necessarily come from any one particular side: The output relates to each input in a manner that doesn't depend on which side they came in.

%%%knows-requisite([1r6]):
In mathematics, a [symmetry_mathematics symmetry] is some structure on a set that is preserved under some transformation of that set. In our case, the structure is the value of the output, which is preserved under the transformation of changing which side the inputs come in on. Formally, let $X^2$ be the set of all pairs of two values from the set $X;$ each point in $X^2$ is a pair $(x_1, x_2).$ $X^2$ can be visualized as an [4w5 $|X|$] by $|X|$ grid. Consider a function $f : X^2 	o Y$ as a structure on $X^2$ that assigns a value $f(x_1, x_2)$ to each point $(x_1, x_2);$ this can be visualized as a terrain atop $X^2$ induced by $f$. Consider the transformation $\operatorname{swap} : X^2 	o X^2$ that maps $(x_1, x_2)$ to $(x_2, x_1),$ and the set $\operatorname{swap}(X^2)$ generated by applying $\operatorname{swap}$ to all points in $X^2$. This can be visualized as reflecting $X^2$ along a diagonal. $f$ also induces a structure on $\operatorname{swap}(X^2).$ If the structure of $f$ on $X^2$ is identical to the structure of $f$ on $\operatorname{swap}(X^2),$ then $f$ is a symmetry of $X^2$ under the transformation $\operatorname{swap}$. This occurs exactly when $f(x_1, x_2)=f(x_2, x_1)$ for all $(x_1, x_2)$ pairs, which is the formal definition of commutativity.
%%%


Bag

In mathematics, a "bag" is an unordered list. A bag differs from a [set_mathematics set] in that it can contain the same value more than once. A bag differs from a list in that its elements are not ordered. For example, $\operatorname{Bag}(1, 1, 2, 3) = \operatorname{Bag}(2, 1, 3, 1)
eq \operatorname{Bag}(1, 2, 3).$


String (of text)

A string (of text) is a series of letters (often denoted by quote marks), such as `"abcd"` or `"hello"` or `"this is a string"`. For example, the function [3jv concat] takes two strings as inputs and puts them together, i.e., `concat("one","two")="onetwo"`.


Domain (of a function)

The domain $\operatorname{dom}(f)$ of a [3jy function] $f : X 	o Y$ is $X$, the set of valid inputs for that function. For example, the domain of $+$ is the set of all pairs of numbers, and the domain of the division operation is the set of all pairs $(x, y)$ of numbers where $y$ is non-zero.

Visualizing a function as a map that takes every point in an input set to one point in an output set, the domain is the input set (pictured on the left in red in the image below).

![Domain, Codomain, and Image](http://i.imgur.com/ZZgVHaI.png)


concat (function)

The string concatenation function `concat` puts two [3jr strings] together, i.e., `concat("one","two")="onetwo"`.


Function

Intuitively, a function $f$  is a procedure (or machine) that takes an input and performs some operations to produce an output. For example, the function "+" takes a pair of numbers as input and produces their sum as output: on input (3, 6) it produces 9 as output, on input (2, 18) it produces 20 as output, and so on.

Formally, in mathematics, a function $f$ is a relationship between a [3jz set] $X$ of inputs and a set $Y$ of outputs, which relates each input to exactly one output. For example, $-$ is a function that relates the pair $(4, 3)$ to $1,$ and $(19, 2)$ to $17,$ and so on. In this case, the input set is all possible pairs of [number numbers], and the output set is numbers. We write [3vl $f : X 	o Y$] (and say "$f$ has the [type_mathematics type] $X$ to $Y$") to denote that $f$ is some function that relates inputs from the set $X$ to outputs from the set $Y$. For example, $- : (\mathbb N 	imes \mathbb N) 	o \mathbb N,$ which is read "subtraction is a function from [natural_number natural number]-pairs to natural numbers."

$X$ is called the [3js domain] of $f.$ $Y$ is called the [3lg codomain] of $f$. We can visualize a function as a mapping between domain and codomain that takes every element of the domain to exactly one element of the codomain, as in the image below.

![Domain, Codomain, and Image](http://i.imgur.com/ZZgVHaI.png)

[fixme: Talk about how they're a pretty dang fundamental concept.]
[fixme: Talk about how we can think of functions as mechanisms.]
[fixme: Add pages on set theoretic and type theoretic formalizations.]
[fixme: Talk about their relationships to programming.]
[fixme: Talk about generalizations including partial functions, multifunctions, etc.]
[fixme: Give some history, e.g. Church-Turing, Ackerman, recursion, etc. (Many of these todos should go into separate lenses; this definition here might be fine?)]

# Examples

There is a function $f : \mathbb{R} 	o \mathbb{R}$ from the [real_number real numbers] to the real numbers which sends every real number to its square; symbolically, we can write $f(x) = x^2$.

(TODO)


Set

A __set__ is an unordered collection of distinct [-5xx objects]. The objects in a set are typically referred to as its __elements__. A set is usually denoted by listing all of its elements between braces. For example, $\{1, 3, 2\}$ denotes a set of three numbers, and because sets are unordered, $\{3, 2, 1\}$ denotes the same set. $\{1, 2, 2, 3, 3, 3\}$ does not denote a set, because the elements of a set must be distinct.

Another way to denote sets is the so-called abstraction method, in which the members of a set are given an explicit description, leaving no need for listing them. For example, the set from the example above $\{1, 3, 2\}$ can be described as the set of all natural numbers $x$ which are less than $4$. Formally that is denoted as $\{x \mid (x < 4) 	ext{ and } (x 	ext{ is a natural number})\}$.

[comment: I am new to using Latex for editing, so I am not sure if this is the best way to display what's above. Also, "x is a natural number" is the same as x∈N but we did not get to the membership operator yet.]

Using the abstraction method allows for denoting sets with infinitely many elements, which would be impossible by listing them all. For example, the set $\{x \mid x = 2n 	ext{ for some natural } n \}$ is the set of all even numbers.

A set doesn't need to contain things of the same type, nor does it need to contain things that can all be brought to the same place: We could define a set $S$ that contains the apple nearest you, the left shoe which you wore last, the number 17, and London (though it's not clear why you'd want to). Rather, a set is an arbitrary boundary that we draw around some collection of objects, for our own purposes.

The use of sets is that we can manipulate representations of sets and study relationships between sets without concern for the actual objects in the sets. For example, we can say "there are 35 ways to choose three objects from a set of seven objects" regardless of whether the objects in the set are apples, people, or abstract concepts. %%note: This is an example of [abstract_over_objects abstracting over the objects].%%

It's also worth noting that a set of elements is itself a single distinct object, different from the things it contains, and in fact, one set can contain other sets among its elements. For example, $\{1,2,\{1,2\}\}$ is a set containing three elements: $1$, $2$ and $\{1,2\}$.

##Examples

 - $\{1,5,8,73\}$ is a set, containing numbers $1$, $5$, $8$ and $73$;
 - $\{\{0,-3,8\}\}$ is a set, containing __one__ element — the set $\{0,-3,8\}$;
 - $\{	ext{Mercury}, 	ext{Venus}, 	ext{Earth}, 	ext{Mars} \}$ is a set of four planets;
 - $\{x \mid x 	ext{ is a human, born on 01.01.2000} \}$ is a set all people whose age is the same as the current year number;
 - $\{	ext{author's favorite mug}, 	ext{Arbital's main page}, 73, 	ext{the tallest man born in London}\}$ is a set of four seemingly random objects.

## Set membership
*Main page: [set_membership Set membership]*

Set membership can be stated using the symbols $∈$ and $∉$. They describe the contents of a set. $∈$ indicates what is in a set.  $∉$ indicates what is **not** in a set. For example, "$x ∈ A$" translated into English is "$x$ is a member of the set $A$." and "$x ∉ A$" translates to "$x$ is **not** in the set $A$."

##Set cardinality
*Main page: [4w5]*

The size of a set is called its __cardinality__. If $A$ is a finite set then the cardinality of $A$, denoted $|A|$, is the number of elements $A$ contains. When $|A| = n$, we say that $A$ is a set of cardinality $n$. There exists a [499 bijection] from any finite set of cardinality $n$ to the set $\{0, ..., (n-1)\}$ containing the first $n$ natural numbers. We can generalize this idea to infinite sets: we say that two infinite sets have the same cardinality if there exists a bijection between them. Any set in bijective correspondence with [45h $\mathbb N$] is called __countably infinite__, while any infinite set that is not in bijective correspondence with $\mathbb N$ is call __uncountably infinite__. All countably infinite sets have the same cardinality, whereas there are multiple distinct uncountably infinite cardinalities.

## See also

* [5s5] - Common [3jy operations] in set theory.
* [sets_examples] - Examples of significant sets.
* [2w0] - A concrete exercise in comparing the cardinalities of infinite sets


Binary function

A binary function $f$ is a [3jy function] of two inputs (i.e., a function with [3h8 arity] 2). For example, $+,$ $-,$ $	imes,$ and $\div$ are all binary functions.


Associativity: Intuition

[summary: Associative functions can be interpreted as families of functions that reduce lists down to a single output by combining adjacent elements in any order. Alternatively, associativity can be seen as a generalization of "listyness," which captures and generalizes the "it doesn't matter whether you added [a, b] to c or a to [b, c], the result is [a, b, c] regardless" aspect of lists.

There are many different ways for a function to be associative, so it is difficult to provide a single litmus test for looking at a function and telling whether it associates (aside from just checking the associative axiom directly). However, a few heuristics can be used to make a good guess.]

Associative functions can be interpreted as families of functions that reduce lists down to a single output by combining adjacent elements in any order. Alternatively, associativity can be seen as a generalization of "listyness," which captures and generalizes the "it doesn't matter whether you added [a, b] to c or a to [b, c], the result is [a, b, c] regardless" aspect of lists.

There are many different ways for a function to be associative, so it is difficult to provide a single litmus test for looking at a function and telling whether it associates (aside from just checking the associative axiom directly). However, a few heuristics can be used to make a good guess.

[toc:]

# Associative operators as natural functions on lists

The [3ms generalized associative law] says that an [3h4 associative] [3jy function] $f : X 	imes X 	o X$ gives rise to a method for combining any non-empty list of $X$ elements into a single output, where the order in which adjacent elements are combined doesn't affect the result. We can flip that result around, and interpret associative operators as the pairwise versions of a certain class of "natural" functions for combining the elements of a list.

On this interpretation, we start by noting that some methods for reducing a list down to a single element can be broken down into pairwise combinations of adjacent elements, while others can't. For example, when we're trying to compute $3 + 4 + 5 + 6,$ we can pick any two adjacent elements and start by combining those using the binary version of $+$. But when we're trying to compute `adjacent_ones([0, 0, 0, 1, 1, 0])` to check whether the list has any two adjacent ones, we're going to run into trouble if we only look for adjacent ones in the pairs (0, 0), (0, 1), and (1, 0).

The lists that can be reduced by pairwise combination of adjacent pairs have a nice locality property; the result can be computed only by looking at adjacent elements (without worrying about the global structure). Locality is a common idiom in physics and mathematics, so we might start by asking what sort of functions on lists have this locality property. The answer is "any list that can be broken down into pairwise combinations of elements such that the order doesn't matter." If we formalize that notion, we get the result that any function on lists with this locality property corresponds (in a one-to-one fashion) to an associative operation. Thus, we can view associativity as the mathematical formalization of this nice "locality" property on lists.

Empirically, this locality property turns out to be quite useful for math, physics, and in computer programming, as evidenced by the commonality of associative operators. See, for example, [3g8], or the pages on [algebraic_semigroup semigroups] and [3h3 monoids].

# Associativity as a generalization of "list"

The above interpretation gives primacy to lists, and interprets associative operators in terms of natural functions on lists. We can invert that argument by treating associativity as a _generalization_ of what it means for something to act "list-like."

A list $[a, b, c, d, \ldots]$ is a set of elements that have been combined by some "combiner" function, where the order of the elements matters, but the order _in which they were combined_ does not matter. For example, if we combine $a$ with $b$ (forming $[a, b]$) and then combine that with $c$, then we get the same list as if we combine $b$ and $c$ into $[b, c]$ first, and then combine $a$ with that.

The very fact that we can unambiguously say "the list $[a, b, c]$" without worrying about the order that the elements were combined in means that lists are built out of an associative "combination" operator. On this interpretation, associativity is capturing part of the essence of listyness, and associativity _in general_ generalizes this notion. For example, associative operators are allowed to be a little forgetful about what exact elements you combined (e.g., 3 + 4 = 2 + 5) so long as you retain the "it doesn't matter what order you combine the things in" property. In other words, we can view associativity as "part of what it means to be list-like."

(One particularly important property of lists — namely, that they can be empty — is not captured by associativity alone. Associative operators on sets that have an element that acts like an "empty list" are called "monoids." For more on the idea of generalizing the notion of "list", refer to the page on [3h3 monoids].)

# Associative mechanisms

The above two interpretations give an intuition for what it _means_ that a function is associative. This still leaves open the question of _how_ a function can be associative. Imagine $f : X 	imes X 	o Y$ as a [3mb physical mechanism] of wheels and gears. Someone says "$f$ is associative." What does that _mean,_ in terms of the function's physical mechanisms? What should we expect to see when we pop the function open, given the knowledge that it "is associative"?

Recall that associativity says that the two methods for combining two instantiations of the function yield the same output:

![Associative paths](http://i.imgur.com/Ezs1P8l.png)

Thus, the ultimate physical test of associativity is hooking up two instantiations of $f$ as in the left diagram, and then checking whether dragging the mechanisms of the lower-right instantiation above the mechanisms of the upper-left instantiation (thereby reconfiguring the system according to the diagram on the right) causes the behavior of the overall system to change. What happens when the right-hand-side instantiation is given access to the middle input first versus second? Does that affect the behavior at all? If not, $f$ is associative.

This is not always an easy property to check by looking at the mechanisms of $f$ alone, and sometimes functions that appear non-associative (at first glance) turn out to be associative by apparent coincidence. In other words, there are many different ways for a function to be associative, so it is difficult to give a single simple criterion for determining associativity by looking at the internals of the function. However, we can use a few heuristics that help one distinguish associative functions from non-associative ones.

## Heuristic: Can two copies of the function operate in parallel?

$f$ is associative if, when using two copies of $f$ to reduce three inputs to one output, then changing whether the right-hand copy gets access to the middle tape first vs second does not affect the output. One heuristic for checking whether this is the case is to check whether both copies of $f$ can make use of the middle input at the same time, without getting in each other's way. If so, $f$ is likely associative.

For example, consider an implementation of $+$ that gets piles of poker chips as input (where a pile of $n$ chips represents the number $n$) and computes the output by simply sweeping all the poker chips from its input belts onto its output belt. To  make a function that adds three piles of chips together, you could set up two two-pile adders in the configuration of the diagram on the left, but you could also have two two-tape sweepers operating on three tapes in parallel, such that they both sweep the middle tape. This parallelization wouldn't change the output, and thus $+$ is associative.

By contrast, consider a mechanism that takes wooden blocks as input, and glues them together, and nails silver-colored caps on either end of the glued block. For example, if you put in a red block on the left and a blue block on the right, you get a silver-red-blue-silver block in the output. You could set up two copies of these like the diagram on the left, but if you tired to parallelize them, you'd get into trouble — each mechanism would be trying to nail one of its caps into the place that the other mechanism was attempting to apply glue. And indeed, this mechanism is non-associative.

This heuristic is imperfect. Some mechanisms that seem difficult to parallelize are still associative. For example, consider the multiplier mechanism, which takes two poker piles as input and puts a copy of the left pile onto the output tape for every chip in the right pile. It would be difficult to parallelize two copies of this function: One would be trying to count the chips in the middle pile while the other was attempting to copy the chips in the middle pile, and the result might not be pretty. However, multiplication _is_ associative, because a pile of $x$-many copies of a ($y$ copies of $z$)-many poker chips has the same number of chips as a pile of ($x$ copies $y$)-many copies of $z$-many poker chips.

## Heuristic: Does the output interpretation match both input interpretations?

Another (vaguer) heuristic is to ask whether the output of the function should _actually_ be treated as the same sort of thing as the input to the function. For example, recall the `adjacent_ones` function from above, which checks a list for adjacent ones, and returns `1` if it finds some and `0` otherwise. The inputs to `adjacent_ones` are 0 and 1, and the output is 0 or 1, but the output interpretation doesn't quite match the input interpretation: Intuitively, the output is _actually_ intended to mean "yes there were adjacent ones" or "not here weren't adjacent ones", and so applying `adjacent_ones` to the output of `adjacent_ones` is possible but ill-advised. If there is a mismatch between the output interpretation and at least one of the input interpretations, then the function probably isn't associative.

For example, imagine a person who is playing a game that works as follows. The board has three positions: red, green, and blue. The player's objective is to complete as many clockwise red-green-blue cycles as possible, without ever backtracking in the counter-clockwise direction.

![3-cycle board](http://i.imgur.com/bxCnXUs.png)

 Each turn, the game offers them a choice of one of the three spaces, and they get to choose whether or not to travel to that square or stay where they are. Clearly, their preferences depend on where they currently are: If they're on "red", "green" is a good move and "blue" is a bad one; but if they're on "blue" then choosing "green" is ill-advised. We can consider a binary function $f$ which takes their current position on the left and the proposed position on the right, and returns the position that the player prefers. For example, $f(red,blue)=red,$ $f(red,green)=green,$ $f(blue,blue)=blue,$ $f(blue,green=blue).$ In this case, the interpretation of the left input is a "player position," the interpretation of the right input is an "offered move", and the interpretation of the output is the resulting "player position." The output interpretation mismatches one of the input interpretations, which implies that $f$ probably isn't associative, and indeed it is not: $f(f(red, green), blue))=blue,$ whereas $f(red, f(green, blue))=red.$ The former expression can be interpreted as "where the player would be if they started at red, and were then offered green, and were then offered blue." The latter expression doesn't have a great interpretation, because it's feeding the output of $f(green, blue)$ (a player position) in as an "offered move."

If the interpretation of the output (in this case, "player position") mismatches the interpretations of at least one of the inputs, then the function likely isn't associative. However, this heuristic is also imperfect: The most obvious interpretations of the inputs and outputs to the subtraction function are "they're all just numbers," and subtraction still fails to associate.

## Further discussion

There are many different ways for a function to be associative, so it is difficult to give a simple litmus test. The ultimate test is always to imagine using two copies of $f$ to combine three outputs into one, and check whether the result changes depending on whether the left-hand copy of $f$ gets to run first (in which case it gets to access the second input belt at the source) or second (in which case its right-hand input is the right-hand copy's output). For examples of functions that pass or fail this ultimate test, refer to the [3mt examples page].


List

A list is an ordered collection of objects, such as `[0, 1, 2, 3]` or `["red", "blue", 0, "shoe"]`. Lists are generally denoted by square brackets, with elements separated by commas. Lists allow repetition (unlike [3jz sets]) and are ordered (unlike [3jk bags]).

Sometimes lists are typed, in which case they contain only one type of element. For example, `[0, 1, 2, 3]` is a well-typed list of numbers, whereas the list `["red", "blue", 0, "shoe"]` is not well-typed (because it contains both numbers and [3jr strings]). The set of all lists of type $X$ is usually denoted $[X]$ or $X^{\le \omega}$, where $\omega$ is the smallest infinite [limit_ordinal limit ordinal]. The set of all finite lists of type $X$ is usually denoted $X^{< \omega}$, and the set of all infinite lists of type $X$ is usually denoted $X^{\omega}$.


Codomain (of a function)

The codomain $\operatorname{cod}(f)$ of a [3jy function] $f : X 	o Y$ is $Y$, the set of possible outputs for the function. For example, the codomain of [3jv concat] is the set of all [3jr strings], and the codomain of the function $+$ is the set of all numbers.

Visualizing a function as a map that takes every point in an input set to one point in an output set, the codomain is the output set (pictured on the right in blue in the image below).

![Domain, Codomain, and Image](http://i.imgur.com/ZZgVHaI.png)

The codomain of a function is not to be confused with the [3lh image] of a function, which is the set of points in $Y$ that can actually be reached by following $f$, and which may not include the whole set $Y$. For example, consider all the functions that take a [real_number real number] as input and produce another real number. Many of those functions cannot be made to produce every possible real number: For example, the function $\operatorname{square} : \mathbb R 	o \mathbb R$ only produces non-negative numbers. For more on the distinction, see the page on [3lv codomain vs image].

[fixme: Add a lens talking about how codomains are arbitrary but often natural/useful. Use example like how we can consider $+$ to have codomain $\mathbb N$, $\mathbb Z$, etc., and examples like Ackerman where the codomain "nat" makes by far the most sense (though "int" is fine too).]


Image (of a function)

The image $\operatorname{im}(f)$ of a [3jy function] $f : X 	o Y$ is the set of all possible outputs of $f$, which is a subset of $Y$. Using [3lj set builder notation], $\operatorname{im}(f) = \{f(x) \mid x \in X\}.$

Visualizing a function as a map that takes every point in an input set to one point in an output set, the image is the set of all places where $f$-arrows land (pictured as the yellow subset of $Y$ in the image below).

![Domain, Codomain, and Image](http://i.imgur.com/ZZgVHaI.png)

The image of a function is not to be confused with the [3lg codomain], which is the _type_ of output that the function produces. For example, consider the [43x Ackermann function], which is a very fast-growing (and difficult to compute) function. When someone asks what sort of thing the Ackermann function produces, the natural answer is not "something from a sparse and hard-to-calculate set of numbers that I can't tell you off the top of my head"; the natural answer is "it outputs a number." In this case, the codomain is "number", while the image is the sparse and hard-to-calculate subset of numbers. For more on this distinction, see the page on [3lv codomain vs image].


Set builder notation

$\{ 2n \mid n \in \mathbb N \}$ denotes the set of all even numbers, using set builder notation. Set builder notation involves an expression on the left and a series of constraints on the right, separated by a pipe and placed between curly braces. The expression on the left makes use of variables that are introduced and constrained on the right. The result denotes the set of all possible values on the left-hand side that obey the constraints on the right-hand side. For example, $\{ (x, y) \mid x \in \mathbb R, y \in \mathbb R, x \cdot y = 1 \}$ is the set of all pairs of real numbers whose product is 1.


Range (of a function)

The "range" of a function is an ambiguous term that is generally used to refer to either the function's [3lg codomain] or [3lh image].


Codomain vs image

[summary:It is useful to distinguish codomain from image both (a) when the type of thing that the function produces is simple to describe, but the specific objects produced are complicated and hard to identify; and (b) when talking about (or making use of) a collection of functions from a set $X$ to a set $Y$, many of which have an image which is a subset of $Y$.]

A function $f : X 	o Y$ with [3js domain] $X$ and [3lg codomain] $Y$ cannot necessarily produce all values in $Y$. For example, consider the function that takes a [real_number real number] and squares it. We could say that this function has codomain $\mathbb R$, because it produces a real number, even though its image is the set of non-negative real numbers.

![Domain, Codomain, and Image](http://i.imgur.com/EHBsE1C.png)

If $f$ maps $X$ to a subset $I$ of $Y$, why not just say that its codomain is $I$ and do away with the distinction between codomain and image? There are at least two cases where the distinction is useful.

1. It is useful to distinguish codomain from image in cases where the _type of thing_ that the function produces is easy to specify, but the _specific values_ are hard to calculate. Consider, for instance, the [43x Ackermann function]. It definitely produces numbers (i.e. its codomain is $\mathbb N$), but it's not easy to figure out what numbers (exactly) it produces. When talking about what sort of outputs to expect from the Ackermann function, the natural answer is "a number," rather than "either 1 through 7, 9, 11, 13, 29, 61, 125, 65533, $2^{65536} − 3$, or some other ungodly large numbers that I lack the resources to calculate." For this purpose, the codomain can be thought of as a natural/simple boundary drawn around a complex image.

2. Cases where we're considering a set of all functions that map into a certain set. For example, if we're discussing all possible functions from the [natural_number natural numbers] to the set $\{0, 1\},$ we want to be able to talk about "functions with codomain $\{0, 1\}$" even if some of those functions are things like "always return 0" (which have an image smaller than $\{0, 1\}$). For this purpose, the codomain can be thought of as a context-dependent tool that's useful when considering (or making use of) a bunch of different functions that are all producing objects of the same type.


Function: Physical metaphor

[summary:Many functions can be visualized as physical mechanisms of wheels and gears, that take their inputs on conveyor belts, manipulate them (using mechanical sensors and tools), and produce an output which is placed on an outgoing conveyor belt.

![A binary function](http://i.imgur.com/lKxAXcK.png)

For example, we can visualize the function $+$ as a function that takes two stacks of poker chips in as input (one on the left conveyor belt and one on the right conveyor belt) and produces its output by dumping both stacks onto the output belt.]

Many functions can be visualized as physical mechanisms of wheels and gears, that take their inputs on conveyor belts, manipulate them (using mechanical sensors and tools), and produce an output which is placed on an outgoing conveyor belt.

![A binary function](http://i.imgur.com/lKxAXcK.png)

For example, we can visualize the function $+$ as a function that takes two stacks of poker chips in as input (one on the left conveyor belt and one on the right conveyor belt) and produces its output by dumping both stacks onto the output belt. We can visualize the function $	imes$ as a function that also gets a stack of poker chips on each input belt, and which puts a copy of the left stack onto the output tape for each poker chip in the right stack.

All [computable_function computable] functions can (in principle) be implemented by a physical system of wheels and gears (or circuits and wires, etc.), and it is conjectured that the computable functions are the _only_ functions that can be implemented by physical systems. (This conjecture is known as the [church_turing_thesis Church-Turing thesis].)


Generalized associative law

[summary: Given an [3h4 associative] [3h7 operator] $\cdot$ and a [3kd list] $[a, b, c, \ldots]$ of parameters, all ways of reducing the list to a single element by combining adjacent pairs yield the same result. In other words, while associativity only talks about the behavior of $f$ when combining three elements, this is sufficient to ensure that $f$ has similar good behavior when combining arbitrarily many elements. Thus, we can unambiguously drop parenthesis when using $\cdot$ to combine multiple elements. In other words, an associative function $f : X 	imes X 	o X$ gives rise to a method for combining any non-empty finite list of elements of $X$ into a single output, such that the result can be computed by combining adjacent elements in any order.]

Given an [3h4 associative] [3h7 operator] $\cdot$ and a [3kd list] $[a, b, c, \ldots]$ of parameters, all ways of reducing the list to a single element by combining adjacent pairs yield the same result. In other words, while associativity only talks about the behavior of $f$ when combining three elements, this is sufficient to ensure that $f$ has similar good behavior when combining arbitrarily many elements

For example, consider the list $[a, b, c, d, e],$ and abbreviate $a \cdot b$ as $ab.$ $((ab)c)(de)$ then denotes the output created by first combining $a$ and $b$, and then combining that with $c$, and then combining $d$ and $e$, and then putting the two results together. $a(b(c(de))$ denotes the result of combining $d$ and $e$ first, and then combining $c$ with that, and then combining $b$ with that, and then combining $a$ with that. The generalized associative law says that all possible orders of reducing the list (by combining adjacent elements) yield the same result, which means we are justified in dropping all parenthesis (and just writing $abcde$ for the result of reducing the list $[a, b, c, d, e]$ using $\cdot$), because the order of application does not matter.

Visually, given an associative [3jy function] $f$, there are five ways to use three copies of $f$ to combine four inputs into one output:

![Five ways of hooking up three instantiations of f](http://i.imgur.com/OvLsjqr.png)

The generalized associative law says that all five yield the same result. Because all five combinations are equivalent, there is only one function $f_4$ that uses three copies of $f$ to combine four inputs into one output. Similarly, there is only one function $f_5$ that combines five elements into one using four applications of $f,$ and so on.

# Proof sketch

Associativity of $\cdot$ lets us shift parenthesis to the left or right when manipulating expressions that use $\cdot$ multiple times in a row, because associativity says that $(x\cdot y) \cdot z = x \cdot (y \cdot z).$ Abbreviating $x \cdot y$ as $xy,$ we can use this rule to prove the generalized associative law.

Let a "reduction path" of a list $[a, b, c, d]$ be an expression of the form $a(b(cd)),$ where the parenthesis tell us explicitly how to use applications of $\cdot$ on adjacent elements to combine the elements of the list into a single output. To prove the generalized associative law for lists of length 4, observe that any reduction path can be turned into any other, by repeatedly shifting the parenthesis left or right. For example:

$a(b(cd))=a((bc)d)=(a(bc))d=((ab)c)d=(ab)(cd).$

In this case, the expression is transformed by shifting the inner parentheses left, then shifting the outer parentheses left, then shifting the inner parentheses left again, then shifting the outer parenthesis right, with each step allowed by associativity. By a similar method (of "walking" the parenthesis all over the reduction path one step at a time), any two reduction paths (of the same list) can be shown equivalent.

# Implications

An associative function $f : X 	imes X 	o X$ gives rise to a single natural function $f_n$ for reducing a list of $n$ inputs to a single output, given any number $n \ge 1$. ($f_1$ is the function that uses zero applications of $f,$ and thus leaves the input untouched.) This means that we can see associative functions not as just binary functions, but as a family of functions for reducing lists to a single output.

Furthermore, whenever we want to use an associative function to reduce a list to a single element, we can do so in a "local" fashion, by repeatedly combining adjacent elements in the list, without worrying about the order in which we do so. This is certainly not true for all functions that reduce a list to a single output! For example, consider the function `adjacent_ones` which takes a list of ones and zeroes and produces a `1` if the list contains any adjacent ones, and a `0` otherwise. `adjacent_ones([0,0,0,1,1,0])=1`, but we can't compute this result by just combining adjacent elements pairwise willy-nilly: If we use the order ((00)(01))(10) we get the answer `0`, when the correct answer is `1`.

Associative functions are precisely those that give rise to a family of functions for reducing lists where the result _can_ be computed by just combining adjacent elements pairwise willy-nilly. Thus, associative operators describe list operations that can be computed _locally,_ only ever looking at two adjacent elements, without worrying that some important global structure (such as "are there two ones in a row?") will be destroyed.

This family of functions also has the nice property that if you reduce one list $[a, b, c, \ldots]$ to a single element $lpha,$ and another list $[x, y, z, \ldots]$ to a single element $\chi,$ then $f(lpha, \chi)$ is the same as reducing $[a, b, c, \ldots, x, y, z, \ldots]:$ in other words, we can reduce two lists separately and then combine the results, and get the same answer as we would have gotten by combining the lists first and then reducing them to a single result.

# What about empty lists?

Any associative function $f$ can be used to reduce a non-empty finite list to a single element, by repeatedly applying $f$ to adjacent elements, without worrying about the order. This works for one-element lists, despite the fact that $f$ takes two parameters, because for one-element lists we can just use zero applications of $f$. But what about zero-element lists?

To create a family $f_n : X^n 	o X$ of functions on lists with the nice locality properties discussed above for all $n \ge 0,$ we need some "canonical" element of $0_X$ in $X$ that $f_0$ can return given an empty list, such that $0_X$ "acts like an empty element" with respect to $f.$ [3h3 Monoids] formalize this notion of "an associative operator plus an element that acts like an empty element," and thus, they capture the idea of a family of functions that can reduce any list (of finite size) to a single output, regardless of what order adjacent elements are combined in.


Associativity: Examples

[summary: Yes: [Addition], [multiplication], [3jv string concatenation]. No: [subtraction], [division], a [-3jy] that concatenates strings and then puts an "x" on the front.]

[toc:]

## Positive examples

### Addition

$(x + y) + z = x + (y + z)$ for all numbers $x, y,$ and $z.$ Thus, addition associates. One easy way to see this fact is to consider a [3mb physical system] that implements addition, e.g., by taking two piles of poker chips (where a poker chip with $n$ chips represents the number $n$) in on two input belts, and producing a pile of poker chips on the output belt. This function can be implemented by simply shoving both input piles onto the output pile. Clearly, when combining three piles, it doesn't matter which piles get shoved onto the output tape in which order, so addition is associative.

### Multiplication

$(x \times y) \times z = x \times (y \times z)$ for all numbers $x, y,$ and $z.$ Thus, multiplication associates. It is, however, a little harder to see why this must be the case for multiplication.

Imagine a physical system implementing multiplication, by taking stacks of poker chips as input (where stacks with $n$ chips in them represents the number $n$), and producing an output by putting a copy of the left stack on the output tape for every chip in the right stack. Imagine using this function to combine three stacks of poker chips, a blue stack (representing $x$), a yellow stack (representing $y$), and a red stack (representing $z$). We can either calculate $(x \times y) \times z$ or $x \times (y \times z).$ In both cases, the result will be a bunch of copies of the original blue $x$ stack. The question is, how many copies will there be in each case?

In the first case, the machine first puts $y$ copies of the blue stack in a line, and then makes $z$ copies of that line. In the second case, the machine first makes $z$ copies of the yellow stack. Each of those yellow stacks corresponds to one of the lines from the first case, and each yellow chip on each yellow stack corresponds to one of the blue stacks in the first case. A blue stack is placed on the output in the second case for each of those yellow chips, so the number of blue stacks in each case is the same.

### Concatenation

The concatenation of [3jr strings] is another associative function: `concat("a",concat("b","c")) = concat(concat("a","b"),"c") = "abc"`.

Concatenation is an example of an associative function that is not [3jb commutative]: When reducing a list of strings to a single string, it doesn't matter what order you combine adjacent elements in, but it _does_ matter that you leave the elements in their original order.

In fact, any function that just sticks its inputs together (possibly with some extra stuff in the middle) is associative: A function which takes wooden blocks as input, and glues them together (with a small white block in the middle) is associative, because if you put in blue, yellow, and red blocks then you get a blue-white-yellow-white-red block out the end, regardless of the order you combine adjacent blocks in.

## Negative examples

### Functions with a history

The function `xconcat` that concatenates its inputs and puts an "x" on the front: `xconcat("A", xconcat("B","C")) = "xAxBC"`, but `xconcat(xconcat("A", "B"), "C") = "xxABC"`. The problem in this case is that the output carries a trace of which adjacent elements were combined in which order, which makes the function non-associative.

In fact, any function that carries a history of the application order with it can't be associative. Thus, if the inputs carry history about which inputs were combined with which other inputs, and the output preserves (and adds to) that history, the function can't be associative. Associativity is, fundamentally, about the output not depending on which path was taken to get to it.

### Subtraction

Subtraction does not associate, as $(5-3)-2=0$ but $5-(3-2)=4.$ To see what went wrong, first notice that all inputs contribute either positively or negatively to the result. Label all inputs (in their original positive states) with up-arrows; we will track whether that input has a positive or negative impact on the final output. Subtraction flips its right-hand input upside down before combining it with its left-hand input, so given inputs labeled $\uparrow$ and $\uparrow$ we should label its output $\uparrow\downarrow.$ When combining three inputs, we can either combine the left two first or the right two first. If we combine the left two first, then the second subtraction is run on inputs labeled $\uparrow\downarrow$ and $\uparrow,$ and produces an output $\uparrow\downarrow\downarrow,$ in which the first input contributes positively and the other inputs contribute negatively. But if we combine the right two inputs first, then the second subtraction is run on inputs labeled $\uparrow$ and $\uparrow\downarrow,$ and produces an output $\uparrow\downarrow\uparrow,$ in which the first and third inputs contribute positively and the second contributes negatively. Thus, the contribution of the third input depends on which adjacent elements are combined in which order, so subtraction does not associate.

### Cyclical preferences

Consider a person who is playing a game that works as follows. The board has three positions: red, green, and blue. The player's objective is to complete as many clockwise red-green-blue cycles as possible, without ever backtracking in the counter-clockwise direction.

![3-cycle board](http://i.imgur.com/bxCnXUs.png)

 Each turn, the game offers them a choice of one of the three spaces, and they get to choose whether or not to travel to that square or stay where they are. Clearly, their preferences depend on where they currently are: If they're on "red", "green" is a good move and "blue" is a bad one; but if they're on "blue" then choosing "green" is ill-advised.

We can consider a binary operation $?$ which takes their current position on the left and the proposed position on the right, and returns the position that the player prefers. Specifically:

\begin{align}
& red \ ?\  red \ &= red\\
& red \ ?\  green \ &= green\\
& red \ ?\  blue \ &= red\\
\end{align}
\begin{align}
& green \ ?\  red \ &= green\\
& green \ ?\  green \ &= green\\
& green \ ?\  blue \ &= blue\\
\end{align}
\begin{align}
& blue \ ?\  red \ &= red\\
& blue \ ?\  green \ &= blue\\
& blue \ ?\  blue \ &= blue
\end{align}

This function does not associate, because $(red\ ?\ green)\ ?\ blue = blue$ but $red\ ?\ (green\ ?\ blue)=red.$ To show that a function is not associative, it is sufficient to simply do out the whole function table and then find any one case that violates the axiom of associativity, as above.


Commutativity: Examples

[summary: Yes: addition, multiplication, maximum, minimum, rock-paper-scissors. No: subtraction, division, string concatenation, matrix multiplication.]

## Positive examples

### Addition

$x+y = y+x$ for all numbers $x$ and $y,$ so addition commutes. One easy way to see this fact is to consider a physical system that implements addition, e.g., by taking two piles of poker chips (where a poker chip with nn chips represents the number nn) in on two input belts, and producing a pile of poker chips on the output belt. This function can be implemented by simply shoving both input piles onto the output pile. Because a pile of chips represents the same number regardless of how those chips are arranged on the output belt, it doesn't matter which pile comes in on which input belt, so addition is commutative.

### Multiplication

$x 	imes y = y 	imes x$ for all numbers $x$ and $y,$ so multiplication also commutes. It is a bit harder to see why this must be so in the case of multiplication, as $x 	imes y$ can be interpreted as "take the $x$ input and stretch it out according to the $y$ input," and it's not entirely obvious that "stretching the $x$ input according to $y$" should yield the same result as "stretching the $y$ input according to $x.$"

To see that multiplication commutes, it is helpful to visualize _copying_ $x$ (rather than stretching it out) into $y$ many rows. This allows us to visualize $x 	imes y$ as a square divided into $x$ parts along one side and $y$ parts along the other side, which makes it clearer that multiplication commutes.

![Multiplicaiton commutes](http://i.imgur.com/Q3QBUT6.png)

It is worth noting that the concept of "multiplication" generalizes beyond the concept of "numbers," and in those contexts, "stretching $x$ out according to $y$" and "stretching $y$ out according to $x$" might be quite different operations. For example, [matrix_multiplication multiplication of matrices] does not commute.

### Maximum and minimum

The [maximum_function max] and [minimum_function min] functions commute, because "which of these elements is largest/smallest?" is a question that doesn't depend upon the order that the elements are presented in.

### Rock, Paper, Scissors

The question of who won in a game of [rock_paper_scissors rock-paper-scissors] is commutative. Writing $r$ for rock, $p$ for paper, $s$ for scissors, and $?$ for the binary operator saying who won, we have $r ? p = p,$ $r ? s = r,$ and $p ? s = s,$ and so on. This function is commutative: who won a game of rock-paper-scissors does not depend upon which side of the judge they were standing on.

This provides an example of a commutative function that is not [3h4 associative]: $r?p=p?r$ and so on, but $(r?p)?s=s$ while $r?(p?s)=r.$ In other words, who won a game of rock-paper-scissors does not depend on which side of the judge they were standing on, but if you have a line of people throw "rock," "paper," or "scissors," then the question "who won?" _does_ depend on which end of the line you start from.

## Negative examples

### Concatenation

Any function that includes its inputs in its outputs _in order_ is not commutative. For example, the function `pair` which puts its inputs into a pair (such that `pair(x, y) = (x, y)`) is not commutative. The function [3jv concat], which also sticks its inputs together in order, is also not commutative, though it is associative.

### Division

Division is not commutative, because $x / y$ does not equal $y / x$ in general. In this case, the function is using $x$ and $y$ in a very different way. One is interpreted as a numerator that will be cut up, the other is treated as a denominator that says how many times to cut the numerator up. Numerator and denominator are not exchangeable concepts: Division needs to know which input is intended to be the numerator and which is intended to be the denominator.

### Matrix multiplication

[matrix_multiplication Matrix multiplication] is not only non-commutative, it may fail to even make sense. Two [linear_algebra_matrix matrices] can be multiplied if and only if the number of columns in the left-hand matrix is equal to the number of rows in the right-hand matrix. Thus, a $2 	imes 3$ matrix can be multiplied with a $3 	imes 5$ matrix if and only if the $2 	imes 3$ matrix is on the left. Clearly, then, matrix multiplication does not commute.


Associativity vs commutativity

[3h4 Associativity] and [3jb commutativity] are often confused, because they are both constraints on how a [3jy function] treats its inputs, and they are both constraints saying "these two paths for calculating the output must yield the same answer." The difference is that commutativity says "this function shouldn't care what order its own arguments come in," while associativity says "different ways of using this function multiple times should yield the same results."

Commutativity is about an invariance in _one_ call to the function: If you run the function once with inputs $x$ and $y,$ you'll get the same result as if you run it once with inputs $y$ and $x.$

Associativity is about an invariance in _multiple_ calls to the function: If you combine four elements using three function calls in the order $a \cdot (b \cdot (c \cdot d)),$ you'll get the same answer as if you use the three function calls in the order $((a \cdot b) \cdot c) \cdot d.$

Commutativity means you can swap the order of the elements. Associativity means you can drop parenthesis when combining multiple elements in a row.

If an operator $\cdot$ is both commutative and associative, then when you're combining multiple elements in a row, you're welcome to combine them in _any_ order: For example, when adding $3 + 2 + (-7) + 5 + (-2) + (-3) + 7,$ we can re-arrange the list to get $3 - 3 + 2 - 2 + 7 - 7 + 5 = 5,$ which makes calculation easy. To see that this is the case, note that the result won't change if we swap any two adjacent elements, because associativity means that we can pick any two adjacent elements to start with and commutativity says that the order of those elements doesn't matter. Then, we can re-arrange the list however we like using a series of two-element swaps.
[fixme: Perhaps conditionalize this parenthetical on something like "cares about abstract algebra" or "wants tidbits they don't understand to point them towards cool things" or something.]
(This is why [3h2 commutative groups] are especially easy to work with.)

## Examples

Addition is both commutative and associative (same with multiplication). Subtraction is neither commutative nor associative (same with division).

[3jv String concatenation] is associative but not commutative: If you're sticking a bunch of [3jr strings] together end on end, then it doesn't matter which adjacent pairs you combine first, but `"onetwo"` is a very different string than `"twoone"`.

Rock-paper-scissors is commutative but not associative. The winner of "rock v scissors" is rock, and the winner of "scissors v rock" is rock, and so on; but if you have a bunch of people in a line make rock, paper, or scissors signs, then who is the winer depends on which end of the line you start from: If the line has three people, and they throw [rock, paper, scissors], then the winner will be whoever threw scissors if you start from the left (by finding the winner between rock and paper,  and then playing that against scissors) or rock if you start on the right (by finding the winner between paper and scissors, and then playing that against rock).

The function `pair` which puts its inputs into a pair (such that `pair(x, y) = (x, y)`) is neither commutative nor associative: `(x, y)` does not equal `(y, x)`, and `(x, (y, z))` does not equal `((x, y), z)`. The output of `pair` preserves the ordering and structure of all its inputs, and leaves a trace that allows one to distinguish which inputs it was called on when. Both commutativity and associativity require the function to be "forgetful" about some aspect of how it was called: Commutative functions need to be insensitive to the ordering of their arguments; associative functions need to leave no trace that can be used to figure out which inputs were combined in what order. Thus, any function that bakes a lot of history about what it was called with into the output is unlikely to be commutative or associative.

## Mnemonics

If it helps you to remember which is which, consider these two mnemonics:

For "commutativity," imagine that the two parameters to the function each live on one side of town and work on the other side. Each morning, they pass each other on their morning _commute,_ as they swap places.

For "associativity," imagine a bunch of _associates_ all standing in a line, ranked according to their hierarchy in a law firm. Any two adjacent people are willing to converse with each other, although people on one end of the line might not be willing to associate with people on the other end.


Eigenvalues and eigenvectors

Consider a linear transformation represented by a matrix $A$, and some vector $v$. If $Av = \lambda v$, we say that $v$ is an _eigenvector_ of $A$ with corresponding eigenvalue $\lambda$. Intuitively, this means that $A$ doesn't rotate or change the direction of $v$; it can only stretch it ($|\lambda| > 1$) or squash it ($|\lambda| < 1$) and maybe flip it ($\lambda < 0$). While this notion may initially seem obscure, it turns out to have many useful applications, and many fundamental properties of a linear transformation can be characterized by its eigenvalues and eigenvectors.