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?
1 Answer
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
$Assumptions
sparingly, for instance, wheneverSimplify
is called and functions that have anAssumptions
option. I don't think you can get M to automatically simplifySqrt[x^2]
tox
(barring overridingPower
). You would have to use something likeSimplify
to get the assumptions to have the desired effect. Often that is sufficiently acceptable. $\endgroup$