Repository: sympy/sympy (Python)

Issue:
Sign of generator of an algebraic numberfield
`AlgebraicField` calls [`to_number_field`](https://github.com/sympy/sympy/blob/master/sympy/polys/domains/algebraicfield.py#L33) in order to create an `AlgebraicNumber` generating the extension field. This number is chosen positive in some cases where the sign can be determined.  This choice is currently implemented, at least in part, when the algebraic number is [created](https://github.com/sympy/sympy/blob/master/sympy/core/numbers.py#L2390), but the implementation is defective as it does not change the minimal polynomial.
```
>>> b = 1 - sqrt(2)
>>> a = to_number_field(b)
>>> a
-1 + sqrt(2)  # positive generator created
>>> minimal_polynomial(b)
_x**2 - 2*_x - 1  # this is correct
>>> minimal_polynomial(a)
_x**2 - 2*_x - 1  # but this is wrong, apparently copied from a.minpoly
>>> a.minpoly
PurePoly(_x**2 - 2*_x - 1, _x, domain='QQ')
>>> minimal_polynomial(-1 + sqrt(2))
_x**2 + 2*_x - 1  # this is the correct minimal polynomial of a
```
I think this could be fixed in two ways:
* Add code to create the changed minimal polynomial.
* Ignore the sign and remove the code changing it.

I am inclined to prefer the latter, simpler solution, but I would also like to hear other suggestions.


Discussion:
In the Diofant:
```python
n [11]: b = 1 - sqrt(2)

In [12]: a = to_number_field(b)

In [13]: a.root
Out[13]: 
    ___    
- ╲╱ 2  + 1

In [14]: a.minpoly
Out[14]: PurePoly(_x**2 - 2*_x - 1, _x, domain='ZZ')

In [15]: a.coeffs()
Out[15]: [1, 0]
```
I think, that's correct.  C.f. with Sympy:
```
In [7]: b = 1 - sqrt(2)

In [8]: a = to_number_field(b)

In [9]: a
Out[9]: -1 + √2

In [10]: a.root
Out[10]: -√2 + 1

In [11]: a.coeffs()
Out[11]: [-1, 0]

In [12]: a.minpoly
Out[12]: PurePoly(_x**2 - 2*_x - 1, _x, domain='QQ')
```
That looks like the sign change was removed? I think that the sign should be the ignored in SymPy as well.
I don't know in deep but yes. I think the sign should be ignored(but maybe this property is used somewhere) as the number must retain its original sign.

well I found
```
>>> a.args
(-sqrt(2) + 1, (-1, 0))
```
and as done [here](https://github.com/sympy/sympy/blob/master/sympy/core/numbers.py#L2406) the `minpoly` of `obj` (`a` here) is `minimal_polynomial` of `b`.

We can also check type of `a` [here](https://github.com/sympy/sympy/blob/master/sympy/polys/numberfields.py#L640) and then return the minimal polynomial of `a.args[0]*a.args[1][0]`.
```
>>> minimal_polynomial(a.args[0]*a.args[1][0])
_x**2 + 2*_x - 1
```
> That looks like the sign change was removed?

@jksuom, apparently so.

> We can also check type of a here and then return the minimal polynomial of a.args[0]*a.args[1][0]

Great idea!  But why not ``a.args[0]*a.args[1][42]``?
`a.args[1]` has only two index 0 and 1. `a.args[1][0]` will be 1 for positive root and -1 for negative root.([here](https://github.com/sympy/sympy/blob/master/sympy/polys/numberfields.py#L2390)) . 

Should I add a PR to remove the sign change?
> a.args[1] has only two index 0 and 1

How did you know that?
Yes! Sorry my fault `len(sarg)` [here](https://github.com/sympy/sympy/blob/master/sympy/core/numbers.py#L2399) could be more than 2 if `alias` is present.

@skirpichev the PR you referenced seem to be of 'diofant' repository. can I open a PR to remove sign change of `a` when it negative in sympy. ?
> Sorry my fault len(sarg) here could be more than 2 if alias is present.

Wow!

> can I open a PR

Sure.



Patch:
diff --git a/sympy/core/numbers.py b/sympy/core/numbers.py
index 610c72120ff5..03658cad1ece 100644
--- a/sympy/core/numbers.py
+++ b/sympy/core/numbers.py
@@ -2387,10 +2387,6 @@ def __new__(cls, expr, coeffs=None, alias=None, **args):
             rep = DMP.from_list([1, 0], 0, dom)
             scoeffs = Tuple(1, 0)
 
-            if root.is_negative:
-                rep = -rep
-                scoeffs = Tuple(-1, 0)
-
         sargs = (root, scoeffs)
 
         if alias is not None:


Test patch:
diff --git a/sympy/core/tests/test_numbers.py b/sympy/core/tests/test_numbers.py
index 8b4b1628e1c7..e3b75380d901 100644
--- a/sympy/core/tests/test_numbers.py
+++ b/sympy/core/tests/test_numbers.py
@@ -582,6 +582,15 @@ def test_Float_issue_2107():
     assert S.Zero + b + (-b) == 0
 
 
+def test_issue_14289():
+    from sympy.polys.numberfields import to_number_field
+
+    a = 1 - sqrt(2)
+    b = to_number_field(a)
+    assert b.as_expr() == a
+    assert b.minpoly(a).expand() == 0
+
+
 def test_Float_from_tuple():
     a = Float((0, '1L', 0, 1))
     b = Float((0, '1', 0, 1))
diff --git a/sympy/polys/tests/test_numberfields.py b/sympy/polys/tests/test_numberfields.py
index 3464c3ed2caf..3f1bd9c0b385 100644
--- a/sympy/polys/tests/test_numberfields.py
+++ b/sympy/polys/tests/test_numberfields.py
@@ -591,7 +591,7 @@ def test_AlgebraicNumber():
     assert a.is_aliased is False
 
     assert AlgebraicNumber( sqrt(3)).rep == DMP([ QQ(1), QQ(0)], QQ)
-    assert AlgebraicNumber(-sqrt(3)).rep == DMP([-QQ(1), QQ(0)], QQ)
+    assert AlgebraicNumber(-sqrt(3)).rep == DMP([ QQ(1), QQ(0)], QQ)
 
     a = AlgebraicNumber(sqrt(2))
     b = AlgebraicNumber(sqrt(2))