Given a conditional expression cE = ConditionalExpression[ Value, Condition ]
how can I extract the Condition of cE
?
I tried indexing, but that didn't help.
is a symbolic construct that represents the expression expr when the condition cond is True.
A conditional expression has the general form. where expr1 is an expression that yields a true or false result. If the result is true, the value of the conditional expression is set to expr2. If the result is false, the value of the conditional expression is set to expr3.
The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'. As conditional operator works on three operands, so it is also known as the ternary operator.
The conditional expression is known as a ternary operation, because it has three operands.
Maybe it is interesting to give a cleaner version that you can use in more complex situations.
Consider ConditionalExpression official documentation example
In[]:= ce = Integrate[x^n, {x, 0, 1}]
with the following output:
1
Out[]= ConditionalExpression[-----, Re[n] > -1]
1 + n
To extract the condition Re[n] > -1
you can use:
In[]:= FirstCase[ce, ConditionalExpression[_, c_] :> c, Missing[], {0,-1}]
which prints:
Out[]= Re[n] > -1
In your comment you mentionned a nested expression, with the previous approach this will work too. For instance:
In[]:= FirstCase[{{5, 6, ce, 1}}, ConditionalExpression[_, c_] :> c, Missing[], {0,-1}]
still returns
Out[]= Re[n] > -1
If the pattern is not found, the command gently returns Missing[]
. For instance with Sin[6]
:
In[]:= FirstCase[Sin[6], ConditionalExpression[_, c_] :> c, Missing[], {0,-1}]
the output is:
Out[]= Missing[]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With