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?
Asked
Active
Viewed 1.3k times
7
1 Answers
9
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
$Assumptionssparingly, for instance, wheneverSimplifyis called and functions that have anAssumptionsoption. I don't think you can get M to automatically simplifySqrt[x^2]tox(barring overridingPower). You would have to use something likeSimplifyto get the assumptions to have the desired effect. Often that is sufficiently acceptable. – Michael E2 Jun 02 '17 at 17:04