5
$\begingroup$

I have a conditional expression

f[x_] := x /; x ∈ Reals

This works fine for normal numbers and symbols, e.g. {f[1], f[Sin[4]], f[I], f[a]} evaluates to {1, Sin[4], f[I], f[a]} as expected.

However, if I have an unknown number $b$ which I know to be real, I cannot get Mathematica to give $f(b)=b$. I have tried

Simplify[f[b], Assumptions -> {b ∈ Reals}] 

but this evaluates to f[b].

$\endgroup$
  • 2
    $\begingroup$ You are mixing things used for representing mathematical concepts with programming constructs. ; is a programming construct: it just influences evaluation. It is not meant o be used this way and Simplify won't (and shouldn't) operate on it. I tried to write an answer but then I realized that the real question is: what do you want to do here? You can take a look at ConditionalExpression which is meant for representing a mathematical concept, but whether it is of use to you depends on what you are trying to do. $\endgroup$ – Szabolcs Jan 22 '15 at 23:41
  • $\begingroup$ Do you mean, "{f[1], f[Sin[4]], f[I], f[a]} evaluates to {1, Sin[4], f[I], f[a]}? $\endgroup$ – bbgodfrey Jan 22 '15 at 23:55
  • $\begingroup$ @bbgodfrey Yes, I meant that, I must have copy-pasted the wrong part. Thanks for mentioning. $\endgroup$ – Nikki Bisschop Jan 23 '15 at 0:11
  • 2
    $\begingroup$ @Nikki After a few minutes thinking I am guessing this question is a duplicate of one of these: (30312), (30322). For example you could write f[x_] /; Simplify[x \[Element] Reals] := "success!" and then Assuming[x \[Element] Reals, Simplify[f[x]]] which should yield "success!". Does this work for you? $\endgroup$ – Mr.Wizard Jan 23 '15 at 0:44
  • 1
    $\begingroup$ Actually, reviewing both of those questions again they each seem rather complicated. I like the simplicity of this question and its potential answer. I shall answer and see what the community wants to make of it. $\endgroup$ – Mr.Wizard Jan 23 '15 at 1:04
7
$\begingroup$

If you are willing to use Assuming, which acts by way of [$Assumptions](http://reference.wolfram.com/language/ref/$Assumptions.html), you can use an `$Assumptions-aware [Condition](http://reference.wolfram.com/language/ref/Condition.html) to achieve what I believe you want. I shall use"success!"` to illustrate that the definition is truly being used and not another transformation.

f[x_] /; Simplify[x ∈ Reals] := "success!"

Now:

Assuming[x ∈ Reals, f[x]]
"success!"

How this works:

  1. Assuming has the attribute HoldRest which keeps f[x] unevaluated until:

  2. Assuming temporarily changes the value of $Assumptions to include x ∈ Reals

  3. f[x] is called and the condition Simplify[x ∈ Reals] is evaluated

  4. Simplify uses the value of $Assumptions and determines that x ∈ Reals is True

  5. The Condition passes and f[x] evaluates to "success!"


This question is arguably a duplicate of each of these:

However in review neither question seemed as direct and simple as this one so I hesitated to close.

$\endgroup$
  • $\begingroup$ This is a little bit frustrating because the problem is simple and the solution is far from being trivial ... (because of the use of the use of /; with Simplify , because you have to use Assuming (and its HoldRest attribute), because Simplify or Refine won't do the job at least directly ... The only alternative I've found is to use Block and $Assumptions : Block[{$Assumptions=Element[a,Reals]},Simplify[f[a]]] or Block[{$Assumptions=Element[a,Reals]},Refine[f[a]]]. Are there other ways ? (without using /; for example ?) $\endgroup$ – SquareOne Jan 23 '15 at 18:01
  • $\begingroup$ @SquareOne Well you could use PatternTest instead of Condition but I don't see that as an improvement per se. Likewise your Block is simply reimplementing Assuming. What exactly is your complaint? Would defining realQ = Simplify[# ∈ Reals] &; and then f[x_?realQ] := "success!" be more pleasing to you? $\endgroup$ – Mr.Wizard Jan 23 '15 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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