Repository: sympy/sympy
Pull request: Added test for sring extension=True error
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->
Fixes #18894 

#### Brief description of what is fixed or changed


#### Other comments


#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
NO ENTRY 
<!-- END RELEASE NOTES -->


Problem statement:
sring extension=True error: nan is not in any domain
Somehow a nan is generated in the intermediate calculations I guess (same for sfield):
```julia
In [1]: items = [S(3)/16 + sqrt(3*sqrt(3) + 10)/8, S(1)/8 + 3*sqrt(3)/16, S(1)/8 + 3*sqrt(3)/16, -S(3)/16 + sqrt(3*sqrt(3) + 10)/8]                           

In [2]: items                                                                                                                                                 
Out[2]: 
⎡       ___________                               ___________⎤
⎢3    ╲╱ 3⋅√3 + 10   1   3⋅√3  1   3⋅√3    3    ╲╱ 3⋅√3 + 10 ⎥
⎢── + ─────────────, ─ + ────, ─ + ────, - ── + ─────────────⎥
⎣16         8        8    16   8    16     16         8      ⎦

In [3]: sring(items, extension=True)                                                                                                                          
---------------------------------------------------------------------------
CoercionFailed                            Traceback (most recent call last)
<ipython-input-3-549325ae401c> in <module>
----> 1 sring(items, extension=True)

~/current/sympy/sympy/sympy/polys/rings.py in sring(exprs, *symbols, **options)
    173 
    174     _ring = PolyRing(opt.gens, opt.domain, opt.order)
--> 175     polys = list(map(_ring.from_dict, reps))
    176 
    177     if single:

~/current/sympy/sympy/sympy/polys/rings.py in from_dict(self, element)
    357 
    358         for monom, coeff in element.items():
--> 359             coeff = domain_new(coeff)
    360             if coeff:
    361                 poly[monom] = coeff

~/current/sympy/sympy/sympy/polys/rings.py in domain_new(self, element, orig_domain)
    316 
    317     def domain_new(self, element, orig_domain=None):
--> 318         return self.domain.convert(element, orig_domain)
    319 
    320     def ground_new(self, coeff):

~/current/sympy/sympy/sympy/polys/domains/domain.py in convert(self, element, base)
    145         if isinstance(element, Basic):
    146             try:
--> 147                 return self.from_sympy(element)
    148             except (TypeError, ValueError):
    149                 pass

~/current/sympy/sympy/sympy/polys/domains/algebraicfield.py in from_sympy(self, a)
     75 
     76         try:
---> 77             return self(to_number_field(a, self.ext).native_coeffs())
     78         except (NotAlgebraic, IsomorphismFailed):
     79             raise CoercionFailed(

~/current/sympy/sympy/sympy/polys/numberfields.py in to_number_field(extension, theta, **args)
   1077             theta = AlgebraicNumber(theta, gen=gen)
   1078 
-> 1079         coeffs = field_isomorphism(root, theta)
   1080 
   1081         if coeffs is not None:

~/current/sympy/sympy/sympy/polys/numberfields.py in field_isomorphism(a, b, **args)
   1043     if args.get('fast', True):
   1044         try:
-> 1045             result = field_isomorphism_pslq(a, b)
   1046 
   1047             if result is not None:

~/current/sympy/sympy/sympy/polys/numberfields.py in field_isomorphism_pslq(a, b)
    975 
    976         coeffs = list(reversed(coeffs))
--> 977         h = Poly(coeffs, f.gen, domain='QQ')
    978 
    979         if f.compose(h).rem(g).is_zero:

~/current/sympy/sympy/sympy/polys/polytools.py in __new__(cls, rep, *gens, **args)
    150                 return cls._from_dict(rep, opt)
    151             else:
--> 152                 return cls._from_list(list(rep), opt)
    153         else:
    154             rep = sympify(rep)

~/current/sympy/sympy/sympy/polys/polytools.py in _from_list(cls, rep, opt)
    245             domain, rep = construct_domain(rep, opt=opt)
    246         else:
--> 247             rep = list(map(domain.convert, rep))
    248 
    249         return cls.new(DMP.from_list(rep, level, domain), *gens)

~/current/sympy/sympy/sympy/polys/domains/domain.py in convert(self, element, base)
    106         """Convert ``element`` to ``self.dtype``. """
    107         if _not_a_coeff(element):
--> 108             raise CoercionFailed('%s is not in any domain' % element)
    109 
    110         if base is not None:

CoercionFailed: nan is not in any domain
```

Hints from issue discussion:
This seems to be working now.
Looks like it was fixed in 97129c5f70b32ca39875caccf3d5bb574c497546 from https://github.com/sympy/sympy/pull/20274
@oscarbenjamin seems it working fine on the master, should it require a test to close?
``` python

>>> items = [S(3)/16 + sqrt(3*sqrt(3) + 10)/8, S(1)/8 + 3*sqrt(3)/16, S(1)/8 + 3*sqrt(3)/16, -S(3)/16 + sqrt(3*sqrt(3) + 10)/8]
>>> items
[3/16 + sqrt(3*sqrt(3) + 10)/8, 1/8 + 3*sqrt(3)/16, 1/8 + 3*sqrt(3)/16, -3/16 + sqrt(3*sqrt(3) + 10)/8]
>>> sring(items, extension=True)
(Polynomial ring in  over QQ<sqrt(3) + sqrt(3*sqrt(3) + 10)> with lex order, [-ANP([MPQ(-1,76), MPQ(3,152), MPQ(3,8), MPQ(87,304)], [MPQ(1,1), MPQ(0,1), MPQ(-26,1), MPQ(-36,1), MPQ(22,1)], QQ), ANP([MPQ(3,152), MPQ(-9,304), MPQ(-3,8), MPQ(-7,304)], [MPQ(1,1), MPQ(0,1), MPQ(-26,1), MPQ(-36,1), MPQ(22,1)], QQ), ANP([MPQ(3,152), MPQ(-9,304), MPQ(-3,8), MPQ(-7,304)], [MPQ(1,1), MPQ(0,1), MPQ(-26,1), MPQ(-36,1), MPQ(22,1)], QQ), -ANP([MPQ(-1,76), MPQ(3,152), MPQ(3,8), MPQ(-27,304)], [MPQ(1,1), MPQ(0,1), MPQ(-26,1), MPQ(-36,1), MPQ(22,1)], QQ)])
```
Yes, there should be a test.

Commit message: Added test for sring extension=True error

Patch to review:
diff --git a/sympy/polys/tests/test_rings.py b/sympy/polys/tests/test_rings.py
index 35d2b3617230..4bd47b0cd00f 100644
--- a/sympy/polys/tests/test_rings.py
+++ b/sympy/polys/tests/test_rings.py
@@ -9,10 +9,12 @@
 from sympy.polys.orderings import lex, grlex
 from sympy.polys.polyerrors import GeneratorsError, \
     ExactQuotientFailed, MultivariatePolynomialError, CoercionFailed
+from sympy.external.gmpy import MPQ
+from sympy.polys.polyclasses import ANP
 
 from sympy.testing.pytest import raises
 from sympy.core import Symbol, symbols
-
+from sympy.core.singleton import S
 from sympy.core.numbers import (oo, pi)
 from sympy.functions.elementary.exponential import exp
 from sympy.functions.elementary.miscellaneous import sqrt
@@ -1455,6 +1457,16 @@ def test_PolyElement_sqf_list():
     assert f.sqf_part() == p
     assert f.sqf_list() == (1, [(g, 1), (h, 2)])
 
+def test_issue_18894():
+    items = [S(3)/16 + sqrt(3*sqrt(3) + 10)/8, S(1)/8 + 3*sqrt(3)/16, S(1)/8 + 3*sqrt(3)/16, -S(3)/16 + sqrt(3*sqrt(3) + 10)/8]
+    R,a = sring(items, extension=True)
+    assert R.domain == QQ.algebraic_field(sqrt(3)+sqrt(3*sqrt(3)+10))
+    assert R.gens == ()
+    result = []
+    for item in items:
+        result.append(R.domain.from_sympy(item))
+    assert a == result
+
 def test_PolyElement_factor_list():
     _, x = ring("x", ZZ)
 


Code review comments:
diff_hunk: @@ -1455,6 +1457,16 @@ def test_PolyElement_sqf_list():
     assert f.sqf_part() == p
     assert f.sqf_list() == (1, [(g, 1), (h, 2)])
 
+def test_issue_18894():
+    items = [S(3)/16 + sqrt(3*sqrt(3) + 10)/8, S(1)/8 + 3*sqrt(3)/16, S(1)/8 + 3*sqrt(3)/16, -S(3)/16 + sqrt(3*sqrt(3) + 10)/8]
+    R,a = sring(items, extension=True)
path: sympy/polys/tests/test_rings.py
text: @user1:
Please use `R, a = sring(...`