# Why does Mathematica not restrict the domains of Rational Expressions?

As taught in High School Mathematics:

$$\begin{array}{ccl} \left\{ {(x-3)(x+3)\over 5(x+3)} : \ x\ne -3 \right\} & \ne & \left\{ {x-3\over 5} : \ x\in\mathbb{R} \right\} \\ \left\{ {(x-3)(x+3)\over 5(x+3)} : \ x\ne -3 \right\} & = & \left\{ {x-3\over 5} : \ x\ne -3 \right\} \end{array}$$

In order to truly say one rational expression is "equal" to another, when canceling variables in the denominator, we need to retain the conditions that make the domains equivalent as well.

Though from what I can tell Mathematica seems to have no problem with the potential for Zero in the Denominator.

For example I would have expected the following to give a conditional output where $x\ne -3$, but they don't:

In[]:= Cancel[((x - 3) (x + 3))/(5 (x + 3))]
Out[]= 1/5 (-3 + x)

In[]:= Reduce[((x - 3) (x + 3))/(5 (x + 3)) == (x - 3)/5, x]
Out[]= True

In[]:= Resolve[ForAll[{x}, ((x - 3) (x + 3))/(5 (x + 3)) == (x - 3)/5]]
Out[]= True

In:= FunctionDomain[((x - 3) (x + 3))/(5 (x + 3)), x, Reals]
Out= True


Etc.

On a practical level I can just manually add the assumptions. But I'd like to increase my familiarity with Mathematica and understand why this behavior arises in the first place.

• You don't even need Reduce/Cancel/Resolve, etc because ((x - 3) (x + 3))/(5 (x + 3)) automatically simplifies to 1/5 (-3 + x) before any of these functions are called. – bill s Feb 24 '15 at 19:24
• I missed this day in high school mathematics. If we follow this logic, then even $\frac{x}{x}$ would have a point where it is undefined. But surely, the limit as x goes to zero of that function is still 1? Any mathematician here to chime in? – Jason B. Dec 11 '15 at 9:18
• So, it skips removable singularities… why do you need to keep them? – J. M.'s ennui Dec 11 '15 at 9:20
• – Patrick Stevens Dec 11 '15 at 9:47
• A bit late for commenting, but I'll mention that a conditional result from Cancel and related functions would be, for practical purposes, useless. Also it would not be correct in the sense of working over fields of rational functions. – Daniel Lichtblau Dec 11 '15 at 15:34

As noted in comment by bill s, given rational expression automatically simplifies even without using Cancel, Reduce, etc.:

((x - 3) (x + 3))/(5 (x + 3))
(* 1/5 (-3 + x) *)


If you want to restrict "domain of expression" in Mathematica you can do it manually using ConditionalExpression. Quoting from its documentation:

ConditionalExpression is automatically propagated from the arguments of mathematical functions, equations and inequalities, and Boolean operators

So you can use it in your equation:

ConditionalExpression[((x - 3) (x + 3))/(5 (x + 3)), x != -3] == (x - 3)/5
% /. x -> 5
%% /. x -> -3
(* ConditionalExpression[True, x != -3] *)
(* True *)
(* Undefined *)


If you want to automatically restrict expression domain you could use FunctionDomain, but since discussed rational expression automatically evaluates to expression which has larger domain, we need to wrap original expression with Unevaluated when passing it to FunctionDomain.

FunctionDomain[Unevaluated[((x - 3) (x + 3))/(5 (x + 3))], x]
(* x < -3 || x > -3 *)


Output of FunctionDomain can be passed as second argument of ConditionalExpression. Automation of this process was implemented in RestrictDomain function from Domains package. Using it we get:

RestrictDomain[((x - 3) (x + 3))/(5 (x + 3)), x]
(* ConditionalExpression[1/5 (-3 + x), x > -3 || x < -3] *)
`