Repository: sympy/sympy

Issue:
`nan` is a rational function
In `is_rational_function()`, there's a line which states that if the intersection of the expression's free symbols is an empty set, then the expression is a *constant rational function*. Thus, `nan.is_rational_function()` is `True`, and this behaviour causes bugs like #8414.

I guess that a simple check of `if self is nan` or so just before `return True` should fix that bug. However, there may be some implications that I miss, so I leave it as an issue rather than as a pull request.

Discussion:
leosartaj: @smichr
debugger22: Please update your branch with master.

Patch:
diff --git a/sympy/core/expr.py b/sympy/core/expr.py
index 0de02ff424..c047f157f3 100644
--- a/sympy/core/expr.py
+++ b/sympy/core/expr.py
@@ -2307,6 +2307,9 @@ def is_rational_function(self, *syms):
         See also is_algebraic_expr().
 
         """
+        if self in [S.NaN, S.Infinity, -S.Infinity, S.ComplexInfinity]:
+            return False
+
         if syms:
             syms = set(map(sympify, syms))
         else:


Test patch:
diff --git a/sympy/core/tests/test_expr.py b/sympy/core/tests/test_expr.py
index 2876515a49..d0fe697665 100644
--- a/sympy/core/tests/test_expr.py
+++ b/sympy/core/tests/test_expr.py
@@ -417,6 +417,11 @@ def test_is_rational_function():
     assert (sin(y)/x).is_rational_function(x) is True
     assert (sin(y)/x).is_rational_function(x, y) is False
 
+    assert (S.NaN).is_rational_function() is False
+    assert (S.Infinity).is_rational_function() is False
+    assert (-S.Infinity).is_rational_function() is False
+    assert (S.ComplexInfinity).is_rational_function() is False
+
 
 def test_is_algebraic_expr():
     assert sqrt(3).is_algebraic_expr(x) is True