Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get condition of a ConditionalExpression

Tags:

Given a conditional expression cE = ConditionalExpression[ Value, Condition ] how can I extract the Condition of cE?

I tried indexing, but that didn't help.

like image 548
loki Avatar asked Dec 08 '17 11:12

loki


People also ask

What is conditional expression in Mathematica?

is a symbolic construct that represents the expression expr when the condition cond is True.

What is condition expression?

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.

What are conditional expressions in C?

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.

Which is known as conditional expression?

The conditional expression is known as a ternary operation, because it has three operands.


1 Answers

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[]
like image 111
Picaud Vincent Avatar answered Sep 22 '22 13:09

Picaud Vincent