I won't have any occasion to have any imaginary number in my code. If there are any, that is an error.

So allowing the imaginary case simply hinders the equation manipulation and simplification.

I simply want to assume that all variables in my code are reals.

I know default Mathematica doesn't provide this feature.

However, from this page

How to tell Mathematica that the argument of a function is real?

I learned it's possible to code up a function that will make a certain pattern (? Though I don't understand his answer quite much) to assume reals.

Then perhaps it's also possible to code up a function that assumes all variables in the code are reals. Is it?


You can do something like this:

(* Sqrt[x^2] *)

$Assumptions = _ ∈ Reals
(* _ ∈ Reals *)

(* Abs[x] *)

This tells those functions that have an Assumptions option that any expression is considered real.

Caveat: This refers to any expression, not just any variable! So you get this now:

Simplify[Sqrt[x] ∈ Reals]
(* True *)

Even though it is not in general assumed that x > 0.

I have not tried this personally and I do not know if it will cause trouble along the way.


A more restrictive version is $Assumptions = _Symbol ∈ Reals. This will not cause Simplify[Sqrt[x] ∈ Reals] to return True. But it will only assume proper symbols to be real. Thus, x will be considered real, but not f[x] and not Subscript[x,1]. Pattern matching is not aware of mathematical meaning.

There are other functions which do not have an Assumptions option but can still work with reals only. These will have a "domain" option, which can be set to real. Examples are Reduce, Solve, FindInstance, etc.


Reduce[x^2 == -1, x]
(* x == -I || x == I *)

Reduce[x^2 == -1, x, Reals]
(* False *)

Another thing to note is that most symbolic processing functions will assume that things appearing in an inequality are real. From the Reduce documentation:

Reduce[expr,vars] assumes by default that quantities appearing algebraically in inequalities are real, while all other quantities are complex.

This means that we get results like this:

Reduce[Sqrt[x] < 0]
(* False *)

Though this is not true for general complex x, Reduce automatically assumes x to be real due to the inequality.

  • $\begingroup$ Note that using things like Reduce and Simplify may perform manipulationa you do not want. The minimal method is use Refine as so: Refine[Conjugate[a+I b], _Symbol ∈ Reals]. For an exhaustive discussion, see here. $\endgroup$ – Jess Riedel Sep 27 '17 at 18:51
  • $\begingroup$ Somehow this makes Simplify[1/(a + b I) [Element] Reals] to be true. $\endgroup$ – ablmf Nov 29 '18 at 16:18
  • $\begingroup$ How did you know assumptions work with patterns? I don't see this documented anywhere. $\endgroup$ – Chip Hurst Mar 13 at 20:46
  • 3
    $\begingroup$ @ChipHurst It's not Assumptions, it's Element. I don't think we can do _ > 0, but we can do Element[_, Reals]. $\endgroup$ – Szabolcs Mar 13 at 21:22

Would ComplexExpand work for you? It expands the expression assuming that all variables are real.


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.