Repository: sympy/sympy

Issue:
Collect with key dependencies
`collect` fails to factorize  in 1.6.1 (and 1.6) when the symbols have dependencies. For example:

```
In [5]: expr                                                                                                                                                                         
Out[5]: a**2*(b + 1) + (7 + 1/b)/a

In [6]: collect(expr, (a**2, 1/a), evaluate=False)                                                                                                                                   
Out[6]: {_Dummy_20**(-2): b + 1, 1/a: 7 + 1/b}
```

collected factors should be replaced in the keys as well, will open fixing PR asap.

Discussion:

Patch:
diff --git a/.mailmap b/.mailmap
index 309a553c20..87ef51e633 100644
--- a/.mailmap
+++ b/.mailmap
@@ -485,4 +485,5 @@ Hugo van Kemenade <hugovk@users.noreply.github.com> Hugo <hugovk@users.noreply.g
 Michal Grňo <m93a.cz@gmail.com> Michal Grno <m93a.cz@gmail.com>
 Mohit Balwani <mohitbalwani.ict17@gmail.com> Mohit Balwani <44258119+Mohitbalwani26@users.noreply.github.com>
 Mohit Balwani <mohitbalwani.ict17@gmail.com> Mohitbalwani26 <44258119+Mohitbalwani26@users.noreply.github.com>
+Mathias Louboutin <mathias.louboutin@gmail.com>
 Psycho-Pirate <prakharsaxena.cer18@itbhu.ac.in> Psycho-Pirate <50932406+Psycho-Pirate@users.noreply.github.com>
diff --git a/sympy/simplify/radsimp.py b/sympy/simplify/radsimp.py
index 9fc899b5b0..fcb03e725b 100644
--- a/sympy/simplify/radsimp.py
+++ b/sympy/simplify/radsimp.py
@@ -178,7 +178,8 @@ def collect(expr, syms, func=None, evaluate=None, exact=False, distribute_order_
         if not isinstance(rv, dict):
             return rv.xreplace(urep)
         else:
-            return {urep.get(k, k): v.xreplace(urep) for k, v in rv.items()}
+            return {urep.get(k, k).xreplace(urep): v.xreplace(urep)
+                    for k, v in rv.items()}
 
     if evaluate is None:
         evaluate = global_parameters.evaluate


Test patch:
diff --git a/sympy/simplify/tests/test_radsimp.py b/sympy/simplify/tests/test_radsimp.py
index 9c3577bc9c..47b143615d 100644
--- a/sympy/simplify/tests/test_radsimp.py
+++ b/sympy/simplify/tests/test_radsimp.py
@@ -460,3 +460,10 @@ def test_collect_abs():
 def test_issue_19149():
     eq = exp(3*x/4)
     assert collect(eq, exp(x)) == eq
+
+def test_issue_19719():
+    a, b = symbols('a, b')
+    expr = a**2 * (b + 1) + (7 + 1/b)/a
+    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}