Is there a math equivalent to the conditional ternary operator?what is ≡ operator equal to in math?Math...
Formatting a table to look nice
Draw bounding region by list of points
I can't die. Who am I?
Being asked to review a paper in conference one has submitted to
Can an earth elemental drown/bury its opponent underground using earth glide?
How to fix my table, centering of columns
How to mitigate "bandwagon attacking" from players?
Is every open circuit a capacitor?
What is better: yes / no radio, or simple checkbox?
Correct physics behind the colors on CD (compact disc)?
Find maximum of the output from reduce
PTIJ: What’s wrong with eating meat and couscous?
How can I highlight parts in a screenshot
3.5% Interest Student Loan or use all of my savings on Tuition?
How do I deal with being envious of my own players?
Where is the fallacy here?
1970s scifi/horror novel where protagonist is used by a crablike creature to feed its larvae, goes mad, and is defeated by retraumatising him
Should we avoid writing fiction about historical events without extensive research?
Is there a math equivalent to the conditional ternary operator?
PTIJ: Aharon, King of Egypt
Would the melodic leap of the opening phrase of Mozart's K545 be considered dissonant?
Split a number into equal parts given the number of parts
How to disable or uninstall iTunes under High Sierra without disabling SIP
When to use mean vs median
Is there a math equivalent to the conditional ternary operator?
what is ≡ operator equal to in math?Math vocab: operator on $S$ and into $S =$?Is there any convention regarding the order of operation of the binary modulo operator?Difference between (partial) order, preference, transitive relation operatorsIs there a multiple function composition operator?Are there dictionaries in math?Cartesian product as an n-ary operatorAn operator that returns 0 for all negative values otherwise returns the inputDoes there exist a math operator that denotes to take the inverse of something?Multiplying two numbers using only the “left shift” operator
$begingroup$
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$.
notation computer-science
$endgroup$
|
show 4 more comments
$begingroup$
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$.
notation computer-science
$endgroup$
4
$begingroup$
@Alex $a = b + 2 - u(c)$
$endgroup$
– eyeballfrog
yesterday
14
$begingroup$
A better question here is: what is good notation in this case. There are many ways of writing this, many ways which one would almost never use (like the accepted answer).
$endgroup$
– Winther
yesterday
5
$begingroup$
In C-derived languages at least, you have made a grievous error! Your expression parses asa = (b + c) > 0 ? 1 : 2
. I always use parentheses in these cases, even when they are not strictly necessary.
$endgroup$
– TonyK
yesterday
3
$begingroup$
Just to clarify, by “ternary operator” you really mean “conditional operator”? Or are interested in any kind of ternary operator, regardless of semantics?
$endgroup$
– Konrad Rudolph
yesterday
1
$begingroup$
What @KonradRudolph said; you are showing the conditional operator, which is a ternary (takes three arguments; compare unary and binary) operator. I don't think the C language has any other ternary operator, and some form of the conditional operator is found in many languages because it's very convenient to have, but there's no rule that says a language couldn't include other ternary operators alongside (or without having) a conditional operator.
$endgroup$
– a CVn
yesterday
|
show 4 more comments
$begingroup$
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$.
notation computer-science
$endgroup$
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$.
notation computer-science
notation computer-science
edited 7 hours ago
PJTraill
683519
683519
asked yesterday
dataphiledataphile
31938
31938
4
$begingroup$
@Alex $a = b + 2 - u(c)$
$endgroup$
– eyeballfrog
yesterday
14
$begingroup$
A better question here is: what is good notation in this case. There are many ways of writing this, many ways which one would almost never use (like the accepted answer).
$endgroup$
– Winther
yesterday
5
$begingroup$
In C-derived languages at least, you have made a grievous error! Your expression parses asa = (b + c) > 0 ? 1 : 2
. I always use parentheses in these cases, even when they are not strictly necessary.
$endgroup$
– TonyK
yesterday
3
$begingroup$
Just to clarify, by “ternary operator” you really mean “conditional operator”? Or are interested in any kind of ternary operator, regardless of semantics?
$endgroup$
– Konrad Rudolph
yesterday
1
$begingroup$
What @KonradRudolph said; you are showing the conditional operator, which is a ternary (takes three arguments; compare unary and binary) operator. I don't think the C language has any other ternary operator, and some form of the conditional operator is found in many languages because it's very convenient to have, but there's no rule that says a language couldn't include other ternary operators alongside (or without having) a conditional operator.
$endgroup$
– a CVn
yesterday
|
show 4 more comments
4
$begingroup$
@Alex $a = b + 2 - u(c)$
$endgroup$
– eyeballfrog
yesterday
14
$begingroup$
A better question here is: what is good notation in this case. There are many ways of writing this, many ways which one would almost never use (like the accepted answer).
$endgroup$
– Winther
yesterday
5
$begingroup$
In C-derived languages at least, you have made a grievous error! Your expression parses asa = (b + c) > 0 ? 1 : 2
. I always use parentheses in these cases, even when they are not strictly necessary.
$endgroup$
– TonyK
yesterday
3
$begingroup$
Just to clarify, by “ternary operator” you really mean “conditional operator”? Or are interested in any kind of ternary operator, regardless of semantics?
$endgroup$
– Konrad Rudolph
yesterday
1
$begingroup$
What @KonradRudolph said; you are showing the conditional operator, which is a ternary (takes three arguments; compare unary and binary) operator. I don't think the C language has any other ternary operator, and some form of the conditional operator is found in many languages because it's very convenient to have, but there's no rule that says a language couldn't include other ternary operators alongside (or without having) a conditional operator.
$endgroup$
– a CVn
yesterday
4
4
$begingroup$
@Alex $a = b + 2 - u(c)$
$endgroup$
– eyeballfrog
yesterday
$begingroup$
@Alex $a = b + 2 - u(c)$
$endgroup$
– eyeballfrog
yesterday
14
14
$begingroup$
A better question here is: what is good notation in this case. There are many ways of writing this, many ways which one would almost never use (like the accepted answer).
$endgroup$
– Winther
yesterday
$begingroup$
A better question here is: what is good notation in this case. There are many ways of writing this, many ways which one would almost never use (like the accepted answer).
$endgroup$
– Winther
yesterday
5
5
$begingroup$
In C-derived languages at least, you have made a grievous error! Your expression parses as
a = (b + c) > 0 ? 1 : 2
. I always use parentheses in these cases, even when they are not strictly necessary.$endgroup$
– TonyK
yesterday
$begingroup$
In C-derived languages at least, you have made a grievous error! Your expression parses as
a = (b + c) > 0 ? 1 : 2
. I always use parentheses in these cases, even when they are not strictly necessary.$endgroup$
– TonyK
yesterday
3
3
$begingroup$
Just to clarify, by “ternary operator” you really mean “conditional operator”? Or are interested in any kind of ternary operator, regardless of semantics?
$endgroup$
– Konrad Rudolph
yesterday
$begingroup$
Just to clarify, by “ternary operator” you really mean “conditional operator”? Or are interested in any kind of ternary operator, regardless of semantics?
$endgroup$
– Konrad Rudolph
yesterday
1
1
$begingroup$
What @KonradRudolph said; you are showing the conditional operator, which is a ternary (takes three arguments; compare unary and binary) operator. I don't think the C language has any other ternary operator, and some form of the conditional operator is found in many languages because it's very convenient to have, but there's no rule that says a language couldn't include other ternary operators alongside (or without having) a conditional operator.
$endgroup$
– a CVn
yesterday
$begingroup$
What @KonradRudolph said; you are showing the conditional operator, which is a ternary (takes three arguments; compare unary and binary) operator. I don't think the C language has any other ternary operator, and some form of the conditional operator is found in many languages because it's very convenient to have, but there's no rule that says a language couldn't include other ternary operators alongside (or without having) a conditional operator.
$endgroup$
– a CVn
yesterday
|
show 4 more comments
10 Answers
10
active
oldest
votes
$begingroup$
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_{i=j}
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, 0right]}}
_{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 apparently the same thing; according to Wikipedia, it's meant as a generalization of the Kronecker delta, except they drop the $delta$ entirely, just putting the condition in square-brackets. In a context in readers expect it, Iverson bracket notation might be preferable if conditionals will be used a lot.
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.
$endgroup$
$begingroup$
wow, this is beautiful. I need it for an infinite series (with some elements excluded i.e. multiplied by zero and others included, multiplied by 1) so this cleaner form is exactly what I need.
$endgroup$
– dataphile
yesterday
$begingroup$
@dataphile Yeah, the Kronecker delta's great for specifying elements like that; it comes in handy if you're using Sigma/Pi/Einstein notations. Worth noting that it's basically the integrated Dirac delta; if you're working in a continuous domain, then the Dirac delta tends to be preferred, though it's largely the same thing.
$endgroup$
– Nat
yesterday
2
$begingroup$
The Kronecker delta isn’t a ternary operator, it’s a plain old binary operator, since it has two operands.
$endgroup$
– Konrad Rudolph
yesterday
$begingroup$
@KonradRudolph Yeah, gotta add a $delta_{operatorname{NOT}left(left[text{condition}right]right)}$-term to get thatfalse
-case behavior in there when it's non-zero.
$endgroup$
– Nat
yesterday
1
$begingroup$
The $1$-if-condition-else-$0$ generalisation of the Kronecker delta is called the Iverson bracket. We can generalise to any two input values we like with a suitable function, of domain ${0,,1}$, wraping the Iverson bracket.
$endgroup$
– J.G.
yesterday
|
show 9 more comments
$begingroup$
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.
$endgroup$
9
$begingroup$
Ternary operator in programming means a conditional statement of form "if A then x else y" and uually is written just as presented in the OP, i.e. A ? x : y. It would also literally be termary if the OP wouldn't have inserted the particular values of 1 and 2.
$endgroup$
– Džuris
yesterday
15
$begingroup$
@Džuris Ternary means "of arity 3", like binary means "of arity 2" and unary "of arity 1". The specific operator is the conditional ternary operator.
$endgroup$
– frabala
yesterday
15
$begingroup$
Once again, clarity beats compactness when it comes to mathematics.
$endgroup$
– Asaf Karagila♦
yesterday
6
$begingroup$
Well-chosen compact notations can contribute to clarity rather than detract from it, though.
$endgroup$
– Henning Makholm
yesterday
7
$begingroup$
@Džuris No it isn’t. The link you cite says, in its very first sentence, that “?:
is a ternary operator” (not “the ternary operator”; emphasis mine). Some people refer to it as “the ternary operator” but this is strictly incorrect (and merits a correction e.g. on Stack Overflow).
$endgroup$
– Konrad Rudolph
yesterday
|
show 9 more comments
$begingroup$
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].
$$
$endgroup$
7
$begingroup$
aka en.wikipedia.org/wiki/Iverson_bracket
$endgroup$
– qwr
yesterday
$begingroup$
@qwr Thank you! I did not recall the name.
$endgroup$
– FredH
yesterday
5
$begingroup$
I would find it clearer to write $a = b + 1[c>0] + 2[cle0]$.
$endgroup$
– Rahul
yesterday
5
$begingroup$
I personally find Iverson brackets less cluttered than indicator functions, so I definitely recommend this notation.
$endgroup$
– J. M. is not a mathematician
yesterday
4
$begingroup$
Surely this is a unary operator, which works much like for example!
in the C programming language: Input is an arbitrary expression, the result is either 0 or 1 depending on the expression.
$endgroup$
– pipe
yesterday
|
show 3 more comments
$begingroup$
Using the indicator function notation:$$a=b+1+mathbb{1}_{(-infty, 0]}(c)$$
$endgroup$
1
$begingroup$
Indicator is definitely the way to go, since the conditional can define an arbitrary set.
$endgroup$
– eyeballfrog
yesterday
$begingroup$
Thank you, that is very elegant. I will definitely try out all of the answers as I often use this in various scenarios.
$endgroup$
– dataphile
yesterday
2
$begingroup$
I often use - and have seen others use - $mathbb{1}$ ("blackboard bold") to denote the indicator function to distinguish it from the number $1$
$endgroup$
– Christopher
yesterday
1
$begingroup$
The indicator function isn’t a ternary operator, it only has two operands.
$endgroup$
– Konrad Rudolph
yesterday
add a comment |
$begingroup$
In math, equations are written in piecewise form by having a curly brace enclose multiple lines; each one with a condition excepting the last which has "otherwise".
There are a few custom operators that also occasionally make an appearance. E.g. the Heavyside function mentioned by Alex, the Dirac function, and the cyclical operator $delta_{ijk}$ - all of which can be used to emulate conditional behaviour.
$endgroup$
add a comment |
$begingroup$
One should realize that operators are just a fancy way of using functions.
So a ternary operator is a function of 3 variables that is notated in a different way. Is that useful? The answer is mostly not. Also realize that any mathematician is allowed to introduce any notation he feels is illustrative.
Let's review why we use binary operators at all like in a+b*c . Because parameters and results are of the same type, it makes sense to leave out parentheses and introduce complicated priority rules. Imagine that a b c are numbers and we have a normal + and a peculiar * that results in dragons. Now the expression doesn't make sense (assumming a high priority *), because there is no way to add numbers and dragons. Thusly most ternary operators results in a mess.
With a proper notation there are examples of ternary operations. For example, there is a special notation for "sum for i from a to b of expression". This takes two boundaries (numbers) and a function from a number of that type that results in another number. (Mathematician, read "element of an addition group" for number.)
The notation for integration is similarly ternary.
So in short ternary operators exist, and you can define your own. They are in general accompagnied with a special notation, or they are not helpful.
Now back to the special case you mention.
Because truth values are implied in math, an expression like "if a then b else c" makes sense if a represens a truth value like (7<12). The above expression is understood in every mathematical context. However in a context where truth values are not considered a set, (if .. then .. else ..) would not be considered an operator/function, but a textual explanation. A general accepted notation could be useful in math, but I'm not aware there is one. That is probably, because like in the above, informal notations are readily understood.
$endgroup$
add a comment |
$begingroup$
Following solution is not defined for $c = 0$; however it uses very basic operations only, which might be useful as you probably look for an expression to implement in a program:
$$a = b + 1lambda + 2(1-lambda)$$
where
$$lambda = frac{ 1 + frac{|c|}{c} }{2}$$
You need to make the problem discrete and make a choice from two values. So, given some value $c in mathbb{R}$ we need to calculate some value $lambda in {0,1}$ depnding on $c<0$ or $c>0$.
Knowing that
$$frac{|c|}{c} in {1,-1}$$
we can calculate the $lambda$ as follows:
$$lambda = frac{ 1 + frac{|c|}{c} }{2}$$
Now that our $lambda in {0,1}$ we can do the "choice" between the two constants $d$ and $e$ as follows:
$$dlambda + e(1-lambda)$$
which equals $d$ for $lambda = 1$, and $e$ for $lambda = 0$.
New contributor
$endgroup$
add a comment |
$begingroup$
There are many good answers that give notation for, “if this condition holds, then 1, else 0.” This corresponds to an even simpler expression in C;(x>1)
is equivalent to (x>1 ? 1 : 0)
.
It’s worth noting that the ternary operator is more general than that. If the arguments are elements of a ring, you could express c ? a : b
with (using Iverson-bracket notation) $(a-b) cdot [c] + b$, but not otherwise. (And compilers frequently use this trick, in a Boolean ring, to compile conditionals without needing to execute a branch instruction.) In a C program, evaluating the expressions $a$ or $b$ might have side-effects, such as deleting a file or printing a message to the screen. In a mathematical function, this isn’t something you would worry about, and a programming language where this is impossible is called functional.
Ross Millikan gave the most standard notation, a cases block. The closest equivalent in mathematical computer science is the if-then-else
function of Lambda Calculus.
$endgroup$
$begingroup$
"in C;(x>1) is equivalent to (x>1 ? 1 : 0)" Not exactly. Rather, C considers any nonzero value as equivalent from a truthiness perspective. So there is no difference between an integer expression taking the value of -1, 1, 42 orINT_MAX
when that expression is treated as a boolean rvalue. In C, the one special integer value, when treated as a boolean, is 0, representing false. That said, if someone actually used(x>1)
as a non-boolean expression and I noticed it, I would likely at least briefly try to find some heavy physical object that could be applied at high speed to their keyboard.
$endgroup$
– a CVn
yesterday
1
$begingroup$
@aCVn The C11 standard states, “Each of the operators<
(less than),>
(greater than),<=
(less than or equal to), and>=
(greater than or equal to) shall yield1
if the specified relation is true and0
if it is false.” Similarly for equality and inequality, “Each of the operators yields1
if the specified relation is true and0
if it is false.”
$endgroup$
– Davislor
yesterday
$begingroup$
@aCVn What you wrote is true, but the relational operators in C will only return the values1
or0
.
$endgroup$
– Davislor
yesterday
add a comment |
$begingroup$
I think mathematicians should not be afraid to use the Iverson bracket, including when teaching, as this is a generally very useful notation whose only slightly unusual feature is to introduce a logical expression in the middle of an algebraic one (but one already regularly finds conditions inside set-theoretic expressions, so it really is not a big deal). It may avoid a lot of clutter, notably many instances of clumsy expressions by cases with a big unmatched brace (which is usually only usable as the right hand side of a definition). Since brackets do have many other uses in mathematics, I personally prefer a typographically distinct representation of Iverson brackets, rendering your example as
$$def[#1]{[![{#1}]!]}
a= b + [c>0]1 + [cnot>0]2.
$$
This works best in additive context (though one can use Iverson brackets in the exponent for optional multiplicative factors). It is not really ideal for general two-way branches, as the condition must be repeated twice, one of them in negated form, but it happens that most of the time one needs $0$ as the value for one branch anyway.
As a more concise two-way branch, I can recall that Algol68 introduced the notation $b+(c>0mid 1mid 2)$ for the right-hand side of your equation; though this is a programming language and not mathematics, it was designed by mathematicians. They also had notation for multi-way branching: thus the solution to the recursion $a_{n+2}=a_{n+1}-a_n$ with initial values $a_0=0$, $a_1=1$ can be written
$$
a_n=(nbmod 6+1mid 0,1,1,0,-1,-1)
$$
(where the "${}+1$" is needed because in 1968 they still counted starting from $1$, which is a mistake), which is reasonably concise and readable, compared to other ways to express this result. Also consider, for month $m$ in year $y$, the number
$$
( m mid 31,(ybmod 4=0land ybmod 100neq0lor ybmod400=0mid 29mid 28)
,31,30,31,30,31,31,30,31,30,31).
$$
$endgroup$
add a comment |
$begingroup$
It can be viewed as inline function:
$$f(c)=begin {cases} 1&c gt 0\
2& c le 0 end {cases}$$
so it comes as:
$$a = b + f(c)$$
used in everyday mathematics.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3135798%2fis-there-a-math-equivalent-to-the-conditional-ternary-operator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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_{i=j}
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, 0right]}}
_{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 apparently the same thing; according to Wikipedia, it's meant as a generalization of the Kronecker delta, except they drop the $delta$ entirely, just putting the condition in square-brackets. In a context in readers expect it, Iverson bracket notation might be preferable if conditionals will be used a lot.
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.
$endgroup$
$begingroup$
wow, this is beautiful. I need it for an infinite series (with some elements excluded i.e. multiplied by zero and others included, multiplied by 1) so this cleaner form is exactly what I need.
$endgroup$
– dataphile
yesterday
$begingroup$
@dataphile Yeah, the Kronecker delta's great for specifying elements like that; it comes in handy if you're using Sigma/Pi/Einstein notations. Worth noting that it's basically the integrated Dirac delta; if you're working in a continuous domain, then the Dirac delta tends to be preferred, though it's largely the same thing.
$endgroup$
– Nat
yesterday
2
$begingroup$
The Kronecker delta isn’t a ternary operator, it’s a plain old binary operator, since it has two operands.
$endgroup$
– Konrad Rudolph
yesterday
$begingroup$
@KonradRudolph Yeah, gotta add a $delta_{operatorname{NOT}left(left[text{condition}right]right)}$-term to get thatfalse
-case behavior in there when it's non-zero.
$endgroup$
– Nat
yesterday
1
$begingroup$
The $1$-if-condition-else-$0$ generalisation of the Kronecker delta is called the Iverson bracket. We can generalise to any two input values we like with a suitable function, of domain ${0,,1}$, wraping the Iverson bracket.
$endgroup$
– J.G.
yesterday
|
show 9 more comments
$begingroup$
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_{i=j}
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, 0right]}}
_{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 apparently the same thing; according to Wikipedia, it's meant as a generalization of the Kronecker delta, except they drop the $delta$ entirely, just putting the condition in square-brackets. In a context in readers expect it, Iverson bracket notation might be preferable if conditionals will be used a lot.
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.
$endgroup$
$begingroup$
wow, this is beautiful. I need it for an infinite series (with some elements excluded i.e. multiplied by zero and others included, multiplied by 1) so this cleaner form is exactly what I need.
$endgroup$
– dataphile
yesterday
$begingroup$
@dataphile Yeah, the Kronecker delta's great for specifying elements like that; it comes in handy if you're using Sigma/Pi/Einstein notations. Worth noting that it's basically the integrated Dirac delta; if you're working in a continuous domain, then the Dirac delta tends to be preferred, though it's largely the same thing.
$endgroup$
– Nat
yesterday
2
$begingroup$
The Kronecker delta isn’t a ternary operator, it’s a plain old binary operator, since it has two operands.
$endgroup$
– Konrad Rudolph
yesterday
$begingroup$
@KonradRudolph Yeah, gotta add a $delta_{operatorname{NOT}left(left[text{condition}right]right)}$-term to get thatfalse
-case behavior in there when it's non-zero.
$endgroup$
– Nat
yesterday
1
$begingroup$
The $1$-if-condition-else-$0$ generalisation of the Kronecker delta is called the Iverson bracket. We can generalise to any two input values we like with a suitable function, of domain ${0,,1}$, wraping the Iverson bracket.
$endgroup$
– J.G.
yesterday
|
show 9 more comments
$begingroup$
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_{i=j}
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, 0right]}}
_{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 apparently the same thing; according to Wikipedia, it's meant as a generalization of the Kronecker delta, except they drop the $delta$ entirely, just putting the condition in square-brackets. In a context in readers expect it, Iverson bracket notation might be preferable if conditionals will be used a lot.
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.
$endgroup$
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_{i=j}
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, 0right]}}
_{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 apparently the same thing; according to Wikipedia, it's meant as a generalization of the Kronecker delta, except they drop the $delta$ entirely, just putting the condition in square-brackets. In a context in readers expect it, Iverson bracket notation might be preferable if conditionals will be used a lot.
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.
edited yesterday
answered yesterday
NatNat
9581713
9581713
$begingroup$
wow, this is beautiful. I need it for an infinite series (with some elements excluded i.e. multiplied by zero and others included, multiplied by 1) so this cleaner form is exactly what I need.
$endgroup$
– dataphile
yesterday
$begingroup$
@dataphile Yeah, the Kronecker delta's great for specifying elements like that; it comes in handy if you're using Sigma/Pi/Einstein notations. Worth noting that it's basically the integrated Dirac delta; if you're working in a continuous domain, then the Dirac delta tends to be preferred, though it's largely the same thing.
$endgroup$
– Nat
yesterday
2
$begingroup$
The Kronecker delta isn’t a ternary operator, it’s a plain old binary operator, since it has two operands.
$endgroup$
– Konrad Rudolph
yesterday
$begingroup$
@KonradRudolph Yeah, gotta add a $delta_{operatorname{NOT}left(left[text{condition}right]right)}$-term to get thatfalse
-case behavior in there when it's non-zero.
$endgroup$
– Nat
yesterday
1
$begingroup$
The $1$-if-condition-else-$0$ generalisation of the Kronecker delta is called the Iverson bracket. We can generalise to any two input values we like with a suitable function, of domain ${0,,1}$, wraping the Iverson bracket.
$endgroup$
– J.G.
yesterday
|
show 9 more comments
$begingroup$
wow, this is beautiful. I need it for an infinite series (with some elements excluded i.e. multiplied by zero and others included, multiplied by 1) so this cleaner form is exactly what I need.
$endgroup$
– dataphile
yesterday
$begingroup$
@dataphile Yeah, the Kronecker delta's great for specifying elements like that; it comes in handy if you're using Sigma/Pi/Einstein notations. Worth noting that it's basically the integrated Dirac delta; if you're working in a continuous domain, then the Dirac delta tends to be preferred, though it's largely the same thing.
$endgroup$
– Nat
yesterday
2
$begingroup$
The Kronecker delta isn’t a ternary operator, it’s a plain old binary operator, since it has two operands.
$endgroup$
– Konrad Rudolph
yesterday
$begingroup$
@KonradRudolph Yeah, gotta add a $delta_{operatorname{NOT}left(left[text{condition}right]right)}$-term to get thatfalse
-case behavior in there when it's non-zero.
$endgroup$
– Nat
yesterday
1
$begingroup$
The $1$-if-condition-else-$0$ generalisation of the Kronecker delta is called the Iverson bracket. We can generalise to any two input values we like with a suitable function, of domain ${0,,1}$, wraping the Iverson bracket.
$endgroup$
– J.G.
yesterday
$begingroup$
wow, this is beautiful. I need it for an infinite series (with some elements excluded i.e. multiplied by zero and others included, multiplied by 1) so this cleaner form is exactly what I need.
$endgroup$
– dataphile
yesterday
$begingroup$
wow, this is beautiful. I need it for an infinite series (with some elements excluded i.e. multiplied by zero and others included, multiplied by 1) so this cleaner form is exactly what I need.
$endgroup$
– dataphile
yesterday
$begingroup$
@dataphile Yeah, the Kronecker delta's great for specifying elements like that; it comes in handy if you're using Sigma/Pi/Einstein notations. Worth noting that it's basically the integrated Dirac delta; if you're working in a continuous domain, then the Dirac delta tends to be preferred, though it's largely the same thing.
$endgroup$
– Nat
yesterday
$begingroup$
@dataphile Yeah, the Kronecker delta's great for specifying elements like that; it comes in handy if you're using Sigma/Pi/Einstein notations. Worth noting that it's basically the integrated Dirac delta; if you're working in a continuous domain, then the Dirac delta tends to be preferred, though it's largely the same thing.
$endgroup$
– Nat
yesterday
2
2
$begingroup$
The Kronecker delta isn’t a ternary operator, it’s a plain old binary operator, since it has two operands.
$endgroup$
– Konrad Rudolph
yesterday
$begingroup$
The Kronecker delta isn’t a ternary operator, it’s a plain old binary operator, since it has two operands.
$endgroup$
– Konrad Rudolph
yesterday
$begingroup$
@KonradRudolph Yeah, gotta add a $delta_{operatorname{NOT}left(left[text{condition}right]right)}$-term to get that
false
-case behavior in there when it's non-zero.$endgroup$
– Nat
yesterday
$begingroup$
@KonradRudolph Yeah, gotta add a $delta_{operatorname{NOT}left(left[text{condition}right]right)}$-term to get that
false
-case behavior in there when it's non-zero.$endgroup$
– Nat
yesterday
1
1
$begingroup$
The $1$-if-condition-else-$0$ generalisation of the Kronecker delta is called the Iverson bracket. We can generalise to any two input values we like with a suitable function, of domain ${0,,1}$, wraping the Iverson bracket.
$endgroup$
– J.G.
yesterday
$begingroup$
The $1$-if-condition-else-$0$ generalisation of the Kronecker delta is called the Iverson bracket. We can generalise to any two input values we like with a suitable function, of domain ${0,,1}$, wraping the Iverson bracket.
$endgroup$
– J.G.
yesterday
|
show 9 more comments
$begingroup$
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.
$endgroup$
9
$begingroup$
Ternary operator in programming means a conditional statement of form "if A then x else y" and uually is written just as presented in the OP, i.e. A ? x : y. It would also literally be termary if the OP wouldn't have inserted the particular values of 1 and 2.
$endgroup$
– Džuris
yesterday
15
$begingroup$
@Džuris Ternary means "of arity 3", like binary means "of arity 2" and unary "of arity 1". The specific operator is the conditional ternary operator.
$endgroup$
– frabala
yesterday
15
$begingroup$
Once again, clarity beats compactness when it comes to mathematics.
$endgroup$
– Asaf Karagila♦
yesterday
6
$begingroup$
Well-chosen compact notations can contribute to clarity rather than detract from it, though.
$endgroup$
– Henning Makholm
yesterday
7
$begingroup$
@Džuris No it isn’t. The link you cite says, in its very first sentence, that “?:
is a ternary operator” (not “the ternary operator”; emphasis mine). Some people refer to it as “the ternary operator” but this is strictly incorrect (and merits a correction e.g. on Stack Overflow).
$endgroup$
– Konrad Rudolph
yesterday
|
show 9 more comments
$begingroup$
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.
$endgroup$
9
$begingroup$
Ternary operator in programming means a conditional statement of form "if A then x else y" and uually is written just as presented in the OP, i.e. A ? x : y. It would also literally be termary if the OP wouldn't have inserted the particular values of 1 and 2.
$endgroup$
– Džuris
yesterday
15
$begingroup$
@Džuris Ternary means "of arity 3", like binary means "of arity 2" and unary "of arity 1". The specific operator is the conditional ternary operator.
$endgroup$
– frabala
yesterday
15
$begingroup$
Once again, clarity beats compactness when it comes to mathematics.
$endgroup$
– Asaf Karagila♦
yesterday
6
$begingroup$
Well-chosen compact notations can contribute to clarity rather than detract from it, though.
$endgroup$
– Henning Makholm
yesterday
7
$begingroup$
@Džuris No it isn’t. The link you cite says, in its very first sentence, that “?:
is a ternary operator” (not “the ternary operator”; emphasis mine). Some people refer to it as “the ternary operator” but this is strictly incorrect (and merits a correction e.g. on Stack Overflow).
$endgroup$
– Konrad Rudolph
yesterday
|
show 9 more comments
$begingroup$
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.
$endgroup$
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.
edited 2 hours ago
J. W. Tanner
2,8211217
2,8211217
answered yesterday
Ross MillikanRoss Millikan
298k24200374
298k24200374
9
$begingroup$
Ternary operator in programming means a conditional statement of form "if A then x else y" and uually is written just as presented in the OP, i.e. A ? x : y. It would also literally be termary if the OP wouldn't have inserted the particular values of 1 and 2.
$endgroup$
– Džuris
yesterday
15
$begingroup$
@Džuris Ternary means "of arity 3", like binary means "of arity 2" and unary "of arity 1". The specific operator is the conditional ternary operator.
$endgroup$
– frabala
yesterday
15
$begingroup$
Once again, clarity beats compactness when it comes to mathematics.
$endgroup$
– Asaf Karagila♦
yesterday
6
$begingroup$
Well-chosen compact notations can contribute to clarity rather than detract from it, though.
$endgroup$
– Henning Makholm
yesterday
7
$begingroup$
@Džuris No it isn’t. The link you cite says, in its very first sentence, that “?:
is a ternary operator” (not “the ternary operator”; emphasis mine). Some people refer to it as “the ternary operator” but this is strictly incorrect (and merits a correction e.g. on Stack Overflow).
$endgroup$
– Konrad Rudolph
yesterday
|
show 9 more comments
9
$begingroup$
Ternary operator in programming means a conditional statement of form "if A then x else y" and uually is written just as presented in the OP, i.e. A ? x : y. It would also literally be termary if the OP wouldn't have inserted the particular values of 1 and 2.
$endgroup$
– Džuris
yesterday
15
$begingroup$
@Džuris Ternary means "of arity 3", like binary means "of arity 2" and unary "of arity 1". The specific operator is the conditional ternary operator.
$endgroup$
– frabala
yesterday
15
$begingroup$
Once again, clarity beats compactness when it comes to mathematics.
$endgroup$
– Asaf Karagila♦
yesterday
6
$begingroup$
Well-chosen compact notations can contribute to clarity rather than detract from it, though.
$endgroup$
– Henning Makholm
yesterday
7
$begingroup$
@Džuris No it isn’t. The link you cite says, in its very first sentence, that “?:
is a ternary operator” (not “the ternary operator”; emphasis mine). Some people refer to it as “the ternary operator” but this is strictly incorrect (and merits a correction e.g. on Stack Overflow).
$endgroup$
– Konrad Rudolph
yesterday
9
9
$begingroup$
Ternary operator in programming means a conditional statement of form "if A then x else y" and uually is written just as presented in the OP, i.e. A ? x : y. It would also literally be termary if the OP wouldn't have inserted the particular values of 1 and 2.
$endgroup$
– Džuris
yesterday
$begingroup$
Ternary operator in programming means a conditional statement of form "if A then x else y" and uually is written just as presented in the OP, i.e. A ? x : y. It would also literally be termary if the OP wouldn't have inserted the particular values of 1 and 2.
$endgroup$
– Džuris
yesterday
15
15
$begingroup$
@Džuris Ternary means "of arity 3", like binary means "of arity 2" and unary "of arity 1". The specific operator is the conditional ternary operator.
$endgroup$
– frabala
yesterday
$begingroup$
@Džuris Ternary means "of arity 3", like binary means "of arity 2" and unary "of arity 1". The specific operator is the conditional ternary operator.
$endgroup$
– frabala
yesterday
15
15
$begingroup$
Once again, clarity beats compactness when it comes to mathematics.
$endgroup$
– Asaf Karagila♦
yesterday
$begingroup$
Once again, clarity beats compactness when it comes to mathematics.
$endgroup$
– Asaf Karagila♦
yesterday
6
6
$begingroup$
Well-chosen compact notations can contribute to clarity rather than detract from it, though.
$endgroup$
– Henning Makholm
yesterday
$begingroup$
Well-chosen compact notations can contribute to clarity rather than detract from it, though.
$endgroup$
– Henning Makholm
yesterday
7
7
$begingroup$
@Džuris No it isn’t. The link you cite says, in its very first sentence, that “
?:
is a ternary operator” (not “the ternary operator”; emphasis mine). Some people refer to it as “the ternary operator” but this is strictly incorrect (and merits a correction e.g. on Stack Overflow).$endgroup$
– Konrad Rudolph
yesterday
$begingroup$
@Džuris No it isn’t. The link you cite says, in its very first sentence, that “
?:
is a ternary operator” (not “the ternary operator”; emphasis mine). Some people refer to it as “the ternary operator” but this is strictly incorrect (and merits a correction e.g. on Stack Overflow).$endgroup$
– Konrad Rudolph
yesterday
|
show 9 more comments
$begingroup$
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].
$$
$endgroup$
7
$begingroup$
aka en.wikipedia.org/wiki/Iverson_bracket
$endgroup$
– qwr
yesterday
$begingroup$
@qwr Thank you! I did not recall the name.
$endgroup$
– FredH
yesterday
5
$begingroup$
I would find it clearer to write $a = b + 1[c>0] + 2[cle0]$.
$endgroup$
– Rahul
yesterday
5
$begingroup$
I personally find Iverson brackets less cluttered than indicator functions, so I definitely recommend this notation.
$endgroup$
– J. M. is not a mathematician
yesterday
4
$begingroup$
Surely this is a unary operator, which works much like for example!
in the C programming language: Input is an arbitrary expression, the result is either 0 or 1 depending on the expression.
$endgroup$
– pipe
yesterday
|
show 3 more comments
$begingroup$
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].
$$
$endgroup$
7
$begingroup$
aka en.wikipedia.org/wiki/Iverson_bracket
$endgroup$
– qwr
yesterday
$begingroup$
@qwr Thank you! I did not recall the name.
$endgroup$
– FredH
yesterday
5
$begingroup$
I would find it clearer to write $a = b + 1[c>0] + 2[cle0]$.
$endgroup$
– Rahul
yesterday
5
$begingroup$
I personally find Iverson brackets less cluttered than indicator functions, so I definitely recommend this notation.
$endgroup$
– J. M. is not a mathematician
yesterday
4
$begingroup$
Surely this is a unary operator, which works much like for example!
in the C programming language: Input is an arbitrary expression, the result is either 0 or 1 depending on the expression.
$endgroup$
– pipe
yesterday
|
show 3 more comments
$begingroup$
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].
$$
$endgroup$
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].
$$
edited yesterday
answered yesterday
FredHFredH
839512
839512
7
$begingroup$
aka en.wikipedia.org/wiki/Iverson_bracket
$endgroup$
– qwr
yesterday
$begingroup$
@qwr Thank you! I did not recall the name.
$endgroup$
– FredH
yesterday
5
$begingroup$
I would find it clearer to write $a = b + 1[c>0] + 2[cle0]$.
$endgroup$
– Rahul
yesterday
5
$begingroup$
I personally find Iverson brackets less cluttered than indicator functions, so I definitely recommend this notation.
$endgroup$
– J. M. is not a mathematician
yesterday
4
$begingroup$
Surely this is a unary operator, which works much like for example!
in the C programming language: Input is an arbitrary expression, the result is either 0 or 1 depending on the expression.
$endgroup$
– pipe
yesterday
|
show 3 more comments
7
$begingroup$
aka en.wikipedia.org/wiki/Iverson_bracket
$endgroup$
– qwr
yesterday
$begingroup$
@qwr Thank you! I did not recall the name.
$endgroup$
– FredH
yesterday
5
$begingroup$
I would find it clearer to write $a = b + 1[c>0] + 2[cle0]$.
$endgroup$
– Rahul
yesterday
5
$begingroup$
I personally find Iverson brackets less cluttered than indicator functions, so I definitely recommend this notation.
$endgroup$
– J. M. is not a mathematician
yesterday
4
$begingroup$
Surely this is a unary operator, which works much like for example!
in the C programming language: Input is an arbitrary expression, the result is either 0 or 1 depending on the expression.
$endgroup$
– pipe
yesterday
7
7
$begingroup$
aka en.wikipedia.org/wiki/Iverson_bracket
$endgroup$
– qwr
yesterday
$begingroup$
aka en.wikipedia.org/wiki/Iverson_bracket
$endgroup$
– qwr
yesterday
$begingroup$
@qwr Thank you! I did not recall the name.
$endgroup$
– FredH
yesterday
$begingroup$
@qwr Thank you! I did not recall the name.
$endgroup$
– FredH
yesterday
5
5
$begingroup$
I would find it clearer to write $a = b + 1[c>0] + 2[cle0]$.
$endgroup$
– Rahul
yesterday
$begingroup$
I would find it clearer to write $a = b + 1[c>0] + 2[cle0]$.
$endgroup$
– Rahul
yesterday
5
5
$begingroup$
I personally find Iverson brackets less cluttered than indicator functions, so I definitely recommend this notation.
$endgroup$
– J. M. is not a mathematician
yesterday
$begingroup$
I personally find Iverson brackets less cluttered than indicator functions, so I definitely recommend this notation.
$endgroup$
– J. M. is not a mathematician
yesterday
4
4
$begingroup$
Surely this is a unary operator, which works much like for example
!
in the C programming language: Input is an arbitrary expression, the result is either 0 or 1 depending on the expression.$endgroup$
– pipe
yesterday
$begingroup$
Surely this is a unary operator, which works much like for example
!
in the C programming language: Input is an arbitrary expression, the result is either 0 or 1 depending on the expression.$endgroup$
– pipe
yesterday
|
show 3 more comments
$begingroup$
Using the indicator function notation:$$a=b+1+mathbb{1}_{(-infty, 0]}(c)$$
$endgroup$
1
$begingroup$
Indicator is definitely the way to go, since the conditional can define an arbitrary set.
$endgroup$
– eyeballfrog
yesterday
$begingroup$
Thank you, that is very elegant. I will definitely try out all of the answers as I often use this in various scenarios.
$endgroup$
– dataphile
yesterday
2
$begingroup$
I often use - and have seen others use - $mathbb{1}$ ("blackboard bold") to denote the indicator function to distinguish it from the number $1$
$endgroup$
– Christopher
yesterday
1
$begingroup$
The indicator function isn’t a ternary operator, it only has two operands.
$endgroup$
– Konrad Rudolph
yesterday
add a comment |
$begingroup$
Using the indicator function notation:$$a=b+1+mathbb{1}_{(-infty, 0]}(c)$$
$endgroup$
1
$begingroup$
Indicator is definitely the way to go, since the conditional can define an arbitrary set.
$endgroup$
– eyeballfrog
yesterday
$begingroup$
Thank you, that is very elegant. I will definitely try out all of the answers as I often use this in various scenarios.
$endgroup$
– dataphile
yesterday
2
$begingroup$
I often use - and have seen others use - $mathbb{1}$ ("blackboard bold") to denote the indicator function to distinguish it from the number $1$
$endgroup$
– Christopher
yesterday
1
$begingroup$
The indicator function isn’t a ternary operator, it only has two operands.
$endgroup$
– Konrad Rudolph
yesterday
add a comment |
$begingroup$
Using the indicator function notation:$$a=b+1+mathbb{1}_{(-infty, 0]}(c)$$
$endgroup$
Using the indicator function notation:$$a=b+1+mathbb{1}_{(-infty, 0]}(c)$$
edited yesterday
answered yesterday
Siong Thye GohSiong Thye Goh
102k1467119
102k1467119
1
$begingroup$
Indicator is definitely the way to go, since the conditional can define an arbitrary set.
$endgroup$
– eyeballfrog
yesterday
$begingroup$
Thank you, that is very elegant. I will definitely try out all of the answers as I often use this in various scenarios.
$endgroup$
– dataphile
yesterday
2
$begingroup$
I often use - and have seen others use - $mathbb{1}$ ("blackboard bold") to denote the indicator function to distinguish it from the number $1$
$endgroup$
– Christopher
yesterday
1
$begingroup$
The indicator function isn’t a ternary operator, it only has two operands.
$endgroup$
– Konrad Rudolph
yesterday
add a comment |
1
$begingroup$
Indicator is definitely the way to go, since the conditional can define an arbitrary set.
$endgroup$
– eyeballfrog
yesterday
$begingroup$
Thank you, that is very elegant. I will definitely try out all of the answers as I often use this in various scenarios.
$endgroup$
– dataphile
yesterday
2
$begingroup$
I often use - and have seen others use - $mathbb{1}$ ("blackboard bold") to denote the indicator function to distinguish it from the number $1$
$endgroup$
– Christopher
yesterday
1
$begingroup$
The indicator function isn’t a ternary operator, it only has two operands.
$endgroup$
– Konrad Rudolph
yesterday
1
1
$begingroup$
Indicator is definitely the way to go, since the conditional can define an arbitrary set.
$endgroup$
– eyeballfrog
yesterday
$begingroup$
Indicator is definitely the way to go, since the conditional can define an arbitrary set.
$endgroup$
– eyeballfrog
yesterday
$begingroup$
Thank you, that is very elegant. I will definitely try out all of the answers as I often use this in various scenarios.
$endgroup$
– dataphile
yesterday
$begingroup$
Thank you, that is very elegant. I will definitely try out all of the answers as I often use this in various scenarios.
$endgroup$
– dataphile
yesterday
2
2
$begingroup$
I often use - and have seen others use - $mathbb{1}$ ("blackboard bold") to denote the indicator function to distinguish it from the number $1$
$endgroup$
– Christopher
yesterday
$begingroup$
I often use - and have seen others use - $mathbb{1}$ ("blackboard bold") to denote the indicator function to distinguish it from the number $1$
$endgroup$
– Christopher
yesterday
1
1
$begingroup$
The indicator function isn’t a ternary operator, it only has two operands.
$endgroup$
– Konrad Rudolph
yesterday
$begingroup$
The indicator function isn’t a ternary operator, it only has two operands.
$endgroup$
– Konrad Rudolph
yesterday
add a comment |
$begingroup$
In math, equations are written in piecewise form by having a curly brace enclose multiple lines; each one with a condition excepting the last which has "otherwise".
There are a few custom operators that also occasionally make an appearance. E.g. the Heavyside function mentioned by Alex, the Dirac function, and the cyclical operator $delta_{ijk}$ - all of which can be used to emulate conditional behaviour.
$endgroup$
add a comment |
$begingroup$
In math, equations are written in piecewise form by having a curly brace enclose multiple lines; each one with a condition excepting the last which has "otherwise".
There are a few custom operators that also occasionally make an appearance. E.g. the Heavyside function mentioned by Alex, the Dirac function, and the cyclical operator $delta_{ijk}$ - all of which can be used to emulate conditional behaviour.
$endgroup$
add a comment |
$begingroup$
In math, equations are written in piecewise form by having a curly brace enclose multiple lines; each one with a condition excepting the last which has "otherwise".
There are a few custom operators that also occasionally make an appearance. E.g. the Heavyside function mentioned by Alex, the Dirac function, and the cyclical operator $delta_{ijk}$ - all of which can be used to emulate conditional behaviour.
$endgroup$
In math, equations are written in piecewise form by having a curly brace enclose multiple lines; each one with a condition excepting the last which has "otherwise".
There are a few custom operators that also occasionally make an appearance. E.g. the Heavyside function mentioned by Alex, the Dirac function, and the cyclical operator $delta_{ijk}$ - all of which can be used to emulate conditional behaviour.
answered yesterday
Paul ChildsPaul Childs
4138
4138
add a comment |
add a comment |
$begingroup$
One should realize that operators are just a fancy way of using functions.
So a ternary operator is a function of 3 variables that is notated in a different way. Is that useful? The answer is mostly not. Also realize that any mathematician is allowed to introduce any notation he feels is illustrative.
Let's review why we use binary operators at all like in a+b*c . Because parameters and results are of the same type, it makes sense to leave out parentheses and introduce complicated priority rules. Imagine that a b c are numbers and we have a normal + and a peculiar * that results in dragons. Now the expression doesn't make sense (assumming a high priority *), because there is no way to add numbers and dragons. Thusly most ternary operators results in a mess.
With a proper notation there are examples of ternary operations. For example, there is a special notation for "sum for i from a to b of expression". This takes two boundaries (numbers) and a function from a number of that type that results in another number. (Mathematician, read "element of an addition group" for number.)
The notation for integration is similarly ternary.
So in short ternary operators exist, and you can define your own. They are in general accompagnied with a special notation, or they are not helpful.
Now back to the special case you mention.
Because truth values are implied in math, an expression like "if a then b else c" makes sense if a represens a truth value like (7<12). The above expression is understood in every mathematical context. However in a context where truth values are not considered a set, (if .. then .. else ..) would not be considered an operator/function, but a textual explanation. A general accepted notation could be useful in math, but I'm not aware there is one. That is probably, because like in the above, informal notations are readily understood.
$endgroup$
add a comment |
$begingroup$
One should realize that operators are just a fancy way of using functions.
So a ternary operator is a function of 3 variables that is notated in a different way. Is that useful? The answer is mostly not. Also realize that any mathematician is allowed to introduce any notation he feels is illustrative.
Let's review why we use binary operators at all like in a+b*c . Because parameters and results are of the same type, it makes sense to leave out parentheses and introduce complicated priority rules. Imagine that a b c are numbers and we have a normal + and a peculiar * that results in dragons. Now the expression doesn't make sense (assumming a high priority *), because there is no way to add numbers and dragons. Thusly most ternary operators results in a mess.
With a proper notation there are examples of ternary operations. For example, there is a special notation for "sum for i from a to b of expression". This takes two boundaries (numbers) and a function from a number of that type that results in another number. (Mathematician, read "element of an addition group" for number.)
The notation for integration is similarly ternary.
So in short ternary operators exist, and you can define your own. They are in general accompagnied with a special notation, or they are not helpful.
Now back to the special case you mention.
Because truth values are implied in math, an expression like "if a then b else c" makes sense if a represens a truth value like (7<12). The above expression is understood in every mathematical context. However in a context where truth values are not considered a set, (if .. then .. else ..) would not be considered an operator/function, but a textual explanation. A general accepted notation could be useful in math, but I'm not aware there is one. That is probably, because like in the above, informal notations are readily understood.
$endgroup$
add a comment |
$begingroup$
One should realize that operators are just a fancy way of using functions.
So a ternary operator is a function of 3 variables that is notated in a different way. Is that useful? The answer is mostly not. Also realize that any mathematician is allowed to introduce any notation he feels is illustrative.
Let's review why we use binary operators at all like in a+b*c . Because parameters and results are of the same type, it makes sense to leave out parentheses and introduce complicated priority rules. Imagine that a b c are numbers and we have a normal + and a peculiar * that results in dragons. Now the expression doesn't make sense (assumming a high priority *), because there is no way to add numbers and dragons. Thusly most ternary operators results in a mess.
With a proper notation there are examples of ternary operations. For example, there is a special notation for "sum for i from a to b of expression". This takes two boundaries (numbers) and a function from a number of that type that results in another number. (Mathematician, read "element of an addition group" for number.)
The notation for integration is similarly ternary.
So in short ternary operators exist, and you can define your own. They are in general accompagnied with a special notation, or they are not helpful.
Now back to the special case you mention.
Because truth values are implied in math, an expression like "if a then b else c" makes sense if a represens a truth value like (7<12). The above expression is understood in every mathematical context. However in a context where truth values are not considered a set, (if .. then .. else ..) would not be considered an operator/function, but a textual explanation. A general accepted notation could be useful in math, but I'm not aware there is one. That is probably, because like in the above, informal notations are readily understood.
$endgroup$
One should realize that operators are just a fancy way of using functions.
So a ternary operator is a function of 3 variables that is notated in a different way. Is that useful? The answer is mostly not. Also realize that any mathematician is allowed to introduce any notation he feels is illustrative.
Let's review why we use binary operators at all like in a+b*c . Because parameters and results are of the same type, it makes sense to leave out parentheses and introduce complicated priority rules. Imagine that a b c are numbers and we have a normal + and a peculiar * that results in dragons. Now the expression doesn't make sense (assumming a high priority *), because there is no way to add numbers and dragons. Thusly most ternary operators results in a mess.
With a proper notation there are examples of ternary operations. For example, there is a special notation for "sum for i from a to b of expression". This takes two boundaries (numbers) and a function from a number of that type that results in another number. (Mathematician, read "element of an addition group" for number.)
The notation for integration is similarly ternary.
So in short ternary operators exist, and you can define your own. They are in general accompagnied with a special notation, or they are not helpful.
Now back to the special case you mention.
Because truth values are implied in math, an expression like "if a then b else c" makes sense if a represens a truth value like (7<12). The above expression is understood in every mathematical context. However in a context where truth values are not considered a set, (if .. then .. else ..) would not be considered an operator/function, but a textual explanation. A general accepted notation could be useful in math, but I'm not aware there is one. That is probably, because like in the above, informal notations are readily understood.
answered yesterday
Albert van der HorstAlbert van der Horst
19115
19115
add a comment |
add a comment |
$begingroup$
Following solution is not defined for $c = 0$; however it uses very basic operations only, which might be useful as you probably look for an expression to implement in a program:
$$a = b + 1lambda + 2(1-lambda)$$
where
$$lambda = frac{ 1 + frac{|c|}{c} }{2}$$
You need to make the problem discrete and make a choice from two values. So, given some value $c in mathbb{R}$ we need to calculate some value $lambda in {0,1}$ depnding on $c<0$ or $c>0$.
Knowing that
$$frac{|c|}{c} in {1,-1}$$
we can calculate the $lambda$ as follows:
$$lambda = frac{ 1 + frac{|c|}{c} }{2}$$
Now that our $lambda in {0,1}$ we can do the "choice" between the two constants $d$ and $e$ as follows:
$$dlambda + e(1-lambda)$$
which equals $d$ for $lambda = 1$, and $e$ for $lambda = 0$.
New contributor
$endgroup$
add a comment |
$begingroup$
Following solution is not defined for $c = 0$; however it uses very basic operations only, which might be useful as you probably look for an expression to implement in a program:
$$a = b + 1lambda + 2(1-lambda)$$
where
$$lambda = frac{ 1 + frac{|c|}{c} }{2}$$
You need to make the problem discrete and make a choice from two values. So, given some value $c in mathbb{R}$ we need to calculate some value $lambda in {0,1}$ depnding on $c<0$ or $c>0$.
Knowing that
$$frac{|c|}{c} in {1,-1}$$
we can calculate the $lambda$ as follows:
$$lambda = frac{ 1 + frac{|c|}{c} }{2}$$
Now that our $lambda in {0,1}$ we can do the "choice" between the two constants $d$ and $e$ as follows:
$$dlambda + e(1-lambda)$$
which equals $d$ for $lambda = 1$, and $e$ for $lambda = 0$.
New contributor
$endgroup$
add a comment |
$begingroup$
Following solution is not defined for $c = 0$; however it uses very basic operations only, which might be useful as you probably look for an expression to implement in a program:
$$a = b + 1lambda + 2(1-lambda)$$
where
$$lambda = frac{ 1 + frac{|c|}{c} }{2}$$
You need to make the problem discrete and make a choice from two values. So, given some value $c in mathbb{R}$ we need to calculate some value $lambda in {0,1}$ depnding on $c<0$ or $c>0$.
Knowing that
$$frac{|c|}{c} in {1,-1}$$
we can calculate the $lambda$ as follows:
$$lambda = frac{ 1 + frac{|c|}{c} }{2}$$
Now that our $lambda in {0,1}$ we can do the "choice" between the two constants $d$ and $e$ as follows:
$$dlambda + e(1-lambda)$$
which equals $d$ for $lambda = 1$, and $e$ for $lambda = 0$.
New contributor
$endgroup$
Following solution is not defined for $c = 0$; however it uses very basic operations only, which might be useful as you probably look for an expression to implement in a program:
$$a = b + 1lambda + 2(1-lambda)$$
where
$$lambda = frac{ 1 + frac{|c|}{c} }{2}$$
You need to make the problem discrete and make a choice from two values. So, given some value $c in mathbb{R}$ we need to calculate some value $lambda in {0,1}$ depnding on $c<0$ or $c>0$.
Knowing that
$$frac{|c|}{c} in {1,-1}$$
we can calculate the $lambda$ as follows:
$$lambda = frac{ 1 + frac{|c|}{c} }{2}$$
Now that our $lambda in {0,1}$ we can do the "choice" between the two constants $d$ and $e$ as follows:
$$dlambda + e(1-lambda)$$
which equals $d$ for $lambda = 1$, and $e$ for $lambda = 0$.
New contributor
edited 12 hours ago
New contributor
answered 12 hours ago
TimurTimur
1213
1213
New contributor
New contributor
add a comment |
add a comment |
$begingroup$
There are many good answers that give notation for, “if this condition holds, then 1, else 0.” This corresponds to an even simpler expression in C;(x>1)
is equivalent to (x>1 ? 1 : 0)
.
It’s worth noting that the ternary operator is more general than that. If the arguments are elements of a ring, you could express c ? a : b
with (using Iverson-bracket notation) $(a-b) cdot [c] + b$, but not otherwise. (And compilers frequently use this trick, in a Boolean ring, to compile conditionals without needing to execute a branch instruction.) In a C program, evaluating the expressions $a$ or $b$ might have side-effects, such as deleting a file or printing a message to the screen. In a mathematical function, this isn’t something you would worry about, and a programming language where this is impossible is called functional.
Ross Millikan gave the most standard notation, a cases block. The closest equivalent in mathematical computer science is the if-then-else
function of Lambda Calculus.
$endgroup$
$begingroup$
"in C;(x>1) is equivalent to (x>1 ? 1 : 0)" Not exactly. Rather, C considers any nonzero value as equivalent from a truthiness perspective. So there is no difference between an integer expression taking the value of -1, 1, 42 orINT_MAX
when that expression is treated as a boolean rvalue. In C, the one special integer value, when treated as a boolean, is 0, representing false. That said, if someone actually used(x>1)
as a non-boolean expression and I noticed it, I would likely at least briefly try to find some heavy physical object that could be applied at high speed to their keyboard.
$endgroup$
– a CVn
yesterday
1
$begingroup$
@aCVn The C11 standard states, “Each of the operators<
(less than),>
(greater than),<=
(less than or equal to), and>=
(greater than or equal to) shall yield1
if the specified relation is true and0
if it is false.” Similarly for equality and inequality, “Each of the operators yields1
if the specified relation is true and0
if it is false.”
$endgroup$
– Davislor
yesterday
$begingroup$
@aCVn What you wrote is true, but the relational operators in C will only return the values1
or0
.
$endgroup$
– Davislor
yesterday
add a comment |
$begingroup$
There are many good answers that give notation for, “if this condition holds, then 1, else 0.” This corresponds to an even simpler expression in C;(x>1)
is equivalent to (x>1 ? 1 : 0)
.
It’s worth noting that the ternary operator is more general than that. If the arguments are elements of a ring, you could express c ? a : b
with (using Iverson-bracket notation) $(a-b) cdot [c] + b$, but not otherwise. (And compilers frequently use this trick, in a Boolean ring, to compile conditionals without needing to execute a branch instruction.) In a C program, evaluating the expressions $a$ or $b$ might have side-effects, such as deleting a file or printing a message to the screen. In a mathematical function, this isn’t something you would worry about, and a programming language where this is impossible is called functional.
Ross Millikan gave the most standard notation, a cases block. The closest equivalent in mathematical computer science is the if-then-else
function of Lambda Calculus.
$endgroup$
$begingroup$
"in C;(x>1) is equivalent to (x>1 ? 1 : 0)" Not exactly. Rather, C considers any nonzero value as equivalent from a truthiness perspective. So there is no difference between an integer expression taking the value of -1, 1, 42 orINT_MAX
when that expression is treated as a boolean rvalue. In C, the one special integer value, when treated as a boolean, is 0, representing false. That said, if someone actually used(x>1)
as a non-boolean expression and I noticed it, I would likely at least briefly try to find some heavy physical object that could be applied at high speed to their keyboard.
$endgroup$
– a CVn
yesterday
1
$begingroup$
@aCVn The C11 standard states, “Each of the operators<
(less than),>
(greater than),<=
(less than or equal to), and>=
(greater than or equal to) shall yield1
if the specified relation is true and0
if it is false.” Similarly for equality and inequality, “Each of the operators yields1
if the specified relation is true and0
if it is false.”
$endgroup$
– Davislor
yesterday
$begingroup$
@aCVn What you wrote is true, but the relational operators in C will only return the values1
or0
.
$endgroup$
– Davislor
yesterday
add a comment |
$begingroup$
There are many good answers that give notation for, “if this condition holds, then 1, else 0.” This corresponds to an even simpler expression in C;(x>1)
is equivalent to (x>1 ? 1 : 0)
.
It’s worth noting that the ternary operator is more general than that. If the arguments are elements of a ring, you could express c ? a : b
with (using Iverson-bracket notation) $(a-b) cdot [c] + b$, but not otherwise. (And compilers frequently use this trick, in a Boolean ring, to compile conditionals without needing to execute a branch instruction.) In a C program, evaluating the expressions $a$ or $b$ might have side-effects, such as deleting a file or printing a message to the screen. In a mathematical function, this isn’t something you would worry about, and a programming language where this is impossible is called functional.
Ross Millikan gave the most standard notation, a cases block. The closest equivalent in mathematical computer science is the if-then-else
function of Lambda Calculus.
$endgroup$
There are many good answers that give notation for, “if this condition holds, then 1, else 0.” This corresponds to an even simpler expression in C;(x>1)
is equivalent to (x>1 ? 1 : 0)
.
It’s worth noting that the ternary operator is more general than that. If the arguments are elements of a ring, you could express c ? a : b
with (using Iverson-bracket notation) $(a-b) cdot [c] + b$, but not otherwise. (And compilers frequently use this trick, in a Boolean ring, to compile conditionals without needing to execute a branch instruction.) In a C program, evaluating the expressions $a$ or $b$ might have side-effects, such as deleting a file or printing a message to the screen. In a mathematical function, this isn’t something you would worry about, and a programming language where this is impossible is called functional.
Ross Millikan gave the most standard notation, a cases block. The closest equivalent in mathematical computer science is the if-then-else
function of Lambda Calculus.
edited yesterday
answered yesterday
DavislorDavislor
2,360815
2,360815
$begingroup$
"in C;(x>1) is equivalent to (x>1 ? 1 : 0)" Not exactly. Rather, C considers any nonzero value as equivalent from a truthiness perspective. So there is no difference between an integer expression taking the value of -1, 1, 42 orINT_MAX
when that expression is treated as a boolean rvalue. In C, the one special integer value, when treated as a boolean, is 0, representing false. That said, if someone actually used(x>1)
as a non-boolean expression and I noticed it, I would likely at least briefly try to find some heavy physical object that could be applied at high speed to their keyboard.
$endgroup$
– a CVn
yesterday
1
$begingroup$
@aCVn The C11 standard states, “Each of the operators<
(less than),>
(greater than),<=
(less than or equal to), and>=
(greater than or equal to) shall yield1
if the specified relation is true and0
if it is false.” Similarly for equality and inequality, “Each of the operators yields1
if the specified relation is true and0
if it is false.”
$endgroup$
– Davislor
yesterday
$begingroup$
@aCVn What you wrote is true, but the relational operators in C will only return the values1
or0
.
$endgroup$
– Davislor
yesterday
add a comment |
$begingroup$
"in C;(x>1) is equivalent to (x>1 ? 1 : 0)" Not exactly. Rather, C considers any nonzero value as equivalent from a truthiness perspective. So there is no difference between an integer expression taking the value of -1, 1, 42 orINT_MAX
when that expression is treated as a boolean rvalue. In C, the one special integer value, when treated as a boolean, is 0, representing false. That said, if someone actually used(x>1)
as a non-boolean expression and I noticed it, I would likely at least briefly try to find some heavy physical object that could be applied at high speed to their keyboard.
$endgroup$
– a CVn
yesterday
1
$begingroup$
@aCVn The C11 standard states, “Each of the operators<
(less than),>
(greater than),<=
(less than or equal to), and>=
(greater than or equal to) shall yield1
if the specified relation is true and0
if it is false.” Similarly for equality and inequality, “Each of the operators yields1
if the specified relation is true and0
if it is false.”
$endgroup$
– Davislor
yesterday
$begingroup$
@aCVn What you wrote is true, but the relational operators in C will only return the values1
or0
.
$endgroup$
– Davislor
yesterday
$begingroup$
"in C;(x>1) is equivalent to (x>1 ? 1 : 0)" Not exactly. Rather, C considers any nonzero value as equivalent from a truthiness perspective. So there is no difference between an integer expression taking the value of -1, 1, 42 or
INT_MAX
when that expression is treated as a boolean rvalue. In C, the one special integer value, when treated as a boolean, is 0, representing false. That said, if someone actually used (x>1)
as a non-boolean expression and I noticed it, I would likely at least briefly try to find some heavy physical object that could be applied at high speed to their keyboard.$endgroup$
– a CVn
yesterday
$begingroup$
"in C;(x>1) is equivalent to (x>1 ? 1 : 0)" Not exactly. Rather, C considers any nonzero value as equivalent from a truthiness perspective. So there is no difference between an integer expression taking the value of -1, 1, 42 or
INT_MAX
when that expression is treated as a boolean rvalue. In C, the one special integer value, when treated as a boolean, is 0, representing false. That said, if someone actually used (x>1)
as a non-boolean expression and I noticed it, I would likely at least briefly try to find some heavy physical object that could be applied at high speed to their keyboard.$endgroup$
– a CVn
yesterday
1
1
$begingroup$
@aCVn The C11 standard states, “Each of the operators
<
(less than), >
(greater than), <=
(less than or equal to), and >=
(greater than or equal to) shall yield 1
if the specified relation is true and 0
if it is false.” Similarly for equality and inequality, “Each of the operators yields 1
if the specified relation is true and 0
if it is false.”$endgroup$
– Davislor
yesterday
$begingroup$
@aCVn The C11 standard states, “Each of the operators
<
(less than), >
(greater than), <=
(less than or equal to), and >=
(greater than or equal to) shall yield 1
if the specified relation is true and 0
if it is false.” Similarly for equality and inequality, “Each of the operators yields 1
if the specified relation is true and 0
if it is false.”$endgroup$
– Davislor
yesterday
$begingroup$
@aCVn What you wrote is true, but the relational operators in C will only return the values
1
or 0
.$endgroup$
– Davislor
yesterday
$begingroup$
@aCVn What you wrote is true, but the relational operators in C will only return the values
1
or 0
.$endgroup$
– Davislor
yesterday
add a comment |
$begingroup$
I think mathematicians should not be afraid to use the Iverson bracket, including when teaching, as this is a generally very useful notation whose only slightly unusual feature is to introduce a logical expression in the middle of an algebraic one (but one already regularly finds conditions inside set-theoretic expressions, so it really is not a big deal). It may avoid a lot of clutter, notably many instances of clumsy expressions by cases with a big unmatched brace (which is usually only usable as the right hand side of a definition). Since brackets do have many other uses in mathematics, I personally prefer a typographically distinct representation of Iverson brackets, rendering your example as
$$def[#1]{[![{#1}]!]}
a= b + [c>0]1 + [cnot>0]2.
$$
This works best in additive context (though one can use Iverson brackets in the exponent for optional multiplicative factors). It is not really ideal for general two-way branches, as the condition must be repeated twice, one of them in negated form, but it happens that most of the time one needs $0$ as the value for one branch anyway.
As a more concise two-way branch, I can recall that Algol68 introduced the notation $b+(c>0mid 1mid 2)$ for the right-hand side of your equation; though this is a programming language and not mathematics, it was designed by mathematicians. They also had notation for multi-way branching: thus the solution to the recursion $a_{n+2}=a_{n+1}-a_n$ with initial values $a_0=0$, $a_1=1$ can be written
$$
a_n=(nbmod 6+1mid 0,1,1,0,-1,-1)
$$
(where the "${}+1$" is needed because in 1968 they still counted starting from $1$, which is a mistake), which is reasonably concise and readable, compared to other ways to express this result. Also consider, for month $m$ in year $y$, the number
$$
( m mid 31,(ybmod 4=0land ybmod 100neq0lor ybmod400=0mid 29mid 28)
,31,30,31,30,31,31,30,31,30,31).
$$
$endgroup$
add a comment |
$begingroup$
I think mathematicians should not be afraid to use the Iverson bracket, including when teaching, as this is a generally very useful notation whose only slightly unusual feature is to introduce a logical expression in the middle of an algebraic one (but one already regularly finds conditions inside set-theoretic expressions, so it really is not a big deal). It may avoid a lot of clutter, notably many instances of clumsy expressions by cases with a big unmatched brace (which is usually only usable as the right hand side of a definition). Since brackets do have many other uses in mathematics, I personally prefer a typographically distinct representation of Iverson brackets, rendering your example as
$$def[#1]{[![{#1}]!]}
a= b + [c>0]1 + [cnot>0]2.
$$
This works best in additive context (though one can use Iverson brackets in the exponent for optional multiplicative factors). It is not really ideal for general two-way branches, as the condition must be repeated twice, one of them in negated form, but it happens that most of the time one needs $0$ as the value for one branch anyway.
As a more concise two-way branch, I can recall that Algol68 introduced the notation $b+(c>0mid 1mid 2)$ for the right-hand side of your equation; though this is a programming language and not mathematics, it was designed by mathematicians. They also had notation for multi-way branching: thus the solution to the recursion $a_{n+2}=a_{n+1}-a_n$ with initial values $a_0=0$, $a_1=1$ can be written
$$
a_n=(nbmod 6+1mid 0,1,1,0,-1,-1)
$$
(where the "${}+1$" is needed because in 1968 they still counted starting from $1$, which is a mistake), which is reasonably concise and readable, compared to other ways to express this result. Also consider, for month $m$ in year $y$, the number
$$
( m mid 31,(ybmod 4=0land ybmod 100neq0lor ybmod400=0mid 29mid 28)
,31,30,31,30,31,31,30,31,30,31).
$$
$endgroup$
add a comment |
$begingroup$
I think mathematicians should not be afraid to use the Iverson bracket, including when teaching, as this is a generally very useful notation whose only slightly unusual feature is to introduce a logical expression in the middle of an algebraic one (but one already regularly finds conditions inside set-theoretic expressions, so it really is not a big deal). It may avoid a lot of clutter, notably many instances of clumsy expressions by cases with a big unmatched brace (which is usually only usable as the right hand side of a definition). Since brackets do have many other uses in mathematics, I personally prefer a typographically distinct representation of Iverson brackets, rendering your example as
$$def[#1]{[![{#1}]!]}
a= b + [c>0]1 + [cnot>0]2.
$$
This works best in additive context (though one can use Iverson brackets in the exponent for optional multiplicative factors). It is not really ideal for general two-way branches, as the condition must be repeated twice, one of them in negated form, but it happens that most of the time one needs $0$ as the value for one branch anyway.
As a more concise two-way branch, I can recall that Algol68 introduced the notation $b+(c>0mid 1mid 2)$ for the right-hand side of your equation; though this is a programming language and not mathematics, it was designed by mathematicians. They also had notation for multi-way branching: thus the solution to the recursion $a_{n+2}=a_{n+1}-a_n$ with initial values $a_0=0$, $a_1=1$ can be written
$$
a_n=(nbmod 6+1mid 0,1,1,0,-1,-1)
$$
(where the "${}+1$" is needed because in 1968 they still counted starting from $1$, which is a mistake), which is reasonably concise and readable, compared to other ways to express this result. Also consider, for month $m$ in year $y$, the number
$$
( m mid 31,(ybmod 4=0land ybmod 100neq0lor ybmod400=0mid 29mid 28)
,31,30,31,30,31,31,30,31,30,31).
$$
$endgroup$
I think mathematicians should not be afraid to use the Iverson bracket, including when teaching, as this is a generally very useful notation whose only slightly unusual feature is to introduce a logical expression in the middle of an algebraic one (but one already regularly finds conditions inside set-theoretic expressions, so it really is not a big deal). It may avoid a lot of clutter, notably many instances of clumsy expressions by cases with a big unmatched brace (which is usually only usable as the right hand side of a definition). Since brackets do have many other uses in mathematics, I personally prefer a typographically distinct representation of Iverson brackets, rendering your example as
$$def[#1]{[![{#1}]!]}
a= b + [c>0]1 + [cnot>0]2.
$$
This works best in additive context (though one can use Iverson brackets in the exponent for optional multiplicative factors). It is not really ideal for general two-way branches, as the condition must be repeated twice, one of them in negated form, but it happens that most of the time one needs $0$ as the value for one branch anyway.
As a more concise two-way branch, I can recall that Algol68 introduced the notation $b+(c>0mid 1mid 2)$ for the right-hand side of your equation; though this is a programming language and not mathematics, it was designed by mathematicians. They also had notation for multi-way branching: thus the solution to the recursion $a_{n+2}=a_{n+1}-a_n$ with initial values $a_0=0$, $a_1=1$ can be written
$$
a_n=(nbmod 6+1mid 0,1,1,0,-1,-1)
$$
(where the "${}+1$" is needed because in 1968 they still counted starting from $1$, which is a mistake), which is reasonably concise and readable, compared to other ways to express this result. Also consider, for month $m$ in year $y$, the number
$$
( m mid 31,(ybmod 4=0land ybmod 100neq0lor ybmod400=0mid 29mid 28)
,31,30,31,30,31,31,30,31,30,31).
$$
answered 12 hours ago
Marc van LeeuwenMarc van Leeuwen
87.8k5111225
87.8k5111225
add a comment |
add a comment |
$begingroup$
It can be viewed as inline function:
$$f(c)=begin {cases} 1&c gt 0\
2& c le 0 end {cases}$$
so it comes as:
$$a = b + f(c)$$
used in everyday mathematics.
$endgroup$
add a comment |
$begingroup$
It can be viewed as inline function:
$$f(c)=begin {cases} 1&c gt 0\
2& c le 0 end {cases}$$
so it comes as:
$$a = b + f(c)$$
used in everyday mathematics.
$endgroup$
add a comment |
$begingroup$
It can be viewed as inline function:
$$f(c)=begin {cases} 1&c gt 0\
2& c le 0 end {cases}$$
so it comes as:
$$a = b + f(c)$$
used in everyday mathematics.
$endgroup$
It can be viewed as inline function:
$$f(c)=begin {cases} 1&c gt 0\
2& c le 0 end {cases}$$
so it comes as:
$$a = b + f(c)$$
used in everyday mathematics.
answered 21 hours ago
yekanchiyekanchi
1012
1012
add a comment |
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3135798%2fis-there-a-math-equivalent-to-the-conditional-ternary-operator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
$begingroup$
@Alex $a = b + 2 - u(c)$
$endgroup$
– eyeballfrog
yesterday
14
$begingroup$
A better question here is: what is good notation in this case. There are many ways of writing this, many ways which one would almost never use (like the accepted answer).
$endgroup$
– Winther
yesterday
5
$begingroup$
In C-derived languages at least, you have made a grievous error! Your expression parses as
a = (b + c) > 0 ? 1 : 2
. I always use parentheses in these cases, even when they are not strictly necessary.$endgroup$
– TonyK
yesterday
3
$begingroup$
Just to clarify, by “ternary operator” you really mean “conditional operator”? Or are interested in any kind of ternary operator, regardless of semantics?
$endgroup$
– Konrad Rudolph
yesterday
1
$begingroup$
What @KonradRudolph said; you are showing the conditional operator, which is a ternary (takes three arguments; compare unary and binary) operator. I don't think the C language has any other ternary operator, and some form of the conditional operator is found in many languages because it's very convenient to have, but there's no rule that says a language couldn't include other ternary operators alongside (or without having) a conditional operator.
$endgroup$
– a CVn
yesterday