<commit_before>"""Tests for computational algebraic number field theory. """

import pytest

from diofant import (Add, GoldenRatio, I, Integer, PurePoly, Rational, cbrt,
                     conjugate, cos, exp, exp_polar, expand,
                     expand_multinomial, nsimplify, oo, pi, root, sin, solve,
                     sqrt)
from diofant.abc import x, y, z
from diofant.domains import QQ
from diofant.polys.numberfields import (field_isomorphism,
                                        is_isomorphism_possible,
                                        minimal_polynomial, primitive_element)
from diofant.polys.polyerrors import CoercionFailed, NotAlgebraic
from diofant.polys.polytools import degree
from diofant.polys.rootoftools import RootOf


__all__ = ()


def test_minimal_polynomial():
    assert minimal_polynomial(-7)(x) == x + 7
    assert minimal_polynomial(-1)(x) == x + 1
    assert minimal_polynomial( 0)(x) == x
    assert minimal_polynomial( 1)(x) == x - 1
    assert minimal_polynomial( 7)(x) == x - 7

    assert minimal_polynomial(Rational(1, 3), method='groebner')(x) == 3*x - 1

    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(pi, method='groebner'))
    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(sin(sqrt(2)), method='groebner'))
    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(2**pi, method='groebner'))

    pytest.raises(ValueError, lambda: minimal_polynomial(1, method='spam'))

    assert minimal_polynomial(sqrt(2))(x) == x**2 - 2
    assert minimal_polynomial(sqrt(5))(x) == x**2 - 5
    assert minimal_polynomial(sqrt(6))(x) == x**2 - 6

    assert minimal_polynomial(2*sqrt(2))(x) == x**2 - 8
    assert minimal_polynomial(3*sqrt(5))(x) == x**2 - 45
    assert minimal_polynomial(4*sqrt(6))(x) == x**2 - 96

    assert minimal_polynomial(2*sqrt(2) + 3)(x) == x**2 - 6*x + 1
    assert minimal_polynomial(3*sqrt(5) + 6)(x) == x**2 - 12*x - 9
    assert minimal_polynomial(4*sqrt(6) + 7)(x) == x**2 - 14*x - 47

    assert minimal_polynomial(2*sqrt(2) - 3)(x) == x**2 + 6*x + 1
    assert minimal_polynomial(3*sqrt(5) - 6)(x) == x**2 + 12*x - 9
    assert minimal_polynomial(4*sqrt(6) - 7)(x) == x**2 + 14*x - 47

    assert minimal_polynomial(sqrt(1 + sqrt(6)))(x) == x**4 - 2*x**2 - 5
    assert minimal_polynomial(sqrt(I + sqrt(6)))(x) == x**8 - 10*x**4 + 49

    assert minimal_polynomial(2*I + sqrt(2 + I))(x) == x**4 + 4*x**2 + 8*x + 37

    assert minimal_polynomial(sqrt(2) + sqrt(3))(x) == x**4 - 10*x**2 + 1
    assert minimal_polynomial(sqrt(2) + sqrt(3) + sqrt(6))(x) == x**4 - 22*x**2 - 48*x - 23

    e = 1/sqrt(sqrt(1 + sqrt(3)) - 4)
    assert minimal_polynomial(e) == minimal_polynomial(e, method='groebner')
    assert minimal_polynomial(e)(x) == (222*x**8 + 240*x**6 +
                                        94*x**4 + 16*x**2 + 1)

    a = 1 - 9*sqrt(2) + 7*sqrt(3)

    assert minimal_polynomial(1/a)(x) == 392*x**4 - 1232*x**3 + 612*x**2 + 4*x - 1
    assert minimal_polynomial(1/sqrt(a))(x) == 392*x**8 - 1232*x**6 + 612*x**4 + 4*x**2 - 1

    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(oo))
    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(2**y))
    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(sin(1)))

    assert minimal_polynomial(sqrt(2))(x) == x**2 - 2

    assert minimal_polynomial(sqrt(2)) == PurePoly(x**2 - 2)
    assert minimal_polynomial(sqrt(2), method='groebner') == PurePoly(x**2 - 2)

    a = sqrt(2)
    b = sqrt(3)

    assert minimal_polynomial(b)(x) == x**2 - 3

    assert minimal_polynomial(a) == PurePoly(x**2 - 2)
    assert minimal_polynomial(b) == PurePoly(x**2 - 3)

    assert minimal_polynomial(sqrt(a)) == PurePoly(x**4 - 2)
    assert minimal_polynomial(a + 1) == PurePoly(x**2 - 2*x - 1)
    assert minimal_polynomial(sqrt(a/2 + 17))(x) == 2*x**4 - 68*x**2 + 577
    assert minimal_polynomial(sqrt(b/2 + 17))(x) == 4*x**4 - 136*x**2 + 1153

    # issue diofant/diofant#431
    K = QQ.algebraic_field(sqrt(2))
    theta = K.to_expr(K([Rational(1, 2), 17]))
    assert minimal_polynomial(theta)(x) == 2*x**2 - 68*x + 577

    K = QQ.algebraic_field(RootOf(x**7 + x - 1, 3))
    theta = K.to_expr(K([1, 2, 0, 0, 1]))
    ans = minimal_polynomial(theta)(x)
    assert ans == (x**7 - 7*x**6 + 19*x**5 - 27*x**4 + 63*x**3 -
                   115*x**2 + 82*x - 147)
    assert minimal_polynomial(theta, method='groebner')(x) == ans
    K = QQ.algebraic_field(RootOf(x**5 + 5*x - 1, 2))
    theta = K.to_expr(K([1, -1, 1]))
    ans = (x**30 - 15*x**28 - 10*x**27 + 135*x**26 + 330*x**25 - 705*x**24 -
           150*x**23 + 3165*x**22 - 6850*x**21 + 7182*x**20 + 3900*x**19 +
           4435*x**18 + 11970*x**17 - 259725*x**16 - 18002*x**15 +
           808215*x**14 - 200310*x**13 - 647115*x**12 + 299280*x**11 -
           1999332*x**10 + 910120*x**9 + 2273040*x**8 - 5560320*x**7 +
           5302000*x**6 - 2405376*x**5 + 1016640*x**4 - 804480*x**3 +
           257280*x**2 - 53760*x + 1280)
    assert minimal_polynomial(sqrt(theta) + root(theta, 3))(x) == ans
    K1 = QQ.algebraic_field(RootOf(x**3 + 4*x - 15, 1))
    K2 = QQ.algebraic_field(RootOf(x**3 - x + 1, 0))
    theta = sqrt(1 + 1/(K1.to_expr(K([1, 0, 1])) +
                        1/(sqrt(3) + K2.to_expr(K2([1, 2, -1])))))
    ans = (2262264837876687263*x**36 - 38939909597855051866*x**34 +
           315720420314462950715*x**32 - 1601958657418182606114*x**30 +
           5699493671077371036494*x**28 - 15096777696140985506150*x**26 +<commit_msg>polys: Adapt test from 791d467cf1 to explicitely use groebner-based method<commit_after>"""Tests for computational algebraic number field theory. """

import pytest

from diofant import (Add, GoldenRatio, I, Integer, PurePoly, Rational, cbrt,
                     conjugate, cos, exp, exp_polar, expand,
                     expand_multinomial, nsimplify, oo, pi, root, sin, solve,
                     sqrt)
from diofant.abc import x, y, z
from diofant.domains import QQ
from diofant.polys.numberfields import (field_isomorphism,
                                        is_isomorphism_possible,
                                        minimal_polynomial, primitive_element)
from diofant.polys.polyerrors import CoercionFailed, NotAlgebraic
from diofant.polys.polytools import degree
from diofant.polys.rootoftools import RootOf


__all__ = ()


def test_minimal_polynomial():
    assert minimal_polynomial(-7)(x) == x + 7
    assert minimal_polynomial(-1)(x) == x + 1
    assert minimal_polynomial( 0)(x) == x
    assert minimal_polynomial( 1)(x) == x - 1
    assert minimal_polynomial( 7)(x) == x - 7

    assert minimal_polynomial(Rational(1, 3), method='groebner')(x) == 3*x - 1

    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(pi, method='groebner'))
    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(sin(sqrt(2)), method='groebner'))
    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(2**pi, method='groebner'))

    pytest.raises(ValueError, lambda: minimal_polynomial(1, method='spam'))

    assert minimal_polynomial(sqrt(2))(x) == x**2 - 2
    assert minimal_polynomial(sqrt(5))(x) == x**2 - 5
    assert minimal_polynomial(sqrt(6))(x) == x**2 - 6

    assert minimal_polynomial(2*sqrt(2))(x) == x**2 - 8
    assert minimal_polynomial(3*sqrt(5))(x) == x**2 - 45
    assert minimal_polynomial(4*sqrt(6))(x) == x**2 - 96

    assert minimal_polynomial(2*sqrt(2) + 3)(x) == x**2 - 6*x + 1
    assert minimal_polynomial(3*sqrt(5) + 6)(x) == x**2 - 12*x - 9
    assert minimal_polynomial(4*sqrt(6) + 7)(x) == x**2 - 14*x - 47

    assert minimal_polynomial(2*sqrt(2) - 3)(x) == x**2 + 6*x + 1
    assert minimal_polynomial(3*sqrt(5) - 6)(x) == x**2 + 12*x - 9
    assert minimal_polynomial(4*sqrt(6) - 7)(x) == x**2 + 14*x - 47

    assert minimal_polynomial(sqrt(1 + sqrt(6)))(x) == x**4 - 2*x**2 - 5
    assert minimal_polynomial(sqrt(I + sqrt(6)))(x) == x**8 - 10*x**4 + 49

    assert minimal_polynomial(2*I + sqrt(2 + I))(x) == x**4 + 4*x**2 + 8*x + 37

    assert minimal_polynomial(sqrt(2) + sqrt(3))(x) == x**4 - 10*x**2 + 1
    assert minimal_polynomial(sqrt(2) + sqrt(3) + sqrt(6))(x) == x**4 - 22*x**2 - 48*x - 23

    e = 1/sqrt(sqrt(1 + sqrt(3)) - 4)
    assert minimal_polynomial(e) == minimal_polynomial(e, method='groebner')
    assert minimal_polynomial(e)(x) == (222*x**8 + 240*x**6 +
                                        94*x**4 + 16*x**2 + 1)

    a = 1 - 9*sqrt(2) + 7*sqrt(3)

    assert minimal_polynomial(1/a)(x) == 392*x**4 - 1232*x**3 + 612*x**2 + 4*x - 1
    assert minimal_polynomial(1/sqrt(a))(x) == 392*x**8 - 1232*x**6 + 612*x**4 + 4*x**2 - 1

    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(oo))
    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(2**y))
    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(sin(1)))

    assert minimal_polynomial(sqrt(2))(x) == x**2 - 2

    assert minimal_polynomial(sqrt(2)) == PurePoly(x**2 - 2)
    assert minimal_polynomial(sqrt(2), method='groebner') == PurePoly(x**2 - 2)

    a = sqrt(2)
    b = sqrt(3)

    assert minimal_polynomial(b)(x) == x**2 - 3

    assert minimal_polynomial(a) == PurePoly(x**2 - 2)
    assert minimal_polynomial(b) == PurePoly(x**2 - 3)

    assert minimal_polynomial(sqrt(a)) == PurePoly(x**4 - 2)
    assert minimal_polynomial(a + 1) == PurePoly(x**2 - 2*x - 1)
    assert minimal_polynomial(sqrt(a/2 + 17))(x) == 2*x**4 - 68*x**2 + 577
    assert minimal_polynomial(sqrt(b/2 + 17))(x) == 4*x**4 - 136*x**2 + 1153

    # issue diofant/diofant#431
    K = QQ.algebraic_field(sqrt(2))
    theta = K.to_expr(K([Rational(1, 2), 17]))
    assert minimal_polynomial(theta)(x) == 2*x**2 - 68*x + 577

    K = QQ.algebraic_field(RootOf(x**7 + x - 1, 3))
    theta = K.to_expr(K([1, 2, 0, 0, 1]))
    ans = minimal_polynomial(theta)(x)
    assert ans == (x**7 - 7*x**6 + 19*x**5 - 27*x**4 + 63*x**3 -
                   115*x**2 + 82*x - 147)
    assert minimal_polynomial(theta, method='groebner')(x) == ans
    K = QQ.algebraic_field(RootOf(x**5 + 5*x - 1, 2))
    theta = K.to_expr(K([1, -1, 1]))
    ans = (x**30 - 15*x**28 - 10*x**27 + 135*x**26 + 330*x**25 - 705*x**24 -
           150*x**23 + 3165*x**22 - 6850*x**21 + 7182*x**20 + 3900*x**19 +
           4435*x**18 + 11970*x**17 - 259725*x**16 - 18002*x**15 +
           808215*x**14 - 200310*x**13 - 647115*x**12 + 299280*x**11 -
           1999332*x**10 + 910120*x**9 + 2273040*x**8 - 5560320*x**7 +
           5302000*x**6 - 2405376*x**5 + 1016640*x**4 - 804480*x**3 +
           257280*x**2 - 53760*x + 1280)
    assert minimal_polynomial(sqrt(theta) + root(theta, 3),
                              method='groebner')(x) == ans
    K1 = QQ.algebraic_field(RootOf(x**3 + 4*x - 15, 1))
    K2 = QQ.algebraic_field(RootOf(x**3 - x + 1, 0))
    theta = sqrt(1 + 1/(K1.to_expr(K([1, 0, 1])) +
                        1/(sqrt(3) + K2.to_expr(K2([1, 2, -1])))))
    ans = (2262264837876687263*x**36 - 38939909597855051866*x**34 +
           315720420314462950715*x**32 - 1601958657418182606114*x**30 +
           5699493671077371036494*x**28 - 15096777696140985506150*x**26 +
{'fraction_non_alphanumeric': 0.1792300805729633, 'fraction_numerical': 0.11396598030438675, 'mean_word_length': 3.021238300935925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '659619', 'n_tokens_mistral': 5302, 'n_tokens_neox': 4422, 'n_words': 824}