def test_diop_ternary_quadratic():
    # Commented out test cases should be uncommented after
    # the bug with factor_list() gets merged.

    assert check_solutions(2*x**2 + z**2 + y**2 - 4*x*y)
    assert check_solutions(x**2 - y**2 - z**2 - x*y - y*z)
    assert check_solutions(3*x**2 - x*y - y*z - x*z)
    assert check_solutions(x**2 - y*z - x*z)
    # assert check_solutions(5*x**2 - 3*x*y - x*z)
    assert check_solutions(4*x**2 - 5*y**2 - x*z)
    assert check_solutions(3*x**2 + 2*y**2 - z**2 - 2*x*y + 5*y*z - 7*y*z)
    assert check_solutions(8*x**2 - 12*y*z)
    assert check_solutions(45*x**2 - 7*y**2 - 8*x*y - z**2)
    assert check_solutions(x**2 - 49*y**2 - z**2 + 13*z*y - 8*x*y)
    assert check_solutions(90*x**2 + 3*y**2 + 5*x*y + 2*z*y + 5*x*z)
    assert check_solutions(x**2 + 3*y**2 + z**2 - x*y - 17*y*z)
    assert check_solutions(x**2 + 3*y**2 + z**2 - x*y - 16*y*z + 12*x*z)
    assert check_solutions(x**2 + 3*y**2 + z**2 - 13*x*y - 16*y*z + 12*x*z)
    assert check_solutions(x*y - 7*y*z + 13*x*z)

    assert diop_ternary_quadratic_normal(x**2 + y**2 + z**2) == (None, None, None)
    assert diop_ternary_quadratic_normal(x**2 + y**2) is None
    pytest.raises(ValueError,
                  lambda: _diop_ternary_quadratic_normal((x, y, z),
                                                         {x*y: 1, x**2: 2,
                                                          y**2: 3, z**2: 0}))
    eq = -2*x*y - 6*x*z + 7*y**2 - 3*y*z + 4*z**2
    assert diop_ternary_quadratic(eq) == (7, 2, 0)
    assert diop_ternary_quadratic_normal(4*x**2 + 5*y**2 - z**2) == (1, 0, 2)
    assert diop_ternary_quadratic(x*y + 2*y*z) == (-2, 0, n1)
    eq = -5*x*y - 8*x*z - 3*y*z + 8*z**2
    assert parametrize_ternary_quadratic(eq) == (64*p**2 - 24*p*q, -64*p*q + 64*q**2, 40*p*q)
    # this cannot be tested with diophantine because it will
    # factor into a product
    assert diop_solve(x*y + 2*y*z) == (-4*p*q, -2*n1*p**2 + 2*p**2, 2*p*q)
def test_diop_ternary_quadratic():
    # Commented out test cases should be uncommented after
    # the bug with factor_list() gets merged.

    assert check_solutions(2 * x**2 + z**2 + y**2 - 4 * x * y)
    assert check_solutions(x**2 - y**2 - z**2 - x * y - y * z)
    assert check_solutions(3 * x**2 - x * y - y * z - x * z)
    assert check_solutions(x**2 - y * z - x * z)
    # assert check_solutions(5*x**2 - 3*x*y - x*z)
    assert check_solutions(4 * x**2 - 5 * y**2 - x * z)
    assert check_solutions(3 * x**2 + 2 * y**2 - z**2 - 2 * x * y + 5 * y * z -
                           7 * y * z)
    assert check_solutions(8 * x**2 - 12 * y * z)
    assert check_solutions(45 * x**2 - 7 * y**2 - 8 * x * y - z**2)
    assert check_solutions(x**2 - 49 * y**2 - z**2 + 13 * z * y - 8 * x * y)
    assert check_solutions(90 * x**2 + 3 * y**2 + 5 * x * y + 2 * z * y +
                           5 * x * z)
    assert check_solutions(x**2 + 3 * y**2 + z**2 - x * y - 17 * y * z)
    assert check_solutions(x**2 + 3 * y**2 + z**2 - x * y - 16 * y * z +
                           12 * x * z)
    assert check_solutions(x**2 + 3 * y**2 + z**2 - 13 * x * y - 16 * y * z +
                           12 * x * z)
    assert check_solutions(x * y - 7 * y * z + 13 * x * z)

    assert diop_ternary_quadratic_normal(x**2 + y**2 + z**2) == (None, None,
                                                                 None)
    assert diop_ternary_quadratic_normal(x**2 + y**2) is None
    pytest.raises(
        ValueError, lambda: _diop_ternary_quadratic_normal((x, y, z), {
            x * y: 1,
            x**2: 2,
            y**2: 3,
            z**2: 0
        }))
    eq = -2 * x * y - 6 * x * z + 7 * y**2 - 3 * y * z + 4 * z**2
    assert diop_ternary_quadratic(eq) == (7, 2, 0)
    assert diop_ternary_quadratic_normal(4 * x**2 + 5 * y**2 - z**2) == (1, 0,
                                                                         2)
    assert diop_ternary_quadratic(x * y + 2 * y * z) == (-2, 0, n1)
    eq = -5 * x * y - 8 * x * z - 3 * y * z + 8 * z**2
    assert parametrize_ternary_quadratic(eq) == (64 * p**2 - 24 * p * q,
                                                 -64 * p * q + 64 * q**2,
                                                 40 * p * q)
    # this cannot be tested with diophantine because it will
    # factor into a product
    assert diop_solve(x * y + 2 * y * z) == (-4 * p * q,
                                             -2 * n1 * p**2 + 2 * p**2,
                                             2 * p * q)
def test_diopcoverage():
    eq = (2*x + y + 1)**2
    assert diop_solve(eq) == {(t_0, -2*t_0 - 1)}
    eq = 2*x**2 + 6*x*y + 12*x + 4*y**2 + 18*y + 18
    assert diop_solve(eq) == {(t_0, -t_0 - 3), (2*t_0 - 3, -t_0)}
    assert diop_quadratic(x + y**2 - 3) == {(-t**2 + 3, -t)}
    assert diop_quadratic(x + y) is None  # wrong type

    assert diop_linear(x + y - 3) == (t_0, 3 - t_0)
    assert diop_linear(x**2 - 1) is None  # wrong type

    assert base_solution_linear(0, 1, 2, t=None) == (0, 0)
    ans = (3*t - 1, -2*t + 1)
    assert base_solution_linear(4, 8, 12, t) == ans
    assert base_solution_linear(4, 8, 12, t=None) == tuple(_.subs({t: 0}) for _ in ans)

    assert cornacchia(1, 1, 20) is None
    assert cornacchia(1, 1, 5) == {(1, 2)}
    assert cornacchia(1, 2, 17) == {(3, 2)}
    assert cornacchia(2, 3, 31) == set()

    pytest.raises(ValueError, lambda: reconstruct(4, 20, 1))

    assert gaussian_reduce(4, 1, 3) == (1, 1)
    eq = -w**2 - x**2 - y**2 + z**2

    assert (diop_general_pythagorean(eq) == diop_general_pythagorean(-eq) ==
            (m1**2 + m2**2 - m3**2, 2*m1*m3, 2*m2*m3, m1**2 + m2**2 + m3**2))

    assert check_param(Integer(3) + x/3, Integer(4) + x/2,
                       Integer(2), x) == (None, None)
    assert check_param(Rational(3, 2), Integer(4) + x,
                       Integer(2), x) == (None, None)
    assert check_param(Integer(4) + x, Rational(3, 2),
                       Integer(2), x) == (None, None)

    assert _nint_or_floor(16, 10) == 2
    assert _odd(1) == (not _even(1)) is True
    assert _odd(0) == (not _even(0)) is False
    assert _remove_gcd(2, 4, 6) == (1, 2, 3)
    pytest.raises(TypeError, lambda: _remove_gcd((2, 4, 6)))
    assert sqf_normal(2 * 3**2 * 5, 2 * 5 * 11, 2 * 7**2 * 11) == (11, 1, 5)

    # it's ok if these pass some day when the solvers are implemented
    pytest.raises(NotImplementedError, lambda: diophantine(x**2 + y**2 + x*y + 2*y*z - 12))
    pytest.raises(NotImplementedError, lambda: diophantine(x**3 + y**2))

    # issue sympy/sympy#11026
    pytest.raises(NotImplementedError, lambda: diophantine(x**3 + y**3 - 2))

    assert transformation_to_DN(x + y) is None  # wrong type
    assert find_DN(x + y) is None  # wrong type
    assert diop_ternary_quadratic(x + y) is None  # wrong type
    assert transformation_to_normal(x + y) is None  # wrong type
    assert parametrize_ternary_quadratic(x + y) is None  # wrong type
    assert diop_general_pythagorean(x + y) is None  # wrong type
    assert diop_general_sum_of_squares(x + y) is None  # wrong type
    assert diop_general_sum_of_even_powers(x + y) is None  # wrong type
def test_diopcoverage():
    eq = (2 * x + y + 1)**2
    assert diop_solve(eq) == {(t_0, -2 * t_0 - 1)}
    eq = 2 * x**2 + 6 * x * y + 12 * x + 4 * y**2 + 18 * y + 18
    assert diop_solve(eq) == {(t_0, -t_0 - 3), (2 * t_0 - 3, -t_0)}
    assert diop_quadratic(x + y**2 - 3) == {(-t**2 + 3, -t)}
    assert diop_quadratic(x + y) is None  # wrong type

    assert diop_linear(x + y - 3) == (t_0, 3 - t_0)
    assert diop_linear(x**2 - 1) is None  # wrong type

    assert base_solution_linear(0, 1, 2, t=None) == (0, 0)
    ans = (3 * t - 1, -2 * t + 1)
    assert base_solution_linear(4, 8, 12, t) == ans
    assert base_solution_linear(4, 8, 12,
                                t=None) == tuple(_.subs({t: 0}) for _ in ans)

    assert cornacchia(1, 1, 20) is None
    assert cornacchia(1, 1, 5) == {(2, 1)}
    assert cornacchia(1, 2, 17) == {(3, 2)}
    assert cornacchia(2, 3, 31) == set()
    assert cornacchia(1, 4, 52) == {(4, 3)}

    pytest.raises(ValueError, lambda: reconstruct(4, 20, 1))

    assert gaussian_reduce(4, 1, 3) == (1, 1)
    eq = -w**2 - x**2 - y**2 + z**2

    assert (diop_general_pythagorean(eq) == diop_general_pythagorean(-eq) ==
            (m1**2 + m2**2 - m3**2, 2 * m1 * m3, 2 * m2 * m3,
             m1**2 + m2**2 + m3**2))

    assert check_param(Integer(3) + x / 3,
                       Integer(4) + x / 2, Integer(2), x) == (None, None)
    assert check_param(Rational(3, 2),
                       Integer(4) + x, Integer(2), x) == (None, None)
    assert check_param(Integer(4) + x, Rational(3, 2), Integer(2),
                       x) == (None, None)

    assert _nint_or_floor(16, 10) == 2
    assert _odd(1) == (not _even(1)) is True
    assert _odd(0) == (not _even(0)) is False
    assert _remove_gcd(2, 4, 6) == (1, 2, 3)
    assert sqf_normal(2 * 3**2 * 5, 2 * 5 * 11, 2 * 7**2 * 11) == (11, 1, 5)

    # it's ok if these pass some day when the solvers are implemented
    pytest.raises(NotImplementedError,
                  lambda: diophantine(x**2 + y**2 + x * y + 2 * y * z - 12))
    pytest.raises(NotImplementedError, lambda: diophantine(x**3 + y**2))

    # issue sympy/sympy#11026
    pytest.raises(NotImplementedError, lambda: diophantine(x**3 + y**3 - 2))

    assert transformation_to_DN(x + y) is None  # wrong type
    assert find_DN(x + y) is None  # wrong type
    assert diop_ternary_quadratic(x + y) is None  # wrong type
    assert transformation_to_normal(x + y) is None  # wrong type
    assert parametrize_ternary_quadratic(x + y) is None  # wrong type
    assert diop_general_pythagorean(x + y) is None  # wrong type
    assert diop_general_sum_of_squares(x + y) is None  # wrong type
    assert diop_general_sum_of_even_powers(x + y) is None  # wrong type

    assert diop_quadratic(x**2 + y**2 - 1**2 - 3**4) == \
        {(-9, -1), (-9, 1), (-1, -9), (-1, 9), (1, -9), (1, 9), (9, -1), (9, 1)}