22
$\begingroup$

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?

$\endgroup$

2 Answers 2

29
$\begingroup$

You can do something like this:

Simplify[Sqrt[x^2]]
(* Sqrt[x^2] *)

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

Simplify[Sqrt[x^2]]
(* 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.


Update

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.

Examples:

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.

$\endgroup$
4
  • $\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$ Commented Sep 27, 2017 at 18:51
  • $\begingroup$ Somehow this makes Simplify[1/(a + b I) [Element] Reals] to be true. $\endgroup$ Commented Nov 29, 2018 at 16:18
  • $\begingroup$ How did you know assumptions work with patterns? I don't see this documented anywhere. $\endgroup$
    – Greg Hurst
    Commented Mar 13, 2021 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
    Commented Mar 13, 2021 at 21:22
1
$\begingroup$

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

$\endgroup$

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.