Repository: sympy/sympy
File: sympy/polys/numberfields/tests/test_subfield.py (Python)

Code before:
    assert primitive_element(
        [sqrt(2)], x, ex=True, polys=True) == (Poly(x**2 - 2, domain='QQ'), [1], [[1, 0]])
    assert primitive_element([sqrt(2), sqrt(3)], x, ex=True, polys=True) == \
        (Poly(x**4 - 10*x**2 + 1, domain='QQ'), [1, 1], [[Q(1, 2), 0, -Q(9, 2),
         0], [-Q(1, 2), 0, Q(11, 2), 0]])

    assert primitive_element([sqrt(2)], polys=True) == (Poly(x**2 - 2), [1])

    raises(ValueError, lambda: primitive_element([], x, ex=False))
    raises(ValueError, lambda: primitive_element([], x, ex=True))

    # Issue 14117
    a, b = I*sqrt(2*sqrt(2) + 3), I*sqrt(-2*sqrt(2) + 3)
    assert primitive_element([a, b, I], x) == (x**4 + 6*x**2 + 1, [1, 0, 0])

    assert primitive_element([sqrt(2), 0], x) == (x**2 - 2, [1, 0])
    assert primitive_element([0, sqrt(2)], x) == (x**2 - 2, [1, 1])
    assert primitive_element([sqrt(2), 0], x, ex=True) == (x**2 - 2, [1, 0], [[MPQ(1,1), MPQ(0,1)], []])
    assert primitive_element([0, sqrt(2)], x, ex=True) == (x**2 - 2, [1, 1], [[], [MPQ(1,1), MPQ(0,1)]])

    # Issue 27798
    a, b = CRootOf(49*x**3 - 49*x**2 + 14*x - 1, 2), CRootOf(49*x**3 - 49*x**2 + 14*x - 1, 0)
    assert primitive_element([a, b], polys=True)[0].primitive()[0] == 1
    assert primitive_element([a, b], polys=True, ex=True)[0].primitive()[0] == 1

def test_to_number_field():
    assert to_number_field(sqrt(2)) == AlgebraicNumber(sqrt(2))
    assert to_number_field(
        [sqrt(2), sqrt(3)]) == AlgebraicNumber(sqrt(2) + sqrt(3))

    a = AlgebraicNumber(sqrt(2) + sqrt(3), [S.Half, S.Zero, Rational(-9, 2), S.Zero])

    assert to_number_field(sqrt(2), sqrt(2) + sqrt(3)) == a
    assert to_number_field(sqrt(2), AlgebraicNumber(sqrt(2) + sqrt(3))) == a

    raises(IsomorphismFailed, lambda: to_number_field(sqrt(2), sqrt(3)))


def test_issue_22561():
    a = to_number_field(sqrt(2), sqrt(2) + sqrt(3))
    b = to_number_field(sqrt(2), sqrt(2) + sqrt(5))
    assert field_isomorphism(a, b) == [1, 0]


def test_issue_22736():
    a = CRootOf(x**4 + x**3 + x**2 + x + 1, -1)
    a._reset()
    b = exp(2*I*pi/5)
    assert field_isomorphism(a, b) == [1, 0]


Review comment (suggestion):
There should be two blank lines between the test functions here and it is better to have a URL for the issue like:
```
https://github.com/sympy/sympy/issues/27798
```
That makes it clear that it is a GitHub issue number and also is easy to click and go straight to the issue.

Code after:

    assert primitive_element(
        [sqrt(2)], x, ex=True, polys=True) == (Poly(x**2 - 2, domain='QQ'), [1], [[1, 0]])
    assert primitive_element([sqrt(2), sqrt(3)], x, ex=True, polys=True) == \
        (Poly(x**4 - 10*x**2 + 1, domain='QQ'), [1, 1], [[Q(1, 2), 0, -Q(9, 2),
         0], [-Q(1, 2), 0, Q(11, 2), 0]])

    assert primitive_element([sqrt(2)], polys=True) == (Poly(x**2 - 2), [1])

    raises(ValueError, lambda: primitive_element([], x, ex=False))
    raises(ValueError, lambda: primitive_element([], x, ex=True))

    # Issue 14117
    a, b = I*sqrt(2*sqrt(2) + 3), I*sqrt(-2*sqrt(2) + 3)
    assert primitive_element([a, b, I], x) == (x**4 + 6*x**2 + 1, [1, 0, 0])

    assert primitive_element([sqrt(2), 0], x) == (x**2 - 2, [1, 0])
    assert primitive_element([0, sqrt(2)], x) == (x**2 - 2, [1, 1])
    assert primitive_element([sqrt(2), 0], x, ex=True) == (x**2 - 2, [1, 0], [[MPQ(1,1), MPQ(0,1)], []])
    assert primitive_element([0, sqrt(2)], x, ex=True) == (x**2 - 2, [1, 1], [[], [MPQ(1,1), MPQ(0,1)]])


def test_to_number_field():
    assert to_number_field(sqrt(2)) == AlgebraicNumber(sqrt(2))
    assert to_number_field(
        [sqrt(2), sqrt(3)]) == AlgebraicNumber(sqrt(2) + sqrt(3))

    a = AlgebraicNumber(sqrt(2) + sqrt(3), [S.Half, S.Zero, Rational(-9, 2), S.Zero])

    assert to_number_field(sqrt(2), sqrt(2) + sqrt(3)) == a
    assert to_number_field(sqrt(2), AlgebraicNumber(sqrt(2) + sqrt(3))) == a

    raises(IsomorphismFailed, lambda: to_number_field(sqrt(2), sqrt(3)))


def test_issue_22561():
    a = to_number_field(sqrt(2), sqrt(2) + sqrt(3))
    b = to_number_field(sqrt(2), sqrt(2) + sqrt(5))
    assert field_isomorphism(a, b) == [1, 0]


def test_issue_22736():
    a = CRootOf(x**4 + x**3 + x**2 + x + 1, -1)
    a._reset()
    b = exp(2*I*pi/5)
    assert field_isomorphism(a, b) == [1, 0]


def test_issue_27798():
    # https://github.com/sympy/sympy/issues/27798
    a, b = CRootOf(49*x**3 - 49*x**2 + 14*x - 1, 2), CRootOf(49*x**3 - 49*x**2 + 14*x - 1, 0)