3
$\begingroup$

I am using Mathematica and I have to define several expression containing a condition. For my purposes the ConditionalExpression syntax seems to be appropriate. But I have a problem about understanding how Mathematica evaluates those expressions. Here is an explaining example of what I mean:

test = ConditionalExpression[1/a, a > 0]

The original case contain lots more variables than one. I am now interested in the behaviour for different parameter values. For example, the value of test when a=2:

test/.a->2

this results in test=0.5 and is perfect. But when I am trying

 test/.a->0

the result should be undefined. Surprisingly, it occurs first an error message that the expression 1/0 is undefined and after that the result Undefined. This implies that Mathematica tries to evaluate the function and checks after that whether the conditions are satisfied or not.

This is unsatisfactory for me, since for my several parameter constellations it would be better if the conditions are checked first and if they are true, then the value of the ConditionalExpression should be evaluated. I also tried it with the If[...] function, but this doesn't work for me neither.

Do you have any solutions or hints of this problem?

I stated in my comment below that I would like to use the If function but I think my posted MWE did not match my situation perfectly. The main problem of using If is the following: Define two variables y and z

y = a^2;
z = If[a > 0, y, Undefined];

Now I would like to evaluate z for a special value of a. And the result is the following:

z /. a -> 3
OUT: a^2

So Mathematica is not able to evaluate the variable y instantaneously when the condition in z is satisfied. Do you have any ideas for solving this problem without using Piecewise?

$\endgroup$
0

2 Answers 2

4
$\begingroup$

It looks like Piecewise checks its conditions before returning errors based on its values:

Piecewise[{{1/a, a > 0}, {Undefined, True}}] /. a -> 0

Which also seems to work:

Which[a > 0, 1/a, True, Undefined] /. a -> 0

There's a discussion of which would be more applicable here. Notably, if you're concerned about whether or not the conditions are tested before any code is run, Which is probably safer.

$\endgroup$
2
  • $\begingroup$ Thank you for your answer! $\endgroup$
    – Frank
    Commented May 14, 2018 at 12:18
  • $\begingroup$ Thank you for your answer! Your linked post is quite interesting and for my problem the Piecewise solution seems to be appropriate and for the simple examlpe it works perfectly, but I am not really satisfied with the behavior in my original complex problem... I tested the If[...] function and it also checks first the condition and evaluates then the expression. So I will try it first with If. $\endgroup$
    – Frank
    Commented May 14, 2018 at 12:24
4
$\begingroup$

Simplify can accept a second argument which are the things assumed true during the process of simplification. Thus

test = ConditionalExpression[1/a, a > 0];
Simplify[test,a==0]

instantly returns the

Undefined

which you are looking for

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.