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
?