In
mathematics
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
, a ternary operation is an ''n''-
ary operation with ''n'' = 3. A ternary operation on a
set ''A'' takes any given three elements of ''A'' and combines them to form a single element of ''A''.
In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, a ternary operator is an
operator that takes three
arguments
An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persua ...
as input and returns one output.
Examples

The
function is an example of a ternary operation on the
integer
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
s (or on any structure where
and
are both defined). Properties of this ternary operation have been used to define
planar ternary rings in the foundations of
projective geometry
In mathematics, projective geometry is the study of geometric properties that are invariant with respect to projective transformations. This means that, compared to elementary Euclidean geometry, projective geometry has a different setting (''p ...
.
In the
Euclidean plane
In mathematics, a Euclidean plane is a Euclidean space of Two-dimensional space, dimension two, denoted \textbf^2 or \mathbb^2. It is a geometric space in which two real numbers are required to determine the position (geometry), position of eac ...
with points ''a'', ''b'', ''c'' referred to an origin, the ternary operation
has been used to define
free vectors. Since (''abc'') = ''d'' implies ''b'' – ''a'' = ''c'' – ''d'', the
directed line segments ''b'' – ''a'' and ''c'' – ''d'' are
equipollent and are associated with the same free vector. Any three points in the plane ''a, b, c'' thus determine a
parallelogram with ''d'' at the fourth vertex.
In
projective geometry
In mathematics, projective geometry is the study of geometric properties that are invariant with respect to projective transformations. This means that, compared to elementary Euclidean geometry, projective geometry has a different setting (''p ...
, the process of finding a
projective harmonic conjugate
In projective geometry, the harmonic conjugate point of a point on the real projective line with respect to two other points is defined by the following construction:
:Given three collinear points , let be a point not lying on their join and le ...
is a ternary operation on three points. In the diagram, points ''A'', ''B'' and ''P'' determine point ''V'', the harmonic conjugate of ''P'' with respect to ''A'' and ''B''. Point ''R'' and the line through ''P'' can be selected arbitrarily, determining ''C'' and ''D''. Drawing ''AC'' and ''BD'' produces the intersection ''Q'', and ''RQ'' then yields ''V''.
Suppose ''A'' and ''B'' are given sets and
is the collection of
binary relation
In mathematics, a binary relation associates some elements of one Set (mathematics), set called the ''domain'' with some elements of another set called the ''codomain''. Precisely, a binary relation over sets X and Y is a set of ordered pairs ...
s between ''A'' and ''B''.
Composition of relations
In the mathematics of binary relations, the composition of relations is the forming of a new binary relation from two given binary relations ''R'' and ''S''. In the calculus of relations, the composition of relations is called relative multiplica ...
is always defined when ''A'' = ''B'', but otherwise a ternary composition can be defined by
where
is the
converse relation
In mathematics, the converse of a binary relation is the relation that occurs when the order of the elements is switched in the relation. For example, the converse of the relation 'child of' is the relation 'parent of'. In formal terms ...
of ''q''. Properties of this ternary relation have been used to set the axioms for a
heap.
In
Boolean algebra
In mathematics and mathematical logic, Boolean algebra is a branch of algebra. It differs from elementary algebra in two ways. First, the values of the variable (mathematics), variables are the truth values ''true'' and ''false'', usually denot ...
,
defines the formula
.
Computer science
In computer science, a ternary operator is an
operator that takes three arguments (or operands).
The arguments and result can be of different types. Many
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s that use
C-like syntax feature a ternary operator,
?:
, which defines a
conditional expression
In computer science, conditionals (that is, conditional statements, conditional expressions and conditional constructs) are programming language constructs that perform different computations or actions or return different values depending on t ...
. In some languages, this operator is referred to as the ''conditional operator''.
In
Python, the ternary conditional operator reads
x if C else y
. Python also supports ternary operations called
array slicing
In computer programming, array slicing is an operation that extracts a subset of elements from an array and packages them as another array, possibly in a different dimension from the original.
Common examples of array slicing are extracting a ...
, e.g.
a :c/code> return an array where the first element is a /code> and last element is a -1/code>. OCaml
OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
expressions provide ternary operations against records, arrays, and strings: a. -c
would mean the string a
where index b
has value c
.
The multiply–accumulate operation is another ternary operator.
Another example of a ternary operator is ''between'', as used in SQL.
The Icon programming language has a "to-by" ternary operator: the expression 1 to 10 by 2
generates the odd integers from 1 through 9.
In Excel formulae, the form is =if(C, x, y).
See also
* Unary operation
In mathematics, a unary operation is an operation with only one operand, i.e. a single input. This is in contrast to ''binary operations'', which use two operands. An example is any function , where is a set; the function is a unary operation ...
* Unary function
* Binary operation
In mathematics, a binary operation or dyadic operation is a rule for combining two elements (called operands) to produce another element. More formally, a binary operation is an operation of arity two.
More specifically, a binary operation ...
* Iterated binary operation
In mathematics, an iterated binary operation is an extension of a binary operation on a set ''S'' to a function on finite sequences of elements of ''S'' through repeated application. Common examples include the extension of the addition operation ...
* Binary function
* Median algebra or Majority function
In Boolean logic, the majority function (also called the median operator) is the Boolean function that evaluates to false when half or more arguments are false and true otherwise, i.e. the value of the function equals the value of the majority of t ...
* Ternary conditional operator
In computer programming, the ternary conditional operator is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator, conditional ...
for a list of ternary operators in computer programming languages
* Ternary Exclusive or
Exclusive or, exclusive disjunction, exclusive alternation, logical non-equivalence, or logical inequality is a logical operator whose negation is the logical biconditional. With two inputs, XOR is true if and only if the inputs differ (on ...
* Ternary equivalence relation
References
External links
*{{Commons category-inline, Ternary operations