Using Assumptions in Mathematica
Use Assumptions to simplify/modify terms. For example
Simplify[Sqrt[x^2]]
returns
Sqrt[x^2]
because x might be negative, in which case the expression cannot be simplified any further.
Mathematica keeps track of global assumptions in the variable (or
whatever that is) $Assumptions
. By default, this just contains True
.
If no other information is given, Mathematica assumes all variables to
be complex.
If you are sure that x is real and positive, you can pass that
information as an assumption to Simplify[]
and related commands. The
following three examples are roughly equivalent.
Simplify[Sqrt[x^2], Assumptions -> x > 0]
Simplify[Sqrt[x^2], x > 0]
Assuming[x > 0, Simplify[Sqrt[x^2]]]
The difference between the three examples above is: The 1st example
uses only the assumption that is given and ignores global
$Assumptions
. The 2nd and 3rd example use the given assumption in
addition to global $Assumptions
.
Of course, you can also modify the global assumptions. This can save you lots of typing.
$Assumptions = {x>0, y>0}
Be careful when using AppendTo
and $Assumptions
: AppendTo
expects
a list, but on the first call $Assumptions
is just an atomic
element (a single element, "True").
I now use the following code to add assumptions to the global $Assumptions:
$Assumptions = Union[Flatten[{$Assumptions, x > 5} ]]