8
$\begingroup$

Suppose I have a function $f(x)$. I want Mathematica to treat $x$ as a real positive number, and hence to interpret $\sqrt{x^{2}}$ as $x$, for instance. How to do that?

$\endgroup$
3
  • $\begingroup$ Assuming[x>0,f[x]] should solve your problem. You could also use $Assumptions command. $\endgroup$
    – ivbc
    Commented Jun 2, 2017 at 16:44
  • $\begingroup$ Depending on what computation you're doing, the answer might be there isn't a (convenient) way. Mathematica applies $Assumptions sparingly, for instance, whenever Simplify is called and functions that have an Assumptions option. I don't think you can get M to automatically simplify Sqrt[x^2] to x (barring overriding Power). You would have to use something like Simplify to get the assumptions to have the desired effect. Often that is sufficiently acceptable. $\endgroup$
    – Michael E2
    Commented Jun 2, 2017 at 17:04
  • $\begingroup$ Related: How to tell Mathematica that the argument of a function is real? $\endgroup$
    – jkuczm
    Commented Jun 4, 2017 at 19:04

1 Answer 1

9
$\begingroup$

I'll use "code" in this answer to symbolize whatever you are doing, just in case it is more complex than just the function you mentioned.

One way to get Mathematica to do what you ask is by:

Assuming[x>0,
"code"
]

But as "code" gets bigger or starts to encompass more than one cell it becomes easier to use

$Assumptions = x > 0;
"code"
$Assumptions = True;

The last line is not strictly necessary, but it might be very important. It clears your assumptions, so that you may use the symbol x freely again. If you have more than one notebook in use at once I recommend using it.

Checkout the tutorials when in doubt (lots of good stuff there):

https://reference.wolfram.com/language/ref/$Assumptions.html?q=%24Assumptions

$\endgroup$
0

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.