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
The function T ( a , b , c ) = a b + c {\displaystyle T(a,b,c)=ab+c} is an example of a ternary operation on the integers (or on any structure where + {\displaystyle +} and × {\displaystyle \times } 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 [ a , b , c ] = a − b + c {\displaystyle [a,b,c]=a-b+c} has been used to define free vectors.2 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 B ( A , B ) {\displaystyle {\mathcal {B}}(A,B)} 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 [ p , q , r ] = p q T r {\displaystyle [p,q,r]=pq^{T}r} where q T {\displaystyle q^{T}} is the converse relation of q. Properties of this ternary relation have been used to set the axioms for a heap.3
In Boolean algebra, T ( A , B , C ) = A C + ( 1 − A ) B {\displaystyle T(A,B,C)=AC+(1-A)B} defines the formula ( A ∨ B ) ∧ ( ¬ A ∨ C ) {\displaystyle (A\lor B)\land (\lnot A\lor C)} .
Computer science
In computer science, a ternary operator is an operator that takes three arguments (or operands).4 The arguments and result can be of different types. Many programming languages that use C-like syntax5 feature a ternary operator, ?:, which defines a conditional expression. 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, e.g. a[b:c] return an array where the first element is a[b] and last element is a[c-1].6 OCaml expressions provide ternary operations against records, arrays, and strings: a.[b]<-c would mean the string a where index b has value c.7
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
- 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
External links
- Media related to Ternary operations at Wikimedia Commons
References
MDN, nmve. "Conditional (ternary) Operator". Mozilla Developer Network. Retrieved 20 February 2017. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator ↩
Jeremiah Certaine (1943) The ternary operation (abc) = a b−1c of a group, Bulletin of the American Mathematical Society 49: 868–77 MR0009953 https://www.ams.org/journals/bull/1943-49-12/S0002-9904-1943-08042-1/S0002-9904-1943-08042-1.pdf ↩
Christopher Hollings (2014) Mathematics across the Iron Curtain: a history of the algebraic theory of semigroups, page 264, History of Mathematics 41, American Mathematical Society ISBN 978-1-4704-1493-1 /wiki/American_Mathematical_Society ↩
MDN, nmve. "Conditional (ternary) Operator". Mozilla Developer Network. Retrieved 20 February 2017. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator ↩
Hoffer, Alex. "Ternary Operator". Cprogramming.com. Retrieved 20 February 2017. https://www.cprogramming.com/reference/operators/ternary-operator.html ↩
"6. Expressions — Python 3.9.1 documentation". docs.python.org. Retrieved 2021-01-19. https://docs.python.org/3/reference/expressions.html ↩
"The OCaml Manual: Chapter 11 The OCaml language: (7) Expressions". ocaml.org. Retrieved 2023-05-03. https://v2.ocaml.org/manual/expr.html ↩