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$
10
  • 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
    Commented Jan 22, 2015 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
    Commented Jan 22, 2015 at 23:55
  • $\begingroup$ @bbgodfrey Yes, I meant that, I must have copy-pasted the wrong part. Thanks for mentioning. $\endgroup$ Commented Jan 23, 2015 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
    Commented Jan 23, 2015 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
    Commented Jan 23, 2015 at 1:04

1 Answer 1

9
$\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$
3
  • $\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
    Commented Jan 23, 2015 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
    Commented Jan 23, 2015 at 18:07
  • $\begingroup$ Refine is more specific to this task than Simplify. $\endgroup$ Commented Apr 9, 2022 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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