Repository: sympy/sympy

Issue:
Bug in assumption computation of expression with matrix element
The following code throws an exception when trying to compute whether `e` is finite with Sympy 1.8
```
from sympy import *
a = MatrixSymbol("a",5,1)
e = cosh(a[4, 0]**2)
e.is_finite
```


Discussion:

Patch:
diff --git a/sympy/core/exprtools.py b/sympy/core/exprtools.py
index 459d896724..1d73d1724c 100644
--- a/sympy/core/exprtools.py
+++ b/sympy/core/exprtools.py
@@ -1101,6 +1101,8 @@ def handle(a):
         # don't treat internal args like terms of an Add
         if not isinstance(a, Expr):
             if isinstance(a, Basic):
+                if not a.args:
+                    return a
                 return a.func(*[handle(i) for i in a.args])
             return type(a)([handle(i) for i in a])
         return gcd_terms(a, isprimitive, clear, fraction)


Test patch:
diff --git a/sympy/core/tests/test_exprtools.py b/sympy/core/tests/test_exprtools.py
index 96e1dad08b..d87397bd31 100644
--- a/sympy/core/tests/test_exprtools.py
+++ b/sympy/core/tests/test_exprtools.py
@@ -464,3 +464,8 @@ def test_issue_17256():
     r1 = s1.xreplace({x:a})
     r2 = s2.xreplace({x:a})
     assert r1 == r2
+
+def test_issue_21623():
+    from sympy import MatrixSymbol, gcd_terms
+    M = MatrixSymbol('X', 2, 2)
+    assert gcd_terms(M[0,0], 1) == M[0,0]