In mathematics, 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, a ternary operator is an operator that takes three arguments as input and returns one output.
Examples
thumb|right|Given A, B and point P, geometric construction yields V, the projective harmonic conjugate of P with respect to A and B.
The function <math>T(a, b, c) = ab + c</math> is an example of a ternary operation on the integers (or on any structure where <math>+</math> and <math>\times</math> are both defined). Properties of this ternary operation have been used to define planar ternary rings in the foundations of projective geometry.
In the Euclidean plane with points a, b, c referred to an origin, the ternary operation <math>[a, b, c] = a - b + c</math> 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, the process of finding a projective harmonic conjugate 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 <math>\mathcal{B}(A, B)</math> is the collection of binary relations between A and B. Composition of relations is always defined when A = B, but otherwise a ternary composition can be defined by <math>[p, q, r] = p q^T r</math> where <math>q^T</math> is the converse relation of q. Properties of this ternary relation have been used to set the axioms for a heap.
In Boolean algebra, <math>T(A,B,C) = AC+(1-A)B</math> defines the formula <math>(A \lor B) \land (\lnot A \lor C)</math>.
Computer science
In computer science, an operator is a ternary operator if it takes three arguments (or operands). feature the ternary conditional operator, <code>?:</code>, which defines a conditional expression that yields a value. This is sometimes referred to simply as the ternary operator, despite that several unrelated ternary operators exist.
In the expression <code>x = a ? b : c</code> the variable x will be assigned the value b if a is true. Otherwise it will be assigned the value c.
Some languages use a different syntax. In Python, the same expression would take the form <code>x = b if a else c</code>. In Excel formulae, the form is <code>=IF(a, b, c)</code>.
Many languages do not have a ternary conditional operator, though some have an alternative. For instance, although Ruby does have the ternary conditional operator, its <code>if/elsif/else</code> flow control structure yields a value, so it can serve the same purpose. In SQL, the <code>CASE</code> expression evaluates many conditionals to yield a value. These examples are not strictly ternary because they may have more than three components.
Ternary operators other than the ternary conditional operator exist.
In Python the expression <code>a[b:c]</code> will slice a portion of an array. The result is a new array containing all the elements of a from b to c-1.
In OCaml the expression <code>a.(b) <- c</code> updates element b of array a to value c.
In some assembly languages the MAD operation is in ternary form. The statement <code>MAD a, b, c</code> multiplies b and c, adds the result to a, and stores the final result in a, all in a single CPU cycle. In some assembly languages the order of the operands may differ. In some, the operation isn't ternary because it requires a fourth operand to indicate the location where the result will be stored.
The SQL expression BETWEEN is ternary, as in <code>age BETWEEN 90 AND 100</code>.
The Icon expression to becomes ternary when used with by, as in <code>1 to 10 by 2</code>, which generates the odd integers from 1 through 9.
See also
- Unary operation
- Unary function
- Binary operation
- Iterated binary operation
- Binary function
- Median algebra or Majority function
- Ternary conditional operator for a list of ternary operators in computer programming languages
- Ternary Exclusive or
- Ternary equivalence relation
