# Cartesian product

In mathematics, specifically set theory, the Cartesian product of two sets A and B, denoted A × B, is the set of all ordered pairs (a, b) where a is in A and b is in B. In terms of set-builder notation, that is $A\times B=\{(a,b)\mid a\in A\ {\mbox{ and }}\ b\in B\}.$
A table can be created by taking the Cartesian product of a set of rows and a set of columns. If the Cartesian product rows × columns is taken, the cells of the table contain ordered pairs of the form (row value, column value).
One can similarly define the Cartesian product of n sets, also known as an n-fold Cartesian product, which can be represented by an n-dimensional array, where each element is an n-tuple. An ordered pair is a 2-tuple or couple. More generally still, one can define the Cartesian product of an indexed family of sets.
The Cartesian product is named after René Descartes, whose formulation of analytic geometry gave rise to the concept, which is further generalized in terms of direct product.

## Set-theoretic definition

A rigorous definition of the Cartesian product requires a domain to be specified in the set-builder notation. In this case the domain would have to contain the Cartesian product itself. For defining the Cartesian product of the sets $A$ and $B$, with the typical Kuratowski's definition of a pair $(a,b)$ as $\{\{a\},\{a,b\}\}$, an appropriate domain is the set ${\mathcal {P}}({\mathcal {P}}(A\cup B))$ where ${\mathcal {P}}$ denotes the power set. Then the Cartesian product of the sets $A$ and $B$ would be defined as $A\times B=\{x\in {\mathcal {P}}({\mathcal {P}}(A\cup B))\mid \exists a\in A\ \exists b\in B:x=(a,b)\}.$

## Examples
### A deck of cards

An illustrative example is the standard 52-card deck. The standard playing card ranks {A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2} form a 13-element set. The card suits {♠, ♥, ♦, ♣} form a four-element set. The Cartesian product of these sets returns a 52-element set consisting of 52 ordered pairs, which correspond to all 52 possible playing cards.
Ranks × Suits returns a set of the form {(A, ♠), (A, ♥), (A, ♦), (A, ♣), (K, ♠), ..., (3, ♣), (2, ♠), (2, ♥), (2, ♦), (2, ♣)}.
Suits × Ranks returns a set of the form {(♠, A), (♠, K), (♠, Q), (♠, J), (♠, 10), ..., (♣, 6), (♣, 5), (♣, 4), (♣, 3), (♣, 2)}.
These two sets are distinct, even disjoint, but there is a natural bijection between them, under which (3, ♣) corresponds to (♣, 3) and so on.

### A two-dimensional coordinate system

The main historical example is the Cartesian plane in analytic geometry. In order to represent geometrical shapes in a numerical way, and extract numerical information from shapes' numerical representations, René Descartes assigned to each point in the plane a pair of real numbers, called its coordinates. Usually, such a pair's first and second components are called its x and y coordinates, respectively (see picture). The set of all such pairs (i.e., the Cartesian product $\mathbb {R} \times \mathbb {R}$, with $\mathbb {R}$ denoting the real numbers) is thus assigned to the set of all points in the plane.

## Most common implementation (set theory)

A formal definition of the Cartesian product from set-theoretical principles follows from a definition of ordered pair. The most common definition of ordered pairs, Kuratowski's definition, is $(x,y)=\{\{x\},\{x,y\}\}$. Under this definition, $(x,y)$ is an element of ${\mathcal {P}}({\mathcal {P}}(X\cup Y))$, and $X\times Y$ is a subset of that set, where ${\mathcal {P}}$ represents the power set operator. Therefore, the existence of the Cartesian product of any two sets in ZFC follows from the axioms of pairing, union, power set, and specification. Since functions are usually defined as a special case of relations, and relations are usually defined as subsets of the Cartesian product, the definition of the two-set Cartesian product is necessarily prior to most other definitions.

### Non-commutativity and non-associativity

Let A, B, C, and D be sets.
The Cartesian product A × B is not commutative, $A\times B\neq B\times A,$ because the ordered pairs are reversed unless at least one of the following conditions is satisfied:
* A is equal to B, or
* A or B is the empty set.
For example:
    A = {1,2}; B = {3,4}
       A × B = {1,2} × {3,4} = {(1,3), (1,4), (2,3), (2,4)}
       B × A = {3,4} × {1,2} = {(3,1), (3,2), (4,1), (4,2)}
    A = B = {1,2}
       A × B = B × A = {1,2} × {1,2} = {(1,1), (1,2), (2,1), (2,2)}
    A = {1,2}; B = ∅
       A × B = {1,2} × ∅ = ∅
       B × A = ∅ × {1,2} = ∅
Strictly speaking, the Cartesian product is not associative (unless one of the involved sets is empty). $(A\times B)\times C\neq A\times (B\times C)$ If for example A = {1}, then (A × A) × A = {((1, 1), 1)} ≠ {(1, (1, 1))} = A × (A × A).

### Intersections, unions, and subsets

A = [1,4], B = [2,5], and C = [4,7], demonstrating A × (B∩C) = (A×B) ∩ (A×C), A × (B∪C) = (A×B) ∪ (A×C), and
A = [2,5], B = [3,7], C = [1,3], D = [2,4], demonstrating
The Cartesian product satisfies the following property with respect to intersections (see middle picture). $(A\cap B)\times (C\cap D)=(A\times C)\cap (B\times D)$
In most cases, the above statement is not true if we replace intersection with union (see rightmost picture). $(A\cup B)\times (C\cup D)\neq (A\times C)\cup (B\times D)$
In fact, we have that: $(A\times C)\cup (B\times D)=[(A\setminus B)\times C]\cup [(A\cap B)\times (C\cup D)]\cup [(B\setminus A)\times D]$
For the set difference, we also have the following identity: $(A\times C)\setminus (B\times D)=[A\times (C\setminus D)]\cup [(A\setminus B)\times C]$
Here are some rules demonstrating distributivity with other operators (see leftmost picture): ${\begin{aligned}A\times (B\cap C)&=(A\times B)\cap (A\times C),\\A\times (B\cup C)&=(A\times B)\cup (A\times C),\\A\times (B\setminus C)&=(A\times B)\setminus (A\times C),\end{aligned}}$ $(A\times B)^{\complement }=\left(A^{\complement }\times B^{\complement }\right)\cup \left(A^{\complement }\times B\right)\cup \left(A\times B^{\complement }\right)\!,$ where $A^{\complement }$ denotes the absolute complement of A.
Other properties related with subsets are:
    ${\text{if }}A\subseteq B{\text{, then }}A\times C\subseteq B\times C;$
${\text{if both }}A,B\neq \emptyset {\text{, then }}A\times B\subseteq C\times D\!\iff \!A\subseteq C{\text{ and }}B\subseteq D.$

### Cardinality

The cardinality of a set is the number of elements of the set. For example, defining two sets: A = {a, b} and B = {5, 6}. Both set A and set B consist of two elements each. Their Cartesian product, written as A × B, results in a new set which has the following elements:
    A × B = {(a,5), (a,6), (b,5), (b,6)}.
where each element of A is paired with each element of B, and where each pair makes up one element of the output set. The number of values in each element of the resulting set is equal to the number of sets whose Cartesian product is being taken; 2 in this case. The cardinality of the output set is equal to the product of the cardinalities of all the input sets. That is,
    |A × B| = |A| · |B|.
In this case, |A × B| = 4
Similarly,
    |A × B × C| = |A| · |B| · |C|
and so on.
The set A × B is infinite if either A or B is infinite, and the other set is not the empty set.

## Cartesian products of several sets
### n-ary Cartesian product

The Cartesian product can be generalized to the n-ary Cartesian product over n sets X$_1$, ..., X$_n$ as the set $X_{1}\times \cdots \times X_{n}=\{(x_{1},\ldots ,x_{n})\mid x_{i}\in X_{i}\ {\text{for every}}\ i\in \{1,\ldots ,n\}\}$
of n-tuples. If tuples are defined as nested ordered pairs, it can be identified with (X$_1$ × ... × X$_{n−1}$) × X$_n$. If a tuple is defined as a function on {1, 2, ..., n} that takes its value at i to be the i-th element of the tuple, then the Cartesian product X$_1$ × ... × X$_n$ is the set of functions $\{x:\{1,\ldots ,n\}\to X_{1}\cup \cdots \cup X_{n}\ |\ x(i)\in X_{i}\ {\text{for every}}\ i\in \{1,\ldots ,n\}\}.$

### n-ary Cartesian power

The Cartesian square of a set X is the Cartesian product X$^2$ = X × X. An example is the 2-dimensional plane R$^2$ = R × R where R is the set of real numbers: R$^2$ is the set of all points (x,y) where x and y are real numbers (see the Cartesian coordinate system).
The n-ary Cartesian power of a set X, denoted $X^{n}$, can be defined as $X^{n}=\underbrace {X\times X\times \cdots \times X} _{n}=\{(x_{1},\ldots ,x_{n})\ |\ x_{i}\in X\ {\text{for every}}\ i\in \{1,\ldots ,n\}\}.$
An example of this is R$^3$ = R × R × R, with R again the set of real numbers, and more generally R$^n$.
The n-ary Cartesian power of a set X is isomorphic to the space of functions from an n-element set to X. As a special case, the 0-ary Cartesian power of X may be taken to be a singleton set, corresponding to the empty function with codomain X.

### Intersections, unions, complements and subsets

Let Cartesian products be given $A=A_{1}\times \dots \times A_{n}$ and $B=B_{1}\times \dots \times B_{n}$. Then
* $A\subseteq B$, if and only if $A_{i}\subseteq B_{i}$ for all $i=1,2,\ldots ,n$;
* $A\cap B=(A_{1}\cap B_{1})\times \dots \times (A_{n}\cap B_{n})$, at the same time, if there exists at least one $i$ such that $A_{i}\cap B_{i}=\varnothing$, then $A\cap B=\varnothing$;
* $A\cup B\subseteq (A_{1}\cup B_{1})\times \dots \times (A_{n}\cup B_{n})$, moreover, equality is possible only in the following cases:
** $A\subseteq B$ or $B\subseteq A$;
** for all $i=1,2,\ldots ,n\quad A_{i}=B_{i}\quad$ except for one from $i$.
* The complement of a Cartesian product $A=A_{1}\times \dots \times A_{n}$ can be calculated, if a universe is defined $U=X_{1}\times \dots \times X_{n}$. To simplify the expressions, we introduce the following notation. Let us denote the Cartesian product as a tuple bounded by square brackets; this tuple includes the sets from which the Cartesian product is formed, e.g.:
    $A=A_{1}\times A_{2}\times \dots \times A_{n}=[A_{1}\quad A_{2}\quad \dots \quad A_{n}]$.
In n-tuple algebra (NTA), such a matrix-like representation of Cartesian products is called a C-n-tuple.
With this in mind, the union of some Cartesian products given in the same universe can be expressed as a matrix bounded by square brackets, in which the rows represent the Cartesian products involved in the union:
    $A\cup B=(A_{1}\times A_{2}\times \dots \times A_{n})\cup (B_{1}\times B_{2}\times \dots \times B_{n})=\left[{\begin{array}{cccc}A_{1}&A_{2}&\dots &A_{n}\\B_{1}&B_{2}&\dots &B_{n}\end{array}}\right]$.
Such a structure is called a C-system in NTA.
Then the complement of the Cartesian product $A$ will look like the following C-system expressed as a matrix of the dimension $n\times n$:
    $A^{\complement }=\left[{\begin{array}{ccccc}A_{1}^{\complement }&X_{2}&\dots &X_{n-1}&X_{n}\\X_{1}&A_{2}^{\complement }&\dots &X_{n-1}&X_{n}\\\dots &\dots &\dots &\dots &\dots \\X_{1}&X_{2}&\dots &A_{n-1}^{\complement }&X_{n}\\X_{1}&X_{2}&\dots &X_{n-1}&A_{n}^{\complement }\end{array}}\right]$.
The diagonal components of this matrix $A_{i}^{\complement }$ are equal correspondingly to $X_{i}\setminus A_{i}$.
In NTA, a diagonal C-system $A^{\complement }$, that represents the complement of a C-n-tuple $A$, can be written concisely as a tuple of diagonal components bounded by inverted square brackets:
    $A^{\complement }=]A_{1}^{\complement }\quad A_{2}^{\complement }\quad \dots \quad A_{n}^{\complement }[$.
This structure is called a D-n-tuple. Then the complement of the C-system $R$ is a structure $R^{\complement }$, represented by a matrix of the same dimension and bounded by inverted square brackets, in which all components are equal to the complements of the components of the initial matrix $R$. Such a structure is called a D-system and is calculated, if necessary, as the intersection of the D-n-tuples contained in it. For instance, if the following C-system is given:
    $R_{1}=\left[{\begin{array}{cccc}A_{1}&A_{2}&\dots &A_{n}\\B_{1}&B_{2}&\dots &B_{n}\end{array}}\right]$,
then its complement will be the D-system
    $R_{1}^{\complement }=\left]{\begin{array}{cccc}A_{1}^{\complement }&A_{2}^{\complement }&\dots &A_{n}^{\complement }\\B_{1}^{\complement }&B_{2}^{\complement }&\dots &B_{n}^{\complement }\end{array}}\right[$.
Let us consider some new relations for structures with Cartesian products obtained in the process of studying the properties of NTA. The structures defined in the same universe are called homotypic ones.
* The intersection of C-systems. Assume the homotypic C-systems are given $P$ and $Q$. Their intersection will yield a C-system containing all non-empty intersections of each C-n-tuple from $P$ with each C-n-tuple from $Q$.
* Checking the inclusion of a C-n-tuple into a D-n-tuple. For the C-n-tuple $P=[P_{1}\quad P_{2}\quad \cdots \quad P_{N}]$ and the D-n-tuple $Q=]Q_{1}\quad Q_{2}\quad \cdots \quad Q_{N}[$ holds $P\subseteq Q$, if and only if, at least for one $i$ holds $P_{i}\subseteq Q_{i}$.
* Checking the inclusion of a C-n-tuple into a D-system. For the C-n-tuple $P$ and the D-system $Q$ is true $P\subseteq Q$, if and only if, for every D-n-tuple $Q_{i}$ from $Q$ holds $P\subseteq Q_{i}$.

### Infinite Cartesian products

It is possible to define the Cartesian product of an arbitrary (possibly infinite) indexed family of sets. If I is any index set, and $\{X_{i}\}_{i\in I}$ is a family of sets indexed by I, then the Cartesian product of the sets in $\{X_{i}\}_{i\in I}$ is defined to be $\prod _{i\in I}X_{i}=\left\{\left.f:I\to \bigcup _{i\in I}X_{i}\ \right|\ \forall i\in I.\ f(i)\in X_{i}\right\},$ that is, the set of all functions defined on the index set I such that the value of the function at a particular index i is an element of X$_i$. Even if each of the X$_i$ is nonempty, the Cartesian product may be empty if the axiom of choice, which is equivalent to the statement that every such product is nonempty, is not assumed. $\prod _{i\in I}X_{i}$ may also be denoted ${\mathsf {X}}$$_{i\in I}X_{i}$.
For each j in I, the function $\pi _{j}:\prod _{i\in I}X_{i}\to X_{j},$ defined by $\pi _{j}(f)=f(j)$ is called the j-th projection map.
Cartesian power is a Cartesian product where all the factors X$_i$ are the same set X. In this case, $\prod _{i\in I}X_{i}=\prod _{i\in I}X$ is the set of all functions from I to X, and is frequently denoted X$^I$. This case is important in the study of cardinal exponentiation. An important special case is when the index set is $\mathbb {N}$, the natural numbers: this Cartesian product is the set of all infinite sequences with the i-th term in its corresponding set X$_i$. For example, each element of $\prod _{n=1}^{\infty }\mathbb {R} =\mathbb {R} \times \mathbb {R} \times \cdots$ can be visualized as a vector with countably infinite real number components. This set is frequently denoted $\mathbb {R} ^{\omega }$, or $\mathbb {R} ^{\mathbb {N} }$.

## Other forms
### Abbreviated form

If several sets are being multiplied together (e.g., X$_1$, X$_2$, X$_3$, ...), then some authors choose to abbreviate the Cartesian product as simply ×X$_i$.

### Cartesian product of functions

If f is a function from X to A and g is a function from Y to B, then their Cartesian product f × g is a function from X × Y to A × B with $(f\times g)(x,y)=(f(x),g(y)).$
This can be extended to tuples and infinite collections of functions. This is different from the standard Cartesian product of functions considered as sets.

### Cylinder

Let $A$ be a set and $B\subseteq A$. Then the cylinder of $B$ with respect to $A$ is the Cartesian product $B\times A$ of $B$ and $A$.
Normally, $A$ is considered to be the universe of the context and is left away. For example, if $B$ is a subset of the natural numbers $\mathbb {N}$, then the cylinder of $B$ is $B\times \mathbb {N}$.

## Definitions outside set theory
### Category theory

Although the Cartesian product is traditionally applied to sets, category theory provides a more general interpretation of the product of mathematical structures. This is distinct from, although related to, the notion of a Cartesian square in category theory, which is a generalization of the fiber product.
Exponentiation is the right adjoint of the Cartesian product; thus any category with a Cartesian product (and a final object) is a Cartesian closed category.

### Graph theory

In graph theory, the Cartesian product of two graphs G and H is the graph denoted by G × H, whose vertex set is the (ordinary) Cartesian product V(G) × V(H) and such that two vertices (u,v) and (u′,v′) are adjacent in G × H, if and only if u = u′ and v is adjacent with v′ in H, or v = v′ and u is adjacent with u′ in G. The Cartesian product of graphs is not a product in the sense of category theory. Instead, the categorical product is known as the tensor product of graphs.