Repository: sympy/sympy

Issue:
simplify(1/Trace(constant_matrix)) throws AttributeError
This is just like https://github.com/sympy/sympy/issues/19327, but unlike that issue happens with no local modifications:

```ipython
In [1]: from sympy import *

In [2]: m = Matrix([[30, 2], [3, 4]])

In [3]: simplify(1/Trace(m))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-61-7221c8bb7585> in <module>
----> 1 simplify(1/Trace(m))

c:\users\wiese\repos\ga\sympy\sympy\simplify\simplify.py in simplify(expr, ratio, measure, rational, inverse, doit, **kwargs)
    600     expr = bottom_up(expr, lambda w: getattr(w, 'normal', lambda: w)())
    601     expr = Mul(*powsimp(expr).as_content_primitive())
--> 602     _e = cancel(expr)
    603     expr1 = shorter(_e, _mexpand(_e).cancel())  # issue 6829
    604     expr2 = shorter(together(expr, deep=True), together(expr1, deep=True))

c:\users\wiese\repos\ga\sympy\sympy\polys\polytools.py in cancel(f, *gens, **args)
   6664
   6665     try:
-> 6666         (F, G), opt = parallel_poly_from_expr((p, q), *gens, **args)
   6667     except PolificationFailed:
   6668         if not isinstance(f, (tuple, Tuple)):

c:\users\wiese\repos\ga\sympy\sympy\polys\polytools.py in parallel_poly_from_expr(exprs, *gens, **args)
   4310     """Construct polynomials from expressions. """
   4311     opt = options.build_options(gens, args)
-> 4312     return _parallel_poly_from_expr(exprs, opt)
   4313
   4314

c:\users\wiese\repos\ga\sympy\sympy\polys\polytools.py in _parallel_poly_from_expr(exprs, opt)
   4363             exprs[i] = exprs[i].as_expr()
   4364
-> 4365     reps, opt = _parallel_dict_from_expr(exprs, opt)
   4366     if not opt.gens:
   4367         raise PolificationFailed(opt, origs, exprs, True)

c:\users\wiese\repos\ga\sympy\sympy\polys\polyutils.py in _parallel_dict_from_expr(exprs, opt)
    332         reps, gens = _parallel_dict_from_expr_if_gens(exprs, opt)
    333     else:
--> 334         reps, gens = _parallel_dict_from_expr_no_gens(exprs, opt)
    335
    336     return reps, opt.clone({'gens': gens})

c:\users\wiese\repos\ga\sympy\sympy\polys\polyutils.py in _parallel_dict_from_expr_no_gens(exprs, opt)
    274         reprs.append(terms)
    275
--> 276     gens = _sort_gens(gens, opt=opt)
    277     k, indices = len(gens), {}
    278

c:\users\wiese\repos\ga\sympy\sympy\polys\polyutils.py in _sort_gens(gens, **args)
    101
    102     try:
--> 103         gens = sorted(gens, key=order_key)
    104     except TypeError:  # pragma: no cover
    105         pass

c:\users\wiese\repos\ga\sympy\sympy\polys\polyutils.py in order_key(gen)
     81                 pass
     82
---> 83         name, index = _re_gen.match(gen).groups()
     84
     85         if index:

AttributeError: 'NoneType' object has no attribute 'groups'
```

Discussion:

Patch:
diff --git a/sympy/polys/polyutils.py b/sympy/polys/polyutils.py
index f51f80098d..d33a0e0e6a 100644
--- a/sympy/polys/polyutils.py
+++ b/sympy/polys/polyutils.py
@@ -21,7 +21,7 @@
 }
 
 _max_order = 1000
-_re_gen = re.compile(r"^(.+?)(\d*)$")
+_re_gen = re.compile(r"^(.*?)(\d*)$", re.MULTILINE)
 
 
 def _nsort(roots, separated=False):


Test patch:
diff --git a/sympy/matrices/tests/test_commonmatrix.py b/sympy/matrices/tests/test_commonmatrix.py
index 3d21b51078..8aeebb78d6 100644
--- a/sympy/matrices/tests/test_commonmatrix.py
+++ b/sympy/matrices/tests/test_commonmatrix.py
@@ -561,6 +561,10 @@ def test_simplify():
     assert M.simplify() == Matrix([[eq]])
     assert M.simplify(ratio=oo) == Matrix([[eq.simplify(ratio=oo)]])
 
+    # https://github.com/sympy/sympy/issues/19353
+    m = Matrix([[30, 2], [3, 4]])
+    assert (1/(m.trace())).simplify() == Rational(1, 34)
+
 
 def test_subs():
     assert OperationsOnlyMatrix([[1, x], [x, 4]]).subs(x, 5) == Matrix([[1, 5], [5, 4]])
diff --git a/sympy/polys/tests/test_polyutils.py b/sympy/polys/tests/test_polyutils.py
index d376426fd8..706f666c4e 100644
--- a/sympy/polys/tests/test_polyutils.py
+++ b/sympy/polys/tests/test_polyutils.py
@@ -1,7 +1,7 @@
 """Tests for useful utilities for higher level polynomial classes. """
 
 from sympy import (S, Integer, sin, cos, sqrt, symbols, pi,
-    Eq, Integral, exp, Mul)
+    Eq, Integral, exp, Mul, Symbol)
 from sympy.testing.pytest import raises
 
 from sympy.polys.polyutils import (
@@ -99,6 +99,11 @@ def test__sort_gens():
     assert _sort_gens([x, p, q], wrt='p', sort='q > x') == (p, q, x)
     assert _sort_gens([x, p, q], wrt='q', sort='p > x') == (q, p, x)
 
+    # https://github.com/sympy/sympy/issues/19353
+    n1 = Symbol('\n1')
+    assert _sort_gens([n1]) == (n1,)
+    assert _sort_gens([x, n1]) == (x, n1)
+
     X = symbols('x0,x1,x2,x10,x11,x12,x20,x21,x22')
 
     assert _sort_gens(X) == X
diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py
index 07acc16d63..d72aa0d91c 100644
--- a/sympy/solvers/tests/test_solvers.py
+++ b/sympy/solvers/tests/test_solvers.py
@@ -2351,8 +2351,3 @@ def test_issue_4886():
     t = b*c/(a**2 + b**2)
     sol = [((b*(t - z) - c)/(-a), t - z), ((b*(t + z) - c)/(-a), t + z)]
     assert solve([x**2 + y**2 - R**2, a*x + b*y - c], x, y) == sol
-
-
-def test_issue_6819():
-    a, b, c, d = symbols('a b c d', positive=True)
-    assert solve(a*b**x - c*d**x, x) == [log(c/a)/log(b/d)]