A metric, sometimes referred to as a distance function, is a function that defines a real nonnegative distance between every two elements of a set. It is commonly denoted by the variable . In colon-to notation, a metric that defines distances between elements of the set is written:
In this case we say is a metric on .
That is, a metric on a set takes as input any two elements and from and outputs a number that is taken to define their distance in under . Apart from being nonnegative real numbers, the distances a metric outputs must follow three other rules in order for the function to meet the definition of a metric. A function that matches the above colon-to notation is called a metric if and only if it satisfies these requirements. The following must hold for any choice of , , and in :
(1) effectively states both that the distance from an element to itself is 0, and that the distance between non-identical elements must be greater than 0. (2) asserts that a metric must be commutative; informally the distance from to must be the same as the distance from to . Finally, (3) is known as the triangle_inequality and asserts that the distance from to is at most as large as the sum of the distances from to and from to . It is named as such because in Euclidean_space, the points , , and form a triangle, and the inequality requires that the length of one side of the triangle is not longer than the sum of the lengths of the other two sides; violating this would mean that the shortest path between two points is no longer the straight line between them.
It is possible (and relatively common!) to deal with multiple different metrics on the same set. This means we are using the same set elements as labels, but treating the distances between elements differently; in this case the different metric spaces we are defining may have very different properties. If multiple metrics are being considered, we must be careful when speaking of distances between elements of the set to specify which metric we are using. For example, if and are both metrics on , we cannot just say "the distance between and in " because it is ambiguous whether we are referring to or to . We could instead say something like "the distance between and under " to remove the ambiguity.
The most commonly-used metric on Cartesian space is the Euclidean metric, defined in two dimensions as , and more generally in dimensions as .
A less-common metric on Cartesian space is the Manhattan metric, defined generally as ; the distance is analogous to the distance taken between two points on a rectangular grid when motion is constrained to be purely vertical or horizontal, but not diagonal.