Repository: sagemath/sage

Issue:
Random failure in ell_field.py with division_field
### Steps To Reproduce

This raises an error:
```
sage: F.<a> = GF(592337197)
sage: E = EllipticCurve([F(341280459), F(489478549), F(203134672), F(127259459), F(366459012)])
sage: E
Elliptic Curve defined by y^2 + 341280459*x*y + 203134672*y = x^3 + 489478549*x^2 + 127259459*x + 366459012 over Finite Field of size 592337197
sage: E.division_field(3)
```
As a result, this doesn't pass for me (OS X):
```
sage -t --long --random-seed=46499345266195225323927627332877868800 src/sage/schemes/elliptic_curves/ell_field.py
```

### Expected Behavior

The test should pass.

### Actual Behavior

An error is raised:
```
TypeError: unable to convert Identity endomorphism of Finite Field of size 592337197 to an element of Finite Field of size 592337197
```

### Additional Information

_No response_

### Environment

```markdown
- OS X 13.6.1, Intel, plus various `homebrew` packages
- Sage version 10.3.beta0
```


### Checklist

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


Discussion:
@JohnCremona 
This seems to be a new bug. I did not get an error (and the specified doctests passed) with v10.2 on intel MacOS 13.6:
```
sage: E.division_field(3)
Finite Field of size 592337197
```
But I get the `TypeError` now that I have upgraded to 10.3b0.
I will take a look. Not today
Probably related to Flint upgrade in #35848
This failure is not specific to macOS. Also shows up randomly in the conda CI on Linux - https://github.com/sagemath/sage/actions/runs/7259211723/job/19837970698?pr=36877#step:11:8314
Minimal failing example:
```sage
sage: F = GF(11)
sage: phi = Hom(F,F).an_element()
sage: R.<x,y> = F[]
sage: x.change_ring(phi)
```

Part of the issue is that *identity* finite-field embeddings are returned as a generic `IdentityMorphism`:
```sage
sage: type(Hom(GF(11), GF(11)).an_element())
<class 'sage.categories.morphism.IdentityMorphism'>
sage: type(Hom(GF(11), GF(11^2)).an_element())
<class 'sage.rings.finite_rings.hom_prime_finite_field.FiniteFieldHomomorphism_prime'>
```
...which then proceeds to cause trouble somewhere in the coercion system.
Can you add `division_field` to the title. I reported this in #35936 because this one didn't pop up in search.

My example is:
```
EllipticCurve(GF(528238639), [78525647, 395630016, 427518312, 245441800, 351879008]).division_field(3)
```

> Minimal failing example:
> 
> ```python
> sage: F = GF(11)
> sage: phi = Hom(F,F).an_element()
> sage: R.<x,y> = F[]
> sage: x.change_ring(phi)
> ```

What do you mean? Is this supposed to work? `phi` is not a ring. So failure is expected.



The input for `.change_ring()` can be either a ring or a ring morphism. The documentation says `"R" -- a ring or morphism` and shows an example where it works. In this case it also works for ring morphisms which aren't the `IdentityMorphism`.

(Edit) Example for your convenience:
```sage
sage: F = GF(11)
sage: phi = Hom(F, F.extension(2)).an_element()
sage: R.<x,y> = F[]
sage: x.change_ring(phi)
x
sage: x.change_ring(phi).parent()
Multivariate Polynomial Ring in x, y over Finite Field in z2 of size 11^2
```
It makes sense. Thanks. 

Then it remains to spot the code that is responsible  in dealing with the morphism input.
I think we should also consider the possibility that having a generic `IdentityMorphism` class is not very useful and it'd be better to instead construct an identity morphism of the same type as all the other morphisms in that category.
Generic `IdentityMorphism` class itself has no fault, but the identity morphism in `Hom(GF(11), GF(11))` should be a ring homomorphism, that is, an instance of `RingHomomorphism` or something. This seems a bug of `Hom`.
I traced the code. At some point, `isinstance(x, morphism.RingHomomorphism_im_gens)` returns `False` for `x` being `Identity endomorphism of Finite Field of size 11` and `type(x)` `<class 'sage.categories.morphism.IdentityMorphism'>`

```diff
--- a/src/sage/rings/polynomial/multi_polynomial.pyx
+++ b/src/sage/rings/polynomial/multi_polynomial.pyx
@@ -878,9 +878,9 @@ cdef class MPolynomial(CommutativePolynomial):
             [x*y]
         """
         if isinstance(R, Map):
-        #if we're given a hom of the base ring extend to a poly hom
+            # if we're given a hom of the base ring, extend to a hom of the polynomial ring
             if R.domain() == self.base_ring():
-                R = self.parent().hom(R, self.parent().change_ring(R.codomain()))
+                R = self.parent().hom(self.parent().change_ring(R.codomain()), base_map=R)
             return R(self)
         else:
             return self.parent().change_ring(R)(self.dict())
```
This seems to fix the original bug (without "fixing identity morphism"). 
Can it be related to https://github.com/sagemath/sage/pull/36472? 
No. That is for ideals. This is for polynomials.
The fix #37159 is ready for review.

Resolving patch:
diff --git a/src/sage/rings/polynomial/multi_polynomial_element.py b/src/sage/rings/polynomial/multi_polynomial_element.py
--- a/src/sage/rings/polynomial/multi_polynomial_element.py
+++ b/src/sage/rings/polynomial/multi_polynomial_element.py
@@ -412,40 +412,6 @@ def __rpow__(self, n):
     def element(self):
         return self.__element
 
-    def change_ring(self, R):
-        r"""
-        Change the base ring of this polynomial to ``R``.
-
-        INPUT:
-
-        - ``R`` -- ring or morphism.
-
-        OUTPUT: a new polynomial converted to ``R``.
-
-        EXAMPLES::
-
-            sage: R.<x,y> = QQ[]
-            sage: f = x^2 + 5*y
-            sage: f.change_ring(GF(5))
-            x^2
-
-        ::
-
-            sage: # needs sage.rings.number_field
-            sage: K.<w> = CyclotomicField(5)
-            sage: R.<x,y> = K[]
-            sage: f = x^2 + w*y
-            sage: f.change_ring(K.embeddings(QQbar)[1])
-            x^2 + (-0.8090169943749474? + 0.5877852522924731?*I)*y
-        """
-        if isinstance(R, Morphism):
-            #if we're given a hom of the base ring extend to a poly hom
-            if R.domain() == self.base_ring():
-                R = self.parent().hom(R, self.parent().change_ring(R.codomain()))
-            return R(self)
-        else:
-            return self.parent().change_ring(R)(self)
-
 
 class MPolynomial_polydict(Polynomial_singular_repr, MPolynomial_element):
     r"""


Test patch: