def test_ordered():
    # Issue sympy/sympy#7210 - this had been failing with python2/3 problems
    assert (list(ordered([{
        1: 3,
        2: 4,
        9: 10
    }, {
        1: 3
    }])) == [{
        1: 3
    }, {
        1: 3,
        2: 4,
        9: 10
    }])
    # warnings should not be raised for identical items
    l = [1, 1]
    assert list(ordered(l, warn=True)) == l
    l = [[1], [2], [1]]
    assert list(ordered(l, warn=True)) == [[1], [1], [2]]
    pytest.raises(
        ValueError, lambda: list(
            ordered(
                ['a', 'ab'], keys=[lambda x: x[0]], default=False, warn=True)))
    pytest.raises(ValueError, lambda: list(ordered(['a', 'b'], default=False)))
Example #2
0
def test_sympyissue_8825():
    t1, t2 = symbols('t1:3')
    d = weakref.WeakKeyDictionary([(t1, 1), (t2, 2)])
    assert sstr(list(ordered(d.items()))) == '[(t1, 1), (t2, 2)]'
    del t1
    clear_cache()
    gc.collect()
    assert sstr(list(ordered(d.items()))) == '[(t2, 2)]'
Example #3
0
def test_postorder_traversal():
    expr = z + w*(x + y)
    expected = [z, w, x, y, x + y, w*(x + y), w*(x + y) + z]
    assert list(postorder_traversal(expr, keys=default_sort_key)) == expected
    assert list(postorder_traversal(expr, keys=True)) == expected

    expr = Piecewise((x, x < 1), (x**2, True))
    expected = [
        x, 1, x, x < 1, ExprCondPair(x, x < 1),
        true, 2, x, x**2,
        ExprCondPair(x**2, True), Piecewise((x, x < 1), (x**2, True))
    ]
    assert list(postorder_traversal(expr, keys=default_sort_key)) == expected
    assert list(postorder_traversal(
        [expr], keys=default_sort_key)) == expected + [[expr]]

    assert list(postorder_traversal(Integral(x**2, (x, 0, 1)),
                                    keys=default_sort_key)) == [
        2, x, x**2, 0, 1, x, Tuple(x, 0, 1),
        Integral(x**2, Tuple(x, 0, 1))
    ]
    assert list(postorder_traversal(('abc', ('d', 'ef')))) == [
        'abc', 'd', 'ef', ('d', 'ef'), ('abc', ('d', 'ef'))]

    assert list(ordered(postorder_traversal(x*(y + z)))) == [x, y, z,
                                                             y + z, x*(y + z)]
Example #4
0
    def __init__(self,
                 clauses,
                 variables,
                 var_settings,
                 symbols=None,
                 heuristic='vsids',
                 clause_learning='none',
                 INTERVAL=500):

        self.var_settings = var_settings
        self.heuristic = heuristic
        self.is_unsatisfied = False
        self._unit_prop_queue = []
        self.update_functions = []
        self.INTERVAL = INTERVAL

        if symbols is None:
            self.symbols = list(ordered(variables))
        else:
            self.symbols = symbols

        self._initialize_variables(variables)
        self._initialize_clauses(clauses)

        if 'vsids' == heuristic:
            self._vsids_init()
            self.heur_calculate = self._vsids_calculate
            self.heur_lit_assigned = self._vsids_lit_assigned
            self.heur_lit_unset = self._vsids_lit_unset
            self.heur_clause_added = self._vsids_clause_added

            # Note: Uncomment this if/when clause learning is enabled
            # self.update_functions.append(self._vsids_decay)

        else:
            raise NotImplementedError

        if 'simple' == clause_learning:
            self.add_learned_clause = self._simple_add_learned_clause
            self.compute_conflict = self.simple_compute_conflict
            self.update_functions.append(self.simple_clean_clauses)
        elif 'none' == clause_learning:
            self.add_learned_clause = lambda x: None
            self.compute_conflict = lambda: None
        else:
            raise NotImplementedError

        # Create the base level
        self.levels = [Level(0)]
        self._current_level.varsettings = var_settings

        # Keep stats
        self.num_decisions = 0
        self.num_learned_clauses = 0
        self.original_num_clauses = len(self.clauses)
Example #5
0
def test_sympyissue_16038():
    sys1 = [
        (2 * x - 8 * y)**2 + (5 * x - 10 * y)**2 + (10 * x - 7 * y)**2 - 437,
        (7 * y - 10 * z)**2 + (8 * y - z)**2 + (10 * y - 9 * z)**2 - 474,
        (-10 * x + 10 * z)**2 + (-5 * x + 9 * z)**2 + (-2 * x + z)**2 - 885
    ]

    sys2 = [
        (2.0 * x - 8 * y)**2 + (5 * x - 10 * y)**2 + (10 * x - 7 * y)**2 - 437,
        (7 * y - 10 * z)**2 + (8 * y - z)**2 + (10 * y - 9 * z)**2 - 474,
        (-10 * x + 10 * z)**2 + (-5 * x + 9 * z)**2 + (-2 * x + z)**2 - 885
    ]

    sols1 = solve_poly_system(sys1, (x, y, z))
    sols2 = solve_poly_system(sys2, (x, y, z))

    assert len(sols1) == len(sols2) == 8
    assert {x: -1, y: -2, z: -3} in sols1
    assert {x: +1, y: +2, z: +3} in sols2
    assert (list(ordered([{k: v.n(7)
                           for k, v in _.items()} for _ in sols1])) == list(
                               ordered([{k: v.n(7)
                                         for k, v in _.items()}
                                        for _ in sols2])))
Example #6
0
def test_ordered():
    assert list(ordered((x, y), hash, default=False)) in [[x, y], [y, x]]
    assert list(ordered((x, y), hash, default=False)) == \
        list(ordered((y, x), hash, default=False))
    assert list(ordered((x, y))) == [x, y]

    seq, keys = [[[1, 2, 1], [0, 3, 1], [1, 1, 3], [2], [1]], (len, sum)]
    assert list(ordered(seq, keys, default=False, warn=False)) == \
        [[1], [2], [1, 2, 1], [0, 3, 1], [1, 1, 3]]
    pytest.raises(ValueError,
                  lambda: list(ordered(seq, keys, default=False, warn=True)))
Example #7
0
def test_sympyissue_9608():
    a = Partition([1, 2, 3], [4])
    b = Partition([1, 2], [3, 4])
    assert list(ordered([a, b], Set._infimum_key))  # does not raise an error