Example #1
0
def test_diop_general_sum_of_squares_quick():
    for i in range(3, 10):
        assert check_solutions(sum(i**2 for i in symbols(':%i' % i)) - i)
    pytest.raises(ValueError, lambda: _diop_general_sum_of_squares((x, y), 2))
    assert _diop_general_sum_of_squares((x, y, z), -2) == set()
    eq = x**2 + y**2 + z**2 - (1 + 4 + 9)
    assert diop_general_sum_of_squares(eq) == {(1, 2, 3)}
    eq = u**2 + v**2 + x**2 + y**2 + z**2 - 1313
    assert len(diop_general_sum_of_squares(eq, 3)) == 3
    # issue sympy/sympy#11016
    var = symbols(':5') + (symbols('6', negative=True),)
    eq = Add(*[i**2 for i in var]) - 112
    assert diophantine(eq) == {
        (0, 1, 1, 5, 6, -7), (1, 1, 1, 3, 6, -8), (2, 3, 3, 4, 5, -7),
        (0, 1, 1, 1, 3, -10), (0, 0, 4, 4, 4, -8), (1, 2, 3, 3, 5, -8),
        (0, 1, 2, 3, 7, -7), (2, 2, 4, 4, 6, -6), (1, 1, 3, 4, 6, -7),
        (0, 2, 3, 3, 3, -9), (0, 0, 2, 2, 2, -10), (1, 1, 2, 3, 4, -9),
        (0, 1, 1, 2, 5, -9), (0, 0, 2, 6, 6, -6), (1, 3, 4, 5, 5, -6),
        (0, 2, 2, 2, 6, -8), (0, 3, 3, 3, 6, -7), (0, 2, 3, 5, 5, -7),
        (0, 1, 5, 5, 5, -6)}
    # handle negated squares with signsimp
    assert diophantine(12 - x**2 - y**2 - z**2) == {(2, 2, 2)}
    # diophantine handles simplification, so classify_diop should
    # not have to look for additional patterns that are removed
    # by diophantine
    eq = a**2 + b**2 + c**2 + d**2 - 4
    pytest.raises(NotImplementedError, lambda: classify_diop(-eq))
Example #2
0
def test_diop_general_sum_of_squares_quick():
    for i in range(3, 10):
        assert check_solutions(sum(i**2 for i in symbols(f':{i:d}')) - i)
    pytest.raises(ValueError, lambda: _diop_general_sum_of_squares((x, y), 2))
    assert _diop_general_sum_of_squares((x, y, z), -2) == set()
    eq = x**2 + y**2 + z**2 - (1 + 4 + 9)
    assert diop_general_sum_of_squares(eq) == {(1, 2, 3)}
    eq = u**2 + v**2 + x**2 + y**2 + z**2 - 1313
    assert len(diop_general_sum_of_squares(eq, 3)) == 3
    # issue sympy/sympy#11016
    var = symbols(':5') + (symbols('6', negative=True), )
    eq = Add(*[i**2 for i in var]) - 112
    assert diophantine(eq) == {(0, 1, 1, 5, 6, -7), (1, 1, 1, 3, 6, -8),
                               (2, 3, 3, 4, 5, -7), (0, 1, 1, 1, 3, -10),
                               (0, 0, 4, 4, 4, -8), (1, 2, 3, 3, 5, -8),
                               (0, 1, 2, 3, 7, -7), (2, 2, 4, 4, 6, -6),
                               (1, 1, 3, 4, 6, -7), (0, 2, 3, 3, 3, -9),
                               (0, 0, 2, 2, 2, -10), (1, 1, 2, 3, 4, -9),
                               (0, 1, 1, 2, 5, -9), (0, 0, 2, 6, 6, -6),
                               (1, 3, 4, 5, 5, -6), (0, 2, 2, 2, 6, -8),
                               (0, 3, 3, 3, 6, -7), (0, 2, 3, 5, 5, -7),
                               (0, 1, 5, 5, 5, -6)}
    # handle negated squares with signsimp
    assert diophantine(12 - x**2 - y**2 - z**2) == {(2, 2, 2)}
    # diophantine handles simplification, so classify_diop should
    # not have to look for additional patterns that are removed
    # by diophantine
    eq = a**2 + b**2 + c**2 + d**2 - 4
    pytest.raises(NotImplementedError, lambda: classify_diop(-eq))
Example #3
0
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
Example #4
0
    def _intersect(self, other):
        from diofant import Dummy
        from diofant.solvers.diophantine import diophantine
        from diofant.sets.sets import imageset
        if self.base_set is S.Integers:
            if isinstance(other, ImageSet) and other.base_set is S.Integers:
                f, g = self.lamda.expr, other.lamda.expr
                n, m = self.lamda.variables[0], other.lamda.variables[0]

                # Diophantine sorts the solutions according to the alphabetic
                # order of the variable names, since the result should not depend
                # on the variable name, they are replaced by the dummy variables
                # below
                a, b = Dummy('a'), Dummy('b')
                f, g = f.subs(n, a), g.subs(m, b)
                solns_set = diophantine(f - g)
                if solns_set == set():
                    return EmptySet()
                solns = list(diophantine(f - g))
                if len(solns) == 1:
                    t = list(solns[0][0].free_symbols)[0]
                else:
                    return

                # since 'a' < 'b'
                return imageset(Lambda(t, f.subs(a, solns[0][0])), S.Integers)

        if other == S.Reals:
            from diofant.solvers.diophantine import diophantine
            from diofant.core.function import expand_complex
            if len(self.lamda.variables
                   ) > 1 or self.base_set is not S.Integers:
                return

            f = self.lamda.expr
            n = self.lamda.variables[0]

            n_ = Dummy(n.name, integer=True)
            f_ = f.subs(n, n_)

            re, im = f_.as_real_imag()
            im = expand_complex(im)

            sols = list(diophantine(im, n_))
            if not sols:
                return S.EmptySet
            elif all(s[0].has(n_) is False for s in sols):
                s = FiniteSet(*[s[0] for s in sols])
            elif len(sols) == 1 and sols[0][0].has(n_):
                s = imageset(Lambda(n_, sols[0][0]), S.Integers)
            else:
                return

            return imageset(Lambda(n_, re), self.base_set.intersect(s))
Example #5
0
def test_assumptions():
    """
    Test whether diophantine respects the assumptions.
    """
    # Test case taken from the below so question regarding assumptions in diophantine module
    # http://stackoverflow.com/questions/23301941/how-can-i-declare-natural-symbols-with-sympy
    m, n = symbols('m n', integer=True, positive=True)
    diof = diophantine(n ** 2 + m * n - 500)
    assert diof == {(5, 20), (40, 10), (95, 5), (121, 4), (248, 2), (499, 1)}

    a, b = symbols('a b', integer=True, positive=False)
    diof = diophantine(a*b + 2*a + 3*b - 6)
    assert diof == {(-15, -3), (-9, -4), (-7, -5), (-6, -6), (-5, -8), (-4, -14)}
Example #6
0
def test_assumptions():
    """
    Test whether diophantine respects the assumptions.
    """
    # Test case taken from the below so question regarding assumptions in diophantine module
    # https//stackoverflow.com/questions/23301941/how-can-i-declare-natural-symbols-with-sympy
    m, n = symbols('m n', integer=True, positive=True)
    diof = diophantine(n ** 2 + m * n - 500)
    assert diof == {(5, 20), (40, 10), (95, 5), (121, 4), (248, 2), (499, 1)}

    a, b = symbols('a b', integer=True, positive=False)
    diof = diophantine(a*b + 2*a + 3*b - 6)
    assert diof == {(-15, -3), (-9, -4), (-7, -5), (-6, -6), (-5, -8), (-4, -14)}
Example #7
0
def check_solutions(eq):
    """
    Determines whether solutions returned by diophantine() satisfy the original
    equation. Hope to generalize this so we can remove functions like check_ternay_quadratic,
    check_solutions_normal, check_solutions()
    """
    s = diophantine(eq)

    terms = factor_list(eq)[1]

    var = list(eq.free_symbols)
    var.sort(key=default_sort_key)

    okay = True

    while len(s) and okay:
        solution = s.pop()

        okay = False

        for term in terms:
            subeq = term[0]

            if simplify(_mexpand(Subs(subeq, var, solution).doit())) == 0:
                okay = True
                break

    return okay
Example #8
0
def test_quadratic_simple_hyperbolic_case():
    # Simple Hyperbolic case: A = C = 0 and B != 0
    assert diop_solve(3*x*y + 34*x - 12*y + 1) == {(-133, -11), (5, -57)}
    assert diop_solve(6*x*y + 2*x + 3*y + 1) == set()
    assert diop_solve(-13*x*y + 2*x - 4*y - 54) == {(27, 0)}
    assert diop_solve(-27*x*y - 30*x - 12*y - 54) == {(-14, -1)}
    assert diop_solve(2*x*y + 5*x + 56*y + 7) == {(-161, -3),
                                                  (-47, -6), (-35, -12), (-29, -69),
                                                  (-27, 64), (-21, 7), (-9, 1),
                                                  (105, -2)}
    assert diop_solve(6*x*y + 9*x + 2*y + 3) == set()
    assert diop_solve(x*y + x + y + 1) == {(-1, t), (t, -1)}
    assert diophantine(48*x*y)
Example #9
0
def test_diop_sum_of_even_powers():
    eq = u**2 + v**2 + x**2 + y**2 + z**2 - 123
    ans = diop_general_sum_of_squares(eq, oo)  # allow oo to be used
    assert len(ans) == 14

    eq = x**4 + y**4 + z**4 - 2673
    assert diop_solve(eq) == {(3, 6, 6), (2, 4, 7)}
    assert diop_general_sum_of_even_powers(eq, 2) == {(3, 6, 6), (2, 4, 7)}
    pytest.raises(NotImplementedError, lambda: diop_general_sum_of_even_powers(-eq, 2))
    neg = symbols('neg', negative=True)
    eq = x**4 + y**4 + neg**4 - 2673
    assert diop_general_sum_of_even_powers(eq) == {(-3, 6, 6)}
    assert diophantine(x**4 + y**4 + 2) == set()
    assert diop_general_sum_of_even_powers(x**4 + y**4 - 2, limit=0) == set()
Example #10
0
def test_quadratic_simple_hyperbolic_case():
    # Simple Hyperbolic case: A = C = 0 and B != 0
    assert diop_solve(3*x*y + 34*x - 12*y + 1) == \
        {(-Integer(133), -Integer(11)), (Integer(5), -Integer(57))}
    assert diop_solve(6*x*y + 2*x + 3*y + 1) == set()
    assert diop_solve(-13*x*y + 2*x - 4*y - 54) == {(Integer(27), Integer(0))}
    assert diop_solve(-27*x*y - 30*x - 12*y - 54) == {(-Integer(14), -Integer(1))}
    assert diop_solve(2*x*y + 5*x + 56*y + 7) == {(-Integer(161), -Integer(3)),
        (-Integer(47), -Integer(6)), (-Integer(35), -Integer(12)), (-Integer(29), -Integer(69)),
        (-Integer(27), Integer(64)), (-Integer(21), Integer(7)), (-Integer(9), Integer(1)),
        (Integer(105), -Integer(2))}
    assert diop_solve(6*x*y + 9*x + 2*y + 3) == set()
    assert diop_solve(x*y + x + y + 1) == {(-Integer(1), t), (t, -Integer(1))}
    assert diophantine(48*x*y)
Example #11
0
def test_quadratic_simple_hyperbolic_case():
    # Simple Hyperbolic case: A = C = 0 and B != 0
    assert diop_solve(3 * x * y + 34 * x - 12 * y + 1) == {(-133, -11),
                                                           (5, -57)}
    assert diop_solve(6 * x * y + 2 * x + 3 * y + 1) == set()
    assert diop_solve(-13 * x * y + 2 * x - 4 * y - 54) == {(27, 0)}
    assert diop_solve(-27 * x * y - 30 * x - 12 * y - 54) == {(-14, -1)}
    assert diop_solve(2 * x * y + 5 * x + 56 * y + 7) == {
        (-161, -3), (-47, -6), (-35, -12), (-29, -69), (-27, 64), (-21, 7),
        (-9, 1), (105, -2)
    }
    assert diop_solve(6 * x * y + 9 * x + 2 * y + 3) == set()
    assert diop_solve(x * y + x + y + 1) == {(-1, t), (t, -1)}
    assert diophantine(48 * x * y)
Example #12
0
def test_diop_sum_of_even_powers():
    eq = u**2 + v**2 + x**2 + y**2 + z**2 - 123
    ans = diop_general_sum_of_squares(eq, oo)  # allow oo to be used
    assert len(ans) == 14

    eq = x**4 + y**4 + z**4 - 2673
    assert diop_solve(eq) == {(3, 6, 6), (2, 4, 7)}
    assert diop_general_sum_of_even_powers(eq, 2) == {(3, 6, 6), (2, 4, 7)}
    pytest.raises(NotImplementedError,
                  lambda: diop_general_sum_of_even_powers(-eq, 2))
    neg = symbols('neg', negative=True)
    eq = x**4 + y**4 + neg**4 - 2673
    assert diop_general_sum_of_even_powers(eq) == {(-3, 6, 6)}
    assert diophantine(x**4 + y**4 + 2) == set()
    assert diop_general_sum_of_even_powers(x**4 + y**4 - 2, limit=0) == set()
Example #13
0
def check_integrality(eq):
    """
    Check that the solutions returned by diophantine() are integers.
    This should be seldom needed except for general quadratic
    equations which are solved with rational transformations.
    """
    def _check_values(x):
        """ Check a number of values. """
        for i in range(-4, 4):
            if not isinstance(simplify(x.subs(t, i)), Integer):
                return False
        return True

    for soln in diophantine(eq, param=t):
        for x in soln:
            if not _check_values(x):
                return False

    return True
Example #14
0
def check_solutions(eq):
    """
    Determines whether solutions returned by diophantine() satisfy the original
    equation. Hope to generalize this so we can remove functions like check_ternay_quadratic,
    check_solutions_normal, check_solutions()
    """
    s = diophantine(eq)

    factors = Mul.make_args(eq)

    var = list(eq.free_symbols)
    var.sort(key=default_sort_key)

    while s:
        solution = s.pop()
        for f in factors:
            if diop_simplify(f.subs(zip(var, solution))) == 0:
                break
        else:
            return False
    return True
Example #15
0
def check_solutions(eq):
    """
    Determines whether solutions returned by diophantine() satisfy the original
    equation. Hope to generalize this so we can remove functions like check_ternay_quadratic,
    check_solutions_normal, check_solutions()
    """
    s = diophantine(eq)

    factors = Mul.make_args(eq)

    var = list(eq.free_symbols)
    var.sort(key=default_sort_key)

    while s:
        solution = s.pop()
        for f in factors:
            if diop_simplify(f.subs(zip(var, solution))) == 0:
                break
        else:
            return False
    return True
Example #16
0
def test_sympyissue_11959():
    solution = {(11, -71), (33, -64), (49, -53), (54, -48), (-59, -41),
                (-61, -38), (65, -32), (-70, -17), (72, -10), (-72, 6),
                (-68, 25), (42, 60), (39, 62), (-24, 69), (18, 71), (-5, 73)}
    assert diophantine(10 * x**2 - 6 * x + 10 * y**2 - 14 * y -
                       52548) == solution
Example #17
0
def test_sympyissue_9539():
    assert diophantine(6*w + 9*y + 20*x - z) == {(t_0, t_1, t_1 + t_2,
                                                  6*t_0 + 29*t_1 + 9*t_2)}
Example #18
0
def test_sympyissue_8943():
    assert diophantine(
        (3 * (x**2 + y**2 + z**2) - 14 * (x * y + y * z + z * x))) == {(0, 0,
                                                                        0)}
Example #19
0
def test_sympyissue_9539():
    assert diophantine(6 * w + 9 * y + 20 * x - z) == {
        (t_0, t_1, t_1 + t_2, 6 * t_0 + 29 * t_1 + 9 * t_2)
    }
Example #20
0
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)}
Example #21
0
def test_diophantine():
    # Commented out test cases should be uncommented after
    # the bug with factor_list() gets merged.

    assert check_solutions((x - y) * (y - z) * (z - x))
    assert check_solutions((x - y) * (x**2 + y**2 - z**2))
    assert check_solutions((x - 3 * y + 7 * z) * (x**2 + y**2 - z**2))
    assert check_solutions((x**2 - 3 * y**2 - 1))
    # assert check_solutions(y**2 + 7*x*y)
    # assert check_solutions(x**2 - 3*x*y + y**2)
    # assert check_solutions(z*(x**2 - y**2 - 15))
    # assert check_solutions(x*(2*y - 2*z + 5))
    assert check_solutions((x**2 - 3 * y**2 - 1) * (x**2 - y**2 - 15))
    assert check_solutions((x**2 - 3 * y**2 - 1) * (y - 7 * z))
    assert check_solutions((x**2 + y**2 - z**2) * (x - 7 * y - 3 * z + 4 * w))
    # Following test case caused problems in parametric representation
    # But this can be solved by factroing out y.
    # No need to use methods for ternary quadratic equations.
    # assert check_solutions(y**2 - 7*x*y + 4*y*z)
    assert check_solutions(x**2 - 2 * x + 1)

    assert diophantine(Integer(0)) == {(t, )}
    assert diophantine(x - y) == diophantine(Eq(x, y))
    assert diophantine(3 * x * pi - 2 * y * pi) == {(2 * t_0, 3 * t_0)}
    assert diophantine(x**2 + y**2 + z**2 - 14) == {(1, 2, 3)}
    assert diophantine(x**2 + 15 * x / 14 - 3) == set()

    # test issue sympy/sympy#11049
    eq = 92 * x**2 - 99 * y**2 - z**2
    coeff = eq.as_coefficients_dict()
    assert _diop_ternary_quadratic_normal((x, y, z), coeff) == (9, 7, 51)
    assert diophantine(eq) == {
        (891 * p**2 + 9 * q**2, -693 * p**2 - 102 * p * q + 7 * q**2,
         5049 * p**2 - 1386 * p * q - 51 * q**2)
    }
    eq = 2 * x**2 + 2 * y**2 - z**2
    coeff = eq.as_coefficients_dict()
    assert _diop_ternary_quadratic_normal((x, y, z), coeff) == (1, 1, 2)
    assert diophantine(eq) == {(2 * p**2 - q**2, -2 * p**2 + 4 * p * q - q**2,
                                4 * p**2 - 4 * p * q + 2 * q**2)}
    eq = 411 * x**2 + 57 * y**2 - 221 * z**2
    coeff = eq.as_coefficients_dict()
    assert _diop_ternary_quadratic_normal((x, y, z),
                                          coeff) == (2021, 2645, 3066)
    assert diophantine(eq) == {
        (115197 * p**2 - 446641 * q**2,
         -150765 * p**2 + 1355172 * p * q - 584545 * q**2,
         174762 * p**2 - 301530 * p * q + 677586 * q**2)
    }
    eq = 573 * x**2 + 267 * y**2 - 984 * z**2
    coeff = eq.as_coefficients_dict()
    assert _diop_ternary_quadratic_normal((x, y, z), coeff) == (49, 233, 127)
    assert diophantine(eq) == {(4361 * p**2 - 16072 * q**2,
                                -20737 * p**2 + 83312 * p * q - 76424 * q**2,
                                11303 * p**2 - 41474 * p * q + 41656 * q**2)}

    # this produces factors during reconstruction
    eq = x**2 + 3 * y**2 - 12 * z**2
    coeff = eq.as_coefficients_dict()
    assert _diop_ternary_quadratic_normal((x, y, z), coeff) == (0, 2, 1)
    assert diophantine(eq) == {(24 * p * q, 2 * p**2 - 24 * q**2,
                                p**2 + 12 * q**2)}
    # solvers have not been written for every type
    pytest.raises(NotImplementedError, lambda: diophantine(x * y**2 + 1))

    # rational expressions
    assert diophantine(1 / x) == set()
    assert diophantine(1 / x + 1 / y - Rational(1, 2)) == {(6, 3), (-2, 1),
                                                           (4, 4), (1, -2),
                                                           (3, 6)}

    # issue sympy/sympy#9538
    eq = x - 3 * y + 2

    assert diophantine(eq, syms=[y, x]) == {(t_0, 3 * t_0 - 2)}
    assert diophantine(eq, syms=[x, y]) == {(3 * t_0 - 2, t_0)}

    pytest.raises(TypeError, lambda: diophantine(eq, syms={y, x}))
Example #22
0
def test_sympyissue_9106():
    eq = -48 - 2*x*(3*x - 1) + y*(3*y - 1)
    v = (x, y)
    for sol in diophantine(eq):
        assert not diop_simplify(eq.xreplace(dict(zip(v, sol))))
Example #23
0
def test_sympyissue_9106():
    eq = -48 - 2 * x * (3 * x - 1) + y * (3 * y - 1)
    v = (x, y)
    for sol in diophantine(eq):
        assert not diop_simplify(eq.xreplace(dict(zip(v, sol))))
Example #24
0
def test_input_format():
    pytest.raises(TypeError, lambda: diophantine(sin(x)))
    pytest.raises(TypeError, lambda: diophantine(3))
    pytest.raises(TypeError, lambda: diophantine(x / pi - 3))
Example #25
0
def test_diophantine():
    # Commented out test cases should be uncommented after
    # the bug with factor_list() gets merged.

    assert check_solutions((x - y)*(y - z)*(z - x))
    assert check_solutions((x - y)*(x**2 + y**2 - z**2))
    assert check_solutions((x - 3*y + 7*z)*(x**2 + y**2 - z**2))
    assert check_solutions((x**2 - 3*y**2 - 1))
    # assert check_solutions(y**2 + 7*x*y)
    # assert check_solutions(x**2 - 3*x*y + y**2)
    # assert check_solutions(z*(x**2 - y**2 - 15))
    # assert check_solutions(x*(2*y - 2*z + 5))
    assert check_solutions((x**2 - 3*y**2 - 1)*(x**2 - y**2 - 15))
    assert check_solutions((x**2 - 3*y**2 - 1)*(y - 7*z))
    assert check_solutions((x**2 + y**2 - z**2)*(x - 7*y - 3*z + 4*w))
    # Following test case caused problems in parametric representation
    # But this can be solved by factroing out y.
    # No need to use methods for ternary quadratic equations.
    # assert check_solutions(y**2 - 7*x*y + 4*y*z)
    assert check_solutions(x**2 - 2*x + 1)

    assert diophantine(Integer(0)) == {(t,)}
    assert diophantine(x - y) == diophantine(Eq(x, y))
    assert diophantine(3*x*pi - 2*y*pi) == {(2*t_0, 3*t_0)}
    assert diophantine(x**2 + y**2 + z**2 - 14) == {(1, 2, 3)}
    assert diophantine(x**2 + 15*x/14 - 3) == set()

    # test issue sympy/sympy#11049
    eq = 92*x**2 - 99*y**2 - z**2
    coeff = eq.as_coefficients_dict()
    assert _diop_ternary_quadratic_normal((x, y, z), coeff) == (9, 7, 51)
    assert diophantine(eq) == {(891*p**2 + 9*q**2, -693*p**2 - 102*p*q + 7*q**2,
                                5049*p**2 - 1386*p*q - 51*q**2)}
    eq = 2*x**2 + 2*y**2 - z**2
    coeff = eq.as_coefficients_dict()
    assert _diop_ternary_quadratic_normal((x, y, z), coeff) == (1, 1, 2)
    assert diophantine(eq) == {(2*p**2 - q**2, -2*p**2 + 4*p*q - q**2,
                                4*p**2 - 4*p*q + 2*q**2)}
    eq = 411*x**2+57*y**2-221*z**2
    coeff = eq.as_coefficients_dict()
    assert _diop_ternary_quadratic_normal((x, y, z), coeff) == (2021, 2645, 3066)
    assert diophantine(eq) == {(115197*p**2 - 446641*q**2,
                                -150765*p**2 + 1355172*p*q - 584545*q**2,
                                174762*p**2 - 301530*p*q + 677586*q**2)}
    eq = 573*x**2+267*y**2-984*z**2
    coeff = eq.as_coefficients_dict()
    assert _diop_ternary_quadratic_normal((x, y, z), coeff) == (49, 233, 127)
    assert diophantine(eq) == {(4361*p**2 - 16072*q**2,
                                -20737*p**2 + 83312*p*q - 76424*q**2,
                                11303*p**2 - 41474*p*q + 41656*q**2)}

    # this produces factors during reconstruction
    eq = x**2 + 3*y**2 - 12*z**2
    coeff = eq.as_coefficients_dict()
    assert _diop_ternary_quadratic_normal((x, y, z), coeff) == (0, 2, 1)
    assert diophantine(eq) == {(24*p*q, 2*p**2 - 24*q**2, p**2 + 12*q**2)}
    # solvers have not been written for every type
    pytest.raises(NotImplementedError, lambda: diophantine(x*y**2 + 1))

    # rational expressions
    assert diophantine(1/x) == set()
    assert diophantine(1/x + 1/y - Rational(1, 2)) == {(6, 3), (-2, 1),
                                                       (4, 4), (1, -2), (3, 6)}
Example #26
0
def test_sympyissue_9538():
    eq = x - 3 * y + 2
    assert diophantine(eq, syms=[y, x]) == {(t_0, 3 * t_0 - 2)}
    pytest.raises(TypeError, lambda: diophantine(eq, syms={y, x}))
    assert diophantine(eq, syms=[x, y]) == {(3 * t_0 - 2, t_0)}
Example #27
0
def test_sympyissue_8943():
    assert diophantine((3*(x**2 + y**2 + z**2) -
                       14*(x*y + y*z + z*x))) == {(0, 0, 0)}
Example #28
0
def test_sympyissue_9538():
    eq = x - 3*y + 2
    assert diophantine(eq, syms=[y, x]) == {(t_0, 3*t_0 - 2)}
    pytest.raises(TypeError, lambda: diophantine(eq, syms={y, x}))
    assert diophantine(eq, syms=[x, y]) == {(3*t_0 - 2, t_0)}
Example #29
0
def test_input_format():
    pytest.raises(TypeError, lambda: diophantine(sin(x)))
    pytest.raises(TypeError, lambda: diophantine(3))
    pytest.raises(TypeError, lambda: diophantine(x/pi - 3))