Repository: sympy/sympy

Issue:
`radsimp` gives wrong result for `1 / (x + sqrt(x**2))`
I got into an accidental discovery of this issue while developing #21343
Generally, even if `1 / (x + sqrt(x**2))` can have a nonzero denominator for `x > 0`, the expression is not rationalizable because multiplying `x - sqrt(x**2)` makes the denominator zero. (`x - sqrt(x**2)` should also not be rationalizable.)

I saw that `radsimp(1 / (x + sqrt(x**2)))` gives `zoo*(x + sqrt(x**2))` which should be a wrong result.
I think that it needs a test for intermediate zero or `NaN, zoo`.

Discussion:

Patch:
diff --git a/sympy/simplify/radsimp.py b/sympy/simplify/radsimp.py
index 4c671317a8..1ea4951763 100644
--- a/sympy/simplify/radsimp.py
+++ b/sympy/simplify/radsimp.py
@@ -9,7 +9,7 @@
 from sympy.core.exprtools import Factors, gcd_terms
 from sympy.core.function import _mexpand
 from sympy.core.mul import _keep_coeff, _unevaluated_Mul
-from sympy.core.numbers import Rational
+from sympy.core.numbers import Rational, zoo, nan
 from sympy.functions import exp, sqrt, log
 from sympy.functions.elementary.complexes import Abs
 from sympy.polys import gcd
@@ -966,6 +966,8 @@ def handle(expr):
             n *= num
             d *= num
             d = powdenest(_mexpand(d), force=symbolic)
+            if d.has(S.Zero, nan, zoo):
+                return expr
             if d.is_Atom:
                 break
 


Test patch:
diff --git a/sympy/simplify/tests/test_radsimp.py b/sympy/simplify/tests/test_radsimp.py
index 47b143615d..a44950c994 100644
--- a/sympy/simplify/tests/test_radsimp.py
+++ b/sympy/simplify/tests/test_radsimp.py
@@ -467,3 +467,8 @@ def test_issue_19719():
     collected = collect(expr, (a**2, 1/a), evaluate=False)
     # Would return {_Dummy_20**(-2): b + 1, 1/a: 7 + 1/b} without xreplace
     assert collected == {a**2: b + 1, 1/a: 7 + 1/b}
+
+
+def test_issue_21355():
+    assert radsimp(1/(x + sqrt(x**2))) == 1/(x + sqrt(x**2))
+    assert radsimp(1/(x - sqrt(x**2))) == 1/(x - sqrt(x**2))