Is there a math expression equivalent to the conditional ternary operator?
Is there a math equivalent of the ternary conditional operator as used in programming?
a = b + (c > 0 ? 1 : 2)
The above means that if $c$ is greater than $0$ then $a = b + 1$, otherwise $a = b + 2$.
From physics, I'm used to seeing the Kronecker delta,$$ {\delta}_{ij} \equiv \left\{ \begin{array}{lll} 1 &\text{if} & i=j \\ 0 &\text{else} \end{array} \right. _{,} $$and I think people who work with it find the slightly generalized notation$$ {\delta}_{\left[\text{condition}\right]} \equiv \left\{ \begin{array}{lll} 1 &\text{if} & \left[\text{condition}\right] \\ 0 &\text{else} \end{array} \right. $$to be pretty natural to them.
So, I tend to use $\delta_{\left[\text{condition}\right]}$ for a lot of things. Just seems so simple and well-understood.
Transforms:
-
Basic Kronecker delta:
To write the basic Kronecker delta in terms of the generalized Kronecker delta, it's just$$ \delta_{ij} \Rightarrow \delta_{i=j} \,.$$It's almost the same notation, and I think most folks can figure it out pretty easily without needing it explained. -
Conditional operator:
The "conditional operator" or "ternary operator" for the simple case of?1:0
:$$ \begin{array}{ccccc} \boxed{ \begin{array}{l} \texttt{if}~\left(\texttt{condition}\right) \\ \{ \\ ~~~~\texttt{return 1;} \\ \} \\ \texttt{else} \\ \{ \\ ~~~~\texttt{return 0;} \\ \} \end{array} ~} & \Rightarrow & \boxed{~ \texttt{condition ? 1 : 0} ~} & \Rightarrow & \delta_{\left[\text{condition}\right]} \end{array} _{.} $$Then if you want a non-zero value for thefalse
-case, you'd just add another Kronecker delta, $\delta_{\operatorname{NOT}\left(\left[\text{condition}\right]\right)} ,$ e.g. $\delta_{i \neq j} .$ -
Indicator function:
@SiongThyeGoh's answer recommended using indicator function notation. I'd rewrite their example like$$ \begin{array}{ccccc} \underbrace{a=b+1+\mathbb{1}_{(-\infty, 0]}(c)} _{\text{their example}} & \Rightarrow & \underbrace{a=b+1+ \delta_{c \in \left(-\infty, 0\right]}} _{\text{direct translation}} & \Rightarrow & \underbrace{a=b+1+ \delta_{c \, {\small{\leq}} \, 0}} _{\text{cleaner form}} \end{array} \,. $$ -
Iverson bracket:
Iverson bracket notation, as suggested in @FredH's answer, is very similar to the generalized Kronekcer delta. For example:$$ \delta_{i=j} ~~ \Rightarrow ~~ \left[i = j \right] \,.$$Dropping the $`` \delta "$ reduces backwards-compatibility withe the basic Kroncker delta, plus it weakens the signal about what the notation means, so it's probably not as good in general contexts right now. However, Iverson bracket notation should be easier to read and write, so when reinforcing the meaning of the notation isn't a big issue, it could be preferable.
Note: "Conditional operator" rather than "ternary operator".
The conditional operator, condition ? trueValue : falseValue
, has 3 arguments, making it an example of a ternary operator. By contrast, most other operators in programming tend to be unary operators (which have 1 argument) or binary operators (which have 2 arguments).
Since the conditional operator is fairly unique in being a ternary operator, it's often been called "the ternary operator", leading many to believe that that's its name. However, "conditional operator" is more specific and should generally be preferred.
The expression b + (c > 0 ? 1 : 2)
is not a ternary operator; it is a function of two variables. There is one operation that results in $a$. You can certainly define a function
$$f(b,c)=\begin {cases} b+1&c \gt 0\\
b+2 & c \le 0 \end {cases}$$
You can also define functions with any number of inputs you want, so you can define $f(a,b,c)=a(b+c^2)$, for example. This is a ternary function.
In Concrete Mathematics by Graham, Knuth and Patashnik, the authors use the "Iverson bracket" notation: Square brackets around a statement represent $1$ if the statement is true and $0$ otherwise. Using this notation, you could write $$ a = b + 2 - [c \gt 0]. $$