Repository: sagemath/sage

Issue:
minimal_model of GradedCommutativeAlgebra misuses cache
### Is there an existing issue for this?

- [X] I have searched the existing issues for a bug report that matches the one I want to file, without success.

### Did you read the documentation and troubleshoot guide?

- [X] I have read the documentation and troubleshoot guide

### Environment

```markdown
- **OS**: Debian Linux
- **Sage Version**: 9.8
```


### Steps To Reproduce

1. Create two GradedCommutativeDifferentialAlgebras with the same names in the generators but different minimal models
2. Compute the minimal model of the first one
3. Compute the minimal model of the second one

### Expected Behavior

The codomain of the minimal model of the second GCDA should be the second algebra itself

### Actual Behavior

It returns the first algebra

### Additional Information

Example:

```
sage: A.<e1,e2,e3,e4,e5,e6> = GradedCommutativeAlgebra(QQ)
sage: B1 = A.cdg_algebra({e5:e1*e2,e6:e3*e4})
sage: B2 = A.cdg_algebra({e5:e1*e2,e6:e1*e2+e3*e4})
sage: B1.minimal_model()
Commutative Differential Graded Algebra morphism:
  From: Commutative Differential Graded Algebra with generators ('x1_0', 'x1_1', 'x1_2', 'x1_3', 'y1_0', 'y1_1') in degrees (1, 1, 1, 1, 1, 1) over Rational Field with differential:
   x1_0 --> 0
   x1_1 --> 0
   x1_2 --> 0
   x1_3 --> 0
   y1_0 --> x1_0*x1_1
   y1_1 --> x1_2*x1_3
  To:   Commutative Differential Graded Algebra with generators ('e1', 'e2', 'e3', 'e4', 'e5', 'e6') in degrees (1, 1, 1, 1, 1, 1) over Rational Field with differential:
   e1 --> 0
   e2 --> 0
   e3 --> 0
   e4 --> 0
   e5 --> e1*e2
   e6 --> e3*e4
  Defn: (x1_0, x1_1, x1_2, x1_3, y1_0, y1_1) --> (e1, e2, e3, e4, e5, e6)
sage: B2.minimal_model()
Commutative Differential Graded Algebra morphism:
  From: Commutative Differential Graded Algebra with generators ('x1_0', 'x1_1', 'x1_2', 'x1_3', 'y1_0', 'y1_1') in degrees (1, 1, 1, 1, 1, 1) over Rational Field with differential:
   x1_0 --> 0
   x1_1 --> 0
   x1_2 --> 0
   x1_3 --> 0
   y1_0 --> x1_0*x1_1
   y1_1 --> x1_2*x1_3
  To:   Commutative Differential Graded Algebra with generators ('e1', 'e2', 'e3', 'e4', 'e5', 'e6') in degrees (1, 1, 1, 1, 1, 1) over Rational Field with differential:
   e1 --> 0
   e2 --> 0
   e3 --> 0
   e4 --> 0
   e5 --> e1*e2
   e6 --> e3*e4
  Defn: (x1_0, x1_1, x1_2, x1_3, y1_0, y1_1) --> (e1, e2, e3, e4, e5, -e5 + e6)
```


Discussion:

Resolving patch:
diff --git a/src/sage/algebras/commutative_dga.py b/src/sage/algebras/commutative_dga.py
--- a/src/sage/algebras/commutative_dga.py
+++ b/src/sage/algebras/commutative_dga.py
@@ -561,7 +561,7 @@ def cohomology(self, n):
         H_basis = [sum(c * b for (c, b) in zip(coeffs, B)) for coeffs in
                    H_basis_raw]
         # Put brackets around classes.
-        H_basis_brackets = [CohomologyClass(b) for b in H_basis]
+        H_basis_brackets = [CohomologyClass(b, A) for b in H_basis]
         return CombinatorialFreeModule(A.base_ring(),
                                        H_basis_brackets,
                                        sorting_key=sorting_keys,
@@ -855,7 +855,7 @@ def cohomology(self, n, total=False):
         H_basis = [sum(c * b for (c, b) in zip(coeffs, B))
                    for coeffs in H_basis_raw]
         # Put brackets around classes.
-        H_basis_brackets = [CohomologyClass(b) for b in H_basis]
+        H_basis_brackets = [CohomologyClass(b, A) for b in H_basis]
         return CombinatorialFreeModule(A.base_ring(),
                                        H_basis_brackets,
                                        sorting_key=sorting_keys,
@@ -3893,10 +3893,55 @@ class CohomologyClass(SageObject, CachedRepresentation):
         sage: CohomologyClass(3)
         [3]
         sage: A.<x,y,z,t> = GradedCommutativeAlgebra(QQ, degrees = (2,2,3,3))
-        sage: CohomologyClass(x^2+2*y*z)
+        sage: CohomologyClass(x^2+2*y*z, A)
         [2*y*z + x^2]
+
+
+    TESTS:
+    
+    In order for the cache to not confuse objects with the same representation,
+    we can pass the parent of the representative as a parameter::
+
+        sage: A.<e1,e2,e3,e4,e5,e6> = GradedCommutativeAlgebra(QQ)
+        sage: B1 = A.cdg_algebra({e5:e1*e2,e6:e3*e4})
+        sage: B2 = A.cdg_algebra({e5:e1*e2,e6:e1*e2+e3*e4})
+        sage: B1.minimal_model()
+        Commutative Differential Graded Algebra morphism:
+          From: Commutative Differential Graded Algebra with generators ('x1_0', 'x1_1', 'x1_2', 'x1_3', 'y1_0', 'y1_1') in degrees (1, 1, 1, 1, 1, 1) over Rational Field with differential:
+           x1_0 --> 0
+           x1_1 --> 0
+           x1_2 --> 0
+           x1_3 --> 0
+           y1_0 --> x1_0*x1_1
+           y1_1 --> x1_2*x1_3
+          To:   Commutative Differential Graded Algebra with generators ('e1', 'e2', 'e3', 'e4', 'e5', 'e6') in degrees (1, 1, 1, 1, 1, 1) over Rational Field with differential:
+           e1 --> 0
+           e2 --> 0
+           e3 --> 0
+           e4 --> 0
+           e5 --> e1*e2
+           e6 --> e3*e4
+          Defn: (x1_0, x1_1, x1_2, x1_3, y1_0, y1_1) --> (e1, e2, e3, e4, e5, e6)
+        sage: B2.minimal_model()
+        Commutative Differential Graded Algebra morphism:
+          From: Commutative Differential Graded Algebra with generators ('x1_0', 'x1_1', 'x1_2', 'x1_3', 'y1_0', 'y1_1') in degrees (1, 1, 1, 1, 1, 1) over Rational Field with differential:
+           x1_0 --> 0
+           x1_1 --> 0
+           x1_2 --> 0
+           x1_3 --> 0
+           y1_0 --> x1_0*x1_1
+           y1_1 --> x1_2*x1_3
+          To:   Commutative Differential Graded Algebra with generators ('e1', 'e2', 'e3', 'e4', 'e5', 'e6') in degrees (1, 1, 1, 1, 1, 1) over Rational Field with differential:
+           e1 --> 0
+           e2 --> 0
+           e3 --> 0
+           e4 --> 0
+           e5 --> e1*e2
+           e6 --> e1*e2 + e3*e4
+          Defn: (x1_0, x1_1, x1_2, x1_3, y1_0, y1_1) --> (e1, e2, e3, e4, e5, -e5 + e6)
+
     """
-    def __init__(self, x):
+    def __init__(self, x, cdga=None):
         """
         EXAMPLES::
 
@@ -3905,6 +3950,7 @@ def __init__(self, x):
             [x - 2]
         """
         self._x = x
+        self._cdga = cdga
 
     def __hash__(self):
         r"""


Test patch: